PikaManager.py
4.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
"""
Author: Caio Marcelo Campoy Guedes
E-Mail: caiomcg@gmail.com
Author: Erickson Silva
E-Mail: erickson.silva@lavid.ufpb.br
Author: Jorismar Barbosa
E-Mail: jorismar.barbosa@lavid.ufpb.br
Author: Wesnydy Lima Ribeiro
E-Mail: wesnydy@lavid.ufpb.br
"""
import pika
import json
class PikaManager:
def __init__(self, ip, username=None, password=None):
"""
Initialize the class with credentials.
Parameters
----------
ip : string
The server IP.
username : string
The user login.
password : string
The user password.
"""
self.server_ip = ip
self.MAX_ERR_ATTEMPT = 3
if not all((username, password)):
self.add_localBlockConnection()
else:
self.add_credentials(username, password)
self.add_blockConnection()
def add_credentials(self, username, password):
"""
Add user credentials.
Parameters
----------
username : string
The user login.
password : string
The user password.
"""
self.credentials = pika.PlainCredentials(username, password)
def add_localBlockConnection(self):
"""
Create the blocking connection object.
"""
self.conn_send = pika.BlockingConnection(pika.ConnectionParameters(host = self.server_ip, heartbeat_interval = 0))
self.conn_receive = pika.BlockingConnection(pika.ConnectionParameters(host = self.server_ip, heartbeat_interval = 0))
def add_blockConnection(self):
"""
Create the blocking connection object. Credentials are used.
"""
self.conn_send = pika.BlockingConnection(pika.ConnectionParameters(host = self.server_ip, credentials = self.credentials, heartbeat_interval = 0))
self.conn_receive = pika.BlockingConnection(pika.ConnectionParameters(host = self.server_ip, credentials = self.credentials, heartbeat_interval = 0))
def _reload_connection(self, connection):
"""
Reload a specific connection.
Parameters
----------
connection : Object
The connection to be reloaded.
"""
try:
connection.close()
except:
pass
connection = pika.BlockingConnection(pika.ConnectionParameters(host = self.server_ip, heartbeat_interval = 0)) # ?
def _setup_channel(self, connection):
"""
Atempt to create a connection.
Parameters
----------
connection : Object
Connection to setup.
Returns
-------
Object
None if cailed to connect. The object if successfuly connected.
"""
attempts = 0
while attempts < self.MAX_ERR_ATTEMPT:
try:
channel = connection.channel()
return channel
except:
self._reload_connection(connection)
attempts += 1
print("Send Error: Attempt(" + str(attempts) + ")")
if attempts == self.MAX_ERR_ATTEMPT:
return None
def send_to_queue(self, queue_name, body, props):
"""
Send a message to the queue.
Parameters
----------
queue_name : string
Queue that receives the message.
body : string
The message to be sent.
props : Object
Object containing a set of 14 properties.
"""
channel = self._setup_channel(self.conn_send)
if channel != None:
channel.queue_declare(queue = queue_name)
channel.basic_publish(exchange = '', routing_key = queue_name, properties = pika.BasicProperties(correlation_id = props.correlation_id), body = body)
channel.close()
def receive_from_queue(self, queue_name, callback):
"""
Receive a message from the queue.
Parameters
----------
queue_name : string
Queue where the message will be received.
callback : function
Function that process the message.
"""
channel = self._setup_channel(self.conn_receive)
if channel != None:
channel.queue_declare(queue = queue_name)
channel.basic_qos(prefetch_count = 1)
channel.basic_consume(callback, queue = queue_name, no_ack = True)
channel.start_consuming()
channel.close()
def get_conn_send(self):
"""
Get the send connection.
Returns
-------
The send connection.
"""
return self.conn_send
def get_conn_receive(self):
"""
Get the receive connection.
Returns
-------
The receive connection.
"""
return self.conn_receive
def close_connections(self):
"""
Close all connections.
"""
try:
self.conn_receive.close()
self.conn_send.close()
except:
pass