def connect_async(self, keep_alive_sec, ack_callback=None):
        self._logger.info("Performing async connect...")
        self._logger.info("Keep-alive: %f sec" % keep_alive_sec)
        self._start_workers()
        self._load_callbacks()
        self._load_username_password()

        try:
            self._client_status.set_status(ClientStatus.CONNECT)
            rc = self._internal_async_client.connect(keep_alive_sec,
                                                     ack_callback)
            if MQTT_ERR_SUCCESS != rc:
                self._logger.error("Connect error: %d", rc)
                raise connectError(rc)
        except Exception as e:
            # Provided any error in connect, we should clean up the threads that have been created
            self._event_consumer.stop()
            if not self._event_consumer.wait_until_it_stops(
                    self._connect_disconnect_timeout_sec):
                self._logger.error(
                    "Time out in waiting for event consumer to stop")
            else:
                self._logger.debug("Event consumer stopped")
            self._client_status.set_status(ClientStatus.IDLE)
            raise e

        return FixedEventMids.CONNACK_MID
예제 #2
0
 def connect_async(self, keep_alive_sec, ack_callback=None):
     self._logger.info("Performing async connect...")
     self._logger.info("Keep-alive: %f sec" % keep_alive_sec)
     self._load_callbacks()
     self._client_status.set_status(ClientStatus.CONNECT)
     rc = self._internal_async_client.connect(keep_alive_sec, ack_callback)
     if MQTT_ERR_SUCCESS != rc:
         self._logger.error("Connect error: %d", rc)
         raise connectError(rc)
     return FixedEventMids.CONNACK_MID
예제 #3
0
 def connect(self, keepAliveInterval=30):
     if keepAliveInterval is None:
         self._log.error("connect: None type inputs detected.")
         raise TypeError("None type inputs detected.")
     if not isinstance(keepAliveInterval, int):
         self._log.error(
             "connect: Wrong input type detected. KeepAliveInterval must be an integer."
         )
         raise TypeError("Non-integer type inputs detected.")
     # Return connect succeeded/failed
     ret = False
     # TLS configuration
     if self._useWebsocket:
         # History issue from Yun SDK where AR9331 embedded Linux only have Python 2.7.3
         # pre-installed. In this version, TLSv1_2 is not even an option.
         # SSLv23 is a work-around which selects the highest TLS version between the client
         # and service. If user installs opensslv1.0.1+, this option will work fine for Mutal
         # Auth.
         # Note that we cannot force TLSv1.2 for Mutual Auth. in Python 2.7.3 and TLS support
         # in Python only starts from Python2.7.
         # See also: https://docs.python.org/2/library/ssl.html#ssl.PROTOCOL_SSLv23
         self._pahoClient.tls_set(ca_certs=self._cafile,
                                  cert_reqs=ssl.CERT_REQUIRED,
                                  tls_version=ssl.PROTOCOL_SSLv23)
         self._log.info("Connection type: Websocket")
     else:
         self._pahoClient.tls_set(self._cafile, self._cert, self._key,
                                  ssl.CERT_REQUIRED,
                                  ssl.PROTOCOL_SSLv23)  # Throw exception...
         self._log.info("Connection type: TLSv1.2 Mutual Authentication")
     # Connect
     self._pahoClient.connect(self._host, self._port,
                              keepAliveInterval)  # Throw exception...
     self._pahoClient.loop_start()
     TenmsCount = 0
     while (TenmsCount != self._connectdisconnectTimeout * 100
            and self._connectResultCode == sys.maxsize):
         TenmsCount += 1
         time.sleep(0.01)
     if (self._connectResultCode == sys.maxsize):
         self._log.error("Connect timeout.")
         self._pahoClient.loop_stop()
         raise connectTimeoutException()
     elif (self._connectResultCode == 0):
         ret = True
         self._log.info("Connected to AWS IoT.")
         self._log.debug("Connect time consumption: " +
                         str(float(TenmsCount) * 10) + "ms.")
     else:
         self._log.error("A connect error happened: " +
                         str(self._connectResultCode))
         self._pahoClient.loop_stop()
         raise connectError(self._connectResultCode)
     return ret
 def connect(self, keepAliveInterval=30):
     if keepAliveInterval is None :
         self._log.error("connect: None type inputs detected.")
         raise TypeError("None type inputs detected.")
     if not isinstance(keepAliveInterval, int):
         self._log.error("connect: Wrong input type detected. KeepAliveInterval must be an integer.")
         raise TypeError("Non-integer type inputs detected.")
     # Return connect succeeded/failed
     ret = False
     # TLS configuration
     if self._useWebsocket:
         # History issue from Yun SDK where AR9331 embedded Linux only have Python 2.7.3 
         # pre-installed. In this version, TLSv1_2 is not even an option.
         # SSLv23 is a work-around which selects the highest TLS version between the client
         # and service. If user installs opensslv1.0.1+, this option will work fine for Mutal
         # Auth.
         # Note that we cannot force TLSv1.2 for Mutual Auth. in Python 2.7.3 and TLS support
         # in Python only starts from Python2.7.
         # See also: https://docs.python.org/2/library/ssl.html#ssl.PROTOCOL_SSLv23
         self._pahoClient.tls_set(ca_certs=self._cafile, cert_reqs=ssl.CERT_REQUIRED, tls_version=ssl.PROTOCOL_SSLv23)
         self._log.info("Connection type: Websocket")
     else:
         self._pahoClient.tls_set(self._cafile, self._cert, self._key, ssl.CERT_REQUIRED, ssl.PROTOCOL_SSLv23)  # Throw exception...
         self._log.info("Connection type: TLSv1.2 Mutual Authentication")
     # Connect
     self._pahoClient.connect(self._host, self._port, keepAliveInterval)  # Throw exception...
     self._pahoClient.loop_start()
     TenmsCount = 0
     while(TenmsCount != self._connectdisconnectTimeout * 100 and self._connectResultCode == sys.maxsize):
         TenmsCount += 1
         time.sleep(0.01)
     if(self._connectResultCode == sys.maxsize):
         self._log.error("Connect timeout.")
         self._pahoClient.loop_stop()
         raise connectTimeoutException()
     elif(self._connectResultCode == 0):
         ret = True
         self._log.info("Connected to AWS IoT.")
         self._log.debug("Connect time consumption: " + str(float(TenmsCount) * 10) + "ms.")
     else:
         self._log.error("A connect error happened: " + str(self._connectResultCode))
         self._pahoClient.loop_stop()
         raise connectError(self._connectResultCode)
     return ret
예제 #5
0
    def connect_async(self, keep_alive_sec, ack_callback=None):
        self._logger.info("Performing async connect...")
        self._logger.info("Keep-alive: %f sec" % keep_alive_sec)
        self._start_workers()
        self._load_callbacks()
        self._load_username_password()

        try:
            self._client_status.set_status(ClientStatus.CONNECT)
            rc = self._internal_async_client.connect(keep_alive_sec, ack_callback)
            if MQTT_ERR_SUCCESS != rc:
                self._logger.error("Connect error: %d", rc)
                raise connectError(rc)
        except Exception as e:
            # Provided any error in connect, we should clean up the threads that have been created
            self._event_consumer.stop()
            if not self._event_consumer.wait_until_it_stops(self._connect_disconnect_timeout_sec):
                self._logger.error("Time out in waiting for event consumer to stop")
            else:
                self._logger.debug("Event consumer stopped")
            self._client_status.set_status(ClientStatus.IDLE)
            raise e

        return FixedEventMids.CONNACK_MID