kopia lustrzana https://github.com/Yakifo/amqtt
Add Unsuscribe packet
rodzic
4884ae2004
commit
82479dfc18
|
@ -0,0 +1,48 @@
|
|||
# Copyright (c) 2015 Nicolas JOUANIN
|
||||
#
|
||||
# See the file license.txt for copying permission.
|
||||
from hbmqtt.mqtt.packet import MQTTPacket, MQTTFixedHeader, PacketType, PacketIdVariableHeader, MQTTPayload, MQTTVariableHeader
|
||||
from hbmqtt.errors import HBMQTTException
|
||||
from hbmqtt.codecs import *
|
||||
|
||||
|
||||
class UnubscribePayload(MQTTPayload):
|
||||
def __init__(self, topics=[]):
|
||||
super().__init__()
|
||||
self.topics = topics
|
||||
|
||||
def to_bytes(self, fixed_header: MQTTFixedHeader, variable_header: MQTTVariableHeader):
|
||||
out = b''
|
||||
for topic in self.topics:
|
||||
out += encode_string(topic)
|
||||
return out
|
||||
|
||||
@classmethod
|
||||
@asyncio.coroutine
|
||||
def from_stream(cls, reader: asyncio.StreamReader, fixed_header: MQTTFixedHeader,
|
||||
variable_header: MQTTVariableHeader):
|
||||
topics = []
|
||||
while True:
|
||||
try:
|
||||
topic = yield from decode_string(reader)
|
||||
topics.append(topic)
|
||||
except NoDataException:
|
||||
break
|
||||
return cls(topics)
|
||||
|
||||
|
||||
class UnsubscribePacket(MQTTPacket):
|
||||
VARIABLE_HEADER = PacketIdVariableHeader
|
||||
PAYLOAD = UnubscribePayload
|
||||
|
||||
def __init__(self, fixed: MQTTFixedHeader=None, variable_header: PacketIdVariableHeader=None, payload=None):
|
||||
if fixed is None:
|
||||
header = MQTTFixedHeader(PacketType.UNSUBSCRIBE, 0x00)
|
||||
else:
|
||||
if fixed.packet_type is not PacketType.UNSUBSCRIBE:
|
||||
raise HBMQTTException("Invalid fixed packet type %s for UnsubscribePacket init" % fixed.packet_type)
|
||||
header = fixed
|
||||
|
||||
super().__init__(header)
|
||||
self.variable_header = variable_header
|
||||
self.payload = payload
|
|
@ -0,0 +1,29 @@
|
|||
# Copyright (c) 2015 Nicolas JOUANIN
|
||||
#
|
||||
# See the file license.txt for copying permission.
|
||||
import unittest
|
||||
|
||||
from hbmqtt.mqtt.unsubscribe import UnsubscribePacket, UnubscribePayload
|
||||
from hbmqtt.mqtt.packet import PacketIdVariableHeader
|
||||
from hbmqtt.codecs import *
|
||||
|
||||
class SubscribePacketTest(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.loop = asyncio.new_event_loop()
|
||||
|
||||
def test_from_stream(self):
|
||||
data = b'\xa0\x0c\x00\n\x00\x03a/b\x00\x03c/d'
|
||||
stream = asyncio.StreamReader(loop=self.loop)
|
||||
stream.feed_data(data)
|
||||
stream.feed_eof()
|
||||
message = self.loop.run_until_complete(UnsubscribePacket.from_stream(stream))
|
||||
self.assertEqual(message.payload.topics[0], 'a/b')
|
||||
self.assertEqual(message.payload.topics[1], 'c/d')
|
||||
|
||||
def test_to_stream(self):
|
||||
variable_header = PacketIdVariableHeader(10)
|
||||
payload = UnubscribePayload(['a/b', 'c/d'])
|
||||
publish = UnsubscribePacket(variable_header=variable_header, payload=payload)
|
||||
out = publish.to_bytes()
|
||||
print(out)
|
||||
self.assertEqual(out, b'\xa0\x0c\x00\n\x00\x03a/b\x00\x03c/d')
|
Ładowanie…
Reference in New Issue