Merge pull request #13 from avatao-content/bottomless_pit

Refactor TFW architecture to support stateless event handling
This commit is contained in:
Bokros Bálint 2018-02-26 17:06:22 +01:00 committed by GitHub
commit 41a32437b2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 133 additions and 84 deletions

View File

@ -24,9 +24,9 @@ class WebideReloadEventHandler(FileSystemEventHandler):
def on_modified(self, event): def on_modified(self, event):
if self._paused: return if self._paused: return
log.debug(event) log.debug(event)
anchor = 'anchor_webide' key = 'webide'
self.uplink.send(anchor, {'anchor': anchor, self.uplink.send(key, {'key': key,
'data': {'command': 'reload'}}) 'data': {'command': 'reload'}})
class DirectoryMonitor: class DirectoryMonitor:

View File

@ -2,7 +2,7 @@ from os.path import isfile, join, relpath
from glob import glob from glob import glob
from tfw.components.mixins import SupervisorMixin 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.components.directory_monitor import DirectoryMonitor
from tfw.config.logs import logging from tfw.config.logs import logging
@ -43,9 +43,9 @@ class FileManager:
return relpath(self._filepath(filename), start=self._workdir) return relpath(self._filepath(filename), start=self._workdir)
class SourceCodeEventHandler(EventHandlerBase, SupervisorMixin): class SourceCodeEventHandler(TriggerlessEventHandler, SupervisorMixin):
def __init__(self, anchor, directory, process_name, selected_file=None): def __init__(self, key, directory, process_name, selected_file=None):
super().__init__(anchor) super().__init__(key)
self.filemanager = FileManager(directory, selected_file=selected_file) self.filemanager = FileManager(directory, selected_file=selected_file)
self.process_name = process_name self.process_name = process_name
@ -81,7 +81,7 @@ class SourceCodeEventHandler(EventHandlerBase, SupervisorMixin):
data['filename'] = self.filemanager.filename data['filename'] = self.filemanager.filename
data['files'] = self.filemanager.files data['files'] = self.filemanager.files
def handle_event(self, anchor, data_json): def handle_event(self, key, data_json):
data = data_json['data'] data = data_json['data']
data_json['data'] = self.commands[data['command']](data) data_json['data'] = self.commands[data['command']](data)
self.attach_fileinfo(data) self.attach_fileinfo(data)

View File

@ -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.components.mixins import SupervisorMixin
from tfw.config import tfwenv from tfw.config import tfwenv
from tfw.config.logs import logging from tfw.config.logs import logging
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
class TerminadoEventHandler(EventHandlerBase, SupervisorMixin): class TerminadoEventHandler(TriggerlessEventHandler, SupervisorMixin):
def __init__(self, anchor, process_name): def __init__(self, key, process_name):
super().__init__(anchor) super().__init__(key)
self.working_directory = tfwenv.TERMINADO_DIR self.working_directory = tfwenv.TERMINADO_DIR
self.process_name = process_name self.process_name = process_name
self.start_process() self.start_process()
def handle_event(self, anchor, data_json): def handle_event(self, key, data_json):
log.debug('TerminadoEventHandler received event for anchor {}'.format(anchor)) log.debug('TerminadoEventHandler received event for key {}'.format(key))
# TODO: wat do? # TODO: wat do?

View File

@ -14,25 +14,32 @@ def cenator():
class EventHandlerBase: class EventHandlerBase:
def __init__(self, anchor): def __init__(self, key):
self.server_connector = ServerConnector() self.server_connector = ServerConnector()
self.anchor = anchor self.key = key
self.subscriptions = set() self.subscriptions = set()
self.subscribe(self.anchor) self.subscribe(self.key)
self.subscribe('reset') self.subscribe('reset')
self.server_connector.register_callback(self.event_handler_callback) self.server_connector.register_callback(self.event_handler_callback)
self.cenerator = cycle(cenator()) self.cenerator = cycle(cenator())
def event_handler_callback(self, msg_parts): def event_handler_callback(self, msg_parts):
anchor, message = deserialize_all(*msg_parts) key, message = deserialize_all(*msg_parts)
from .message_sender import MessageSender from .message_sender import MessageSender
ms = MessageSender() ms = MessageSender()
ms.send('JOHN CENA', next(self.cenerator)) 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 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 raise NotImplementedError
def handle_reset(self, data_json): def handle_reset(self, data_json):
@ -41,26 +48,41 @@ class EventHandlerBase:
def cleanup(self): def cleanup(self):
pass pass
def message_other(self, anchor, data): def message_other(self, key, data):
message = { message = {
'anchor': anchor, 'key': key,
'data': data 'data': data
} }
self.server_connector.send(anchor, message) self.server_connector.send(key, message)
def subscribe(self, anchor): def subscribe(self, key):
if anchor not in self.subscriptions: if key not in self.subscriptions:
self.subscriptions.add(anchor) self.subscriptions.add(key)
self.server_connector.subscribe(anchor) self.server_connector.subscribe(key)
def unsubscribe(self, anchor): def unsubscribe(self, key):
try: try:
self.subscriptions.remove(anchor) self.subscriptions.remove(key)
self.server_connector.unsubscribe(anchor) self.server_connector.unsubscribe(key)
except KeyError: except KeyError:
pass pass
def unsubscribe_all(self): def unsubscribe_all(self):
for sub in self.subscriptions: for sub in self.subscriptions:
self.server_connector.unsubscribe(anchor=sub) self.server_connector.unsubscribe(key=sub)
self.subscriptions.clear() 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)

View File

@ -7,7 +7,7 @@ class FSMBase:
states, transitions = [], [] states, transitions = [], []
def __init__(self, initial: str = None, accepted_states: List[str] = None): 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.accepted_states = accepted_states or [self.states[-1]]
self.machine = Machine(model=self, self.machine = Machine(model=self,
states=self.states, states=self.states,
@ -15,18 +15,17 @@ class FSMBase:
initial=initial or self.states[0], initial=initial or self.states[0],
send_event=True, send_event=True,
ignore_invalid_triggers=True, ignore_invalid_triggers=True,
after_state_change='forward_message') after_state_change='execute_callbacks')
def forward_message(self, event_data): def execute_callbacks(self, event_data):
message = event_data.kwargs.get('message') for callback in self.callbacks:
for msghandler in self.message_handlers: callback(event_data.kwargs)
msghandler(message)
def subscribe_message_handler(self, msghandler): def subscribe(self, callback):
self.message_handlers.append(msghandler) self.callbacks.append(callback)
def unsubscribe_message_handler(self, msghandler): def unsubscribe(self, callback):
self.message_handlers.remove(msghandler) self.callbacks.remove(callback)
def is_solved(self): def is_solved(self):
return self.state in self.accepted_states return self.state in self.accepted_states

View File

@ -4,9 +4,9 @@ from tfw.networking.event_handlers.server_connector import ServerUplinkConnector
class MessageSender: class MessageSender:
def __init__(self, custom_anchor: str = None): def __init__(self, custom_key: str = None):
self.server_connector = ServerUplinkConnector() self.server_connector = ServerUplinkConnector()
self.anchor = custom_anchor or 'message' self.key = custom_key or 'message'
def send(self, originator, message): def send(self, originator, message):
data = { data = {
@ -15,7 +15,7 @@ class MessageSender:
'message': message 'message': message
} }
response = { response = {
'anchor': self.anchor, 'key': self.key,
'data': data 'data': data
} }
self.server_connector.send(self.anchor, response) self.server_connector.send(self.key, response)

View File

@ -25,8 +25,8 @@ class ServerUplinkConnector(ZMQConnectorBase):
self._zmq_push_socket = self._zmq_context.socket(zmq.PUSH) self._zmq_push_socket = self._zmq_context.socket(zmq.PUSH)
self._zmq_push_socket.connect('tcp://localhost:{}'.format(tfwenv.RECEIVER_PORT)) self._zmq_push_socket.connect('tcp://localhost:{}'.format(tfwenv.RECEIVER_PORT))
def send(self, anchor, response): def send(self, key, response):
self._zmq_push_socket.send_multipart(serialize_all(anchor, response)) self._zmq_push_socket.send_multipart(serialize_all(key, response))
class ServerConnector(ServerUplinkConnector, ServerDownlinkConnector): class ServerConnector(ServerUplinkConnector, ServerDownlinkConnector):

View File

@ -34,7 +34,7 @@ class EventHandlerConnector(EventHandlerDownlinkConnector, EventHandlerUplinkCon
def register_callback(self, callback): def register_callback(self, callback):
self._zmq_pull_stream.on_recv(callback) self._zmq_pull_stream.on_recv(callback)
def send_message(self, message: dict, anchor: str = None): def send_message(self, message: dict, key: str = None):
if not anchor: if not key:
anchor = message['anchor'] key = message['key']
self._zmq_pub_socket.send_multipart(serialize_all(anchor, message)) self._zmq_pub_socket.send_multipart(serialize_all(key, message))

View File

@ -1,14 +1,21 @@
from tornado.web import Application from tornado.web import Application
from tfw.networking.server.controller_responder import ControllerResponder 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: class TFWServer:
def __init__(self, fsm_type): def __init__(self, fsm_type):
self._fsm = fsm_type() self._fsm = fsm_type()
self.fsm_updater = FSMUpdater(self._fsm)
self._fsm.subscribe(self.fsm_updater.update)
self.application = Application( 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) self.controller_responder = ControllerResponder(self.fsm)
@ -16,5 +23,31 @@ class TFWServer:
def fsm(self): def fsm(self):
return self._fsm 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): def listen(self, port):
self.application.listen(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

View File

@ -23,40 +23,35 @@ class ZMQWebSocketHandler(WebSocketHandler):
@staticmethod @staticmethod
def zmq_callback(msg_parts): 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)) log.debug('Received on pull socket: {}'.format(data))
for instance in ZMQWebSocketHandler.instances: for instance in ZMQWebSocketHandler.instances:
instance.write_message(data) instance.write_message(data)
def on_message(self, message): def on_message(self, message):
log.debug('Received on WebSocket: {}'.format(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): def make_response(self, message):
self._event_handler_connector.send_message(message, anchor) raise NotImplementedError
def send_message(self, message: dict, key: str = None):
self._event_handler_connector.send_message(message, key)
# much secure, very cors, wow # much secure, very cors, wow
def check_origin(self, origin): def check_origin(self, origin):
return True return True
class FSMManagingSocketHandler(ZMQWebSocketHandler): class ZMQWebSocketProxy(ZMQWebSocketHandler):
def initialize(self, fsm): def initialize(self, make_response, proxy_filter):
self.fsm = fsm self._make_response = make_response
self.fsm.subscribe_message_handler(self.handle_fsm_message) self._proxy_filter = proxy_filter
def on_close(self): def on_message(self, message):
super().on_close() message = json.loads(message)
self.fsm.unsubscribe_message_handler(self.handle_fsm_message) if self._proxy_filter(message):
super().on_message(message)
def handle_fsm_message(self, message):
self._event_handler_connector.send_message(message)
def make_response(self, message): def make_response(self, message):
self.fsm.trigger(message['anchor'], message=message) return self._make_response(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

View File

@ -6,8 +6,8 @@ from tfw.config import tfwenv
if __name__ == '__main__': if __name__ == '__main__':
eventhandlers = {SourceCodeEventHandler('anchor_webide', tfwenv.WEBIDE_WD, 'login'), eventhandlers = {SourceCodeEventHandler('webide', tfwenv.WEBIDE_WD, 'login'),
TerminadoEventHandler('anchor_terminado', 'terminado')} TerminadoEventHandler('terminado', 'terminado')}
try: try:
IOLoop.instance().start() IOLoop.instance().start()
finally: finally:

View File

@ -12,13 +12,13 @@ class SQLInjectionFSM(FSMBase):
'end', 'end',
] ]
transitions = [ transitions = [
{'trigger': 'anchor_webide', 'source': '*', 'dest': 'stripped_code'}, # TODO: delet this {'trigger': 'webide', 'source': '*', 'dest': 'stripped_code'}, # TODO: delet this
{'trigger': 'anchor_webide', 'source': 'start', 'dest': 'stripped_code'}, {'trigger': 'webide', 'source': 'start', 'dest': 'stripped_code'},
{'trigger': 'anchor_login', 'source': 'stripped_code', 'dest': 'sql'}, {'trigger': 'login', 'source': 'stripped_code', 'dest': 'sql'},
{'trigger': 'anchor_logger', 'source': 'sql', 'dest': 'commented_code'}, {'trigger': 'logger', 'source': 'sql', 'dest': 'commented_code'},
{'trigger': 'anchor_webide', 'source': 'commented_code', 'dest': 'sql_with_substitutions'}, {'trigger': 'webide', 'source': 'commented_code', 'dest': 'sql_with_substitutions'},
{'trigger': 'anchor_logger', 'source': 'sql_with_substitutions', 'dest': 'sql_output'}, {'trigger': 'logger', 'source': 'sql_with_substitutions', 'dest': 'sql_output'},
{'trigger': 'anchor_logger', 'source': 'sql_output', 'dest': 'end'}, {'trigger': 'logger', 'source': 'sql_output', 'dest': 'end'},
{'trigger': 'reset', 'source': 'end', 'dest': 'start'}, {'trigger': 'reset', 'source': 'end', 'dest': 'start'},
] ]