2018-04-02 22:41:10 +00:00
|
|
|
# pylint: disable=E1101
|
2018-04-02 23:47:50 +00:00
|
|
|
# ^
|
|
|
|
# fags
|
|
|
|
# v
|
|
|
|
import zmq
|
2018-04-02 22:41:10 +00:00
|
|
|
from lib import Service
|
|
|
|
from lib.net import ActionManager, Message
|
|
|
|
|
|
|
|
|
|
|
|
class Listener(Service):
|
2018-04-02 23:47:50 +00:00
|
|
|
"""Base class for listeners"""
|
2018-04-02 22:41:10 +00:00
|
|
|
def __init__(self, thread, id, root):
|
|
|
|
super().__init__(thread, id, root)
|
|
|
|
self._logger.add_field('service', 'Listener')
|
|
|
|
self._logger.add_field('vname', self.__class__.__name__)
|
|
|
|
|
|
|
|
class ZMQListener(Listener):
|
2018-04-02 23:47:50 +00:00
|
|
|
"""ZMQ (Zero MQ) listener - uses my own shitty legacy proto"""
|
2018-04-02 22:41:10 +00:00
|
|
|
def __init__(self, id, root):
|
|
|
|
super().__init__(self.__run, id, root)
|
|
|
|
|
|
|
|
def _pre_start(self):
|
|
|
|
self._running = True
|
|
|
|
self._z_ctx = zmq.Context()
|
|
|
|
self._z_sck = self._z_ctx.socket(zmq.REP)
|
2018-07-28 12:21:15 +00:00
|
|
|
self._z_sck.bind("tcp://%s:%s" % (self.lcnf.get('listen', '127.0.0.1'), self.lcnf.get('port', 12321)))
|
2018-04-02 22:41:10 +00:00
|
|
|
|
|
|
|
def _post_stop(self):
|
|
|
|
self._z_ctx.destroy()
|
|
|
|
|
|
|
|
def __run(self):
|
|
|
|
while self._running:
|
|
|
|
try:
|
|
|
|
reply = Message()
|
|
|
|
msg = None
|
|
|
|
try:
|
|
|
|
msg = Message.load(self._z_sck.recv())
|
|
|
|
if msg:
|
|
|
|
self._logger.info("Requested %s", msg.get('action'))
|
|
|
|
action = ActionManager.get(msg.get('action'))(self._datapool)
|
|
|
|
reply.set('data', action.run(msg.get('data')))
|
|
|
|
reply.set('status', True)
|
|
|
|
except Exception as e:
|
|
|
|
self._logger.warn("Action %s failed", msg.get('action'))
|
|
|
|
self._logger.debug(e)
|
|
|
|
reply.set('data', e)
|
|
|
|
reply.set('status', False)
|
|
|
|
self._z_sck.send(reply.dump())
|
|
|
|
except Exception as e:
|
|
|
|
self._logger.error(e)
|