mirror of
https://github.com/avatao-content/baseimage-tutorial-framework
synced 2024-11-23 04:51:33 +00:00
39 lines
1.4 KiB
Python
39 lines
1.4 KiB
Python
# Copyright (C) 2018 Avatao.com Innovative Learning Kft.
|
|
# All Rights Reserved. See LICENSE file for details.
|
|
|
|
from tfw.networking import deserialize_all, serialize_all, ControllerConnector
|
|
|
|
|
|
class ControllerResponder:
|
|
def __init__(self, fsm):
|
|
self.fsm = fsm
|
|
self.token = None
|
|
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,
|
|
'token': self.handle_token_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(self.token, response))
|
|
|
|
def handle_test_request(self, data):
|
|
# pylint: disable=unused-argument,no-self-use
|
|
return 'OK'
|
|
|
|
def handle_token_request(self, data):
|
|
if self.token is None:
|
|
self.token = data
|
|
return {'token': self.token}
|
|
|
|
def handle_solution_check_request(self, data):
|
|
# pylint: disable=unused-argument
|
|
return {
|
|
'solved': self.fsm.is_solved(),
|
|
'message': 'solved' if self.fsm.is_solved() else 'not solved'
|
|
}
|