def connect(self, cleansession=True): self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) self.sock.settimeout(5.0) self.sock.connect((self.host, self.port)) connect = MQTTSN.Connects() connect.ClientId = self.clientid connect.CleanSession = cleansession connect.KeepAliveTimer = 0 self.sock.send(connect.pack()) response, address = MQTTSN.unpackPacket(MQTTSN.getPacket(self.sock)) assert response.mh.MsgType == MQTTSN.CONNACK self.startReceiver()
def connect(self, cleansession=True): self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) #self.sock.settimeout(5.0) self.sock.connect((self.host, self.port)) connect = MQTTSN.Connects() connect.ClientId = self.clientid connect.CleanSession = cleansession connect.KeepAliveTimer = 0 self.sock.send(connect.pack()) response, address = MQTTSN.unpackPacket(MQTTSN.getPacket(self.sock)) assert response.mh.MsgType == MQTTSN.CONNACK self.startReceiver()
def receive(self, callback=None): packet = None try: packet, address = MQTTSN.unpackPacket(MQTTSN.getPacket(self.socket)) except: if sys.exc_info()[0] != socket.timeout: print "unexpected exception", sys.exc_info() raise sys.exc_info() if packet == None: time.sleep(0.1) return elif debug: print packet if self.observe == packet.mh.MsgType: print "observed", packet self.observed.append(packet) elif packet.mh.MsgType == MQTTSN.ADVERTISE: if hasattr(callback, "advertise"): callback.advertise(address, packet.GwId, packet.Duration) elif packet.mh.MsgType == MQTTSN.REGISTER: if callback and hasattr(callback, "register"): callback.register(packet.TopicId, packet.Topicname) elif packet.mh.MsgType == MQTTSN.PUBACK: "check if we are expecting a puback" if self.outMsgs.has_key(packet.MsgId) and \ self.outMsgs[packet.MsgId].Flags.QoS == 1: del self.outMsgs[packet.MsgId] if hasattr(callback, "published"): callback.published(packet.MsgId) else: raise Exception("No QoS 1 message with message id "+str(packet.MsgId)+" sent") elif packet.mh.MsgType == MQTTSN.PUBREC: if self.outMsgs.has_key(packet.MsgId): self.pubrel.MsgId = packet.MsgId self.socket.send(self.pubrel.pack()) else: raise Exception("PUBREC received for unknown msg id "+ \ str(packet.MsgId)) elif packet.mh.MsgType == MQTTSN.PUBREL: "release QOS 2 publication to client, & send PUBCOMP" msgid = packet.MsgId if not self.inMsgs.has_key(msgid): pass # what should we do here? else: pub = self.inMsgs[packet.MsgId] if callback == None or \ callback.messageArrived(pub.TopicName, pub.Data, 2, pub.Flags.Retain, pub.MsgId): del self.inMsgs[packet.MsgId] self.pubcomp.MsgId = packet.MsgId self.socket.send(self.pubcomp.pack()) if callback == None: return (pub.TopicName, pub.Data, 2, pub.Flags.Retain, pub.MsgId) elif packet.mh.MsgType == MQTTSN.PUBCOMP: "finished with this message id" if self.outMsgs.has_key(packet.MsgId): del self.outMsgs[packet.MsgId] if hasattr(callback, "published"): callback.published(packet.MsgId) else: raise Exception("PUBCOMP received for unknown msg id "+ \ str(packet.MsgId)) elif packet.mh.MsgType == MQTTSN.PUBLISH: "finished with this message id" if packet.Flags.QoS in [0, 3]: qos = packet.Flags.QoS topicname = packet.TopicName data = packet.Data if qos == 3: qos = -1 if packet.Flags.TopicIdType == MQTTSN.TOPICID: topicname = packet.Data[:packet.TopicId] data = packet.Data[packet.TopicId:] if callback == None: return (topicname, data, qos, packet.Flags.Retain, packet.MsgId) else: callback.messageArrived(topicname, data, qos, packet.Flags.Retain, packet.MsgId) elif packet.Flags.QoS == 1: if callback == None: return (packet.topicName, packet.Data, 1, packet.Flags.Retain, packet.MsgId) else: if callback.messageArrived(packet.TopicName, packet.Data, 1, packet.Flags.Retain, packet.MsgId): self.puback.MsgId = packet.MsgId self.socket.send(self.puback.pack()) elif packet.Flags.QoS == 2: self.inMsgs[packet.MsgId] = packet self.pubrec.MsgId = packet.MsgId self.socket.send(self.pubrec.pack()) else: raise Exception("Unexpected packet"+str(packet)) return packet
def receive(self, topicmap, callback=None): packet = None try: packet, address = MQTTSN.unpackPacket(MQTTSN.getPacket( self.socket)) except: if sys.exc_info()[0] != socket.timeout: #print("unexpected exception", sys.exc_info()) raise sys.exc_info() if packet == None: time.sleep(0.1) return elif debug: print(packet) if self.observe == packet.mh.MsgType: #print("observed", packet) self.observed.append(packet) elif packet.mh.MsgType == MQTTSN.ADVERTISE: if hasattr(callback, "advertise"): callback.advertise(address, packet.GwId, packet.Duration) elif packet.mh.MsgType == MQTTSN.REGISTER: topicmap.register(packet.TopicId, packet.TopicName) elif packet.mh.MsgType == MQTTSN.PUBACK: "check if we are expecting a puback" if packet.MsgId in self.outMsgs and \ self.outMsgs[packet.MsgId].Flags.QoS == 1: del self.outMsgs[packet.MsgId] if hasattr(callback, "published"): callback.published(packet.MsgId) else: raise Exception("No QoS 1 message with message id " + str(packet.MsgId) + " sent") elif packet.mh.MsgType == MQTTSN.PUBREC: if packet.MsgId in self.outMsgs: self.pubrel.MsgId = packet.MsgId self.socket.send(self.pubrel.pack()) else: raise Exception("PUBREC received for unknown msg id "+ \ str(packet.MsgId)) elif packet.mh.MsgType == MQTTSN.PUBREL: "release QOS 2 publication to client, & send PUBCOMP" msgid = packet.MsgId if packet.MsgId not in self.inMsgs: pass # what should we do here? else: pub = self.inMsgs[packet.MsgId] topicname = topicmap.registered[pub.TopicId] if callback == None or \ callback.messageArrived(topicname, pub.Data, 2, pub.Flags.Retain, pub.MsgId): del self.inMsgs[packet.MsgId] self.pubcomp.MsgId = packet.MsgId self.socket.send(self.pubcomp.pack()) if callback == None: return (topicname, pub.Data, 2, pub.Flags.Retain, pub.MsgId) elif packet.mh.MsgType == MQTTSN.PUBCOMP: "finished with this message id" if packet.MsgId in self.outMsgs: del self.outMsgs[packet.MsgId] if hasattr(callback, "published"): callback.published(packet.MsgId) else: raise Exception("PUBCOMP received for unknown msg id "+ \ str(packet.MsgId)) elif packet.mh.MsgType == MQTTSN.PUBLISH: "finished with this message id" if packet.Flags.QoS in [0, 3]: qos = packet.Flags.QoS topicname = topicmap.registered[packet.TopicId] data = packet.Data if qos == 3: qos = -1 if packet.Flags.TopicIdType == MQTTSN.TOPICID: topicname = packet.Data[:packet.TopicId] data = packet.Data[packet.TopicId:] if callback == None: return (topicname, data, qos, packet.Flags.Retain, packet.MsgId) else: callback.messageArrived(topicname, data, qos, packet.Flags.Retain, packet.MsgId) elif packet.Flags.QoS == 1: topicname = topicmap.registered[packet.TopicId] if callback == None: return (topicname, packet.Data, 1, packet.Flags.Retain, packet.MsgId) else: if callback.messageArrived(topicname, packet.Data, 1, packet.Flags.Retain, packet.MsgId): self.puback.MsgId = packet.MsgId self.socket.send(self.puback.pack()) elif packet.Flags.QoS == 2: self.inMsgs[packet.MsgId] = packet self.pubrec.MsgId = packet.MsgId self.socket.send(self.pubrec.pack()) else: raise Exception("Unexpected packet" + str(packet)) return packet