def subscribe(self, sock, packet): topics = [] optionss = [] respqoss = [] for topicFilter, subsoption in packet.data: QoS = subsoption.QoS if topicFilter == "test/nosubscribe": respqoss.append(MQTTV5.ReasonCodes(MQTTV5.PacketTypes.SUBACK, "Unspecified error")) else: if topicFilter == "test/QoS 1 only": respqoss.append(MQTTV5.ReasonCodes(MQTTV5.PacketTypes.SUBACK, identifier=MQTTV5.ReasonCodes.min(1, QoS))) elif topicFilter == "test/QoS 0 only": respqoss.append(MQTTV5.ReasonCodes(MQTTV5.PacketTypes.SUBACK, identifier=min(0, QoS))) else: respqoss.append(MQTTV5.ReasonCodes(MQTTV5.PacketTypes.SUBACK, identifier=QoS)) topics.append(topicFilter) subsoption.QoS = respqoss[-1].value # might have been downgraded optionss.append((subsoption, packet.properties)) if len(topics) > 0: self.broker.subscribe(self.clients[sock].id, topics, optionss) resp = MQTTV5.Subacks() logger.info("[MQTT-2.3.1-7][MQTT-3.8.4-2] Suback has same message id as subscribe") logger.info("[MQTT-3.8.4-1] Must respond with suback") resp.packetIdentifier = packet.packetIdentifier logger.info("[MQTT-3.8.4-5] return code must be returned for each topic in subscribe") logger.info("[MQTT-3.9.3-1] the order of return codes must match order of topics in subscribe") resp.reasonCodes = respqoss respond(sock, resp)
def unsubscribe(self, aClientid, aTopic): rc = [] matchedAny = False if type(aTopic) == type([]): if len(aTopic) > 1: logger.info( "[MQTT-3.10.4-6] each topic must be processed in sequence") for t in aTopic: matched = self.__unsubscribe(aClientid, t) rc.append( MQTTV5.ReasonCodes(MQTTV5.PacketTypes.UNSUBACK, "Success") if matched else MQTTV5.ReasonCodes( MQTTV5.PacketTypes.UNSUBACK, "No subscription found")) if not matchedAny: matchedAny = matched else: matchedAny = self.__unsubscribe(aClientid, aTopic) rc.append( ReasonCodes(UNSUBACK, "Success") if matched else ReasonCodes( UNSUBACK, "No subscription found")) if not matchedAny: logger.info( "[MQTT-3.10.4-5] Unsuback must be sent even if no topics are matched" ) return rc