mirror of
https://github.com/avatao-content/baseimage-tutorial-framework
synced 2024-11-22 18:41:32 +00:00
Move JSON (de)serialization to socket boundaries
This commit is contained in:
parent
31fea13a9a
commit
96762575ba
@ -1,5 +1,3 @@
|
|||||||
import json
|
|
||||||
|
|
||||||
from .networking.serialization import deserialize_all
|
from .networking.serialization import deserialize_all
|
||||||
from .networking.server_connector import ServerConnector
|
from .networking.server_connector import ServerConnector
|
||||||
|
|
||||||
@ -15,10 +13,9 @@ class EventHandlerBase:
|
|||||||
|
|
||||||
def event_handler_callback(self, msg_parts):
|
def event_handler_callback(self, msg_parts):
|
||||||
anchor, message = deserialize_all(*msg_parts)
|
anchor, message = deserialize_all(*msg_parts)
|
||||||
data_json = json.loads(message)
|
response = self.handle_event(anchor, message) if anchor != 'reset' else self.handle_reset(message)
|
||||||
response = self.handle_event(anchor, data_json) if anchor != 'reset' else self.handle_reset(data_json)
|
|
||||||
if response is None: return
|
if response is None: return
|
||||||
self.server_connector.send(anchor, json.dumps(response))
|
self.server_connector.send(anchor, response)
|
||||||
|
|
||||||
def handle_event(self, anchor, data_json):
|
def handle_event(self, anchor, data_json):
|
||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
@ -31,7 +28,7 @@ class EventHandlerBase:
|
|||||||
'anchor': anchor,
|
'anchor': anchor,
|
||||||
'data': data
|
'data': data
|
||||||
}
|
}
|
||||||
self.server_connector.send(anchor, json.dumps(message))
|
self.server_connector.send(anchor, message)
|
||||||
|
|
||||||
def subscribe(self, anchor):
|
def subscribe(self, anchor):
|
||||||
if anchor not in self.subscriptions:
|
if anchor not in self.subscriptions:
|
||||||
|
@ -1,4 +1,3 @@
|
|||||||
import json
|
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
|
||||||
from .networking.server_connector import ServerUplinkConnector
|
from .networking.server_connector import ServerUplinkConnector
|
||||||
@ -19,4 +18,4 @@ class MessageSender:
|
|||||||
'anchor': self.anchor,
|
'anchor': self.anchor,
|
||||||
'data': data
|
'data': data
|
||||||
}
|
}
|
||||||
self.server_connector.send(self.anchor, json.dumps(response))
|
self.server_connector.send(self.anchor, response)
|
||||||
|
@ -6,7 +6,7 @@ from .serialization import serialize_all
|
|||||||
from ..config import PUBLISHER_PORT, RECEIVER_PORT
|
from ..config import PUBLISHER_PORT, RECEIVER_PORT
|
||||||
from ..config.logs import logging
|
from ..config.logs import logging
|
||||||
log = logging.getLogger(__name__)
|
log = logging.getLogger(__name__)
|
||||||
from ..util import parse_anchor_from_message, ZMQConnectorBase
|
from ..util import ZMQConnectorBase
|
||||||
|
|
||||||
|
|
||||||
ioloop.install()
|
ioloop.install()
|
||||||
@ -38,7 +38,7 @@ class EventHandlerConnector(EventHandlerDownlinkConnector, EventHandlerUplinkCon
|
|||||||
def register_callback(self, callback):
|
def register_callback(self, callback):
|
||||||
self._zmq_pull_stream.on_recv(callback)
|
self._zmq_pull_stream.on_recv(callback)
|
||||||
|
|
||||||
def send_message(self, message: str, anchor: str = None):
|
def send_message(self, message: dict, anchor: str = None):
|
||||||
if not anchor:
|
if not anchor:
|
||||||
anchor = parse_anchor_from_message(message)
|
anchor = message['anchor']
|
||||||
self._zmq_pub_socket.send_multipart(serialize_all(anchor, message))
|
self._zmq_pub_socket.send_multipart(serialize_all(anchor, message))
|
||||||
|
@ -1,3 +1,6 @@
|
|||||||
|
import json
|
||||||
|
|
||||||
|
|
||||||
def encode_if_needed(value):
|
def encode_if_needed(value):
|
||||||
if isinstance(value, str):
|
if isinstance(value, str):
|
||||||
value = value.encode('utf-8')
|
value = value.encode('utf-8')
|
||||||
@ -10,9 +13,9 @@ def decode_if_needed(value):
|
|||||||
return value
|
return value
|
||||||
|
|
||||||
|
|
||||||
def serialize_all(*args):
|
def serialize_all(anchor, message):
|
||||||
return [encode_if_needed(a) for a in args]
|
return [encode_if_needed(a) for a in (anchor, json.dumps(message))]
|
||||||
|
|
||||||
|
|
||||||
def deserialize_all(*args):
|
def deserialize_all(anchor, message):
|
||||||
return [decode_if_needed(a) for a in args]
|
return decode_if_needed(anchor), json.loads(message)
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
import json
|
import json
|
||||||
|
|
||||||
from tornado.websocket import WebSocketHandler
|
from tornado.websocket import WebSocketHandler
|
||||||
|
|
||||||
from .serialization import deserialize_all
|
from .serialization import deserialize_all
|
||||||
from ..util import parse_anchor_from_message
|
|
||||||
from .event_handler_connector import EventHandlerConnector
|
from .event_handler_connector import EventHandlerConnector
|
||||||
from ..config.logs import logging
|
from ..config.logs import logging
|
||||||
log = logging.getLogger(__name__)
|
log = logging.getLogger(__name__)
|
||||||
@ -27,12 +27,12 @@ class ZMQWebSocketHandler(WebSocketHandler):
|
|||||||
|
|
||||||
def on_message(self, message):
|
def on_message(self, message):
|
||||||
log.debug('Received on WebSocket: {}'.format(message))
|
log.debug('Received on WebSocket: {}'.format(message))
|
||||||
self.send_message(*self.make_response(message))
|
self.send_message(*self.make_response(json.loads(message)))
|
||||||
|
|
||||||
def make_response(self, message):
|
def make_response(self, message):
|
||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
|
|
||||||
def send_message(self, message: str, anchor: str = None):
|
def send_message(self, message: dict, anchor: str = None):
|
||||||
self._event_handler_connector.send_message(message, anchor)
|
self._event_handler_connector.send_message(message, anchor)
|
||||||
|
|
||||||
def on_close(self):
|
def on_close(self):
|
||||||
@ -53,10 +53,10 @@ class FSMManagingSocketHandler(ZMQWebSocketHandler):
|
|||||||
self._event_handler_connector.send_message(message)
|
self._event_handler_connector.send_message(message)
|
||||||
|
|
||||||
def make_response(self, message):
|
def make_response(self, message):
|
||||||
self.fsm.trigger(parse_anchor_from_message(message), message=message)
|
self.fsm.trigger(message['anchor'], message=message)
|
||||||
anchor = 'FSMUpdate'
|
anchor = 'FSMUpdate'
|
||||||
response = {'anchor': anchor,
|
response = {'anchor': anchor,
|
||||||
'data': {'current_state': self.fsm.state,
|
'data': {'current_state': self.fsm.state,
|
||||||
'valid_transitions':
|
'valid_transitions':
|
||||||
[{'trigger': trigger} for trigger in self.fsm.machine.get_triggers()]}}
|
[{'trigger': trigger} for trigger in self.fsm.machine.get_triggers()]}}
|
||||||
return json.dumps(response), anchor
|
return response, anchor
|
||||||
|
@ -3,11 +3,6 @@ import json, xmlrpc.client, zmq
|
|||||||
from .config.envvars import SUPERVISOR_HTTP_URI
|
from .config.envvars import SUPERVISOR_HTTP_URI
|
||||||
|
|
||||||
|
|
||||||
def parse_anchor_from_message(message):
|
|
||||||
message_json = json.loads(message)
|
|
||||||
return message_json['anchor']
|
|
||||||
|
|
||||||
|
|
||||||
def create_source_code_response_data(filename, content, language):
|
def create_source_code_response_data(filename, content, language):
|
||||||
return {
|
return {
|
||||||
'filename': filename,
|
'filename': filename,
|
||||||
|
Loading…
Reference in New Issue
Block a user