mirror of
https://github.com/avatao-content/baseimage-tutorial-framework
synced 2024-11-05 21:11:21 +00:00
Refactor Component to Component and ComponentBase
This commit is contained in:
parent
d507b3d066
commit
66d684f022
@ -1,31 +1,20 @@
|
|||||||
import json
|
import json
|
||||||
from functools import partial
|
from functools import partial
|
||||||
|
|
||||||
import zmq
|
from component_base import ComponentBase
|
||||||
from zmq.eventloop.zmqstream import ZMQStream
|
|
||||||
from zmq.eventloop import ioloop
|
|
||||||
|
|
||||||
from config import RECEIVER_PORT, PUBLISHER_PORT
|
|
||||||
|
|
||||||
ioloop.install()
|
|
||||||
|
|
||||||
|
|
||||||
class Component:
|
class Component(ComponentBase):
|
||||||
def __init__(self, anchor, event_handler, zmq_context=None):
|
def __init__(self, anchor, event_handler, zmq_context=None):
|
||||||
self.anchor = anchor
|
super().__init__(anchor, event_handler, zmq_context)
|
||||||
self.event_handler = event_handler
|
|
||||||
self.zmq_context = zmq_context or zmq.Context.instance()
|
|
||||||
self.zmq_sub_socket = self.zmq_context.socket(zmq.SUB)
|
|
||||||
self.zmq_sub_socket.setsockopt_string(zmq.SUBSCRIBE, anchor)
|
|
||||||
self.zmq_sub_socket.connect('tcp://localhost:{}'.format(PUBLISHER_PORT))
|
|
||||||
self.zmq_sub_stream = ZMQStream(self.zmq_sub_socket)
|
|
||||||
self.zmq_push_socket = self.zmq_context.socket(zmq.PUSH)
|
|
||||||
self.zmq_push_socket.connect('tcp://localhost:{}'.format(RECEIVER_PORT))
|
|
||||||
|
|
||||||
def wrapper(msg_parts, handler):
|
def wrapper(msg_parts, handler):
|
||||||
anchor, message = msg_parts
|
anchor, message = msg_parts
|
||||||
data_json = json.loads(message)
|
data_json = json.loads(message)
|
||||||
response = json.dumps(handler(data_json)).encode('utf-8')
|
response = handler(data_json, self)
|
||||||
self.zmq_push_socket.send_multipart([anchor, response])
|
encoded_response = json.dumps(response).encode('utf-8')
|
||||||
|
self.zmq_push_socket.send_multipart([anchor, encoded_response])
|
||||||
|
|
||||||
self.zmq_sub_stream.on_recv(partial(wrapper, handler=event_handler))
|
self.zmq_sub_stream.on_recv(partial(wrapper, handler=event_handler))
|
||||||
|
|
||||||
|
|
||||||
|
21
src/components/component_base.py
Normal file
21
src/components/component_base.py
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
import zmq
|
||||||
|
from zmq.eventloop import ioloop
|
||||||
|
from zmq.eventloop.zmqstream import ZMQStream
|
||||||
|
|
||||||
|
from config import PUBLISHER_PORT, RECEIVER_PORT
|
||||||
|
|
||||||
|
ioloop.install()
|
||||||
|
|
||||||
|
|
||||||
|
class ComponentBase:
|
||||||
|
def __init__(self, anchor, event_handler, zmq_context=None):
|
||||||
|
self.anchor = anchor
|
||||||
|
self.event_handler = event_handler
|
||||||
|
self.zmq_context = zmq_context or zmq.Context.instance()
|
||||||
|
self.zmq_sub_socket = self.zmq_context.socket(zmq.SUB)
|
||||||
|
self.zmq_sub_socket.setsockopt_string(zmq.SUBSCRIBE, anchor)
|
||||||
|
self.zmq_sub_socket.connect('tcp://localhost:{}'.format(PUBLISHER_PORT))
|
||||||
|
self.zmq_sub_stream = ZMQStream(self.zmq_sub_socket)
|
||||||
|
self.zmq_push_socket = self.zmq_context.socket(zmq.PUSH)
|
||||||
|
self.zmq_push_socket.connect('tcp://localhost:{}'.format(RECEIVER_PORT))
|
||||||
|
|
Loading…
Reference in New Issue
Block a user