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_process_void0(self):
        """Processing VOID does nothing"""
        void_object = None
        command_processor = CommandProcessor(void_object)

        command = Command('VOID')
        void = command_processor.process(command)

        self.assertEqual(void, None)
    def test_process_status0(self):
        # classes without to_json should raise errors
        """Processing STATUS of None raises Exception"""
        status_object = None
        command_processor = CommandProcessor(status_object)

        with self.assertRaises(AttributeError):
            command = Command('STATUS')
            status = command_processor.process(command)
    def test_process_shutdown1(self):
        """Processing SHUTDOWN with None raises ExitProcess"""
        # make sure the processor raises exception with any object
        shutdown_object = None
        command_processor = CommandProcessor(shutdown_object)

        command = Command('SHUTDOWN')

        with self.assertRaises(ExitProcess):
            command_processor.process(command)
    def test_process_shutdown1(self):
        """Processing SHUTDOWN of TestStruct raises ExitProcess"""
        # make sure the processor raises exception with fake struct
        shutdown_object = TestStruct('a', 'b', 'c', 'd', 'e', 'f')

        command_processor = CommandProcessor(shutdown_object)

        command = Command('SHUTDOWN')

        with self.assertRaises(ExitProcess):
            command_processor.process(command)
    def test_process_status1(self):
        """Processing STATUS of TestStruct returns JSON to reconstruct TestStruct"""
        status_object = TestStruct('a', 'b', 'c', 'd', 'e', 'f')
        command_processor = CommandProcessor(status_object)

        command = Command('STATUS')
        status = command_processor.process(command)

        status_object_reconstructed = TestStruct.from_json(status)
        self.assertEqual(
            status_object,
            status_object_reconstructed
        )
    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