From 7ed0715f4cef10b30b84c3b795b06705d46f1233 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krist=C3=B3f=20T=C3=B3th?= Date: Fri, 29 Jun 2018 15:33:45 +0200 Subject: [PATCH] Reorder stuff to follow the teachings of Uncle Bob --- .../server/zmq_websocket_handler.py | 100 +++++++++--------- 1 file changed, 50 insertions(+), 50 deletions(-) diff --git a/lib/tfw/networking/server/zmq_websocket_handler.py b/lib/tfw/networking/server/zmq_websocket_handler.py index 508049f..85d6c60 100644 --- a/lib/tfw/networking/server/zmq_websocket_handler.py +++ b/lib/tfw/networking/server/zmq_websocket_handler.py @@ -11,56 +11,6 @@ from tfw.config.logs import logging LOG = logging.getLogger(__name__) -class TFWProxy: - def __init__(self, to_source, to_destination): - self.to_source = to_source - self.to_destination = to_destination - - self.proxy_filters = CallbackMixin() - self.proxy_callbacks = CallbackMixin() - - self.proxy_filters.subscribe_callback(self.validate_message) - - self.keyhandlers = { - 'mirror': self.mirror, - 'broadcast': self.broadcast - } - - @staticmethod - def validate_message(message): - if 'key' not in message: - raise ValueError('Invalid TFW message format!') - - def __call__(self, message): - try: - self.proxy_filters._execute_callbacks(message) - except ValueError: - LOG.exception('Invalid TFW message received!') - return - - self.proxy_callbacks._execute_callbacks(message) - - if message['key'] not in self.keyhandlers: - self.to_destination(message) - else: - handler = self.keyhandlers[message['key']] - try: - handler(message) - except KeyError: - LOG.error('Invalid "%s" message format! Ignoring.', handler.__name__) - - def mirror(self, message): - message = message['data'] - LOG.debug('Mirroring message: %s', message) - self.to_source(message) - - def broadcast(self, message): - message = message['data'] - LOG.debug('Broadcasting message: %s', message) - self.to_source(message) - self.to_destination(message) - - class ZMQWebSocketProxy(WebSocketHandler): instances = set() @@ -119,3 +69,53 @@ class ZMQWebSocketProxy(WebSocketHandler): # much secure, very cors, wow def check_origin(self, origin): return True + + +class TFWProxy: + def __init__(self, to_source, to_destination): + self.to_source = to_source + self.to_destination = to_destination + + self.proxy_filters = CallbackMixin() + self.proxy_callbacks = CallbackMixin() + + self.proxy_filters.subscribe_callback(self.validate_message) + + self.keyhandlers = { + 'mirror': self.mirror, + 'broadcast': self.broadcast + } + + @staticmethod + def validate_message(message): + if 'key' not in message: + raise ValueError('Invalid TFW message format!') + + def __call__(self, message): + try: + self.proxy_filters._execute_callbacks(message) + except ValueError: + LOG.exception('Invalid TFW message received!') + return + + self.proxy_callbacks._execute_callbacks(message) + + if message['key'] not in self.keyhandlers: + self.to_destination(message) + else: + handler = self.keyhandlers[message['key']] + try: + handler(message) + except KeyError: + LOG.error('Invalid "%s" message format! Ignoring.', handler.__name__) + + def mirror(self, message): + message = message['data'] + LOG.debug('Mirroring message: %s', message) + self.to_source(message) + + def broadcast(self, message): + message = message['data'] + LOG.debug('Broadcasting message: %s', message) + self.to_source(message) + self.to_destination(message)