baseimage-tutorial-framework/src/app/handlers/zmq_websocket_handler.py

43 lines
1.4 KiB
Python
Raw Normal View History

import logging
import json
from tornado.websocket import WebSocketHandler
from util import parse_anchor_from_message
# from buttons import fsm
from sql_injection_fsm import fsm
import event_handler_connector
class ZMQWebSocketHandler(WebSocketHandler):
def __init__(self, application, request, **kwargs):
super().__init__(application, request, **kwargs)
def open(self, *args, **kwargs):
logging.debug('WebSocket connection initiated')
event_handler_connector.register_callback(self.zmq_callback)
def zmq_callback(self, msg_parts):
anchor, data = msg_parts
logging.debug('Received on pull socket: {}'.format(data.decode()))
self.write_message(data.decode())
def on_message(self, message):
logging.debug('Received on WebSocket: {}'.format(message))
fsm.trigger(parse_anchor_from_message(message), message=message)
anchor = 'FSMUpdate'
response = { 'anchor': anchor,
'data': { 'current_state': fsm.state,
'valid_transitions': [{'trigger': trigger} for trigger in fsm.machine.get_triggers()]}}
self.send_message(json.dumps(response), anchor)
def send_message(self, message: str, anchor: str = None):
event_handler_connector.send_message(message, anchor)
def on_close(self):
pass
# much secure, very cors, wow
def check_origin(self, origin):
return True