def __heartbeat_thread(self): c = 1 logger.log_info ('HB started') while self.__connected: if self.__lastHbAck == None: self.__lastHbAck = time.time() if c == self.__hbGap: c = 1 proceed = False diff = 0 diff = abs(int(time.time() - self.__lastHbAck)) if diff <= self.__timeoutSeconds: logger.log_debug('Heartbeat! Differnece with last HbAck ' + str(diff)) proceed = True try: if proceed: self.__send(self.__createHeartbeatRequest()) else: raise Exception('Heartbeat timeout error') except Exception as ex: logger.log_error('Heartbeat Error ' + str(ex)) self.stop() break time.sleep(1) c += 1 logger.log_info ('HB stopped')
def __send(self, msg): try: msg = msg + '\0' self.__socket.send(msg.encode()) except Exception as ex: logger.log_error('Socket Send Error ' + str(ex)) self.__disconnectHandler() raise Exception(ex)
def unsub(self, channelName, callback): try: if (not self.__connected): raise Exception('client not connected') self.__unsubCallbacks[channelName] = callback logger.log_info(("sub '{0}'").format(channelName)) self.__send(self.__createUnsubRequest(channelName)) except Exception as e: logger.log_error('in unsub: ' + str(e)) callback(self.statusCodes.NETWORK_ERROR, channelName)
def auth(self, authJson, callback): try: self.__authCallback = callback self.__authJson = authJson if self.__opened: logger.log_info('auth..') self.__send(self.__createAuthRequest(authJson)) else: self.__isAuthPending = True self.__open() except Exception as e: logger.log_error('in auth: ' + str(e)) callback(self.statusCodes.NETWORK_ERROR, '', '', '')
def pub(self, channelName, data, retainFlag, qos, callback): packetId = str(self.__packetCounter) self.__increaseCounter() try: if (not self.__connected): raise Exception('client not connected') self.__pubCallbacks[packetId] = callback logger.log_debug(("pub packet # '{0}' qos {1} retain {2}").format(packetId, qos, retainFlag)) self.__send(self.__createPubRequest(channelName, data, retainFlag, qos, packetId)) except Exception as e: logger.log_error('in pub: ' + str(e)) callback(self.statusCodes.NETWORK_ERROR, packetId, None) return packetId
def __open(self): if not self.__opened: try: logger.log_info('Opening socket ' + self.__settings.REMOTE_HOST + ':' + str(self.__settings.REMOTE_PORT)) self.__socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) if self.__settings.ENABLE_SSL: self.__socket = ssl.wrap_socket(self.__socket, keyfile=self.__settings.SSL_KEY_PATH, certfile=self.__settings.SSL_CERT_PATH) self.__socket.connect((self.__settings.REMOTE_HOST, self.__settings.REMOTE_PORT)) self.__opened = True t = threading.Thread(target=self.__read) t.daemon = True t.start() self.__openHandler() except Exception as ex: logger.log_error('Socket Error ' + str(ex)) self.__disconnectHandler()
def conn(self, clientId, authToken, callback): if self.__connected: logger.log_info('already connected..') callback(self.statusCodes.OK) return try: self.__connectCallback = callback self.__clientId = clientId self.__authToken = authToken if self.__opened: logger.log_info('conn..') self.__send(self.__createConnRequest(authToken, clientId)) else: self.__isConnectPending = True self.__open() except Exception as e: logger.log_error('in conn: ' + str(e)) callback(self.statusCodes.NETWORK_ERROR)
def __read(self): logger.log_info ('Read started') buffer = bytes() try: while self.__opened: part = self.__socket.recv(1024) if part == b'': break buffer += part packets = PacketParser.get_packets_from_buffer(buffer) if len(packets) > 0: buffer = bytes() t = threading.Thread(target=self.__handle_packets, args=[packets]) t.daemon = True t.start() except Exception as ex: logger.log_error('Socket Read Error ' + str(ex)) self.__disconnectHandler() logger.log_info ('Read stopped')
def pushAck(self, packetId): try: logger.log_info(("pushAck packet # '{0}'").format(packetId)) self.__send(self.__createPushResponse(packetId)) except Exception as e: logger.log_error('in pushAck: ' + str(e))