Update paho-mqtt==1.6.1

This commit is contained in:
JonnyWong16 2021-11-28 13:49:48 -08:00
parent d981f6e51f
commit 966a6696d1
No known key found for this signature in database
GPG key ID: B1F1F9807184697A
9 changed files with 680 additions and 502 deletions

View file

@ -1,4 +1,4 @@
__version__ = "1.5.1" __version__ = "1.6.1"
class MQTTException(Exception): class MQTTException(Exception):

File diff suppressed because it is too large Load diff

View file

@ -1,9 +1,9 @@
class MQTTMatcher(object): class MQTTMatcher(object):
"""Intended to manage topic filters including wildcards. """Intended to manage topic filters including wildcards.
Internally, MQTTMatcher use a prefix tree (trie) to store Internally, MQTTMatcher use a prefix tree (trie) to store
values associated with filters, and has an iter_match() values associated with filters, and has an iter_match()
method to iterate efficiently over all filters that match method to iterate efficiently over all filters that match
some topic name.""" some topic name."""
class Node(object): class Node(object):
@ -55,7 +55,7 @@ class MQTTMatcher(object):
del parent._children[k] del parent._children[k]
def iter_match(self, topic): def iter_match(self, topic):
"""Return an iterator on all values associated with filters """Return an iterator on all values associated with filters
that match the :topic""" that match the :topic"""
lst = topic.split('/') lst = topic.split('/')
normal = not topic.startswith('$') normal = not topic.startswith('$')

View file

@ -3,7 +3,7 @@
Copyright (c) 2017, 2019 IBM Corp. Copyright (c) 2017, 2019 IBM Corp.
All rights reserved. This program and the accompanying materials All rights reserved. This program and the accompanying materials
are made available under the terms of the Eclipse Public License v1.0 are made available under the terms of the Eclipse Public License v2.0
and Eclipse Distribution License v1.0 which accompany this distribution. and Eclipse Distribution License v1.0 which accompany this distribution.
The Eclipse Public License is available at The Eclipse Public License is available at

View file

@ -3,7 +3,7 @@
Copyright (c) 2017, 2019 IBM Corp. Copyright (c) 2017, 2019 IBM Corp.
All rights reserved. This program and the accompanying materials All rights reserved. This program and the accompanying materials
are made available under the terms of the Eclipse Public License v1.0 are made available under the terms of the Eclipse Public License v2.0
and Eclipse Distribution License v1.0 which accompany this distribution. and Eclipse Distribution License v1.0 which accompany this distribution.
The Eclipse Public License is available at The Eclipse Public License is available at
@ -16,7 +16,8 @@
******************************************************************* *******************************************************************
""" """
import sys, struct import struct
import sys
from .packettypes import PacketTypes from .packettypes import PacketTypes
@ -268,6 +269,30 @@ class Properties(object):
if self.packetType not in self.properties[self.getIdentFromName(name)][1]: if self.packetType not in self.properties[self.getIdentFromName(name)][1]:
raise MQTTException("Property %s does not apply to packet type %s" raise MQTTException("Property %s does not apply to packet type %s"
% (name, PacketTypes.Names[self.packetType])) % (name, PacketTypes.Names[self.packetType]))
# Check for forbidden values
if type(value) != type([]):
if name in ["ReceiveMaximum", "TopicAlias"] \
and (value < 1 or value > 65535):
raise MQTTException(
"%s property value must be in the range 1-65535" % (name))
elif name in ["TopicAliasMaximum"] \
and (value < 0 or value > 65535):
raise MQTTException(
"%s property value must be in the range 0-65535" % (name))
elif name in ["MaximumPacketSize", "SubscriptionIdentifier"] \
and (value < 1 or value > 268435455):
raise MQTTException(
"%s property value must be in the range 1-268435455" % (name))
elif name in ["RequestResponseInformation", "RequestProblemInformation", "PayloadFormatIndicator"] \
and (value != 0 and value != 1):
raise MQTTException(
"%s property value must be 0 or 1" % (name))
if self.allowsMultiple(name): if self.allowsMultiple(name):
if type(value) != type([]): if type(value) != type([]):
value = [value] value = [value]
@ -294,7 +319,11 @@ class Properties(object):
for name in self.names.keys(): for name in self.names.keys():
compressedName = name.replace(' ', '') compressedName = name.replace(' ', '')
if hasattr(self, compressedName): if hasattr(self, compressedName):
data[compressedName] = getattr(self, compressedName) val = getattr(self, compressedName)
if compressedName == 'CorrelationData' and isinstance(val, bytes):
data[compressedName] = val.hex()
else:
data[compressedName] = val
return data return data
def isEmpty(self): def isEmpty(self):
@ -391,10 +420,10 @@ class Properties(object):
buffer = buffer[VBIlen:] # strip the bytes used by the VBI buffer = buffer[VBIlen:] # strip the bytes used by the VBI
propslenleft = propslen propslenleft = propslen
while propslenleft > 0: # properties length is 0 if there are none while propslenleft > 0: # properties length is 0 if there are none
identifier, VBIlen = VariableByteIntegers.decode( identifier, VBIlen2 = VariableByteIntegers.decode(
buffer) # property identifier buffer) # property identifier
buffer = buffer[VBIlen:] # strip the bytes used by the VBI buffer = buffer[VBIlen2:] # strip the bytes used by the VBI
propslenleft -= VBIlen propslenleft -= VBIlen2
attr_type = self.properties[identifier][0] attr_type = self.properties[identifier][0]
value, valuelen = self.readProperty( value, valuelen = self.readProperty(
buffer, attr_type, propslenleft) buffer, attr_type, propslenleft)

View file

@ -1,7 +1,7 @@
# Copyright (c) 2014 Roger Light <roger@atchoo.org> # Copyright (c) 2014 Roger Light <roger@atchoo.org>
# #
# All rights reserved. This program and the accompanying materials # All rights reserved. This program and the accompanying materials
# are made available under the terms of the Eclipse Public License v1.0 # are made available under the terms of the Eclipse Public License v2.0
# and Eclipse Distribution License v1.0 which accompany this distribution. # and Eclipse Distribution License v1.0 which accompany this distribution.
# #
# The Eclipse Public License is available at # The Eclipse Public License is available at
@ -21,13 +21,15 @@ broker, then disconnect and nothing else is required.
from __future__ import absolute_import from __future__ import absolute_import
import collections import collections
try: try:
from collections.abc import Iterable from collections.abc import Iterable
except ImportError: except ImportError:
from collections import Iterable from collections import Iterable
from . import client as paho
from .. import mqtt from .. import mqtt
from . import client as paho
def _do_publish(client): def _do_publish(client):
"""Internal function""" """Internal function"""
@ -52,6 +54,9 @@ def _on_connect(client, userdata, flags, rc):
else: else:
raise mqtt.MQTTException(paho.connack_string(rc)) raise mqtt.MQTTException(paho.connack_string(rc))
def _on_connect_v5(client, userdata, flags, rc, properties):
"""Internal v5 callback"""
_on_connect(client, userdata, flags, rc)
def _on_publish(client, userdata, mid): def _on_publish(client, userdata, mid):
"""Internal callback""" """Internal callback"""
@ -131,11 +136,15 @@ def multiple(msgs, hostname="localhost", port=1883, client_id="", keepalive=60,
if not isinstance(msgs, Iterable): if not isinstance(msgs, Iterable):
raise TypeError('msgs must be an iterable') raise TypeError('msgs must be an iterable')
client = paho.Client(client_id=client_id, userdata=collections.deque(msgs), client = paho.Client(client_id=client_id, userdata=collections.deque(msgs),
protocol=protocol, transport=transport) protocol=protocol, transport=transport)
client.on_publish = _on_publish client.on_publish = _on_publish
client.on_connect = _on_connect if protocol == mqtt.client.MQTTv5:
client.on_connect = _on_connect_v5
else:
client.on_connect = _on_connect
if proxy_args is not None: if proxy_args is not None:
client.proxy_set(**proxy_args) client.proxy_set(**proxy_args)

View file

@ -3,7 +3,7 @@
Copyright (c) 2017, 2019 IBM Corp. Copyright (c) 2017, 2019 IBM Corp.
All rights reserved. This program and the accompanying materials All rights reserved. This program and the accompanying materials
are made available under the terms of the Eclipse Public License v1.0 are made available under the terms of the Eclipse Public License v2.0
and Eclipse Distribution License v1.0 which accompany this distribution. and Eclipse Distribution License v1.0 which accompany this distribution.
The Eclipse Public License is available at The Eclipse Public License is available at
@ -17,13 +17,14 @@
""" """
import sys import sys
from .packettypes import PacketTypes from .packettypes import PacketTypes
class ReasonCodes: class ReasonCodes:
"""MQTT version 5.0 reason codes class. """MQTT version 5.0 reason codes class.
See ReasonCodes.names for a list of possible numeric values along with their See ReasonCodes.names for a list of possible numeric values along with their
names and the packets to which they apply. names and the packets to which they apply.
""" """
@ -37,7 +38,7 @@ class ReasonCodes:
aName: the String name of the reason code to be created. Ignored aName: the String name of the reason code to be created. Ignored
if the identifier is set. if the identifier is set.
identifier: an integer value of the reason code to be created. identifier: an integer value of the reason code to be created.
""" """
@ -120,7 +121,7 @@ class ReasonCodes:
} }
if identifier == -1: if identifier == -1:
if packetType == PacketTypes.DISCONNECT and aName == "Success": if packetType == PacketTypes.DISCONNECT and aName == "Success":
aName = "Normal disconnection" aName = "Normal disconnection"
self.set(aName) self.set(aName)
else: else:
self.value = identifier self.value = identifier
@ -153,7 +154,7 @@ class ReasonCodes:
if self.packetType in self.names[code][name]: if self.packetType in self.names[code][name]:
identifier = code identifier = code
break break
assert identifier != None, name assert identifier is not None, name
return identifier return identifier
def set(self, name): def set(self, name):
@ -188,4 +189,4 @@ class ReasonCodes:
return self.getName() return self.getName()
def pack(self): def pack(self):
return bytearray([self.value]) return bytearray([self.value])

View file

@ -1,7 +1,7 @@
# Copyright (c) 2016 Roger Light <roger@atchoo.org> # Copyright (c) 2016 Roger Light <roger@atchoo.org>
# #
# All rights reserved. This program and the accompanying materials # All rights reserved. This program and the accompanying materials
# are made available under the terms of the Eclipse Public License v1.0 # are made available under the terms of the Eclipse Public License v2.0
# and Eclipse Distribution License v1.0 which accompany this distribution. # and Eclipse Distribution License v1.0 which accompany this distribution.
# #
# The Eclipse Public License is available at # The Eclipse Public License is available at
@ -20,10 +20,11 @@ you to pass a callback for processing of messages.
""" """
from __future__ import absolute_import from __future__ import absolute_import
from . import client as paho
from .. import mqtt from .. import mqtt
from . import client as paho
def _on_connect(client, userdata, flags, rc):
def _on_connect_v5(client, userdata, flags, rc, properties):
"""Internal callback""" """Internal callback"""
if rc != 0: if rc != 0:
raise mqtt.MQTTException(paho.connack_string(rc)) raise mqtt.MQTTException(paho.connack_string(rc))
@ -34,6 +35,10 @@ def _on_connect(client, userdata, flags, rc):
else: else:
client.subscribe(userdata['topics'], userdata['qos']) client.subscribe(userdata['topics'], userdata['qos'])
def _on_connect(client, userdata, flags, rc):
"""Internal v5 callback"""
_on_connect_v5(client, userdata, flags, rc, None)
def _on_message_callback(client, userdata, message): def _on_message_callback(client, userdata, message):
"""Internal callback""" """Internal callback"""
@ -142,7 +147,10 @@ def callback(callback, topics, qos=0, userdata=None, hostname="localhost",
protocol=protocol, transport=transport, protocol=protocol, transport=transport,
clean_session=clean_session) clean_session=clean_session)
client.on_message = _on_message_callback client.on_message = _on_message_callback
client.on_connect = _on_connect if protocol == mqtt.client.MQTTv5:
client.on_connect = _on_connect_v5
else:
client.on_connect = _on_connect
if proxy_args is not None: if proxy_args is not None:
client.proxy_set(**proxy_args) client.proxy_set(**proxy_args)

View file

@ -3,7 +3,7 @@
Copyright (c) 2017, 2019 IBM Corp. Copyright (c) 2017, 2019 IBM Corp.
All rights reserved. This program and the accompanying materials All rights reserved. This program and the accompanying materials
are made available under the terms of the Eclipse Public License v1.0 are made available under the terms of the Eclipse Public License v2.0
and Eclipse Distribution License v1.0 which accompany this distribution. and Eclipse Distribution License v1.0 which accompany this distribution.
The Eclipse Public License is available at The Eclipse Public License is available at