mirror of
https://github.com/avatao-content/baseimage-tutorial-framework
synced 2024-11-22 19:31:33 +00:00
Move responding-to-controller related stuff to its own class
This commit is contained in:
parent
6f785b3ca7
commit
86aecd18a8
27
lib/tfw/networking/server/controller_responder.py
Normal file
27
lib/tfw/networking/server/controller_responder.py
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
from tfw.networking.controller_connector import ControllerConnector
|
||||||
|
from tfw.networking.serialization import deserialize_all, serialize_all
|
||||||
|
|
||||||
|
|
||||||
|
class ControllerResponder:
|
||||||
|
def __init__(self, fsm):
|
||||||
|
self.fsm = fsm
|
||||||
|
self.controller_connector = ControllerConnector()
|
||||||
|
self.controller_connector.register_callback(self.handle_controller_request)
|
||||||
|
self.controller_request_handlers = {
|
||||||
|
'solution_check': self.handle_solution_check_request,
|
||||||
|
'test': self.handle_test_request
|
||||||
|
}
|
||||||
|
|
||||||
|
def handle_controller_request(self, stream, msg_parts):
|
||||||
|
key, data = deserialize_all(*msg_parts)
|
||||||
|
response = self.controller_request_handlers[key](data)
|
||||||
|
stream.send_multipart(serialize_all(key, response))
|
||||||
|
|
||||||
|
def handle_test_request(self, data):
|
||||||
|
return 'OK'
|
||||||
|
|
||||||
|
def handle_solution_check_request(self, data):
|
||||||
|
return {
|
||||||
|
'solved': self.fsm.is_solved(),
|
||||||
|
'message': 'solved' if self.fsm.is_solved() else 'not solved'
|
||||||
|
}
|
@ -1,7 +1,6 @@
|
|||||||
from tornado.web import Application
|
from tornado.web import Application
|
||||||
|
|
||||||
from tfw.networking.controller_connector import ControllerConnector
|
from tfw.networking.server.controller_responder import ControllerResponder
|
||||||
from tfw.networking.serialization import deserialize_all, serialize_all
|
|
||||||
from tfw.networking.server.zmq_websocket_handler import FSMManagingSocketHandler
|
from tfw.networking.server.zmq_websocket_handler import FSMManagingSocketHandler
|
||||||
|
|
||||||
|
|
||||||
@ -11,30 +10,11 @@ class TFWServer:
|
|||||||
self.application = Application(
|
self.application = Application(
|
||||||
[(r'/ws', FSMManagingSocketHandler, {'fsm': self.fsm})]
|
[(r'/ws', FSMManagingSocketHandler, {'fsm': self.fsm})]
|
||||||
)
|
)
|
||||||
self.controller_connector = ControllerConnector()
|
self.controller_responder = ControllerResponder(self.fsm)
|
||||||
self.controller_connector.register_callback(self.handle_controller_request)
|
|
||||||
self.controller_request_handlers = {
|
|
||||||
'solution_check': self.handle_solution_check_request,
|
|
||||||
'test': self.handle_test_request
|
|
||||||
}
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def fsm(self):
|
def fsm(self):
|
||||||
return self._fsm
|
return self._fsm
|
||||||
|
|
||||||
def handle_controller_request(self, stream, msg_parts):
|
|
||||||
key, data = deserialize_all(*msg_parts)
|
|
||||||
response = self.controller_request_handlers[key](data)
|
|
||||||
stream.send_multipart(serialize_all(key, response))
|
|
||||||
|
|
||||||
def handle_test_request(self, data):
|
|
||||||
return 'OK'
|
|
||||||
|
|
||||||
def handle_solution_check_request(self, data):
|
|
||||||
return {
|
|
||||||
'solved': self.fsm.is_solved(),
|
|
||||||
'message': 'solved' if self.fsm.is_solved() else 'not solved'
|
|
||||||
}
|
|
||||||
|
|
||||||
def listen(self, port):
|
def listen(self, port):
|
||||||
self.application.listen(port)
|
self.application.listen(port)
|
||||||
|
Loading…
Reference in New Issue
Block a user