Exemple #1
0
 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')
Exemple #2
0
 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)
Exemple #3
0
 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)
Exemple #4
0
 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, '', '', '')
Exemple #5
0
 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
Exemple #6
0
 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()
Exemple #7
0
    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)
Exemple #8
0
 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')
Exemple #9
0
 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))