Exemplo n.º 1
0
 def test_to_stream(self):
     variable_header = PacketIdVariableHeader(10)
     payload = SubscribePayload([('a/b', QOS_1), ('c/d', QOS_2)])
     publish = SubscribePacket(variable_header=variable_header,
                               payload=payload)
     out = publish.to_bytes()
     self.assertEqual(out,
                      b'\x82\x0e\x00\x0a\x00\x03a/b\x01\x00\x03c/d\x02')
Exemplo n.º 2
0
 def test_to_stream(self):
     variable_header = PacketIdVariableHeader(10)
     payload = SubscribePayload(
         [
             ('a/b', QOS_1),
             ('c/d', QOS_2)
         ])
     publish = SubscribePacket(variable_header=variable_header, payload=payload)
     out = publish.to_bytes()
     self.assertEqual(out, b'\x82\x0e\x00\x0a\x00\x03a/b\x01\x00\x03c/d\x02')
Exemplo n.º 3
0
 def test_to_stream(self):
     variable_header = PacketIdVariableHeader(10)
     payload = SubscribePayload(
         [
             {'filter': 'a/b', 'qos': 0x01},
             {'filter': 'c/d', 'qos': 0x02}
         ])
     publish = SubscribePacket(variable_header=variable_header, payload=payload)
     out = publish.to_bytes()
     self.assertEqual(out, b'\x82\x0e\x00\x0a\x00\x03a/b\x01\x00\x03c/d\x02')
Exemplo n.º 4
0
 def test_to_stream(self):
     variable_header = PacketIdVariableHeader(10)
     payload = SubscribePayload([{
         'filter': 'a/b',
         'qos': 0x01
     }, {
         'filter': 'c/d',
         'qos': 0x02
     }])
     publish = SubscribePacket(variable_header=variable_header,
                               payload=payload)
     out = publish.to_bytes()
     self.assertEqual(out,
                      b'\x82\x0e\x00\x0a\x00\x03a/b\x01\x00\x03c/d\x02')
Exemplo n.º 5
0
 def test_from_stream(self):
     data = b'\x80\x0e\x00\x0a\x00\x03a/b\x01\x00\x03c/d\x02'
     stream = BufferReader(data)
     message = self.loop.run_until_complete(SubscribePacket.from_stream(stream))
     self.assertEqual(message.payload.topics[0]['filter'], 'a/b')
     self.assertEqual(message.payload.topics[0]['qos'], 0x01)
     self.assertEqual(message.payload.topics[1]['filter'], 'c/d')
     self.assertEqual(message.payload.topics[1]['qos'], 0x02)
Exemplo n.º 6
0
 def test_from_stream(self):
     data = b'\x80\x0e\x00\x0a\x00\x03a/b\x01\x00\x03c/d\x02'
     stream = BufferReader(data)
     message = self.loop.run_until_complete(
         SubscribePacket.from_stream(stream))
     self.assertEqual(message.payload.topics[0]['filter'], 'a/b')
     self.assertEqual(message.payload.topics[0]['qos'], 0x01)
     self.assertEqual(message.payload.topics[1]['filter'], 'c/d')
     self.assertEqual(message.payload.topics[1]['qos'], 0x02)
Exemplo n.º 7
0
 def test_from_stream(self):
     data = b'\x80\x0e\x00\x0a\x00\x03a/b\x01\x00\x03c/d\x02'
     stream = BufferReader(data)
     message = self.loop.run_until_complete(SubscribePacket.from_stream(stream))
     (topic, qos) = message.payload.topics[0]
     self.assertEqual(topic, 'a/b')
     self.assertEqual(qos, QOS_1)
     (topic, qos) = message.payload.topics[1]
     self.assertEqual(topic, 'c/d')
     self.assertEqual(qos, QOS_2)
Exemplo n.º 8
0
 def test_from_stream(self):
     data = b'\x80\x0e\x00\x0a\x00\x03a/b\x01\x00\x03c/d\x02'
     stream = BufferReader(data)
     message = self.loop.run_until_complete(
         SubscribePacket.from_stream(stream))
     (topic, qos) = message.payload.topics[0]
     self.assertEqual(topic, 'a/b')
     self.assertEqual(qos, QOS_1)
     (topic, qos) = message.payload.topics[1]
     self.assertEqual(topic, 'c/d')
     self.assertEqual(qos, QOS_2)
Exemplo n.º 9
0
 def mqtt_subscribe(self, topics, packet_id):
     """
     :param topics: array of topics [{'filter':'/a/b', 'qos': 0x00}, ...]
     :return:
     """
     subscribe = SubscribePacket.build(topics, packet_id)
     yield from self.outgoing_queue.put(subscribe)
     waiter = futures.Future(loop=self._loop)
     self._subscriptions_waiter[subscribe.variable_header.packet_id] = waiter
     return_codes = yield from waiter
     del self._subscriptions_waiter[subscribe.variable_header.packet_id]
     return return_codes
Exemplo n.º 10
0
 def mqtt_subscribe(self, topics, packet_id):
     """
     :param topics: array of topics [{'filter':'/a/b', 'qos': 0x00}, ...]
     :return:
     """
     subscribe = SubscribePacket.build(topics, packet_id)
     yield from self.outgoing_queue.put(subscribe)
     waiter = futures.Future(loop=self._loop)
     self._subscriptions_waiter[
         subscribe.variable_header.packet_id] = waiter
     return_codes = yield from waiter
     del self._subscriptions_waiter[subscribe.variable_header.packet_id]
     return return_codes
Exemplo n.º 11
0
    def mqtt_subscribe(self, topics, packet_id):
        """
        :param topics: array of topics [{'filter':'/a/b', 'qos': 0x00}, ...]
        :return:
        """

        # Build and send SUBSCRIBE message
        subscribe = SubscribePacket.build(topics, packet_id)
        yield from self._send_packet(subscribe)

        # Wait for SUBACK is received
        waiter = futures.Future(loop=self._loop)
        self._subscriptions_waiter[subscribe.variable_header.packet_id] = waiter
        return_codes = yield from waiter

        del self._subscriptions_waiter[subscribe.variable_header.packet_id]
        return return_codes
Exemplo n.º 12
0
    def mqtt_subscribe(self, topics, packet_id):
        """
        :param topics: array of topics [{'filter':'/a/b', 'qos': 0x00}, ...]
        :return:
        """

        # Build and send SUBSCRIBE message
        subscribe = SubscribePacket.build(topics, packet_id)
        yield from self._send_packet(subscribe)

        # Wait for SUBACK is received
        waiter = futures.Future(loop=self._loop)
        self._subscriptions_waiter[subscribe.variable_header.packet_id] = waiter
        return_codes = yield from waiter

        del self._subscriptions_waiter[subscribe.variable_header.packet_id]
        return return_codes
Exemplo n.º 13
0
    async def mqtt_subscribe(self, topics, packet_id):
        """
        :param topics: array of topics [{'filter':'/a/b', 'qos': 0x00}, ...]
        :return:
        """

        waiter = Future()
        self._subscriptions_waiter[packet_id] = waiter
        try:
            # Build and send SUBSCRIBE message
            subscribe = SubscribePacket.build(topics, packet_id)
            await self._send_packet(subscribe)

            # Wait for SUBACK is received
            return_codes = await waiter.get()

        finally:
            del self._subscriptions_waiter[packet_id]
        return return_codes
Exemplo n.º 14
0
    def mqtt_subscribe(self, topics, packet_id):
        """

        :param topics: array of topics [{'filter':'/a/b', 'qos': 0x00}, ...]
        :return:
        """

        def acknowledged_predicate():
            if self.subscriptions[subscribe.variable_header.packet_id].state == "acknowledged":
                return True
            else:
                return False

        subscribe = SubscribePacket.build(topics, packet_id)
        yield from self.outgoing_queue.put(subscribe)
        subscription = Subscription(subscribe.variable_header.packet_id, topics)
        subscription.subscribe()
        self.subscriptions[subscribe.variable_header.packet_id] = subscription
        yield from self._subscriptions_changed.acquire()
        yield from self._subscriptions_changed.wait_for(acknowledged_predicate)
        subscription = self.subscriptions.pop(subscribe.variable_header.packet_id)
        self._subscriptions_changed.release()
        return subscription