Rename Messaging to ServerConnector

This commit is contained in:
Bálint Bokros 2018-01-26 15:16:34 +01:00
parent 585cd35e48
commit 80ada91cec
4 changed files with 13 additions and 13 deletions

View File

@ -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()

View File

@ -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

View File

@ -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])

View File

@ -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()