2014-09-08 16:55:17 +00:00
|
|
|
import zmq
|
|
|
|
import logging
|
2014-09-10 12:17:28 +00:00
|
|
|
import json
|
2014-10-27 10:25:55 +00:00
|
|
|
import threading as th
|
2014-09-08 16:55:17 +00:00
|
|
|
|
|
|
|
class ZmqReader(object):
|
|
|
|
|
|
|
|
'''Read missiles from zmq'''
|
|
|
|
|
2014-10-27 10:25:55 +00:00
|
|
|
def __init__(self, queue):
|
|
|
|
self.queue = queue
|
|
|
|
self.quit = th.Event()
|
2014-09-08 16:55:17 +00:00
|
|
|
self.log = logging.getLogger(__name__)
|
|
|
|
context = zmq.Context()
|
2014-09-10 12:17:28 +00:00
|
|
|
self.socket = context.socket(zmq.PULL)
|
|
|
|
self.socket.connect(self.queue)
|
|
|
|
self.log.info("ZMQ: Waiting for missiles from '%s'" % self.queue)
|
2014-09-08 16:55:17 +00:00
|
|
|
|
|
|
|
def __iter__(self):
|
|
|
|
while True:
|
|
|
|
try:
|
2014-10-27 10:25:55 +00:00
|
|
|
if not self.socket.poll(500):
|
|
|
|
if self.quit.is_set():
|
|
|
|
raise StopIteration()
|
|
|
|
continue
|
|
|
|
|
|
|
|
data = tuple(self.socket.recv_json())
|
|
|
|
|
|
|
|
if len(data) != 3:
|
|
|
|
if data[0] == "stop":
|
|
|
|
raise StopIteration()
|
|
|
|
continue
|
|
|
|
|
|
|
|
yield(data)
|
|
|
|
except StopIteration:
|
|
|
|
raise
|
2014-09-08 16:55:17 +00:00
|
|
|
except Exception as e:
|
|
|
|
print(e)
|
2014-10-27 10:25:55 +00:00
|
|
|
|
|
|
|
def stop(self):
|
|
|
|
self.quit.set()
|