mirror of
https://github.com/avatao-content/baseimage-tutorial-framework
synced 2024-11-25 05:51:31 +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):
|
||||
self.connector.subscribe('frontend.ready')
|
||||
|
||||
def handle_event(self, message, connector):
|
||||
def handle_event(self, message, _):
|
||||
try:
|
||||
message = self.command_handlers[message['key']](message)
|
||||
if message:
|
||||
fsm_update_message = self._fsm_updater.fsm_update
|
||||
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:
|
||||
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)
|
||||
]
|
||||
if not self.fsm.event_log:
|
||||
return {}
|
||||
return None
|
||||
last_fsm_event = self.fsm.event_log[-1]
|
||||
return {
|
||||
'current_state': self.fsm.state,
|
||||
|
@ -1,2 +1,2 @@
|
||||
# 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 .fsm_aware_event_handler import FSMAwareEventHandler
|
||||
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
|
||||
def _event_callback(self, message):
|
||||
if message.get('intent') != Intent.EVENT.value:
|
||||
self.handle_event(message, self.connector)
|
||||
def _validate_message(self, message):
|
||||
return message.get('intent') != Intent.EVENT.value
|
||||
|
@ -9,7 +9,11 @@ class EventHandler:
|
||||
self.connector.register_callback(self._event_callback)
|
||||
|
||||
def _event_callback(self, message):
|
||||
self.handle_event(message, self.connector)
|
||||
if self._validate_message(message):
|
||||
self.handle_event(message, self.connector)
|
||||
|
||||
def _validate_message(self, message):
|
||||
return True
|
||||
|
||||
def handle_event(self, message, connector):
|
||||
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 .control_event_handler import ControlEventHandler
|
||||
from .event_handler import EventHandler
|
||||
|
||||
LOG = logging.getLogger(__name__)
|
||||
|
||||
|
||||
# pylint: disable=abstract-method
|
||||
class SignedEventHandler(ControlEventHandler):
|
||||
class SignedEventHandler(EventHandler):
|
||||
def __init__(self, connector):
|
||||
self._auth_key = KeyManager().auth_key
|
||||
super().__init__(connector)
|
||||
|
||||
def _event_callback(self, message):
|
||||
if verify_message(self._auth_key, message):
|
||||
self.handle_event(message, self.connector)
|
||||
else:
|
||||
def _validate_message(self, message):
|
||||
is_valid = verify_message(self._auth_key, message)
|
||||
if not is_valid:
|
||||
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):
|
||||
message['scope'] = scope.value
|
||||
if isinstance(intent, Intent):
|
||||
if intent is not None:
|
||||
message['intent'] = intent.value
|
||||
self._zmq_push_socket.send_multipart(serialize_tfw_msg(message))
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user