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