def mqtt_publish(self, topic, message, qos, retain): if qos: packet_id = self.session.next_packet_id if packet_id in self.session.outgoing_msg: self.logger.warn( "%s A message with the same packet ID is already in flight" % self.session.client_id) else: packet_id = None packet = PublishPacket.build(topic, message, packet_id, False, qos, retain) yield from self.outgoing_queue.put(packet) if qos != QOS_0: inflight_message = OutgoingInFlightMessage(packet, qos, loop=self._loop) inflight_message.sent_publish() self.session.outgoing_msg[packet_id] = inflight_message ack = yield from inflight_message.wait_acknowledge() while not ack: #Retry publish packet = PublishPacket.build(topic, message, packet_id, True, qos, retain) self.logger.debug("Retry delivery of packet %s" % repr(packet)) inflight_message.publish_packet = packet yield from self.outgoing_queue.put(packet) inflight_message.retry_publish() ack = yield from inflight_message.wait_acknowledge() del self.session.outgoing_msg[packet_id]
def build_publish_packet(self, dup=False): """ Build :class:`hbmqtt.mqtt.publish.PublishPacket` from attributes :param dup: force dup flag :return: :class:`hbmqtt.mqtt.publish.PublishPacket` built from ApplicationMessage instance attributes """ return PublishPacket.build(self.topic, self.data, self.packet_id, dup, self.qos, self.retain)
def test_publish_qos2_retry(self): @asyncio.coroutine def server_mock(reader, writer): try: packet = yield from PublishPacket.from_stream(reader) self.assertEqual(packet.topic_name, '/topic') self.assertEqual(packet.qos, QOS_2) self.assertIsNotNone(packet.packet_id) self.assertIn(packet.packet_id, self.session.inflight_out) self.assertIn(packet.packet_id, self.handler._pubrec_waiters) pubrec = PubrecPacket.build(packet.packet_id) yield from pubrec.to_stream(writer) pubrel = yield from PubrelPacket.from_stream(reader) self.assertIn(packet.packet_id, self.handler._pubcomp_waiters) pubcomp = PubcompPacket.build(packet.packet_id) yield from pubcomp.to_stream(writer) except Exception as ae: future.set_exception(ae) @asyncio.coroutine def test_coro(): try: reader, writer = yield from asyncio.open_connection( '127.0.0.1', 8888, loop=self.loop) reader_adapted, writer_adapted = adapt(reader, writer) self.handler = ProtocolHandler(self.plugin_manager, loop=self.loop) self.handler.attach(self.session, reader_adapted, writer_adapted) yield from self.handler.start() yield from self.stop_handler(self.handler, self.session) if not future.done(): future.set_result(True) except Exception as ae: future.set_exception(ae) self.handler = None self.session = Session() message = OutgoingApplicationMessage(1, '/topic', QOS_2, b'test_data', False) message.publish_packet = PublishPacket.build('/topic', b'test_data', rand_packet_id(), False, QOS_2, False) self.session.inflight_out[1] = message future = asyncio.Future(loop=self.loop) coro = asyncio.start_server(server_mock, '127.0.0.1', 8888, loop=self.loop) server = self.loop.run_until_complete(coro) self.loop.run_until_complete(test_coro()) server.close() self.loop.run_until_complete(server.wait_closed()) if future.exception(): raise future.exception()
def publish(self, topic, payload): packet_id = self.get_next_packet_id() packet = PublishPacket.build(topic, payload, packet_id, qos=0, retain=False, dup_flag=None) yield from packet.to_stream(self.writer)
def server_mock(reader, writer): try: packet = PublishPacket.build('/topic', b'test_data', rand_packet_id(), False, QOS_1, False) yield from packet.to_stream(writer) puback = yield from PubackPacket.from_stream(reader) self.assertIsNotNone(puback) self.assertEqual(packet.packet_id, puback.packet_id) except Exception as ae: print(ae) future.set_exception(ae)
def server_mock(reader, writer): try: packet = PublishPacket.build("/topic", b"test_data", rand_packet_id(), False, QOS_1, False) yield from packet.to_stream(writer) puback = yield from PubackPacket.from_stream(reader) self.assertIsNotNone(puback) self.assertEqual(packet.packet_id, puback.packet_id) except Exception as ae: print(ae) future.set_exception(ae)
async def server_mock(reader, writer): try: packet = PublishPacket.build("/topic", b"test_data", rand_packet_id(), False, QOS_1, False) await packet.to_stream(writer) puback = await PubackPacket.from_stream(reader) self.assertIsNotNone(puback) self.assertEqual(packet.packet_id, puback.packet_id) except Exception as ae: print(ae) future.set_exception(ae)
async def server_mock(stream): try: packet = PublishPacket.build('/topic', b'test_data', rand_packet_id(), False, QOS_1, False) await packet.to_stream(stream) puback = await PubackPacket.from_stream(stream) self.assertIsNotNone(puback) self.assertEqual(packet.packet_id, puback.packet_id) except Exception as ae: print(ae) raise
def test_publish_qos1_retry(self): async def server_mock(reader, writer): packet = await PublishPacket.from_stream(reader) try: self.assertEqual(packet.topic_name, "/topic") self.assertEqual(packet.qos, QOS_1) self.assertIsNotNone(packet.packet_id) self.assertIn(packet.packet_id, self.session.inflight_out) self.assertIn(packet.packet_id, self.handler._puback_waiters) puback = PubackPacket.build(packet.packet_id) await puback.to_stream(writer) except Exception as ae: future.set_exception(ae) async def test_coro(): try: reader, writer = await asyncio.open_connection("127.0.0.1", 8888, loop=self.loop) reader_adapted, writer_adapted = adapt(reader, writer) self.handler = ProtocolHandler(self.plugin_manager, loop=self.loop) self.handler.attach(self.session, reader_adapted, writer_adapted) await self.handler.start() await self.stop_handler(self.handler, self.session) if not future.done(): future.set_result(True) except Exception as ae: future.set_exception(ae) self.handler = None self.session = Session() message = OutgoingApplicationMessage(1, "/topic", QOS_1, b"test_data", False) message.publish_packet = PublishPacket.build("/topic", b"test_data", rand_packet_id(), False, QOS_1, False) self.session.inflight_out[1] = message future = asyncio.Future(loop=self.loop) coro = asyncio.start_server(server_mock, "127.0.0.1", 8888, loop=self.loop) server = self.loop.run_until_complete(coro) self.loop.run_until_complete(test_coro()) server.close() self.loop.run_until_complete(server.wait_closed()) if future.exception(): raise future.exception()
async def test_coro(stream_adapted): self.session = Session(None) message = OutgoingApplicationMessage(1, '/topic', QOS_2, b'test_data', False) message.publish_packet = PublishPacket.build( '/topic', b'test_data', rand_packet_id(), False, QOS_2, False) self.session.inflight_out[1] = message try: self.handler = ProtocolHandler(self.plugin_manager) self.handler.attach(self.session, stream_adapted) await self.handler.start() await self.stop_handler(self.handler, self.session) except Exception as ae: raise
def mqtt_publish(self, topic, message, qos, retain): if qos: packet_id = self.session.next_packet_id if packet_id in self.session.outgoing_msg: self.logger.warn("%s A message with the same packet ID is already in flight" % self.session.client_id) else: packet_id = None packet = PublishPacket.build(topic, message, packet_id, False, qos, retain) yield from self.outgoing_queue.put(packet) if qos != QOS_0: inflight_message = OutgoingInFlightMessage(packet, qos, loop=self._loop) inflight_message.sent_publish() self.session.outgoing_msg[packet_id] = inflight_message ack = yield from inflight_message.wait_acknowledge() while not ack: #Retry publish packet = PublishPacket.build(topic, message, packet_id, True, qos, retain) self.logger.debug("Retry delivery of packet %s" % repr(packet)) inflight_message.publish_packet = packet yield from self.outgoing_queue.put(packet) inflight_message.retry_publish() ack = yield from inflight_message.wait_acknowledge() del self.session.outgoing_msg[packet_id]
def server_mock(reader, writer): try: packet = PublishPacket.build('/topic', b'test_data', rand_packet_id(), False, QOS_2, False) yield from packet.to_stream(writer) pubrec = yield from PubrecPacket.from_stream(reader) self.assertIsNotNone(pubrec) self.assertEqual(packet.packet_id, pubrec.packet_id) self.assertIn(packet.packet_id, self.handler._pubrel_waiters) pubrel = PubrelPacket.build(packet.packet_id) yield from pubrel.to_stream(writer) pubcomp = yield from PubcompPacket.from_stream(reader) self.assertIsNotNone(pubcomp) self.assertEqual(packet.packet_id, pubcomp.packet_id) except Exception as ae: future.set_exception(ae)
def server_mock(reader, writer): try: packet = PublishPacket.build("/topic", b"test_data", rand_packet_id(), False, QOS_2, False) yield from packet.to_stream(writer) pubrec = yield from PubrecPacket.from_stream(reader) self.assertIsNotNone(pubrec) self.assertEqual(packet.packet_id, pubrec.packet_id) self.assertIn(packet.packet_id, self.handler._pubrel_waiters) pubrel = PubrelPacket.build(packet.packet_id) yield from pubrel.to_stream(writer) pubcomp = yield from PubcompPacket.from_stream(reader) self.assertIsNotNone(pubcomp) self.assertEqual(packet.packet_id, pubcomp.packet_id) except Exception as ae: future.set_exception(ae)
def test_publish_qos2_retry(self): @asyncio.coroutine def server_mock(reader, writer): try: packet = yield from PublishPacket.from_stream(reader) self.assertEquals(packet.topic_name, "/topic") self.assertEquals(packet.qos, QOS_2) self.assertIsNotNone(packet.packet_id) self.assertIn(packet.packet_id, self.session.inflight_out) self.assertIn(packet.packet_id, self.handler._pubrec_waiters) pubrec = PubrecPacket.build(packet.packet_id) yield from pubrec.to_stream(writer) pubrel = yield from PubrelPacket.from_stream(reader) self.assertIn(packet.packet_id, self.handler._pubcomp_waiters) pubcomp = PubcompPacket.build(packet.packet_id) yield from pubcomp.to_stream(writer) except Exception as ae: future.set_exception(ae) @asyncio.coroutine def test_coro(): try: reader, writer = yield from asyncio.open_connection("127.0.0.1", 8888, loop=self.loop) reader_adapted, writer_adapted = adapt(reader, writer) self.handler = ProtocolHandler(self.plugin_manager, loop=self.loop) self.handler.attach(self.session, reader_adapted, writer_adapted) yield from self.handler.start() yield from self.stop_handler(self.handler, self.session) if not future.done(): future.set_result(True) except Exception as ae: future.set_exception(ae) self.handler = None self.session = Session() message = OutgoingApplicationMessage(1, "/topic", QOS_2, b"test_data", False) message.publish_packet = PublishPacket.build("/topic", b"test_data", rand_packet_id(), False, QOS_2, False) self.session.inflight_out[1] = message future = asyncio.Future(loop=self.loop) coro = asyncio.start_server(server_mock, "127.0.0.1", 8888, loop=self.loop) server = self.loop.run_until_complete(coro) self.loop.run_until_complete(test_coro()) server.close() self.loop.run_until_complete(server.wait_closed()) if future.exception(): raise future.exception()
def mqtt_publish(self, topic, message, packet_id, dup, qos, retain): def qos_0_predicate(): ret = False try: if self.inflight_messages.get(packet_id).state == "published": ret = True # self.logger.debug("qos_0 predicate return %s" % ret) return ret except KeyError: return False def qos_1_predicate(): ret = False try: if self.inflight_messages.get(packet_id).state == "acknowledged": ret = True # self.logger.debug("qos_1 predicate return %s" % ret) return ret except KeyError: return False def qos_2_predicate(): ret = False try: if self.inflight_messages.get(packet_id).state == "completed": ret = True # self.logger.debug("qos_1 predicate return %s" % ret) return ret except KeyError: return False if packet_id in self.inflight_messages: self.logger.warn("A message with the same packet ID is already in flight") packet = PublishPacket.build(topic, message, packet_id, dup, qos, retain) yield from self.outgoing_queue.put(packet) inflight_message = InFlightMessage(packet.variable_header.packet_id, qos) inflight_message.publish() self.inflight_messages[packet.variable_header.packet_id] = inflight_message yield from self._inflight_changed.acquire() if qos == 0x00: yield from self._inflight_changed.wait_for(qos_0_predicate) if qos == 0x01: yield from self._inflight_changed.wait_for(qos_1_predicate) if qos == 0x02: yield from self._inflight_changed.wait_for(qos_2_predicate) self.inflight_messages.pop(packet.variable_header.packet_id) self._inflight_changed.release() return packet
async def server_mock(stream): try: packet = PublishPacket.build('/topic', b'test_data', rand_packet_id(), False, QOS_2, False) await packet.to_stream(stream) pubrec = await PubrecPacket.from_stream(stream) self.assertIsNotNone(pubrec) self.assertEqual(packet.packet_id, pubrec.packet_id) self.assertIn(packet.packet_id, self.handler._pubrel_waiters) pubrel = PubrelPacket.build(packet.packet_id) await pubrel.to_stream(stream) pubcomp = await PubcompPacket.from_stream(stream) self.assertIsNotNone(pubcomp) self.assertEqual(packet.packet_id, pubcomp.packet_id) except Exception as ae: raise
def test_build(self): packet = PublishPacket.build("/topic", b"data", 1, False, QOS_0, False) self.assertEqual(packet.packet_id, 1) self.assertFalse(packet.dup_flag) self.assertEqual(packet.qos, QOS_0) self.assertFalse(packet.retain_flag) packet = PublishPacket.build("/topic", b"data", 1, False, QOS_1, False) self.assertEqual(packet.packet_id, 1) self.assertFalse(packet.dup_flag) self.assertEqual(packet.qos, QOS_1) self.assertFalse(packet.retain_flag) packet = PublishPacket.build("/topic", b"data", 1, False, QOS_2, False) self.assertEqual(packet.packet_id, 1) self.assertFalse(packet.dup_flag) self.assertEqual(packet.qos, QOS_2) self.assertFalse(packet.retain_flag) packet = PublishPacket.build("/topic", b"data", 1, True, QOS_0, False) self.assertEqual(packet.packet_id, 1) assert packet.dup_flag self.assertEqual(packet.qos, QOS_0) self.assertFalse(packet.retain_flag) packet = PublishPacket.build("/topic", b"data", 1, True, QOS_1, False) self.assertEqual(packet.packet_id, 1) assert packet.dup_flag self.assertEqual(packet.qos, QOS_1) self.assertFalse(packet.retain_flag) packet = PublishPacket.build("/topic", b"data", 1, True, QOS_2, False) self.assertEqual(packet.packet_id, 1) assert packet.dup_flag self.assertEqual(packet.qos, QOS_2) self.assertFalse(packet.retain_flag) packet = PublishPacket.build("/topic", b"data", 1, False, QOS_0, True) self.assertEqual(packet.packet_id, 1) self.assertFalse(packet.dup_flag) self.assertEqual(packet.qos, QOS_0) assert packet.retain_flag packet = PublishPacket.build("/topic", b"data", 1, False, QOS_1, True) self.assertEqual(packet.packet_id, 1) self.assertFalse(packet.dup_flag) self.assertEqual(packet.qos, QOS_1) assert packet.retain_flag packet = PublishPacket.build("/topic", b"data", 1, False, QOS_2, True) self.assertEqual(packet.packet_id, 1) self.assertFalse(packet.dup_flag) self.assertEqual(packet.qos, QOS_2) assert packet.retain_flag packet = PublishPacket.build("/topic", b"data", 1, True, QOS_0, True) self.assertEqual(packet.packet_id, 1) assert packet.dup_flag self.assertEqual(packet.qos, QOS_0) assert packet.retain_flag packet = PublishPacket.build("/topic", b"data", 1, True, QOS_1, True) self.assertEqual(packet.packet_id, 1) assert packet.dup_flag self.assertEqual(packet.qos, QOS_1) assert packet.retain_flag packet = PublishPacket.build("/topic", b"data", 1, True, QOS_2, True) self.assertEqual(packet.packet_id, 1) assert packet.dup_flag self.assertEqual(packet.qos, QOS_2) assert packet.retain_flag
def server_mock(reader, writer): packet = PublishPacket.build("/topic", b"test_data", rand_packet_id(), False, QOS_0, False) yield from packet.to_stream(writer)
def server_mock(reader, writer): packet = PublishPacket.build('/topic', b'test_data', rand_packet_id(), False, QOS_0, False) yield from packet.to_stream(writer)
def test_build(self): packet = PublishPacket.build('/topic', b'data', 1, False, QOS_0, False) self.assertEquals(packet.packet_id, 1) self.assertFalse(packet.dup_flag) self.assertEquals(packet.qos, QOS_0) self.assertFalse(packet.retain_flag) packet = PublishPacket.build('/topic', b'data', 1, False, QOS_1, False) self.assertEquals(packet.packet_id, 1) self.assertFalse(packet.dup_flag) self.assertEquals(packet.qos, QOS_1) self.assertFalse(packet.retain_flag) packet = PublishPacket.build('/topic', b'data', 1, False, QOS_2, False) self.assertEquals(packet.packet_id, 1) self.assertFalse(packet.dup_flag) self.assertEquals(packet.qos, QOS_2) self.assertFalse(packet.retain_flag) packet = PublishPacket.build('/topic', b'data', 1, True, QOS_0, False) self.assertEquals(packet.packet_id, 1) self.assertTrue(packet.dup_flag) self.assertEquals(packet.qos, QOS_0) self.assertFalse(packet.retain_flag) packet = PublishPacket.build('/topic', b'data', 1, True, QOS_1, False) self.assertEquals(packet.packet_id, 1) self.assertTrue(packet.dup_flag) self.assertEquals(packet.qos, QOS_1) self.assertFalse(packet.retain_flag) packet = PublishPacket.build('/topic', b'data', 1, True, QOS_2, False) self.assertEquals(packet.packet_id, 1) self.assertTrue(packet.dup_flag) self.assertEquals(packet.qos, QOS_2) self.assertFalse(packet.retain_flag) packet = PublishPacket.build('/topic', b'data', 1, False, QOS_0, True) self.assertEquals(packet.packet_id, 1) self.assertFalse(packet.dup_flag) self.assertEquals(packet.qos, QOS_0) self.assertTrue(packet.retain_flag) packet = PublishPacket.build('/topic', b'data', 1, False, QOS_1, True) self.assertEquals(packet.packet_id, 1) self.assertFalse(packet.dup_flag) self.assertEquals(packet.qos, QOS_1) self.assertTrue(packet.retain_flag) packet = PublishPacket.build('/topic', b'data', 1, False, QOS_2, True) self.assertEquals(packet.packet_id, 1) self.assertFalse(packet.dup_flag) self.assertEquals(packet.qos, QOS_2) self.assertTrue(packet.retain_flag) packet = PublishPacket.build('/topic', b'data', 1, True, QOS_0, True) self.assertEquals(packet.packet_id, 1) self.assertTrue(packet.dup_flag) self.assertEquals(packet.qos, QOS_0) self.assertTrue(packet.retain_flag) packet = PublishPacket.build('/topic', b'data', 1, True, QOS_1, True) self.assertEquals(packet.packet_id, 1) self.assertTrue(packet.dup_flag) self.assertEquals(packet.qos, QOS_1) self.assertTrue(packet.retain_flag) packet = PublishPacket.build('/topic', b'data', 1, True, QOS_2, True) self.assertEquals(packet.packet_id, 1) self.assertTrue(packet.dup_flag) self.assertEquals(packet.qos, QOS_2) self.assertTrue(packet.retain_flag)
async def server_mock(stream): packet = PublishPacket.build('/topic', b'test_data', rand_packet_id(), False, QOS_0, False) await packet.to_stream(stream)
async def server_mock(reader, writer): packet = PublishPacket.build("/topic", b"test_data", rand_packet_id(), False, QOS_0, False) await packet.to_stream(writer)