-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathZMQMessenger.py
339 lines (242 loc) · 9.99 KB
/
ZMQMessenger.py
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
import zmq
import zmq.ssh
import json
import base64
import uuid
import time
"""Disclaimer
This material was prepared as an account of work sponsored by an agency of the United States Government. Neither the United States Government nor the United States Department of Energy, nor Battelle, nor any of their employees, nor any jurisdiction or organization that has cooperated in the development of these materials, makes any warranty, express or implied, or assumes any legal liability or responsibility for the accuracy, completeness, or usefulness or any information, apparatus, product, software, or process disclosed, or represents that its use would not infringe privately owned rights.
Reference herein to any specific commercial product, process, or service by trade name, trademark, manufacturer, or otherwise does not necessarily constitute or imply its endorsement, recommendation, or favoring by the United States Government or any agency thereof, or Battelle Memorial Institute. The views and opinions of authors expressed herein do not necessarily state or reflect those of the United States Government or any agency thereof.
PACIFIC NORTHWEST NATIONAL LABORATORY
operated by
BATTELLE
for the
UNITED STATES DEPARTMENT OF ENERGY
under Contract DE-AC05-76RL01830
"""
class ZMQMessenger:
def __init__(self, ip, port, connection_type, identifier="", ssh_server=""):
self.id = identifier
self.ip = ip
self.context = None
self.socket = None
self.port = port
self.ssh_server = ssh_server
# used for messages with a fixed reply like "ping"
self.ReplySocket = None
if connection_type == "PUB":
self.ConnectAsPublisher()
if connection_type == "SUB":
self.ConnectAsSubscriber()
if connection_type == "REQ":
self.ConnectAsRequest()
if connection_type == "REP":
self.ConnectAsReply()
if connection_type == "tunnelREQ":
self.ConnectAsTunnelRequest()
if connection_type == "tunnelREP":
self.ConnectAsTunnelReply()
# socket seems to take a finite time to establish
time.sleep(1)
def ConnectAsPublisher(self):
self.context = zmq.Context()
self.socket = self.context.socket(zmq.PUB)
connect = "tcp://*:" + str(self.port)
print(self.id + " publisher binding to: " + connect)
self.socket.bind(connect)
def ConnectAsSubscriber(self):
self.context = zmq.Context()
self.socket = self.context.socket(zmq.SUB)
connect = "tcp://" + self.ip + ":" + str(self.port)
print(self.id + " subscriber connecting to: " + connect)
self.socket.connect(connect)
self.socket.subscribe("") # Subscribe to all messages, no filter
def ConnectAsRequest(self):
self.context = zmq.Context()
self.socket = self.context.socket(zmq.REQ)
connect = "tcp://*:" + str(self.port)
print(self.id + " request binding to: " + connect)
self.socket.bind(connect)
def ConnectAsTunnelRequest(self):
connect = "tcp://" + self.ip + ":" + str(self.port)
print(self.id + " request binding to: " + connect)
print("server: ", self.ssh_server)
self.context = zmq.Context()
self.socket = self.context.socket(zmq.REQ)
print("before tunnel")
zmq.ssh.tunnel.tunnel_connection(self.socket, "tcp://127.0.0.1:5558", "zmq@localhost", keyfile="zmq_private")
print("after tunnel")
# revisit this, these are hacks I made to make it work on the other side of the tunnel
def ConnectAsReply(self):
self.context = zmq.Context()
self.socket = self.context.socket(zmq.REP)
connect = "tcp://" + self.ip + ":" + str(self.port)
print(self.id + " reply connecting to: " + connect)
self.socket.connect(connect)
def ConnectAsTunnelReply(self):
self.context = zmq.Context()
self.socket = self.context.socket(zmq.REP)
connect = "tcp://*:" + str(self.port)
print(self.id + " reply connecting to: " + connect)
self.socket.bind(connect)
def CreateSocketPair(self, pairedSocket):
pairedSocket.ReplySocket = self
self.ReplySocket = pairedSocket
def SendImageBinary (self, filename, flags):
try:
f = open(filename, 'rb')
img_bytes = bytearray(f.read())
f.close()
byte_str = base64.b64encode(img_bytes)
self.socket.send(byte_str, flags)
print("sent as binary image: ", filename)
return filename
except Exception as e:
print(e)
return None
def NewHeader(self, message_type, params, u_id):
""" header is a dictionary of meta data including the parameters for the message """
try:
message = {"msgType": message_type}
# if this is a reply, send it back with the same UUID the request came with
if u_id is not None:
message["uuid"] = u_id
else:
message["uuid"] = str(uuid.uuid4())
header = {"message": message, "parameters": params}
return header
except Exception as e:
print(e)
return
def SendHeader(self, header, flags):
""" header is a dictionary of meta data including the parameters for the message """
try:
# send the header
self.socket.send_json(header, flags)
print("sent header: ")
self.PrettyPrint(header)
except Exception as e:
print(e)
return
def PrettyPrint(self, json_str):
json_formatted_str = json.dumps(json_str, indent=2)
print(json_formatted_str)
def SendImage(self, filename, header):
""" multi-part message, header and image """
""" header is a dictionary of meta data """
""" image is a byte array """
try:
print("Send Header")
# send the header
self.SendHeader(header, zmq.SNDMORE)
print("Send binary image")
# then send the image, suitably encoded
self.SendImageBinary(filename, 0)
except Exception as e:
print(e)
return
def GetSingleImage(self, filename):
"""
gets a single image (as a binary block) from the message queue
stores the image locally to fName
error returns None, success returns fName
"""
try:
# no message throws error
img = self.socket.recv(zmq.NOBLOCK)
# null message
if img is None:
print("no image in queue")
return None
image = bytearray(base64.b64decode(img))
try:
f = open(filename, 'wb')
f.write(image)
f.close()
except:
print("error extracting image")
return None
return filename
# catches the NO_BLOCK exception
except Exception as e:
print(e)
return None
def ClearQueue(self):
""" clears the subscribe queue"""
data = self.socket.recv(zmq.NOBLOCK)
while data is not None:
data = self.socket.recv(zmq.NOBLOCK)
def GetHeader(self, message_type):
""" returns a header of a certain message type """
try:
# print("read header")
header = self.socket.recv_json(zmq.NOBLOCK)
if header is None:
return None
print("received header")
self.PrettyPrint(header)
if "message" not in header.keys():
print("missing message")
return None
message = header["message"]
# special case handled internally
if message["msgType"] == "ping":
print("was pinged")
message["msgType"] = "pingReply"
self.ReplySocket.SendHeader(header, 0)
print("sent ping reply")
return None
if message["msgType"] != message_type:
print("message is not: " + message_type)
return None
return header
# catches the NO_BLOCK exception
except Exception as e:
return None
def GetImage(self, destination_filename):
args = dict()
header = self.GetHeader("image")
if header is None:
return None
args["header"] = header
filename = self.GetSingleImage(destination_filename)
if filename is None:
print("missing incoming image")
return None
args["image"] = filename
return args
def PollFunction(self):
"""
polls for a function call
"""
header = self.GetHeader("function")
return header
def ReturnFunction(self, header):
self.SendHeader(header, 0)
def CallFunction(self, function, params, u_id):
header = self.NewHeader("function", params, u_id)
header["message"]["function"] = function
self.SendHeader(header, 0)
uuid = header["message"]["uuid"]
return uuid
def Send(self, message):
self.socket.send(message)
def Receive(self, flags):
message = self.socket.recv(flags)
return message
def Ping(self, sender, receiver, timeout):
pingHeader = sender.NewHeader("ping", [], None)
print(pingHeader)
sender.SendHeader(pingHeader, 0)
increment = 1
elapsed = 0
print("awaiting ping reply from " + receiver.id)
head = None
while elapsed < timeout and head is None:
elapsed += increment
time.sleep(increment)
head = receiver.GetHeader("pingReply")
print(".", end="", flush=True)
reply = (head is not None)
print(sender.id + " ping = " + str(reply))
return reply