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 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)