mirror of
https://github.com/avatao-content/baseimage-tutorial-framework
synced 2024-11-22 18:21:31 +00:00
Further improve EventHandler <-> EventHandlerBase port situation
This commit is contained in:
parent
49844c7b58
commit
ca697b41b3
@ -1,10 +1,9 @@
|
|||||||
from tfw.event_handlers import EventHandlerBase
|
from tfw.event_handlers import EventHandlerBase
|
||||||
from tfw.networking import Scope
|
|
||||||
|
|
||||||
from .tfw_server_connector import TFWServerConnector
|
from .tfw_server_connector import TFWServerConnector
|
||||||
|
|
||||||
|
|
||||||
class EventHandler(EventHandlerBase):
|
class EventHandler(EventHandlerBase):
|
||||||
# pylint: disable=abstract-method
|
# pylint: disable=abstract-method
|
||||||
def __init__(self, key, scope=Scope.ZMQ):
|
def _build_server_connector(self):
|
||||||
super().__init__(key, TFWServerConnector(), scope=scope)
|
return TFWServerConnector()
|
||||||
|
@ -1,12 +1,10 @@
|
|||||||
from abc import ABC
|
|
||||||
|
|
||||||
from tfw.components import FSMAware
|
from tfw.components import FSMAware
|
||||||
from tfw.networking import Scope
|
from tfw.networking import Scope
|
||||||
|
|
||||||
from .event_handler import EventHandler
|
from .event_handler import EventHandler
|
||||||
|
|
||||||
|
|
||||||
class FSMAwareEventHandler(EventHandler, FSMAware, ABC):
|
class FSMAwareEventHandler(EventHandler, FSMAware):
|
||||||
# pylint: disable=abstract-method
|
# pylint: disable=abstract-method
|
||||||
"""
|
"""
|
||||||
Abstract base class for EventHandlers which automatically
|
Abstract base class for EventHandlers which automatically
|
||||||
|
@ -1,19 +1,25 @@
|
|||||||
from functools import partial
|
|
||||||
|
|
||||||
from tfw.networking import ServerUplinkConnector, ServerConnector
|
from tfw.networking import ServerUplinkConnector, ServerConnector
|
||||||
from tfw.config import TFWENV
|
from tfw.config import TFWENV
|
||||||
|
|
||||||
|
|
||||||
UPLINK_CONN_ADDR = f'tcp://localhost:{TFWENV.PULL_PORT}'
|
class ConnAddrMixin:
|
||||||
DOWNLINK_CONN_ADDR = f'tcp://localhost:{TFWENV.PUB_PORT}'
|
@property
|
||||||
|
def uplink_conn_addr(self):
|
||||||
|
return f'tcp://localhost:{TFWENV.PULL_PORT}'
|
||||||
|
|
||||||
|
@property
|
||||||
|
def downlink_conn_addr(self):
|
||||||
|
return f'tcp://localhost:{TFWENV.PUB_PORT}'
|
||||||
|
|
||||||
|
|
||||||
TFWServerUplinkConnector = partial(
|
class TFWServerUplinkConnector(ServerUplinkConnector, ConnAddrMixin):
|
||||||
ServerUplinkConnector,
|
def __init__(self):
|
||||||
connect_addr=UPLINK_CONN_ADDR
|
super().__init__(self.uplink_conn_addr)
|
||||||
)
|
|
||||||
TFWServerConnector = partial(
|
|
||||||
ServerConnector,
|
class TFWServerConnector(ServerConnector, ConnAddrMixin):
|
||||||
downlink_connect_addr=DOWNLINK_CONN_ADDR,
|
def __init__(self):
|
||||||
uplink_connect_addr=UPLINK_CONN_ADDR
|
super().__init__(
|
||||||
|
self.downlink_conn_addr,
|
||||||
|
self.uplink_conn_addr
|
||||||
)
|
)
|
||||||
|
@ -1,10 +1,13 @@
|
|||||||
import logging
|
import logging
|
||||||
|
from abc import ABC, abstractmethod
|
||||||
from typing import Iterable
|
from typing import Iterable
|
||||||
|
|
||||||
|
from tfw.networking import Scope
|
||||||
|
|
||||||
LOG = logging.getLogger(__name__)
|
LOG = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
class EventHandlerBase:
|
class EventHandlerBase(ABC):
|
||||||
"""
|
"""
|
||||||
Abstract base class for all Python based EventHandlers. Useful implementation template
|
Abstract base class for all Python based EventHandlers. Useful implementation template
|
||||||
for other languages.
|
for other languages.
|
||||||
@ -13,9 +16,9 @@ class EventHandlerBase:
|
|||||||
"""
|
"""
|
||||||
_instances = set()
|
_instances = set()
|
||||||
|
|
||||||
def __init__(self, key, server_connector, scope):
|
def __init__(self, key, scope=Scope.ZMQ):
|
||||||
type(self)._instances.add(self)
|
type(self)._instances.add(self)
|
||||||
self.server_connector = server_connector
|
self.server_connector = self._build_server_connector()
|
||||||
self.scope = scope
|
self.scope = scope
|
||||||
self.keys = []
|
self.keys = []
|
||||||
if isinstance(key, str):
|
if isinstance(key, str):
|
||||||
@ -26,6 +29,10 @@ class EventHandlerBase:
|
|||||||
self.subscribe(*self.keys)
|
self.subscribe(*self.keys)
|
||||||
self.server_connector.register_callback(self.event_handler_callback)
|
self.server_connector.register_callback(self.event_handler_callback)
|
||||||
|
|
||||||
|
@abstractmethod
|
||||||
|
def _build_server_connector(self):
|
||||||
|
raise NotImplementedError()
|
||||||
|
|
||||||
def subscribe(self, *keys):
|
def subscribe(self, *keys):
|
||||||
"""
|
"""
|
||||||
Subscribe this EventHandler to receive events for given keys.
|
Subscribe this EventHandler to receive events for given keys.
|
||||||
|
Loading…
Reference in New Issue
Block a user