mirror of
				https://github.com/avatao-content/baseimage-tutorial-framework
				synced 2025-10-31 13:42:54 +00:00 
			
		
		
		
	EventHandlers now handle anchor 'reset' in a dedicated method
This commit is contained in:
		| @@ -24,7 +24,7 @@ class EventHandlerBase: | |||||||
|     def event_handler_callback(self, msg_parts): |     def event_handler_callback(self, msg_parts): | ||||||
|         anchor, message = msg_parts |         anchor, message = msg_parts | ||||||
|         data_json = json.loads(message) |         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 |         if response is None: return | ||||||
|         encoded_response = json.dumps(response).encode('utf-8') |         encoded_response = json.dumps(response).encode('utf-8') | ||||||
|         self.zmq_push_socket.send_multipart([anchor, encoded_response]) |         self.zmq_push_socket.send_multipart([anchor, encoded_response]) | ||||||
| @@ -32,6 +32,9 @@ class EventHandlerBase: | |||||||
|     def handle_event(self, anchor, data_json): |     def handle_event(self, anchor, data_json): | ||||||
|         raise NotImplementedError |         raise NotImplementedError | ||||||
|  |  | ||||||
|  |     def handle_reset(self, data_json): | ||||||
|  |         return None | ||||||
|  |  | ||||||
|     def message_other(self, anchor, data): |     def message_other(self, anchor, data): | ||||||
|         encoded_anchor = anchor.encode('utf-8') |         encoded_anchor = anchor.encode('utf-8') | ||||||
|         message = { |         message = { | ||||||
|   | |||||||
| @@ -5,7 +5,6 @@ from functools import partial | |||||||
| import source_code | import source_code | ||||||
| from event_handler_base import EventHandlerBase | from event_handler_base import EventHandlerBase | ||||||
| from source_code_event_handler import SourceCodeEventHandler | from source_code_event_handler import SourceCodeEventHandler | ||||||
| from stateful_event_handler import StatefulEventHandler |  | ||||||
| from tornado.ioloop import IOLoop | from tornado.ioloop import IOLoop | ||||||
|  |  | ||||||
| from login_component import authorize_login | from login_component import authorize_login | ||||||
|   | |||||||
| @@ -22,13 +22,14 @@ class SourceCodeEventHandler(EventHandlerBase): | |||||||
|  |  | ||||||
|     def handle_event(self, anchor, data_json): |     def handle_event(self, anchor, data_json): | ||||||
|         data = data_json['data'] |         data = data_json['data'] | ||||||
|         if anchor == b'reset': |  | ||||||
|             self.file = self.create_initial_state(process_is_running=True) |  | ||||||
|         command_handlers = {'read':  self.read_file, |         command_handlers = {'read':  self.read_file, | ||||||
|                             'write': self.write_file} |                             'write': self.write_file} | ||||||
|         command_handlers[data['command']](data_json) |         command_handlers[data['command']](data_json) | ||||||
|         return 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): |     def read_file(self, data_json): | ||||||
|         with open(self.file, 'r') as ifile: |         with open(self.file, 'r') as ifile: | ||||||
|             content = ifile.read() |             content = ifile.read() | ||||||
|   | |||||||
							
								
								
									
										33
									
								
								src/event_handlers/terminado_event_handler.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										33
									
								
								src/event_handlers/terminado_event_handler.py
									
									
									
									
									
										Normal 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() | ||||||
		Reference in New Issue
	
	Block a user