def handle_event(self, action, device): """ action: the action that caused this event (eg add, remove) device: the usb device that prompted the event """ operation = Operation(action) if not device.attributes: usb_device = self.usb_bus.find_with(path=device.device_path) new_op = UsbOperation(operation=operation, usb_device=usb_device) message = DocumentMessage(document=new_op.to_json()) request = Request( id=message.id, dispatcher=self.usb_monitor_pair.send_string, args=message.serialize(), hop_limit=10 ) self.request_buffer.append(request) self.usb_bus.remove(usb_device) else: usb_device = UsbDevice(device) self.usb_bus.add(usb_device) new_op = UsbOperation(operation=operation, usb_device=usb_device) message = DocumentMessage(document=new_op.to_json()) request = Request( id=message.id, dispatcher=self.usb_monitor_pair.send_string, args=message.serialize(), hop_limit=10 ) self.request_buffer.append(message)
def test_struct_reconstruction(self): """Message's serialize/deserialize methods on TestStruct returns original""" test_object = TestStruct( 102130943, 12.3240193, "This is a cool struct", True, 1230914 ) test_id = "10000" app_name = "testing_app" version = "1231094416" document = test_object.to_json() message = DocumentMessage( id=test_id, app_name=app_name, version=version, document=document ) serialized = message.serialize() new_message = DocumentMessage.deserialize(serialized) new_document = new_message.document reconstructed_test_object = TestStruct.from_json(new_document) self.assertEqual(test_object, reconstructed_test_object)
def test_document_message_deconstruction_reconstruction(self): """DocumentMessage survives serialization/deserialization (no filter)""" original_message = DocumentMessage( id="9z8x7y6w", app_name="test_app", version="2130123.231.31494251", document="{\"test_struct\":{\"data\"=\"fake\"}}" ) serialized = original_message.serialize() deserialized_message = DocumentMessage.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 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_document_message_construction_via_deserialize1(self): """DocumentMessage deserialize constructor works (without filter)""" id = "123456" app_name = "testing_app" version = "0.0.1.1.1.2" intent = "DOCUMENT" document = "{\"test_struct\":{\"data\"=\"fake\"}}" in_args = [id, app_name, version, intent, document] message_string = "{}" message_string = message_string + (DELIMITER+"{}")*4 message_string = message_string.format( id, app_name, version, intent, document ) document_message = DocumentMessage.deserialize(message_string) out_args = [ document_message.id, document_message.app_name, document_message.version, document_message.intent, document_message.contents ] self.assertEqual(in_args, out_args)
def test_document_message_serialize(self): """DocumentMessage serialize method works""" intent = "DOCUMENT" filter = "10000" id = "1" app_name = "testing_app" version = "0.0.1" document = "{\"test_struct\":{\"data\"=\"fake\"}}" in_args = [filter, id, app_name, version, intent, document] document_message = DocumentMessage( filter=filter, id=id, app_name=app_name, version=version, document=document ) serialized = document_message.serialize() out_args = serialized.split(DELIMITER) self.assertEqual(in_args, out_args)