mirror of
https://github.com/avatao-content/baseimage-tutorial-framework
synced 2024-11-22 22:11:32 +00:00
OO Refactor event_handler_connector.py temporary solution
This commit is contained in:
parent
16dad5e2cd
commit
6e6d775a05
@ -1,4 +1,4 @@
|
|||||||
import json, xmlrpc.client
|
import json, xmlrpc.client, zmq
|
||||||
|
|
||||||
from config.envvars import SUPERVISOR_HTTP_URI
|
from config.envvars import SUPERVISOR_HTTP_URI
|
||||||
|
|
||||||
@ -15,5 +15,11 @@ def create_source_code_response_data(filename, content, language):
|
|||||||
'language': language
|
'language': language
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
class SupervisorMixin:
|
class SupervisorMixin:
|
||||||
supervisor = xmlrpc.client.ServerProxy(SUPERVISOR_HTTP_URI).supervisor
|
supervisor = xmlrpc.client.ServerProxy(SUPERVISOR_HTTP_URI).supervisor
|
||||||
|
|
||||||
|
|
||||||
|
class ZMQConnectorBase:
|
||||||
|
def __init__(self, zmq_context=None):
|
||||||
|
self._zmq_context = zmq_context or zmq.Context.instance()
|
||||||
|
@ -5,30 +5,45 @@ from zmq.eventloop import ioloop
|
|||||||
from zmq.eventloop.zmqstream import ZMQStream
|
from zmq.eventloop.zmqstream import ZMQStream
|
||||||
|
|
||||||
from config import PUBLISHER_PORT, RECEIVER_PORT
|
from config import PUBLISHER_PORT, RECEIVER_PORT
|
||||||
from util import parse_anchor_from_message
|
from util import parse_anchor_from_message, ZMQConnectorBase
|
||||||
|
|
||||||
ioloop.install()
|
ioloop.install()
|
||||||
|
|
||||||
_zmq_context = zmq.Context.instance()
|
|
||||||
_zmq_pull_socket = _zmq_context.socket(zmq.PULL)
|
|
||||||
_zmq_pull_stream = ZMQStream(_zmq_pull_socket)
|
|
||||||
_zmq_pub_socket = _zmq_context.socket(zmq.PUB)
|
|
||||||
|
|
||||||
pub_socket_address = 'tcp://*:{}'.format(PUBLISHER_PORT)
|
class EventHandlerDownlinkConnector(ZMQConnectorBase):
|
||||||
_zmq_pub_socket.bind(pub_socket_address)
|
def __init__(self, zmq_context=None):
|
||||||
logging.debug('Pub socket bound to {}'.format(pub_socket_address))
|
super().__init__(zmq_context)
|
||||||
|
self._zmq_pull_socket = self._zmq_context.socket(zmq.PULL)
|
||||||
pull_socket_address = 'tcp://*:{}'.format(RECEIVER_PORT)
|
self._zmq_pull_stream = ZMQStream(self._zmq_pull_socket)
|
||||||
_zmq_pull_socket.bind(pull_socket_address)
|
address = 'tcp://*:{}'.format(RECEIVER_PORT)
|
||||||
logging.debug('Pull socket bound to {}'.format(pull_socket_address))
|
self._zmq_pull_socket.bind(address)
|
||||||
|
logging.debug('Pull socket bound to {}'.format(address))
|
||||||
|
|
||||||
|
|
||||||
def register_callback(callback):
|
class EventHandlerUplinkConnector(ZMQConnectorBase):
|
||||||
_zmq_pull_stream.on_recv(callback)
|
def __init__(self, zmq_context=None):
|
||||||
|
super().__init__(zmq_context)
|
||||||
|
self._zmq_pub_socket = self._zmq_context.socket(zmq.PUB)
|
||||||
|
address = 'tcp://*:{}'.format(PUBLISHER_PORT)
|
||||||
|
self._zmq_pub_socket.bind(address)
|
||||||
|
logging.debug('Pub socket bound to {}'.format(address))
|
||||||
|
|
||||||
|
|
||||||
def send_message(message: str, anchor: str = None):
|
class EventHandlerConnector(EventHandlerDownlinkConnector, EventHandlerUplinkConnector):
|
||||||
|
def __init__(self, zmq_context=None):
|
||||||
|
self.downlink = EventHandlerDownlinkConnector(zmq_context)
|
||||||
|
self.uplink = EventHandlerUplinkConnector(zmq_context)
|
||||||
|
#EventHandlerDownlinkConnector.__init__(self, zmq_context) # TODO: solve this with multiple inheritance
|
||||||
|
#EventHandlerUplinkConnector.__init__(self, zmq_context)
|
||||||
|
|
||||||
|
def register_callback(self, callback):
|
||||||
|
self.downlink._zmq_pull_stream.on_recv(callback)
|
||||||
|
|
||||||
|
def send_message(self, message: str, anchor: str = None):
|
||||||
if not anchor:
|
if not anchor:
|
||||||
anchor = parse_anchor_from_message(message)
|
anchor = parse_anchor_from_message(message)
|
||||||
encoded_message = [part.encode('utf-8') for part in (anchor, message)]
|
encoded_message = [part.encode('utf-8') for part in (anchor, message)]
|
||||||
_zmq_pub_socket.send_multipart(encoded_message)
|
self.uplink._zmq_pub_socket.send_multipart(encoded_message)
|
||||||
|
|
||||||
|
|
||||||
|
ehc = EventHandlerConnector()
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
from transitions import Machine
|
from transitions import Machine
|
||||||
|
|
||||||
import event_handler_connector
|
from event_handler_connector import ehc
|
||||||
|
|
||||||
|
|
||||||
class FSMBase:
|
class FSMBase:
|
||||||
@ -17,4 +17,4 @@ class FSMBase:
|
|||||||
|
|
||||||
def forward_message(self, event_data):
|
def forward_message(self, event_data):
|
||||||
message = event_data.kwargs.get('message')
|
message = event_data.kwargs.get('message')
|
||||||
event_handler_connector.send_message(message)
|
ehc.send_message(message)
|
||||||
|
@ -3,8 +3,7 @@ import json
|
|||||||
from tornado.websocket import WebSocketHandler
|
from tornado.websocket import WebSocketHandler
|
||||||
from util import parse_anchor_from_message
|
from util import parse_anchor_from_message
|
||||||
|
|
||||||
import event_handler_connector
|
from event_handler_connector import ehc
|
||||||
|
|
||||||
|
|
||||||
class ZMQWebSocketHandler(WebSocketHandler):
|
class ZMQWebSocketHandler(WebSocketHandler):
|
||||||
def __init__(self, application, request, **kwargs):
|
def __init__(self, application, request, **kwargs):
|
||||||
@ -12,7 +11,7 @@ class ZMQWebSocketHandler(WebSocketHandler):
|
|||||||
|
|
||||||
def open(self, *args, **kwargs):
|
def open(self, *args, **kwargs):
|
||||||
logging.debug('WebSocket connection initiated')
|
logging.debug('WebSocket connection initiated')
|
||||||
event_handler_connector.register_callback(self.zmq_callback)
|
ehc.register_callback(self.zmq_callback)
|
||||||
|
|
||||||
def zmq_callback(self, msg_parts):
|
def zmq_callback(self, msg_parts):
|
||||||
anchor, data = msg_parts
|
anchor, data = msg_parts
|
||||||
@ -27,7 +26,7 @@ class ZMQWebSocketHandler(WebSocketHandler):
|
|||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
|
|
||||||
def send_message(self, message: str, anchor: str = None):
|
def send_message(self, message: str, anchor: str = None):
|
||||||
event_handler_connector.send_message(message, anchor)
|
ehc.send_message(message, anchor)
|
||||||
|
|
||||||
def on_close(self):
|
def on_close(self):
|
||||||
pass
|
pass
|
||||||
|
Loading…
Reference in New Issue
Block a user