def run(self): logger.info("Transmitter Application Controller starting") # Kill broadcast subscriber kbsubscriber = NetworkingManager.KillBroadcastSubscriber() usb_monitor_pair = NetworkingManager.UsbMonitorPairClient() self.router = NetworkingManager.TACParent() self.poller = NetworkingManager.Poller() self.poller.register(kbsubscriber, NetworkingManager.POLLIN) self.poller.register(usb_monitor_pair, NetworkingManager.POLLIN) self.poller.register(self.router, NetworkingManager.POLLIN) while True: self.request_buffer.dispatch_all() socks = dict(self.poller.poll(50)) if self.router.contained_in(socks): child_id, string = self.router.recv_string() # do some stuff with reply app = self.transmitter_applications[child_id] if app.last_request == Command.SHUTDOWN: reply = CommandMessage.deserialize(string) self.request_buffer.process_reply(reply) if app.process.is_alive(): app.process.terminate() del self.transmitter_applications[child_id] if usb_monitor_pair.contained_in(socks): # a message from the usb port controller indicates # a usb device has been added or removed from the system request = DocumentMessage.deserialize( usb_monitor_pair.recv_string() ) logger.info("Request received: " + request.serialize()) usb_operation = UsbOperation.from_json(request.contents) self.process_usb_operation(usb_operation) # send a void reply to confirm receipt reply = CommandMessage.VoidReply(request) usb_monitor_pair.send_string(reply.serialize()) if kbsubscriber.contained_in(socks): message = CommandMessage.deserialize( kbsubscriber.recv_string() ) reply_structure = None try: command = Command(message.command) command_processor = CommandProcessor(self) reply_structure = command_processor.process(command) except ExitProcess: self.request_buffer.clear() self.stop_all_transmitter_applications() self.request_buffer.dispatch_all() logger.info("Transmitter Application Controller stopping") break finally: if reply_structure: pass
def test_command_message_deconstruction_reconstruction1(self): """CommandMessage survives serialization and deserialization (no filter)""" original_message = CommandMessage( id="1a2b3c4d", app_name="test_app", version="10.0.1.101.12301", command="TESTINSHIT" ) serialized = original_message.serialize() deserialized_message = CommandMessage.deserialize(serialized) self.assertEqual(original_message, deserialized_message)
def run(self): logger.info("USB Port Controller starting") context = pyudev.Context() monitor = pyudev.Monitor.from_netlink(context) monitor.filter_by(subsystem="usb", device_type="usb_device") observer = pyudev.MonitorObserver(monitor, self.handle_event) observer.start() kbsubscriber = NetworkingManager.KillBroadcastSubscriber() usb_monitor_pair = NetworkingManager.UsbMonitorPairServer() poller = NetworkingManager.Poller() poller.register(kbsubscriber, NetworkingManager.POLLIN) poller.register(usb_monitor_pair, NetworkingManager.POLLIN) for device in self.usb_bus.devices: new_op = UsbOperation(usb_device=device, operation=Operation("add")) message = DocumentMessage(document=new_op.to_json()) request = Request( id=message.id, dispatcher=usb_monitor_pair.send_string, args=message.serialize(), hop_limit=10 ) self.request_buffer.append(request) while True: self.request_buffer.dispatch_all() socks = dict(poller.poll(50)) if usb_monitor_pair.contained_in(socks): reply = CommandMessage.deserialize(usb_monitor_pair.recv_string()) self.request_buffer.process_reply(reply) if kbsubscriber.contained_in(socks): message = CommandMessage.deserialize(kbsubscriber.recv_string()) reply_structure = None try: command = Command(message.command) command_processor = CommandProcessor(self) reply_structure = command_processor.process(command) except ExitProcess: observer.stop() usb_monitor_pair.unbind() logger.info("USB Port Controller stopping") break finally: # send message if applicable if reply_structure: pass
def test_command_message_construction_via_deserialize1(self): """CommandMessage deserialize constructor works (without filter)""" id = "123456" app_name = "testing_app" version = "0.0.1.1.1.2" intent = "COMMAND" command = "PERFORM_TEST" in_args = [id, app_name, version, intent, command] message_string = "{}" message_string = message_string + (DELIMITER+"{}")*4 message_string = message_string.format( id, app_name, version, intent, command ) command_message = CommandMessage.deserialize(message_string) out_args = [ command_message.id, command_message.app_name, command_message.version, command_message.intent, command_message.contents ] self.assertEqual(in_args, out_args)
def test_command_message_serialize(self): """CommandMessage serialize method works""" intent = "COMMAND" filter = "10000" id = "1" app_name = "testing_app" version = "0.0.1" command = "TEST" in_args = [filter, id, app_name, version, intent, command] command_message = CommandMessage( filter=filter, id=id, app_name=app_name, version=version, command=command ) serialized = command_message.serialize() out_args = serialized.split(DELIMITER) self.assertEqual(in_args, out_args)