mirror of
https://github.com/avatao-content/baseimage-tutorial-framework
synced 2024-11-05 13:51:21 +00:00
Merge pull request #13 from avatao-content/bottomless_pit
Refactor TFW architecture to support stateless event handling
This commit is contained in:
commit
41a32437b2
@ -24,9 +24,9 @@ class WebideReloadEventHandler(FileSystemEventHandler):
|
||||
def on_modified(self, event):
|
||||
if self._paused: return
|
||||
log.debug(event)
|
||||
anchor = 'anchor_webide'
|
||||
self.uplink.send(anchor, {'anchor': anchor,
|
||||
'data': {'command': 'reload'}})
|
||||
key = 'webide'
|
||||
self.uplink.send(key, {'key': key,
|
||||
'data': {'command': 'reload'}})
|
||||
|
||||
|
||||
class DirectoryMonitor:
|
||||
|
@ -2,7 +2,7 @@ from os.path import isfile, join, relpath
|
||||
from glob import glob
|
||||
|
||||
from tfw.components.mixins import SupervisorMixin
|
||||
from tfw.event_handler_base import EventHandlerBase
|
||||
from tfw.event_handler_base import TriggerlessEventHandler
|
||||
from tfw.components.directory_monitor import DirectoryMonitor
|
||||
|
||||
from tfw.config.logs import logging
|
||||
@ -43,9 +43,9 @@ class FileManager:
|
||||
return relpath(self._filepath(filename), start=self._workdir)
|
||||
|
||||
|
||||
class SourceCodeEventHandler(EventHandlerBase, SupervisorMixin):
|
||||
def __init__(self, anchor, directory, process_name, selected_file=None):
|
||||
super().__init__(anchor)
|
||||
class SourceCodeEventHandler(TriggerlessEventHandler, SupervisorMixin):
|
||||
def __init__(self, key, directory, process_name, selected_file=None):
|
||||
super().__init__(key)
|
||||
self.filemanager = FileManager(directory, selected_file=selected_file)
|
||||
self.process_name = process_name
|
||||
|
||||
@ -81,7 +81,7 @@ class SourceCodeEventHandler(EventHandlerBase, SupervisorMixin):
|
||||
data['filename'] = self.filemanager.filename
|
||||
data['files'] = self.filemanager.files
|
||||
|
||||
def handle_event(self, anchor, data_json):
|
||||
def handle_event(self, key, data_json):
|
||||
data = data_json['data']
|
||||
data_json['data'] = self.commands[data['command']](data)
|
||||
self.attach_fileinfo(data)
|
||||
|
@ -1,17 +1,17 @@
|
||||
from tfw.event_handler_base import EventHandlerBase
|
||||
from tfw.event_handler_base import TriggerlessEventHandler
|
||||
from tfw.components.mixins import SupervisorMixin
|
||||
from tfw.config import tfwenv
|
||||
from tfw.config.logs import logging
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class TerminadoEventHandler(EventHandlerBase, SupervisorMixin):
|
||||
def __init__(self, anchor, process_name):
|
||||
super().__init__(anchor)
|
||||
class TerminadoEventHandler(TriggerlessEventHandler, SupervisorMixin):
|
||||
def __init__(self, key, process_name):
|
||||
super().__init__(key)
|
||||
self.working_directory = tfwenv.TERMINADO_DIR
|
||||
self.process_name = process_name
|
||||
self.start_process()
|
||||
|
||||
def handle_event(self, anchor, data_json):
|
||||
log.debug('TerminadoEventHandler received event for anchor {}'.format(anchor))
|
||||
def handle_event(self, key, data_json):
|
||||
log.debug('TerminadoEventHandler received event for key {}'.format(key))
|
||||
# TODO: wat do?
|
||||
|
@ -14,25 +14,32 @@ def cenator():
|
||||
|
||||
|
||||
class EventHandlerBase:
|
||||
def __init__(self, anchor):
|
||||
def __init__(self, key):
|
||||
self.server_connector = ServerConnector()
|
||||
self.anchor = anchor
|
||||
self.key = key
|
||||
self.subscriptions = set()
|
||||
self.subscribe(self.anchor)
|
||||
self.subscribe(self.key)
|
||||
self.subscribe('reset')
|
||||
self.server_connector.register_callback(self.event_handler_callback)
|
||||
self.cenerator = cycle(cenator())
|
||||
|
||||
def event_handler_callback(self, msg_parts):
|
||||
anchor, message = deserialize_all(*msg_parts)
|
||||
key, message = deserialize_all(*msg_parts)
|
||||
from .message_sender import MessageSender
|
||||
ms = MessageSender()
|
||||
ms.send('JOHN CENA', next(self.cenerator))
|
||||
response = self.handle_event(anchor, message) if anchor != 'reset' else self.handle_reset(message)
|
||||
response = self.dispatch_handling(key, message)
|
||||
if response is None: return
|
||||
self.server_connector.send(anchor, response)
|
||||
self.server_connector.send(key, response)
|
||||
|
||||
def handle_event(self, anchor, data_json):
|
||||
def dispatch_handling(self, key, message):
|
||||
raise NotImplementedError
|
||||
|
||||
def _dispatch_handling(self, key, message):
|
||||
if key != 'reset': return self.handle_event(key, message)
|
||||
else: return self.handle_reset(message)
|
||||
|
||||
def handle_event(self, key, data_json):
|
||||
raise NotImplementedError
|
||||
|
||||
def handle_reset(self, data_json):
|
||||
@ -41,26 +48,41 @@ class EventHandlerBase:
|
||||
def cleanup(self):
|
||||
pass
|
||||
|
||||
def message_other(self, anchor, data):
|
||||
def message_other(self, key, data):
|
||||
message = {
|
||||
'anchor': anchor,
|
||||
'key': key,
|
||||
'data': data
|
||||
}
|
||||
self.server_connector.send(anchor, message)
|
||||
self.server_connector.send(key, message)
|
||||
|
||||
def subscribe(self, anchor):
|
||||
if anchor not in self.subscriptions:
|
||||
self.subscriptions.add(anchor)
|
||||
self.server_connector.subscribe(anchor)
|
||||
def subscribe(self, key):
|
||||
if key not in self.subscriptions:
|
||||
self.subscriptions.add(key)
|
||||
self.server_connector.subscribe(key)
|
||||
|
||||
def unsubscribe(self, anchor):
|
||||
def unsubscribe(self, key):
|
||||
try:
|
||||
self.subscriptions.remove(anchor)
|
||||
self.server_connector.unsubscribe(anchor)
|
||||
self.subscriptions.remove(key)
|
||||
self.server_connector.unsubscribe(key)
|
||||
except KeyError:
|
||||
pass
|
||||
|
||||
def unsubscribe_all(self):
|
||||
for sub in self.subscriptions:
|
||||
self.server_connector.unsubscribe(anchor=sub)
|
||||
self.server_connector.unsubscribe(key=sub)
|
||||
self.subscriptions.clear()
|
||||
|
||||
|
||||
class TriggerlessEventHandler(EventHandlerBase):
|
||||
def dispatch_handling(self, key, message):
|
||||
return self._dispatch_handling(key, message)
|
||||
|
||||
|
||||
class TriggeredEventHandler(EventHandlerBase):
|
||||
def __init__(self, key, trigger):
|
||||
super().__init__(key)
|
||||
self.trigger = trigger
|
||||
|
||||
def dispatch_handling(self, key, message):
|
||||
if message.get('trigger') == self.trigger:
|
||||
return self._dispatch_handling(key, message)
|
||||
|
@ -7,7 +7,7 @@ class FSMBase:
|
||||
states, transitions = [], []
|
||||
|
||||
def __init__(self, initial: str = None, accepted_states: List[str] = None):
|
||||
self.message_handlers = []
|
||||
self.callbacks = []
|
||||
self.accepted_states = accepted_states or [self.states[-1]]
|
||||
self.machine = Machine(model=self,
|
||||
states=self.states,
|
||||
@ -15,18 +15,17 @@ class FSMBase:
|
||||
initial=initial or self.states[0],
|
||||
send_event=True,
|
||||
ignore_invalid_triggers=True,
|
||||
after_state_change='forward_message')
|
||||
after_state_change='execute_callbacks')
|
||||
|
||||
def forward_message(self, event_data):
|
||||
message = event_data.kwargs.get('message')
|
||||
for msghandler in self.message_handlers:
|
||||
msghandler(message)
|
||||
def execute_callbacks(self, event_data):
|
||||
for callback in self.callbacks:
|
||||
callback(event_data.kwargs)
|
||||
|
||||
def subscribe_message_handler(self, msghandler):
|
||||
self.message_handlers.append(msghandler)
|
||||
def subscribe(self, callback):
|
||||
self.callbacks.append(callback)
|
||||
|
||||
def unsubscribe_message_handler(self, msghandler):
|
||||
self.message_handlers.remove(msghandler)
|
||||
def unsubscribe(self, callback):
|
||||
self.callbacks.remove(callback)
|
||||
|
||||
def is_solved(self):
|
||||
return self.state in self.accepted_states
|
||||
|
@ -4,9 +4,9 @@ from tfw.networking.event_handlers.server_connector import ServerUplinkConnector
|
||||
|
||||
|
||||
class MessageSender:
|
||||
def __init__(self, custom_anchor: str = None):
|
||||
def __init__(self, custom_key: str = None):
|
||||
self.server_connector = ServerUplinkConnector()
|
||||
self.anchor = custom_anchor or 'message'
|
||||
self.key = custom_key or 'message'
|
||||
|
||||
def send(self, originator, message):
|
||||
data = {
|
||||
@ -15,7 +15,7 @@ class MessageSender:
|
||||
'message': message
|
||||
}
|
||||
response = {
|
||||
'anchor': self.anchor,
|
||||
'key': self.key,
|
||||
'data': data
|
||||
}
|
||||
self.server_connector.send(self.anchor, response)
|
||||
self.server_connector.send(self.key, response)
|
||||
|
@ -25,8 +25,8 @@ class ServerUplinkConnector(ZMQConnectorBase):
|
||||
self._zmq_push_socket = self._zmq_context.socket(zmq.PUSH)
|
||||
self._zmq_push_socket.connect('tcp://localhost:{}'.format(tfwenv.RECEIVER_PORT))
|
||||
|
||||
def send(self, anchor, response):
|
||||
self._zmq_push_socket.send_multipart(serialize_all(anchor, response))
|
||||
def send(self, key, response):
|
||||
self._zmq_push_socket.send_multipart(serialize_all(key, response))
|
||||
|
||||
|
||||
class ServerConnector(ServerUplinkConnector, ServerDownlinkConnector):
|
||||
|
@ -34,7 +34,7 @@ class EventHandlerConnector(EventHandlerDownlinkConnector, EventHandlerUplinkCon
|
||||
def register_callback(self, callback):
|
||||
self._zmq_pull_stream.on_recv(callback)
|
||||
|
||||
def send_message(self, message: dict, anchor: str = None):
|
||||
if not anchor:
|
||||
anchor = message['anchor']
|
||||
self._zmq_pub_socket.send_multipart(serialize_all(anchor, message))
|
||||
def send_message(self, message: dict, key: str = None):
|
||||
if not key:
|
||||
key = message['key']
|
||||
self._zmq_pub_socket.send_multipart(serialize_all(key, message))
|
||||
|
@ -1,14 +1,21 @@
|
||||
from tornado.web import Application
|
||||
|
||||
from tfw.networking.server.controller_responder import ControllerResponder
|
||||
from tfw.networking.server.zmq_websocket_handler import FSMManagingSocketHandler
|
||||
from tfw.networking.server.zmq_websocket_handler import ZMQWebSocketProxy
|
||||
from tfw.networking.event_handlers.server_connector import ServerUplinkConnector
|
||||
from tfw.config.logs import logging
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class TFWServer:
|
||||
def __init__(self, fsm_type):
|
||||
self._fsm = fsm_type()
|
||||
self.fsm_updater = FSMUpdater(self._fsm)
|
||||
self._fsm.subscribe(self.fsm_updater.update)
|
||||
|
||||
self.application = Application(
|
||||
[(r'/ws', FSMManagingSocketHandler, {'fsm': self.fsm})]
|
||||
[(r'/ws', ZMQWebSocketProxy, {'make_response': self.make_response,
|
||||
'proxy_filter': self.proxy_filter})]
|
||||
)
|
||||
self.controller_responder = ControllerResponder(self.fsm)
|
||||
|
||||
@ -16,5 +23,31 @@ class TFWServer:
|
||||
def fsm(self):
|
||||
return self._fsm
|
||||
|
||||
def make_response(self, message):
|
||||
trigger = message.get('trigger', '')
|
||||
try: self.fsm.trigger(trigger, message=message)
|
||||
except AttributeError: log.debug('FSM failed to execute nonexistent trigger: "{}"'.format(trigger))
|
||||
return message
|
||||
|
||||
def proxy_filter(self, message):
|
||||
return True
|
||||
|
||||
def listen(self, port):
|
||||
self.application.listen(port)
|
||||
|
||||
|
||||
class FSMUpdater:
|
||||
def __init__(self, fsm):
|
||||
self.fsm = fsm
|
||||
self.uplink = ServerUplinkConnector()
|
||||
|
||||
def update(self, kwargs_dict):
|
||||
self.uplink.send(*self.generate_fsm_update())
|
||||
|
||||
def generate_fsm_update(self):
|
||||
key = 'FSMUpdate'
|
||||
response = {'key': key,
|
||||
'data': {'current_state': self.fsm.state,
|
||||
'valid_transitions':
|
||||
[{'trigger': trigger} for trigger in self.fsm.machine.get_triggers(self.fsm.state)]}}
|
||||
return key, response
|
||||
|
@ -23,40 +23,35 @@ class ZMQWebSocketHandler(WebSocketHandler):
|
||||
|
||||
@staticmethod
|
||||
def zmq_callback(msg_parts):
|
||||
anchor, data = deserialize_all(*msg_parts)
|
||||
key, data = deserialize_all(*msg_parts)
|
||||
log.debug('Received on pull socket: {}'.format(data))
|
||||
for instance in ZMQWebSocketHandler.instances:
|
||||
instance.write_message(data)
|
||||
|
||||
def on_message(self, message):
|
||||
log.debug('Received on WebSocket: {}'.format(message))
|
||||
self.send_message(*self.make_response(json.loads(message)))
|
||||
self.send_message(self.make_response(message))
|
||||
|
||||
def send_message(self, message: dict, anchor: str = None):
|
||||
self._event_handler_connector.send_message(message, anchor)
|
||||
def make_response(self, message):
|
||||
raise NotImplementedError
|
||||
|
||||
def send_message(self, message: dict, key: str = None):
|
||||
self._event_handler_connector.send_message(message, key)
|
||||
|
||||
# much secure, very cors, wow
|
||||
def check_origin(self, origin):
|
||||
return True
|
||||
|
||||
|
||||
class FSMManagingSocketHandler(ZMQWebSocketHandler):
|
||||
def initialize(self, fsm):
|
||||
self.fsm = fsm
|
||||
self.fsm.subscribe_message_handler(self.handle_fsm_message)
|
||||
class ZMQWebSocketProxy(ZMQWebSocketHandler):
|
||||
def initialize(self, make_response, proxy_filter):
|
||||
self._make_response = make_response
|
||||
self._proxy_filter = proxy_filter
|
||||
|
||||
def on_close(self):
|
||||
super().on_close()
|
||||
self.fsm.unsubscribe_message_handler(self.handle_fsm_message)
|
||||
|
||||
def handle_fsm_message(self, message):
|
||||
self._event_handler_connector.send_message(message)
|
||||
def on_message(self, message):
|
||||
message = json.loads(message)
|
||||
if self._proxy_filter(message):
|
||||
super().on_message(message)
|
||||
|
||||
def make_response(self, message):
|
||||
self.fsm.trigger(message['anchor'], message=message)
|
||||
anchor = 'FSMUpdate'
|
||||
response = {'anchor': anchor,
|
||||
'data': {'current_state': self.fsm.state,
|
||||
'valid_transitions':
|
||||
[{'trigger': trigger} for trigger in self.fsm.machine.get_triggers()]}}
|
||||
return response, anchor
|
||||
return self._make_response(message)
|
||||
|
@ -6,8 +6,8 @@ from tfw.config import tfwenv
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
eventhandlers = {SourceCodeEventHandler('anchor_webide', tfwenv.WEBIDE_WD, 'login'),
|
||||
TerminadoEventHandler('anchor_terminado', 'terminado')}
|
||||
eventhandlers = {SourceCodeEventHandler('webide', tfwenv.WEBIDE_WD, 'login'),
|
||||
TerminadoEventHandler('terminado', 'terminado')}
|
||||
try:
|
||||
IOLoop.instance().start()
|
||||
finally:
|
||||
|
@ -12,13 +12,13 @@ class SQLInjectionFSM(FSMBase):
|
||||
'end',
|
||||
]
|
||||
transitions = [
|
||||
{'trigger': 'anchor_webide', 'source': '*', 'dest': 'stripped_code'}, # TODO: delet this
|
||||
{'trigger': 'anchor_webide', 'source': 'start', 'dest': 'stripped_code'},
|
||||
{'trigger': 'anchor_login', 'source': 'stripped_code', 'dest': 'sql'},
|
||||
{'trigger': 'anchor_logger', 'source': 'sql', 'dest': 'commented_code'},
|
||||
{'trigger': 'anchor_webide', 'source': 'commented_code', 'dest': 'sql_with_substitutions'},
|
||||
{'trigger': 'anchor_logger', 'source': 'sql_with_substitutions', 'dest': 'sql_output'},
|
||||
{'trigger': 'anchor_logger', 'source': 'sql_output', 'dest': 'end'},
|
||||
{'trigger': 'webide', 'source': '*', 'dest': 'stripped_code'}, # TODO: delet this
|
||||
{'trigger': 'webide', 'source': 'start', 'dest': 'stripped_code'},
|
||||
{'trigger': 'login', 'source': 'stripped_code', 'dest': 'sql'},
|
||||
{'trigger': 'logger', 'source': 'sql', 'dest': 'commented_code'},
|
||||
{'trigger': 'webide', 'source': 'commented_code', 'dest': 'sql_with_substitutions'},
|
||||
{'trigger': 'logger', 'source': 'sql_with_substitutions', 'dest': 'sql_output'},
|
||||
{'trigger': 'logger', 'source': 'sql_output', 'dest': 'end'},
|
||||
{'trigger': 'reset', 'source': 'end', 'dest': 'start'},
|
||||
]
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user