diff --git a/Dockerfile b/Dockerfile index f385b45..b39af93 100644 --- a/Dockerfile +++ b/Dockerfile @@ -27,8 +27,8 @@ ENV PATH="$PYENV_ROOT/bin:$PATH" ENV TFW_APP_DIR="/srv/app" COPY src/app ${TFW_APP_DIR} -ENV TFW_COMPONENTS_DIR="/opt/components" -COPY src/components ${TFW_COMPONENTS_DIR} +ENV TFW_EVENT_HANDLERS_DIR="/opt/event_handlers" +COPY src/event_handlers ${TFW_EVENT_HANDLERS_DIR} # Copy config ENV TFW_CONFIG_DIR="/usr/local/lib/" diff --git a/README.md b/README.md index 05bb328..f265263 100644 --- a/README.md +++ b/README.md @@ -31,6 +31,6 @@ PYTHONPATH="../../lib/" python app.py in one, and ``` cd src/components/ -PYTHONPATH="../../lib/" python component_example.py +PYTHONPATH="../../lib/" python event_handler_example.py ``` in the other. diff --git a/src/app/buttons.py b/src/app/buttons.py index 0140ede..1e84545 100644 --- a/src/app/buttons.py +++ b/src/app/buttons.py @@ -1,6 +1,6 @@ from transitions import Machine -import component_connector +import event_handler_connector class Buttons: @@ -22,7 +22,7 @@ class Buttons: def forward_message(self, event_data): message = event_data.kwargs.get('message') - component_connector.send_message(message) + event_handler_connector.send_message(message) fsm = Buttons() \ No newline at end of file diff --git a/src/app/component_connector.py b/src/app/event_handler_connector.py similarity index 100% rename from src/app/component_connector.py rename to src/app/event_handler_connector.py diff --git a/src/app/handlers/zmq_websocket_handler.py b/src/app/handlers/zmq_websocket_handler.py index 9519cff..2426962 100644 --- a/src/app/handlers/zmq_websocket_handler.py +++ b/src/app/handlers/zmq_websocket_handler.py @@ -5,7 +5,7 @@ from util import parse_anchor_from_message # from buttons import fsm from sql_injection_fsm import fsm -import component_connector +import event_handler_connector class ZMQWebSocketHandler(WebSocketHandler): @@ -18,14 +18,14 @@ class ZMQWebSocketHandler(WebSocketHandler): logging.debug('Received on pull socket: {}'.format(data.decode())) self.write_message(data.decode()) - component_connector.register_callback(zmq_callback) + event_handler_connector.register_callback(zmq_callback) def on_message(self, message): logging.debug('Received on WebSocket: {}'.format(message)) fsm.trigger(parse_anchor_from_message(message), message=message) def send_message(self, message: str, anchor: str = None): - component_connector.send_message(message, anchor) + event_handler_connector.send_message(message, anchor) def on_close(self): pass diff --git a/src/app/sql_injection_fsm.py b/src/app/sql_injection_fsm.py index 196c12a..3df3d45 100644 --- a/src/app/sql_injection_fsm.py +++ b/src/app/sql_injection_fsm.py @@ -1,6 +1,6 @@ from transitions import Machine -import component_connector +import event_handler_connector class SQLInjectionFSM: @@ -26,7 +26,7 @@ class SQLInjectionFSM: def forward_message(self, event_data): message = event_data.kwargs.get('message') - component_connector.send_message(message) + event_handler_connector.send_message(message) fsm = SQLInjectionFSM() diff --git a/src/components/__init__.py b/src/event_handlers/__init__.py similarity index 100% rename from src/components/__init__.py rename to src/event_handlers/__init__.py diff --git a/src/components/component.py b/src/event_handlers/event_handler.py similarity index 62% rename from src/components/component.py rename to src/event_handlers/event_handler.py index 1564f6f..9505ff1 100644 --- a/src/components/component.py +++ b/src/event_handlers/event_handler.py @@ -1,12 +1,12 @@ import json from functools import partial -from component_base import ComponentBase +from event_handler_base import EventHandlerBase -class Component(ComponentBase): - def __init__(self, anchor, event_handler, zmq_context=None): - super().__init__(anchor, event_handler, zmq_context) +class EventHandler(EventHandlerBase): + def __init__(self, anchor, event_handler_function, zmq_context=None): + super().__init__(anchor, event_handler_function, zmq_context) def wrapper(msg_parts, handler): anchor, message = msg_parts @@ -15,6 +15,4 @@ class Component(ComponentBase): encoded_response = json.dumps(response).encode('utf-8') self.zmq_push_socket.send_multipart([anchor, encoded_response]) - self.zmq_sub_stream.on_recv(partial(wrapper, handler=event_handler)) - - + self.zmq_sub_stream.on_recv(partial(wrapper, handler=event_handler_function)) diff --git a/src/components/component_base.py b/src/event_handlers/event_handler_base.py similarity index 91% rename from src/components/component_base.py rename to src/event_handlers/event_handler_base.py index 3139e95..04b40e7 100644 --- a/src/components/component_base.py +++ b/src/event_handlers/event_handler_base.py @@ -8,10 +8,10 @@ from config import PUBLISHER_PORT, RECEIVER_PORT ioloop.install() -class ComponentBase: - def __init__(self, anchor, event_handler, zmq_context=None): +class EventHandlerBase: + def __init__(self, anchor, event_handler_function, zmq_context=None): self.anchor = anchor - self.event_handler = event_handler + self.event_handler_function = event_handler_function self.zmq_context = zmq_context or zmq.Context.instance() self.zmq_sub_socket = self.zmq_context.socket(zmq.SUB) self.subscriptions = {self.anchor} diff --git a/src/components/component_example.py b/src/event_handlers/event_handler_example.py similarity index 80% rename from src/components/component_example.py rename to src/event_handlers/event_handler_example.py index 3e5479e..a65fc14 100644 --- a/src/components/component_example.py +++ b/src/event_handlers/event_handler_example.py @@ -2,8 +2,8 @@ import codecs import sqlite3 import source_code -from component import Component -from stateful_component import StatefulComponent +from event_handler import EventHandler +from stateful_event_handler import StatefulEventHandler from tornado.ioloop import IOLoop from login_component import authorize_login @@ -70,17 +70,17 @@ def login_handler(data, component): yield ('anchor_login', '# Login page\n' + response) -def source_code_handler(data, component): - component.unsubscribe(data['anchor']) +def source_code_handler(data, event_handler): + event_handler.unsubscribe(data['anchor']) yield (data['anchor'], source_code.get_source_code(authorize_login, strip_comments=True)) if __name__ == '__main__': - anchor_a = Component('anchor_a', change_case_handler) - anchor_b = Component('anchor_b', rot13_handler) - anchor_c = Component('anchor_c', reverse_handler) - anchor_login = StatefulComponent('anchor_login', login_handler) - anchor_webide = StatefulComponent('anchor_webide', source_code_handler) + anchor_a = EventHandler('anchor_a', change_case_handler) + anchor_b = EventHandler('anchor_b', rot13_handler) + anchor_c = EventHandler('anchor_c', reverse_handler) + anchor_login = StatefulEventHandler('anchor_login', login_handler) + anchor_webide = StatefulEventHandler('anchor_webide', source_code_handler) IOLoop.instance().start() diff --git a/src/components/login_component.py b/src/event_handlers/login_component.py similarity index 100% rename from src/components/login_component.py rename to src/event_handlers/login_component.py diff --git a/src/components/stateful_component.py b/src/event_handlers/stateful_event_handler.py similarity index 93% rename from src/components/stateful_component.py rename to src/event_handlers/stateful_event_handler.py index f9e142b..9f3fd38 100644 --- a/src/components/stateful_component.py +++ b/src/event_handlers/stateful_event_handler.py @@ -3,10 +3,10 @@ from functools import partial import zmq -from component_base import ComponentBase +from event_handler_base import EventHandlerBase -class StatefulComponent(ComponentBase): +class StatefulEventHandler(EventHandlerBase): def __init__(self, anchor, event_handler, zmq_context=None): super().__init__(anchor, event_handler, zmq_context) self.generator = None diff --git a/src/components/users.db b/src/event_handlers/users.db similarity index 100% rename from src/components/users.db rename to src/event_handlers/users.db diff --git a/supervisord.conf b/supervisord.conf index 6728117..da26747 100644 --- a/supervisord.conf +++ b/supervisord.conf @@ -17,7 +17,7 @@ serverurl=unix:///tmp/supervisor.sock directory=%(ENV_TFW_APP_DIR)s command=env python app.py -[program:component_example] -directory=%(ENV_TFW_COMPONENTS_DIR)s -command=env python component_example.py +[program:event_handler_example] +directory=%(ENV_TFW_EVENT_HANDLERS_DIR)s +command=env python event_handler_example.py autorestart=true