EventHandlers now handle anchor 'reset' in a dedicated method

This commit is contained in:
Kristóf Tóth 2018-01-17 16:33:34 +01:00
parent 8ee658e62a
commit 677bd57ba4
4 changed files with 40 additions and 4 deletions

View File

@ -24,7 +24,7 @@ class EventHandlerBase:
def event_handler_callback(self, msg_parts):
anchor, message = msg_parts
data_json = json.loads(message)
response = self.handle_event(anchor, data_json)
response = self.handle_event(anchor, data_json) if anchor != b'reset' else self.handle_reset(data_json)
if response is None: return
encoded_response = json.dumps(response).encode('utf-8')
self.zmq_push_socket.send_multipart([anchor, encoded_response])
@ -32,6 +32,9 @@ class EventHandlerBase:
def handle_event(self, anchor, data_json):
raise NotImplementedError
def handle_reset(self, data_json):
return None
def message_other(self, anchor, data):
encoded_anchor = anchor.encode('utf-8')
message = {

View File

@ -5,7 +5,6 @@ from functools import partial
import source_code
from event_handler_base import EventHandlerBase
from source_code_event_handler import SourceCodeEventHandler
from stateful_event_handler import StatefulEventHandler
from tornado.ioloop import IOLoop
from login_component import authorize_login

View File

@ -22,13 +22,14 @@ class SourceCodeEventHandler(EventHandlerBase):
def handle_event(self, anchor, data_json):
data = data_json['data']
if anchor == b'reset':
self.file = self.create_initial_state(process_is_running=True)
command_handlers = {'read': self.read_file,
'write': self.write_file}
command_handlers[data['command']](data_json)
return data_json
def handle_reset(self, data_json):
self.create_initial_state(process_is_running=True)
def read_file(self, data_json):
with open(self.file, 'r') as ifile:
content = ifile.read()

View File

@ -0,0 +1,33 @@
import logging
from event_handler_base import EventHandlerBase
from tornado.ioloop import IOLoop
from tornado.web import Application
from terminado import TermSocket, SingleTermManager
class CORSTermSocket(TermSocket):
def check_origin(self, origin):
return True
class TerminadoEventHandler(EventHandlerBase):
def __init__(self, anchor, zmq_context=None):
super().__init__(anchor, zmq_context)
def handle_event(self, anchor, data_json):
raise NotImplementedError # TODO: wat do?
if __name__ == '__main__':
application = Application(
[(
r'/terminal',
CORSTermSocket,
{'term_manager': SingleTermManager(shell_command=['bash'])}
)]
)
HOST, PORT = 'localhost', 7878
application.listen(PORT, HOST)
logging.getLogger().setLevel(logging.DEBUG)
logging.info('Terminado Mini Server listening on {}:{}'.format(HOST, PORT))
IOLoop.instance().start()