コード例 #1
0
    def __init__(self, client, prefix, site=None):
        assert site is not None
        super().__init__()

        self.clientid = clientcommon.clientid('serialclient')

        staticfmt = {'site': site}
        _topics = mqttcommon.generate_topics_dict(
            self.SERVER.TOPICS, prefix, self.SERVER.AGENTTOPIC, staticfmt)

        _print_wrapper = self.async_print_handle_readlinebuff()
        line_cb = _print_wrapper(self.line_handler)
        error_cb = _print_wrapper(self.error_cb)

        self.topics = {
            'line': mqttcommon.ChannelClient(_topics['line'], line_cb),
            'linestart': mqttcommon.RequestClient(
                _topics['line'], 'start', clientid=self.clientid),
            'linestop': mqttcommon.RequestClient(
                _topics['line'], 'stop', clientid=self.clientid),

            'stop': mqttcommon.RequestClient(
                _topics['node'], 'stop', clientid=self.clientid),

            'stopall': mqttcommon.RequestClient(
                _topics['agenttopic'], 'stopall', clientid=self.clientid),

            'error': mqttcommon.ErrorClient(_topics['agenttopic'],
                                            callback=error_cb),
        }

        self.client = client
        self.client.topics = list(self.topics.values())
コード例 #2
0
ファイル: radiosniffer.py プロジェクト: yyachi/iot-lab-mqtt
    def __init__(self, client, prefix='', site=None):
        assert site is not None
        super().__init__()

        clientid = clientcommon.clientid('radiosnifferclient')

        staticfmt = {'site': site}
        _topics = mqttcommon.generate_topics_dict(self.SERVER.TOPICS, prefix,
                                                  self.SERVER.AGENTTOPIC,
                                                  staticfmt)

        _print_wrapper = self.async_print_handle_readlinebuff()
        error_cb = _print_wrapper(self.error_cb)
        raw_cb = _print_wrapper(self.raw_handler)
        self.topics = {
            'raw':
            mqttcommon.ChannelClient(_topics['noderaw'], raw_cb),
            'rawheader':
            mqttcommon.RequestClient(_topics['raw'],
                                     'rawheader',
                                     clientid=clientid),
            'rawstart':
            mqttcommon.RequestClient(_topics['noderaw'],
                                     'start',
                                     clientid=clientid),
            'stop':
            mqttcommon.RequestClient(_topics['node'],
                                     'stop',
                                     clientid=clientid),
            'stopall':
            mqttcommon.RequestClient(_topics['agenttopic'],
                                     'stopall',
                                     clientid=clientid),
            'error':
            mqttcommon.ErrorClient(_topics['agenttopic'], callback=error_cb),
        }

        self.pcap_files = PcapFiles()
        self.client = client
        self.client.topics = list(self.topics.values())
コード例 #3
0
ファイル: process.py プロジェクト: adjih/iot-lab-mqtt
    def __init__(self, client, prefix, site=None, agenttopic=None):
        assert site is not None or agenttopic is not None
        super().__init__()

        self.clientid = clientcommon.clientid('serialclient')

        _topics = self._topics_dict(prefix, site, agenttopic)

        _print_wrapper = self.async_print_handle_readlinebuff()
        stdout_cb = _print_wrapper(self.stdout_cb)
        stderr_cb = _print_wrapper(self.stderr_cb)
        returncode_cb = _print_wrapper(self.returncode_cb)
        error_cb = _print_wrapper(self.error_cb)

        self.topics = {
            'new': mqttcommon.RequestClient(
                _topics['agenttopic'], 'new', clientid=self.clientid),
            'list': mqttcommon.RequestClient(
                _topics['agenttopic'], 'list', clientid=self.clientid),
            'free': mqttcommon.RequestClient(
                _topics['agenttopic'], 'free', clientid=self.clientid),
            'freeall': mqttcommon.RequestClient(
                _topics['agenttopic'], 'freeall', clientid=self.clientid),

            'procrun': mqttcommon.RequestClient(
                _topics['process'], 'run', clientid=self.clientid),
            'procpoll': mqttcommon.RequestClient(
                _topics['process'], 'poll', clientid=self.clientid),
            'prockill': mqttcommon.RequestClient(
                _topics['process'], 'kill', clientid=self.clientid),
            'procrerun': mqttcommon.RequestClient(
                _topics['process'], 'rerun', clientid=self.clientid),

            'procstdin': mqttcommon.InputClient(_topics['procstdin']),
            'procstdout': mqttcommon.OutputClient(_topics['procstdout'],
                                                  callback=stdout_cb),
            'procstderr': mqttcommon.OutputClient(_topics['procstderr'],
                                                  callback=stderr_cb),

            'procret': mqttcommon.OutputClient(_topics['procret'],
                                               callback=returncode_cb),

            'error': mqttcommon.ErrorClient(_topics['agenttopic'],
                                            callback=error_cb),
        }

        self.client = client
        self.client.topics = list(self.topics.values())
コード例 #4
0
    def __init__(self, client, prefix, site=None):
        assert site is not None
        super().__init__()

        clientid = clientcommon.clientid('nodeclient')

        staticfmt = {'site': site}
        _topics = mqttcommon.generate_topics_dict(self.SERVER.TOPICS, prefix,
                                                  self.SERVER.AGENTTOPIC,
                                                  staticfmt)

        _print_wrapper = self.async_print_handle_readlinebuff()
        error_cb = _print_wrapper(self.error_cb)

        self.topics = {
            'reset':
            mqttcommon.RequestClient(_topics['node'],
                                     'reset',
                                     clientid=clientid),
            'update':
            mqttcommon.RequestClient(_topics['node'],
                                     'update',
                                     clientid=clientid),
            'poweron':
            mqttcommon.RequestClient(_topics['node'],
                                     'poweron',
                                     clientid=clientid),
            'poweroff':
            mqttcommon.RequestClient(_topics['node'],
                                     'poweroff',
                                     clientid=clientid),
            'error':
            mqttcommon.ErrorClient(_topics['agenttopic'], callback=error_cb),
        }

        self.client = client
        self.client.topics = list(self.topics.values())
コード例 #5
0
ファイル: process_client.py プロジェクト: yyachi/iot-lab-mqtt
    def __init__(self, client, prefix, agenttopic):
        _topics = mqttcommon.generate_topics_dict(PROCESS_SERVER.TOPICS,
                                                  prefix, agenttopic)

        clientid = clientcommon.clientid('customprocessclient')
        # Copied from clients/process.py
        self.topics = {
            'new':
            mqttcommon.RequestClient(_topics['agenttopic'],
                                     'new',
                                     clientid=clientid),
            'list':
            mqttcommon.RequestClient(_topics['agenttopic'],
                                     'list',
                                     clientid=clientid),
            'free':
            mqttcommon.RequestClient(_topics['agenttopic'],
                                     'free',
                                     clientid=clientid),
            'freeall':
            mqttcommon.RequestClient(_topics['agenttopic'],
                                     'freeall',
                                     clientid=clientid),

            # I use common callback for all processes
            'procrun':
            mqttcommon.RequestClient(_topics['process'],
                                     'run',
                                     clientid=clientid),
            'procpoll':
            mqttcommon.RequestClient(_topics['process'],
                                     'poll',
                                     clientid=clientid),
            'prockill':
            mqttcommon.RequestClient(_topics['process'],
                                     'kill',
                                     clientid=clientid),
            'procrerun':
            mqttcommon.RequestClient(_topics['process'],
                                     'rerun',
                                     clientid=clientid),
            'procstdin':
            mqttcommon.InputClient(_topics['procstdin']),
            'procstdout':
            mqttcommon.OutputClient(_topics['procstdout'],
                                    callback=self._stdout_cb),
            'procstderr':
            mqttcommon.OutputClient(_topics['procstderr'],
                                    callback=self._stderr_cb),
            'procret':
            mqttcommon.OutputClient(_topics['procret'],
                                    callback=self._returncode_cb),
            'error':
            mqttcommon.ErrorClient(_topics['agenttopic'],
                                   callback=self.error_cb),
        }
        self._rlock = threading.RLock()
        self._process_queues = {}

        self.client = client
        self.client.topics = list(self.topics.values())
コード例 #6
0
    def test_request_topics(self):
        """Test Request Topics."""
        clientid = clientcommon.clientid('testrequest')
        topicname = '{archi}/{num}/line'

        server_cb = mock.Mock()

        client_topics = {
            'linestart':
            mqttcommon.RequestClient(topicname, 'start', clientid=clientid)
        }
        server_topics = {
            'linestart':
            mqttcommon.RequestServer(topicname, 'start', wrap_mock(server_cb))
        }

        client = mqttclient_mock.MQTTClientMock('localhost', 1883,
                                                list(client_topics.values()))
        server = mqttclient_mock.MQTTClientMock('localhost', 1883,
                                                list(server_topics.values()))

        # Simple request answering
        req_topic = mqttcommon.common.topic_lazyformat(
            server_topics['linestart'].topic,
            archi='m3',
            num='1',
            clientid=clientid)

        server_cb.return_value = b'result'
        request_msg = mqttclient_mock.mqttmessage(
            req_topic.format(requestid=1), b'queryone')
        ret = client_topics['linestart'].request(client,
                                                 b'queryone',
                                                 archi='m3',
                                                 num='1')
        server_cb.assert_called_with(request_msg, archi='m3', num='1')
        server_cb.reset_mock()
        self.assertEqual(ret, b'result')

        # Async answer
        def _server_cb(msg, archi, num):
            thr = threading.Thread(target=__server__cb,
                                   args=(msg.reply_publisher, b'responsetwo'))
            thr.start()
            return None

        def __server__cb(publisher, payload):
            import time
            time.sleep(1)
            publisher(payload)

        server_cb.side_effect = _server_cb
        request_msg = mqttclient_mock.mqttmessage(
            req_topic.format(requestid=2), b'querytwo')
        ret = client_topics['linestart'].request(client,
                                                 b'querytwo',
                                                 timeout=5,
                                                 archi='m3',
                                                 num='1')
        server_cb.assert_called_with(request_msg, archi='m3', num='1')
        server_cb.reset_mock()
        server_cb.side_effect = None
        self.assertEqual(ret, b'responsetwo')

        # Timeout from the server
        server_cb.return_value = None  # ignore return value here
        request_msg = mqttclient_mock.mqttmessage(
            req_topic.format(requestid=3), b'querythree')

        self.assertRaises(RuntimeError,
                          client_topics['linestart'].request,
                          client,
                          b'querythree',
                          timeout=5,
                          archi='m3',
                          num='1')
        server_cb.assert_called_with(request_msg, archi='m3', num='1')
        server_cb.reset_mock()

        # Publish timeout
        server_cb.return_value = b''
        client.publish_delay = 5
        server.publish_delay = 0
        request_msg = mqttclient_mock.mqttmessage(
            req_topic.format(requestid=4), b'queryfour')
        self.assertRaises(RuntimeError,
                          client_topics['linestart'].request,
                          client,
                          b'queryfour',
                          timeout=0.1,
                          archi='m3',
                          num='1')
        self.assertFalse(server_cb.called)

        # Cleanup
        # Wait until callback called
        self.assertEqualTimeout(lambda: server_cb.called, True, timeout=10)