mirror of
https://github.com/avatao-content/baseimage-tutorial-framework
synced 2024-11-14 15:47:17 +00:00
28 lines
1020 B
Python
28 lines
1020 B
Python
|
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'
|
||
|
}
|