mirror of
https://github.com/avatao-content/baseimage-tutorial-framework
synced 2024-11-22 18:41:32 +00:00
Move TFWServer to tfw.networking.server package
This commit is contained in:
parent
0f6ec24d45
commit
4fe9d58681
33
lib/tfw/networking/server/tfw_server.py
Normal file
33
lib/tfw/networking/server/tfw_server.py
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
from tornado.web import Application
|
||||||
|
|
||||||
|
from tfw.networking.controller_connector import ControllerConnector
|
||||||
|
from tfw.networking.serialization import deserialize_all, serialize_all
|
||||||
|
from tfw.networking.server.zmq_websocket_handler import FSMManagingSocketHandler
|
||||||
|
|
||||||
|
|
||||||
|
class TFWServer:
|
||||||
|
def __init__(self, fsm_type):
|
||||||
|
self._fsm = fsm_type()
|
||||||
|
self.application = Application(
|
||||||
|
[(r'/ws', FSMManagingSocketHandler, {'fsm': self.fsm})],
|
||||||
|
autoreload=True
|
||||||
|
)
|
||||||
|
self.controller_connector = ControllerConnector()
|
||||||
|
self.controller_connector.register_callback(self.zmq_callback)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def fsm(self):
|
||||||
|
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):
|
||||||
|
self.application.listen(port)
|
@ -1,45 +1,13 @@
|
|||||||
import sys
|
import sys
|
||||||
import tornado
|
import tornado
|
||||||
import zmq
|
import zmq
|
||||||
from tornado.web import Application
|
|
||||||
from tornado.ioloop import IOLoop
|
from tornado.ioloop import IOLoop
|
||||||
|
|
||||||
from sql_injection_fsm import SQLInjectionFSM
|
from sql_injection_fsm import SQLInjectionFSM
|
||||||
|
from tfw.networking.server.tfw_server import TFWServer
|
||||||
from tfw.config import tfwenv
|
from tfw.config import tfwenv
|
||||||
from tfw.config.logs import logging
|
from tfw.config.logs import logging
|
||||||
from tfw.networking.controller_connector import ControllerConnector
|
|
||||||
from tfw.networking.serialization import deserialize_all, serialize_all
|
|
||||||
|
|
||||||
log = logging.getLogger(__name__)
|
log = logging.getLogger(__name__)
|
||||||
from tfw.networking.server.zmq_websocket_handler import FSMManagingSocketHandler
|
|
||||||
|
|
||||||
|
|
||||||
class TFWServer:
|
|
||||||
def __init__(self, fsm_type):
|
|
||||||
self._fsm = fsm_type()
|
|
||||||
self.application = Application(
|
|
||||||
[(r'/ws', FSMManagingSocketHandler, {'fsm': self.fsm})],
|
|
||||||
autoreload=True
|
|
||||||
)
|
|
||||||
self.controller_connector = ControllerConnector()
|
|
||||||
self.controller_connector.register_callback(self.zmq_callback)
|
|
||||||
|
|
||||||
@property
|
|
||||||
def fsm(self):
|
|
||||||
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):
|
|
||||||
self.application.listen(port)
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
Loading…
Reference in New Issue
Block a user