mirror of
https://github.com/avatao-content/baseimage-tutorial-framework
synced 2024-11-25 10:51:32 +00:00
Fix issues in PR #64
This commit is contained in:
parent
2e5867cc49
commit
965973a32f
@ -27,13 +27,13 @@ class FSMHandler:
|
|||||||
def start(self):
|
def start(self):
|
||||||
self.connector.subscribe('frontend.ready')
|
self.connector.subscribe('frontend.ready')
|
||||||
|
|
||||||
def handle_event(self, message, connector):
|
def handle_event(self, message, _):
|
||||||
try:
|
try:
|
||||||
message = self.command_handlers[message['key']](message)
|
message = self.command_handlers[message['key']](message)
|
||||||
if message:
|
if message:
|
||||||
fsm_update_message = self._fsm_updater.fsm_update
|
fsm_update_message = self._fsm_updater.fsm_update
|
||||||
sign_message(self.auth_key, fsm_update_message)
|
sign_message(self.auth_key, fsm_update_message)
|
||||||
connector.send_message(fsm_update_message, Scope.BROADCAST, Intent.EVENT)
|
self.connector.send_message(fsm_update_message, Scope.BROADCAST, Intent.EVENT)
|
||||||
except KeyError:
|
except KeyError:
|
||||||
LOG.error('IGNORING MESSAGE: Invalid message received: %s', message)
|
LOG.error('IGNORING MESSAGE: Invalid message received: %s', message)
|
||||||
|
|
||||||
|
@ -16,7 +16,7 @@ class FSMUpdater:
|
|||||||
for trigger in self.fsm.get_triggers(self.fsm.state)
|
for trigger in self.fsm.get_triggers(self.fsm.state)
|
||||||
]
|
]
|
||||||
if not self.fsm.event_log:
|
if not self.fsm.event_log:
|
||||||
return {}
|
return None
|
||||||
last_fsm_event = self.fsm.event_log[-1]
|
last_fsm_event = self.fsm.event_log[-1]
|
||||||
return {
|
return {
|
||||||
'current_state': self.fsm.state,
|
'current_state': self.fsm.state,
|
||||||
|
@ -1,2 +1,2 @@
|
|||||||
# pylint: disable=unused-import
|
# pylint: disable=unused-import
|
||||||
from tfw.internals.event_handling import EventHandler, ControlEventHandler, FSMAwareEventHandler, SignedEventHandler
|
from tfw.internals.event_handling import EventHandler, ControlEventHandler, FSMAwareEventHandler, SignedEventHandler, SignedControlEventHandler
|
||||||
|
@ -3,3 +3,4 @@ from .event_handler_factory_base import EventHandlerFactoryBase
|
|||||||
from .control_event_handler import ControlEventHandler
|
from .control_event_handler import ControlEventHandler
|
||||||
from .fsm_aware_event_handler import FSMAwareEventHandler
|
from .fsm_aware_event_handler import FSMAwareEventHandler
|
||||||
from .signed_event_handler import SignedEventHandler
|
from .signed_event_handler import SignedEventHandler
|
||||||
|
from .signed_control_event_handler import SignedControlEventHandler
|
||||||
|
@ -4,6 +4,5 @@ from .event_handler import EventHandler
|
|||||||
|
|
||||||
|
|
||||||
class ControlEventHandler(EventHandler): # pylint: disable=abstract-method
|
class ControlEventHandler(EventHandler): # pylint: disable=abstract-method
|
||||||
def _event_callback(self, message):
|
def _validate_message(self, message):
|
||||||
if message.get('intent') != Intent.EVENT.value:
|
return message.get('intent') != Intent.EVENT.value
|
||||||
self.handle_event(message, self.connector)
|
|
||||||
|
@ -9,8 +9,12 @@ class EventHandler:
|
|||||||
self.connector.register_callback(self._event_callback)
|
self.connector.register_callback(self._event_callback)
|
||||||
|
|
||||||
def _event_callback(self, message):
|
def _event_callback(self, message):
|
||||||
|
if self._validate_message(message):
|
||||||
self.handle_event(message, self.connector)
|
self.handle_event(message, self.connector)
|
||||||
|
|
||||||
|
def _validate_message(self, message):
|
||||||
|
return True
|
||||||
|
|
||||||
def handle_event(self, message, connector):
|
def handle_event(self, message, connector):
|
||||||
raise NotImplementedError()
|
raise NotImplementedError()
|
||||||
|
|
||||||
|
@ -0,0 +1,9 @@
|
|||||||
|
from .control_event_handler import ControlEventHandler
|
||||||
|
from .signed_event_handler import SignedEventHandler
|
||||||
|
|
||||||
|
class SignedControlEventHandler(ControlEventHandler, SignedEventHandler):
|
||||||
|
def _validate_message(self, message):
|
||||||
|
return (
|
||||||
|
ControlEventHandler._validate_message(self, message) and
|
||||||
|
SignedEventHandler._validate_message(self, message)
|
||||||
|
)
|
@ -2,19 +2,19 @@ import logging
|
|||||||
|
|
||||||
from tfw.internals.crypto import KeyManager, verify_message
|
from tfw.internals.crypto import KeyManager, verify_message
|
||||||
|
|
||||||
from .control_event_handler import ControlEventHandler
|
from .event_handler import EventHandler
|
||||||
|
|
||||||
LOG = logging.getLogger(__name__)
|
LOG = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
# pylint: disable=abstract-method
|
# pylint: disable=abstract-method
|
||||||
class SignedEventHandler(ControlEventHandler):
|
class SignedEventHandler(EventHandler):
|
||||||
def __init__(self, connector):
|
def __init__(self, connector):
|
||||||
self._auth_key = KeyManager().auth_key
|
self._auth_key = KeyManager().auth_key
|
||||||
super().__init__(connector)
|
super().__init__(connector)
|
||||||
|
|
||||||
def _event_callback(self, message):
|
def _validate_message(self, message):
|
||||||
if verify_message(self._auth_key, message):
|
is_valid = verify_message(self._auth_key, message)
|
||||||
self.handle_event(message, self.connector)
|
if not is_valid:
|
||||||
else:
|
|
||||||
LOG.error('Message does not have valid signature: %s', message)
|
LOG.error('Message does not have valid signature: %s', message)
|
||||||
|
return is_valid
|
||||||
|
@ -54,7 +54,7 @@ class ZMQUplinkConnector:
|
|||||||
|
|
||||||
def send_message(self, message, scope=Scope.ZMQ, intent=None):
|
def send_message(self, message, scope=Scope.ZMQ, intent=None):
|
||||||
message['scope'] = scope.value
|
message['scope'] = scope.value
|
||||||
if isinstance(intent, Intent):
|
if intent is not None:
|
||||||
message['intent'] = intent.value
|
message['intent'] = intent.value
|
||||||
self._zmq_push_socket.send_multipart(serialize_tfw_msg(message))
|
self._zmq_push_socket.send_multipart(serialize_tfw_msg(message))
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user