root / snf-common / synnefo / lib / queue.py @ 74d988b0
History | View | Annotate | Download (5.1 kB)
1 |
# Copyright 2012 GRNET S.A. All rights reserved.
|
---|---|
2 |
#
|
3 |
# Redistribution and use in source and binary forms, with or
|
4 |
# without modification, are permitted provided that the following
|
5 |
# conditions are met:
|
6 |
#
|
7 |
# 1. Redistributions of source code must retain the above
|
8 |
# copyright notice, this list of conditions and the following
|
9 |
# disclaimer.
|
10 |
#
|
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.
|
15 |
#
|
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.
|
28 |
#
|
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.
|
33 |
|
34 |
import pika |
35 |
import json |
36 |
import uuid |
37 |
|
38 |
from urlparse import urlparse |
39 |
from hashlib import sha1 |
40 |
from random import random |
41 |
from time import time |
42 |
|
43 |
|
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': |
48 |
return None |
49 |
if len(parts.path) < 2 or not parts.path.startswith('/'): |
50 |
return None |
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)
|
58 |
|
59 |
def exchange_close(conn): |
60 |
connection, channel, exchange = conn |
61 |
connection.close() |
62 |
|
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, |
67 |
routing_key=key, |
68 |
body=json.dumps(value)) |
69 |
|
70 |
|
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, |
77 |
queue=queue, |
78 |
routing_key=key) |
79 |
|
80 |
def queue_callback(conn, queue, cb): |
81 |
|
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,
|
85 |
# body)
|
86 |
if cb:
|
87 |
cb(json.loads(body)) |
88 |
channel.basic_ack(delivery_tag=method_frame.delivery_tag) |
89 |
|
90 |
connection, channel, exchange = conn |
91 |
channel.basic_consume(handle_delivery, queue=queue) |
92 |
|
93 |
def queue_start(conn): |
94 |
connection, channel, exchange = conn |
95 |
channel.start_consuming() |
96 |
|
97 |
class Receipt(object): |
98 |
def __init__(self, client, user, instance, resource, value, details={}): |
99 |
self.eventVersion = '1.0' |
100 |
self.occurredMillis = int(time() * 1000) |
101 |
self.receivedMillis = self.occurredMillis |
102 |
self.clientID = client
|
103 |
self.userID = user
|
104 |
self.instanceID = instance
|
105 |
self.resource = resource
|
106 |
self.value = value
|
107 |
self.details = details
|
108 |
hash = sha1() |
109 |
hash.update(json.dumps([client, user, resource, value, details, random()]))
|
110 |
self.id = hash.hexdigest() |
111 |
|
112 |
def format(self): |
113 |
return self.__dict__ |
114 |
|
115 |
class UserEvent(object): |
116 |
def __init__(self, client, user, is_active, eventType, details=None): |
117 |
self.eventVersion = '1' |
118 |
self.occurredMillis = int(time() * 1000) |
119 |
self.receivedMillis = self.occurredMillis |
120 |
self.clientID = client
|
121 |
self.userID = user
|
122 |
self.isActive = is_active
|
123 |
self.role = 'default' |
124 |
self.eventType = eventType
|
125 |
self.details = details or {} |
126 |
hash = sha1() |
127 |
hash.update(json.dumps([client,
|
128 |
self.userID,
|
129 |
self.isActive,
|
130 |
self.role,
|
131 |
self.eventType,
|
132 |
self.details,
|
133 |
self.occurredMillis
|
134 |
] |
135 |
) |
136 |
) |
137 |
self.id = hash.hexdigest() |
138 |
|
139 |
def format(self): |
140 |
return self.__dict__ |