mirror of
https://github.com/avatao-content/baseimage-tutorial-framework
synced 2024-11-22 19:31:33 +00:00
Merge pull request #7 from avatao-content/controller
Initial controller implementation
This commit is contained in:
commit
f395adc8c3
14
lib/tfw/networking/async_solvable_connector.py
Normal file
14
lib/tfw/networking/async_solvable_connector.py
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
from tfw.networking.serialization import deserialize_all
|
||||||
|
from tfw.networking.solvable_connector import SolvableConnector
|
||||||
|
|
||||||
|
|
||||||
|
class AsyncSolvableConnector(SolvableConnector):
|
||||||
|
def __init__(self, async_zmq_context=None):
|
||||||
|
if async_zmq_context is None:
|
||||||
|
from zmq.eventloop.future import Context
|
||||||
|
async_zmq_context = Context.instance()
|
||||||
|
super(AsyncSolvableConnector, self).__init__(async_zmq_context)
|
||||||
|
|
||||||
|
async def recv(self):
|
||||||
|
response = await self._zmq_req_socket.recv_multipart()
|
||||||
|
return deserialize_all(*response)
|
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
|
||||||
|
|
||||||
|
|
||||||
@ -9,25 +8,13 @@ class TFWServer:
|
|||||||
def __init__(self, fsm_type):
|
def __init__(self, fsm_type):
|
||||||
self._fsm = fsm_type()
|
self._fsm = fsm_type()
|
||||||
self.application = Application(
|
self.application = Application(
|
||||||
[(r'/ws', FSMManagingSocketHandler, {'fsm': self.fsm})],
|
[(r'/ws', FSMManagingSocketHandler, {'fsm': self.fsm})]
|
||||||
autoreload=True
|
|
||||||
)
|
)
|
||||||
self.controller_connector = ControllerConnector()
|
self.controller_responder = ControllerResponder(self.fsm)
|
||||||
self.controller_connector.register_callback(self.zmq_callback)
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def fsm(self):
|
def fsm(self):
|
||||||
return self._fsm
|
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):
|
def listen(self, port):
|
||||||
self.application.listen(port)
|
self.application.listen(port)
|
||||||
|
@ -4,13 +4,13 @@ from tornado.web import Application
|
|||||||
from tao.config import taoenv
|
from tao.config import taoenv
|
||||||
from tfw.config import tfwenv
|
from tfw.config import tfwenv
|
||||||
from handlers import SolutionCheckHandler, TestHandler
|
from handlers import SolutionCheckHandler, TestHandler
|
||||||
from tfw.networking.solvable_connector import SolvableConnector
|
from tfw.networking.async_solvable_connector import AsyncSolvableConnector
|
||||||
|
|
||||||
from tfw.config.logs import logging
|
from tfw.config.logs import logging
|
||||||
log = logging.getLogger(__name__)
|
log = logging.getLogger(__name__)
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
solvable_connector = SolvableConnector()
|
solvable_connector = AsyncSolvableConnector()
|
||||||
routes = [
|
routes = [
|
||||||
(r'/{secret}/?'.format(secret=taoenv.SECRET), SolutionCheckHandler, {'solvable_connector': solvable_connector}),
|
(r'/{secret}/?'.format(secret=taoenv.SECRET), SolutionCheckHandler, {'solvable_connector': solvable_connector}),
|
||||||
(r'/{secret}/test/?'.format(secret=taoenv.SECRET), TestHandler, {'solvable_connector': solvable_connector})
|
(r'/{secret}/test/?'.format(secret=taoenv.SECRET), TestHandler, {'solvable_connector': solvable_connector})
|
||||||
|
@ -7,9 +7,9 @@ class SolutionCheckHandler(RequestHandler):
|
|||||||
def initialize(self, solvable_connector):
|
def initialize(self, solvable_connector):
|
||||||
self.solvable_connector = solvable_connector
|
self.solvable_connector = solvable_connector
|
||||||
|
|
||||||
def get(self):
|
async def get(self):
|
||||||
log.debug('Sending request to solvable')
|
log.debug('Sending request to solvable')
|
||||||
self.solvable_connector.send('solution_check', {})
|
self.solvable_connector.send('solution_check', {})
|
||||||
resp_key, resp_data = self.solvable_connector.recv()
|
resp_key, resp_data = await self.solvable_connector.recv()
|
||||||
log.debug('Received answer from solvable')
|
log.debug('Received answer from solvable')
|
||||||
self.write(resp_data)
|
self.write(resp_data)
|
||||||
|
@ -5,7 +5,7 @@ class TestHandler(RequestHandler):
|
|||||||
def initialize(self, solvable_connector):
|
def initialize(self, solvable_connector):
|
||||||
self.solvable_connector = solvable_connector
|
self.solvable_connector = solvable_connector
|
||||||
|
|
||||||
def get(self):
|
async def get(self):
|
||||||
self.solvable_connector.send('test', {})
|
self.solvable_connector.send('test', {})
|
||||||
resp_key, resp_data = self.solvable_connector.recv()
|
resp_key, resp_data = await self.solvable_connector.recv()
|
||||||
self.write(resp_data)
|
self.write(resp_data)
|
||||||
|
Loading…
Reference in New Issue
Block a user