Refactor solvable responses

This commit is contained in:
Bálint Bokros 2018-02-12 11:28:35 +01:00
parent e04cc08dd6
commit 6f785b3ca7

View File

@ -12,21 +12,29 @@ class TFWServer:
[(r'/ws', FSMManagingSocketHandler, {'fsm': self.fsm})]
)
self.controller_connector = ControllerConnector()
self.controller_connector.register_callback(self.zmq_callback)
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
def fsm(self):
return self._fsm
def zmq_callback(self, stream, msg_parts):
def handle_controller_request(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, {
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):
self.application.listen(port)