1 # Copyright 2012 GRNET S.A. All rights reserved.
3 # Redistribution and use in source and binary forms, with or
4 # without modification, are permitted provided that the following
7 # 1. Redistributions of source code must retain the above
8 # copyright notice, this list of conditions and the following
11 # 2. Redistributions in binary form must reproduce the above
12 # copyright notice, this list of conditions and the following
13 # disclaimer in the documentation and/or other materials
14 # provided with the distribution.
16 # THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17 # OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20 # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 # USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 # AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 # POSSIBILITY OF SUCH DAMAGE.
29 # The views and conclusions contained in the software and
30 # documentation are those of the authors and should not be
31 # interpreted as representing official policies, either expressed
32 # or implied, of GRNET S.A.
38 from urlparse import urlparse
39 from hashlib import sha1
40 from random import random
44 def exchange_connect(exchange, vhost='/'):
45 """Format exchange as a URI: rabbitmq://user:pass@host:port/exchange"""
46 parts = urlparse(exchange)
47 if parts.scheme != 'rabbitmq':
49 if len(parts.path) < 2 or not parts.path.startswith('/'):
51 exchange = parts.path[1:]
52 connection = pika.BlockingConnection(pika.ConnectionParameters(
53 host=parts.hostname, port=parts.port, virtual_host=vhost,
54 credentials=pika.PlainCredentials(parts.username, parts.password)))
55 channel = connection.channel()
56 channel.exchange_declare(exchange=exchange, type='topic', durable=True)
57 return (connection, channel, exchange)
59 def exchange_close(conn):
60 connection, channel, exchange = conn
63 def exchange_send(conn, key, value):
64 """Messages are sent to exchanges at a key."""
65 connection, channel, exchange = conn
66 channel.basic_publish(exchange=exchange,
68 body=json.dumps(value))
71 def exchange_route(conn, key, queue):
72 """Set up routing of keys to queue."""
73 connection, channel, exchange = conn
74 channel.queue_declare(queue=queue, durable=True,
75 exclusive=False, auto_delete=False)
76 channel.queue_bind(exchange=exchange,
80 def queue_callback(conn, queue, cb):
82 def handle_delivery(channel, method_frame, header_frame, body):
83 #print 'Basic.Deliver %s delivery-tag %i: %s' % (header_frame.content_type,
84 # method_frame.delivery_tag,
88 channel.basic_ack(delivery_tag=method_frame.delivery_tag)
90 connection, channel, exchange = conn
91 channel.basic_consume(handle_delivery, queue=queue)
93 def queue_start(conn):
94 connection, channel, exchange = conn
95 channel.start_consuming()
97 class Receipt(object):
98 def __init__(self, client, user, resource, value, details={}):
99 self.eventVersion = '1.0'
100 self.occurredMillis = int(time() * 1000)
101 self.receivedMillis = self.occurredMillis
102 self.clientID = client
104 self.resource = resource
106 self.details = details
108 hash.update(json.dumps([client, user, resource, value, details, random()]))
109 self.id = hash.hexdigest()