Move socket management to a separate package

This commit is contained in:
Bálint Bokros 2017-11-27 18:16:35 +01:00
parent 9b97e3dd19
commit d507b3d066
3 changed files with 46 additions and 33 deletions

View File

@ -1,5 +1,7 @@
from transitions import Machine
import component_connector
class Buttons:
states = ['ayy', 'bee', 'cee']
@ -9,8 +11,7 @@ class Buttons:
{'trigger': 'anchor_c', 'source': 'cee', 'dest': 'ayy'},
]
def __init__(self, handler=None):
self.handler = handler
def __init__(self):
self.machine = Machine(model=self,
states=Buttons.states,
transitions=Buttons.transitions,
@ -21,4 +22,4 @@ class Buttons:
def forward_message(self, event_data):
message = event_data.kwargs.get('message')
self.handler.send_message(message)
component_connector.send_message(message)

View File

@ -0,0 +1,35 @@
import logging
import json
import zmq
from zmq.eventloop import ioloop
from zmq.eventloop.zmqstream import ZMQStream
from config import PUBLISHER_PORT, RECEIVER_PORT
from util import parse_anchor_from_message
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)
_zmq_pub_socket.bind(pub_socket_address)
logging.debug('Pub socket bound to {}'.format(pub_socket_address))
pull_socket_address = 'tcp://*:{}'.format(RECEIVER_PORT)
_zmq_pull_socket.bind(pull_socket_address)
logging.debug('Pull socket bound to {}'.format(pull_socket_address))
def register_callback(callback):
_zmq_pull_stream.on_recv(callback)
def send_message(message: str, anchor: str = None):
if not anchor:
anchor = parse_anchor_from_message(message)
encoded_message = [part.encode('utf-8') for part in (anchor, message)]
_zmq_pub_socket.send_multipart(encoded_message)

View File

@ -1,60 +1,37 @@
import logging
import json
from tornado.websocket import WebSocketHandler
import zmq
from zmq.eventloop.zmqstream import ZMQStream
from zmq.eventloop import ioloop
from config import PUBLISHER_PORT, RECEIVER_PORT
from buttons import Buttons
from util import parse_anchor_from_message
ioloop.install()
import component_connector
class ZMQWebSocketHandler(WebSocketHandler):
def __init__(self, application, request, zmq_context=None, **kwargs):
def __init__(self, application, request, **kwargs):
super().__init__(application, request, **kwargs)
self.zmq_context = zmq_context or zmq.Context.instance()
self.zmq_pull_socket = self.zmq_context.socket(zmq.PULL)
self.zmq_pull_stream = ZMQStream(self.zmq_pull_socket)
self.zmq_pub_socket = self.zmq_context.socket(zmq.PUB)
self.fsm = Buttons(self)
def open(self, *args, **kwargs):
pub_socket_address = 'tcp://*:{}'.format(PUBLISHER_PORT)
self.zmq_pub_socket.bind(pub_socket_address)
logging.debug('Pub socket bound to {}'.format(pub_socket_address))
pull_socket_address = 'tcp://*:{}'.format(RECEIVER_PORT)
self.zmq_pull_socket.bind(pull_socket_address)
logging.debug('Pull socket bound to {}'.format(pull_socket_address))
def zmq_callback(msg_parts):
anchor, data = msg_parts
logging.debug('Received on pull socket: {}'.format(data.decode()))
self.write_message(data.decode())
self.zmq_pull_stream.on_recv(zmq_callback)
component_connector.register_callback(zmq_callback)
def on_message(self, message):
logging.debug('Received on WebSocket: {}'.format(message))
self.fsm.trigger(self._parse_anchor(message), message=message)
def send_message(self, message, anchor: str = None):
if not anchor:
anchor = self._parse_anchor(message)
encoded_message = [part.encode('utf-8') for part in (anchor, message)]
self.zmq_pub_socket.send_multipart(encoded_message)
def send_message(self, message: str, anchor: str = None):
component_connector.send_message(message, anchor)
def on_close(self):
self.zmq_pull_socket.close()
self.zmq_pub_socket.close()
pass
# much secure, very cors, wow
def check_origin(self, origin):
return True
@staticmethod
def _parse_anchor(message):
message_json = json.loads(message)
return message_json['anchor']