def connect_build(self, nyamuk, keepalive, clean_session, retain = 0, dup = 0, version = 3): """Build packet for CONNECT command.""" will = 0; will_topic = None byte = 0 client_id = utf8encode(nyamuk.client_id) username = utf8encode(nyamuk.username) if nyamuk.username is not None else None password = utf8encode(nyamuk.password) if nyamuk.password is not None else None #payload len payload_len = 2 + len(client_id) if nyamuk.will is not None: will = 1 will_topic = utf8encode(nyamuk.will.topic) payload_len = payload_len + 2 + len(will_topic) + 2 + nyamuk.will.payloadlen if username is not None: payload_len = payload_len + 2 + len(username) if password != None: payload_len = payload_len + 2 + len(password) self.command = NC.CMD_CONNECT self.remaining_length = 12 + payload_len rc = self.alloc() if rc != NC.ERR_SUCCESS: return rc #var header self.write_string(getattr(NC, 'PROTOCOL_NAME_{0}'.format(version))) self.write_byte( getattr(NC, 'PROTOCOL_VERSION_{0}'.format(version))) byte = (clean_session & 0x1) << 1 if will: byte = byte | ((nyamuk.will.retain & 0x1) << 5) | ((nyamuk.will.qos & 0x3) << 3) | ((will & 0x1) << 2) if nyamuk.username is not None: byte = byte | 0x1 << 7 if nyamuk.password is not None: byte = byte | 0x1 << 6 self.write_byte(byte) self.write_uint16(keepalive) #payload self.write_string(client_id) if will: self.write_string(will_topic) self.write_string(nyamuk.will.payload) if username is not None: self.write_string(username) if password is not None: self.write_string(password) nyamuk.keep_alive = keepalive return NC.ERR_SUCCESS
def send_publish(self, mid, topic, payload, qos, retain, dup): """Send PUBLISH.""" self.logger.debug("Send PUBLISH") if self.sock == NC.INVALID_SOCKET: return NC.ERR_NO_CONN #NOTE: payload may be any kind of data # yet if it is a unicode string we utf8-encode it as convenience return self._do_send_publish(mid, utf8encode(topic), utf8encode(payload), qos, retain, dup)
def unsubscribe(self, topic): """Unsubscribe to some topic.""" if self.sock == NC.INVALID_SOCKET: return NC.ERR_NO_CONN self.logger.info("UNSUBSCRIBE: %s", topic) return self.send_unsubscribe(False, [utf8encode(topic)])
def subscribe(self, topic, qos): """Subscribe to some topic.""" if self.sock == NC.INVALID_SOCKET: return NC.ERR_NO_CONN self.logger.info("SUBSCRIBE: %s", topic) return self.send_subscribe(False, [(utf8encode(topic), qos)])
def setURL(self, URL=None): LogEvent(INFO, self.session.jabberID, "setURL %s" % URL) try: self.bos.setURL(utils.utf8encode(URL)) except AttributeError: #self.alertUser(lang.get("sessionnotactive", config.jid) pass
def unsubscribe_multi(self, topics): """Unsubscribe to some topics.""" if self.sock == NC.INVALID_SOCKET: return NC.ERR_NO_CONN self.logger.info("UNSUBSCRIBE: %s", ', '.join(topics)) return self.send_unsubscribe(False, [utf8encode(topic) for topic in topics])
def subscribe_multi(self, topics): """Subscribe to some topics.""" if self.sock == NC.INVALID_SOCKET: return NC.ERR_NO_CONN self.logger.info("SUBSCRIBE: %s", ', '.join([t for (t,q) in topics])) return self.send_subscribe(False, [(utf8encode(topic), qos) for (topic, qos) in topics])
def connect(self, version = 3, clean_session = 1, will = None): """Connect to server.""" self.clean_session = clean_session self.will = None if will is not None: self.will = NyamukMsg( topic = will['topic'], # unicode text needs to be utf8 encoded to be sent on the wire # str or bytearray are kept as it is payload = utf8encode(will.get('message','')), qos = will.get('qos', 0), retain = will.get('retain', False) ) #CONNECT packet pkt = MqttPkt() pkt.connect_build(self, self.keep_alive, clean_session, version = version) #create socket self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) if self.ssl: opts = { 'do_handshake_on_connect': True, 'ssl_version': ssl.PROTOCOL_TLSv1 } opts.update(self.ssl_opts) #print opts, self.port try: self.sock = ssl.wrap_socket(self.sock, **opts) except Exception, e: self.logger.error("failed to initiate SSL connection: {0}".format(e)) return NC.ERR_UNKNOWN
def subscribe_multi(self, topics): """Subscribe to some topics.""" if self.sock == NC.INVALID_SOCKET: return NC.ERR_NO_CONN self.logger.info("SUBSCRIBE: %s", ', '.join([t for (t, q) in topics])) return self.send_subscribe(False, [(utf8encode(topic), qos) for (topic, qos) in topics])
def setBack(self, backMessage=None): LogEvent(INFO, self.session.jabberID) try: self.bos.awayResponses = {} self.bos.setBack(utils.utf8encode(backMessage)) except AttributeError: #self.alertUser(lang.get("sessionnotactive", config.jid)) pass
def connect(self, version=3, clean_session=1, will=None): """Connect to server.""" self.clean_session = clean_session self.will = None if will is not None: self.will = NyamukMsg( topic=will['topic'], # unicode text needs to be utf8 encoded to be sent on the wire # str or bytearray are kept as it is payload=utf8encode(will.get('message', '')), qos=will.get('qos', 0), retain=will.get('retain', False)) #CONNECT packet pkt = MqttPkt() pkt.connect_build(self, self.keep_alive, clean_session, version=version) #create socket self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) if self.ssl: opts = { 'do_handshake_on_connect': True, 'ssl_version': ssl.PROTOCOL_TLSv1 } opts.update(self.ssl_opts) #print opts, self.port try: self.sock = ssl.wrap_socket(self.sock, **opts) except Exception, e: self.logger.error( "failed to initiate SSL connection: {0}".format(e)) return NC.ERR_UNKNOWN
def connect_build(self, nyamuk, keepalive, clean_session, retain=0, dup=0, version=3): """Build packet for CONNECT command.""" will = 0 will_topic = None byte = 0 client_id = utf8encode(nyamuk.client_id) username = utf8encode( nyamuk.username) if nyamuk.username is not None else None password = utf8encode( nyamuk.password) if nyamuk.password is not None else None #payload len payload_len = 2 + len(client_id) if nyamuk.will is not None: will = 1 will_topic = utf8encode(nyamuk.will.topic) payload_len = payload_len + 2 + len( will_topic) + 2 + nyamuk.will.payloadlen if username is not None: payload_len = payload_len + 2 + len(username) if password != None: payload_len = payload_len + 2 + len(password) self.command = NC.CMD_CONNECT self.remaining_length = 12 + payload_len rc = self.alloc() if rc != NC.ERR_SUCCESS: return rc #var header self.write_string(getattr(NC, 'PROTOCOL_NAME_{0}'.format(version))) self.write_byte(getattr(NC, 'PROTOCOL_VERSION_{0}'.format(version))) byte = (clean_session & 0x1) << 1 if will: byte = byte | ((nyamuk.will.retain & 0x1) << 5) | ( (nyamuk.will.qos & 0x3) << 3) | ((will & 0x1) << 2) if nyamuk.username is not None: byte = byte | 0x1 << 7 if nyamuk.password is not None: byte = byte | 0x1 << 6 self.write_byte(byte) self.write_uint16(keepalive) #payload self.write_string(client_id) if will: self.write_string(will_topic) self.write_string(nyamuk.will.payload) if username is not None: self.write_string(username) if password is not None: self.write_string(password) nyamuk.keep_alive = keepalive return NC.ERR_SUCCESS