mirror of
https://github.com/avatao-content/baseimage-tutorial-framework
synced 2024-12-23 02:31:56 +00:00
Rename Messaging to ServerConnector
This commit is contained in:
parent
585cd35e48
commit
80ada91cec
@ -1,16 +1,16 @@
|
||||
import json
|
||||
|
||||
from messaging import Messaging
|
||||
from server_connector import ServerConnector
|
||||
|
||||
|
||||
class EventHandlerBase:
|
||||
def __init__(self, anchor):
|
||||
self.messaging = Messaging()
|
||||
self.server_connector = ServerConnector()
|
||||
self.anchor = anchor
|
||||
self.subscriptions = set()
|
||||
self.subscribe(self.anchor)
|
||||
self.subscribe('reset')
|
||||
self.messaging.register_callback(self.event_handler_callback)
|
||||
self.server_connector.register_callback(self.event_handler_callback)
|
||||
|
||||
def event_handler_callback(self, msg_parts):
|
||||
anchor, message = msg_parts
|
||||
@ -18,7 +18,7 @@ class EventHandlerBase:
|
||||
response = self.handle_event(anchor, data_json) if anchor != b'reset' else self.handle_reset(data_json)
|
||||
if response is None: return
|
||||
encoded_response = json.dumps(response).encode('utf-8')
|
||||
self.messaging.send(anchor, encoded_response)
|
||||
self.server_connector.send(anchor, encoded_response)
|
||||
|
||||
def handle_event(self, anchor, data_json):
|
||||
raise NotImplementedError
|
||||
@ -33,21 +33,21 @@ class EventHandlerBase:
|
||||
'data': data
|
||||
}
|
||||
encoded_message = json.dumps(message).encode('utf-8')
|
||||
self.messaging.send(encoded_anchor, encoded_message)
|
||||
self.server_connector.send(encoded_anchor, encoded_message)
|
||||
|
||||
def subscribe(self, anchor):
|
||||
if anchor not in self.subscriptions:
|
||||
self.subscriptions.add(anchor)
|
||||
self.messaging.subscribe(anchor)
|
||||
self.server_connector.subscribe(anchor)
|
||||
|
||||
def unsubscribe(self, anchor):
|
||||
try:
|
||||
self.subscriptions.remove(anchor)
|
||||
self.messaging.unsubscribe(anchor)
|
||||
self.server_connector.unsubscribe(anchor)
|
||||
except KeyError:
|
||||
pass
|
||||
|
||||
def unsubscribe_all(self):
|
||||
for sub in self.subscriptions:
|
||||
self.messaging.unsubscribe(anchor=sub)
|
||||
self.server_connector.unsubscribe(anchor=sub)
|
||||
self.subscriptions.clear()
|
||||
|
@ -4,7 +4,7 @@ from functools import partial
|
||||
|
||||
import source_code
|
||||
from event_handler_base import EventHandlerBase
|
||||
from messaging import Messaging
|
||||
from server_connector import ServerConnector
|
||||
from source_code_event_handler import SourceCodeEventHandler
|
||||
from terminado_event_handler import TerminadoEventHandler
|
||||
from tornado.ioloop import IOLoop
|
||||
|
@ -9,7 +9,7 @@ from config import PUBLISHER_PORT, RECEIVER_PORT
|
||||
ioloop.install()
|
||||
|
||||
|
||||
class Messaging:
|
||||
class ServerConnector:
|
||||
def __init__(self):
|
||||
self._zmq_context = zmq.Context.instance()
|
||||
self._zmq_sub_socket = self._zmq_context.socket(zmq.SUB)
|
||||
@ -21,6 +21,6 @@ class Messaging:
|
||||
self.subscribe = partial(self._zmq_sub_socket.setsockopt_string, zmq.SUBSCRIBE)
|
||||
self.unsubscribe = partial(self._zmq_sub_socket.setsockopt_string, zmq.UNSUBSCRIBE)
|
||||
self.register_callback = self._zmq_sub_stream.on_recv
|
||||
|
||||
|
||||
def send(self, anchor, response):
|
||||
self._zmq_push_socket.send_multipart([anchor, response])
|
@ -29,9 +29,9 @@ class StatefulEventHandler(EventHandlerBase):
|
||||
'data': response_data,
|
||||
})
|
||||
response = [r.encode('utf-8') for r in (response_anchor, response_data)]
|
||||
self.messaging.send(*response)
|
||||
self.server_connector.send(*response)
|
||||
|
||||
self.messaging.register_callback(event_handler_callback)
|
||||
self.server_connector.register_callback(event_handler_callback)
|
||||
|
||||
def unsubscribe_all(self):
|
||||
super().unsubscribe_all()
|
||||
|
Loading…
Reference in New Issue
Block a user