2018-02-11 11:32:30 +00:00
|
|
|
from tornado.web import Application
|
|
|
|
|
|
|
|
from tfw.networking.controller_connector import ControllerConnector
|
|
|
|
from tfw.networking.serialization import deserialize_all, serialize_all
|
|
|
|
from tfw.networking.server.zmq_websocket_handler import FSMManagingSocketHandler
|
|
|
|
|
|
|
|
|
|
|
|
class TFWServer:
|
|
|
|
def __init__(self, fsm_type):
|
|
|
|
self._fsm = fsm_type()
|
|
|
|
self.application = Application(
|
2018-02-12 10:09:28 +00:00
|
|
|
[(r'/ws', FSMManagingSocketHandler, {'fsm': self.fsm})]
|
2018-02-11 11:32:30 +00:00
|
|
|
)
|
|
|
|
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)
|