Move queue functions to lib.
authorAntony Chazapis <chazapis@gmail.com>
Sat, 28 Jan 2012 10:06:51 +0000 (12:06 +0200)
committerAntony Chazapis <chazapis@gmail.com>
Sat, 28 Jan 2012 10:06:51 +0000 (12:06 +0200)
Refs #1792

pithos/lib/queue.py [new file with mode: 0755]
pithos/tools/dispatcher.py

diff --git a/pithos/lib/queue.py b/pithos/lib/queue.py
new file mode 100755 (executable)
index 0000000..0cfe830
--- /dev/null
@@ -0,0 +1,91 @@
+# Copyright 2012 GRNET S.A. All rights reserved.
+#
+# Redistribution and use in source and binary forms, with or
+# without modification, are permitted provided that the following
+# conditions are met:
+#
+#   1. Redistributions of source code must retain the above
+#      copyright notice, this list of conditions and the following
+#      disclaimer.
+#
+#   2. Redistributions in binary form must reproduce the above
+#      copyright notice, this list of conditions and the following
+#      disclaimer in the documentation and/or other materials
+#      provided with the distribution.
+#
+# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
+# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
+# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
+# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+# POSSIBILITY OF SUCH DAMAGE.
+#
+# The views and conclusions contained in the software and
+# documentation are those of the authors and should not be
+# interpreted as representing official policies, either expressed
+# or implied, of GRNET S.A.
+
+import pika
+import json
+
+from urlparse import urlparse
+
+
+def exchange_connect(exchange, vhost='/'):
+    """Format exchange as a URI: rabbitmq://user:pass@host:port/exchange"""
+    parts = urlparse(exchange)
+    if parts.scheme != 'rabbitmq':
+        return None
+    if len(parts.path) < 2 or not parts.path.startswith('/'):
+        return None
+    exchange = parts.path[1:]
+    connection = pika.BlockingConnection(pika.ConnectionParameters(
+                    host=parts.hostname, port=parts.port, virtual_host=vhost,
+                    credentials=pika.PlainCredentials(parts.username, parts.password)))
+    channel = connection.channel()
+    channel.exchange_declare(exchange=exchange, type='topic', durable=True)
+    return (connection, channel, exchange)
+
+def exchange_close(conn):
+    connection, channel, exchange = conn
+    connection.close()
+
+def exchange_send(conn, key, value):
+    """Messages are sent to exchanges at a key."""
+    connection, channel, exchange = conn
+    channel.basic_publish(exchange=exchange,
+                          routing_key=key,
+                          body=json.dumps(value))
+
+    
+def exchange_route(conn, key, queue):
+    """Set up routing of keys to queue."""
+    connection, channel, exchange = conn
+    channel.queue_declare(queue=queue, durable=True,
+                          exclusive=False, auto_delete=False)
+    channel.queue_bind(exchange=exchange,
+                       queue=queue,
+                       routing_key=key)
+
+def queue_callback(conn, queue, cb):
+    
+    def handle_delivery(channel, method_frame, header_frame, body):
+        #print 'Basic.Deliver %s delivery-tag %i: %s' % (header_frame.content_type,
+        #                                                method_frame.delivery_tag,
+        #                                                body)
+        if cb:
+            cb(json.loads(body))
+        channel.basic_ack(delivery_tag=method_frame.delivery_tag)
+    
+    connection, channel, exchange = conn
+    channel.basic_consume(handle_delivery, queue=queue)
+
+def queue_start(conn):
+    connection, channel, exchange = conn
+    channel.start_consuming()
index d3539a9..c1f59f9 100755 (executable)
@@ -36,8 +36,8 @@
 import sys
 import logging
 
-import pika
-import json
+from pithos.lib.queue import (exchange_connect, exchange_close,
+    exchange_send, exchange_route, queue_callback, queue_start)
 
 from optparse import OptionParser
 
@@ -88,16 +88,11 @@ if __name__ == '__main__':
                         level=logging.DEBUG if DEBUG else logging.INFO)
     logger = logging.getLogger('dispatcher')
     
-    connection = pika.BlockingConnection(pika.ConnectionParameters(
-                    host=opts.host, port=opts.port, virtual_host=opts.vhost,
-                    credentials=pika.PlainCredentials(opts.user, opts.password)))
-    channel = connection.channel()
-    channel.exchange_declare(exchange=opts.exchange, type='topic', durable=True)
+    exchange = 'rabbitmq://%s:%s@%s:%s/%s' % (opts.user, opts.password, opts.host, opts.port, opts.exchange)
+    connection = exchange_connect(exchange)
     if opts.test:
-        channel.basic_publish(exchange=opts.exchange,
-                              routing_key=opts.key,
-                              body=json.dumps({"test": "0123456789"}))
-        connection.close()
+        exchange_send(connection, opts.key, {"test": "0123456789"})
+        exchange_close(connection)
         sys.exit()
     
     callback = None
@@ -108,22 +103,15 @@ if __name__ == '__main__':
             cb_module = sys.modules[cb[0]]
             callback = getattr(cb_module, cb[1])
     
-    def handle_delivery(channel, method_frame, header_frame, body):
-        logger.debug('Basic.Deliver %s delivery-tag %i: %s', header_frame.content_type,
-                                                             method_frame.delivery_tag,
-                                                             body)
+    def handle_message(msg):
+        logger.debug('%s', msg)
         if callback:
-            callback(json.loads(message_data))
-        channel.basic_ack(delivery_tag=method_frame.delivery_tag)
+            callback(msg)
     
-    channel.queue_declare(queue=opts.queue, durable=True,
-                          exclusive=False, auto_delete=False)
-    channel.queue_bind(exchange=opts.exchange,
-                       queue=opts.queue,
-                       routing_key=opts.key)
-    channel.basic_consume(handle_delivery, queue=opts.queue)
+    exchange_route(connection, opts.key, opts.queue)
+    queue_callback(connection, opts.queue, handle_message)
     try:
-        channel.start_consuming()
+        queue_start(connection)
     except KeyboardInterrupt:
         pass