From bcd31de8521c1541bd79a0470e78c1e6c359bf78 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krist=C3=B3f=20T=C3=B3th?= Date: Sun, 11 Feb 2018 11:50:40 +0100 Subject: [PATCH] Begin refactoring app.py to TFWServer class --- src/app/app.py | 49 ++++++++++++++++++++++++++++--------------------- 1 file changed, 28 insertions(+), 21 deletions(-) diff --git a/src/app/app.py b/src/app/app.py index 70d3c65..3435903 100644 --- a/src/app/app.py +++ b/src/app/app.py @@ -14,30 +14,37 @@ log = logging.getLogger(__name__) from tfw.networking.server.zmq_websocket_handler import FSMManagingSocketHandler -def zmq_callback(stream, msg_parts): - key, data = deserialize_all(*msg_parts) - if key == 'test': - stream.send_multipart(serialize_all(key, 'OK')) - if key == 'solution_check': - stream.send_multipart(serialize_all(key, { - 'solved': fsm.is_solved(), - 'message': 'solved' if fsm.is_solved() else 'not solved' - })) +class TFWServer: + def __init__(self, fsm_type): + self._fsm = fsm_type() + self.application = Application( + [(r'/ws', FSMManagingSocketHandler, {'fsm': self.fsm})], + autoreload=True + ) + self.controller_connector = ControllerConnector() + self.controller_connector.register_callback(self.zmq_callback) + + @property + def fsm(self): + return self._fsm + + def zmq_callback(self, stream, msg_parts): + key, data = deserialize_all(*msg_parts) + if key == 'test': + stream.send_multipart(serialize_all(key, 'OK')) + if key == 'solution_check': + stream.send_multipart(serialize_all(key, { + 'solved': self.fsm.is_solved(), + 'message': 'solved' if self.fsm.is_solved() else 'not solved' + })) + + def listen(self, port): + self.application.listen(port) if __name__ == '__main__': - fsm = SQLInjectionFSM() - controller_connector = ControllerConnector() - controller_connector.register_callback(zmq_callback) - routes = [ - (r'/ws', FSMManagingSocketHandler, {'fsm': fsm}), - ] - application = Application( - routes, - autoreload=True - ) - - application.listen(tfwenv.WEB_PORT) + server = TFWServer(SQLInjectionFSM) + server.listen(tfwenv.WEB_PORT) log.debug('Python version: {}'.format(sys.version[:5])) log.debug('Tornado version: {}'.format(tornado.version)) log.debug('ZeroMQ version: {}'.format(zmq.zmq_version()))