Exemplo n.º 1
0
    def check_spacebridge_pings(self):
        """
        Check when was the last time we received a ping from spacebridge. If it exceeds the threshold, force a
        disconnect by stopping the event loop
        """

        current_time = time_utils.get_current_timestamp()
        seconds_since_ping = current_time - self.last_spacebridge_ping

        self.logger.debug("Time since last spacebridge ping current_time={}, last_spacebridge_ping={}, "
                          "seconds_since_ping={} seconds, self={}"
                          .format(current_time, self.last_spacebridge_ping, seconds_since_ping, id(self)))

        if seconds_since_ping > self.SPACEBRIDGE_RECONNECT_THRESHOLD_SECONDS:
            self.logger.info("Seconds since last ping exceeded threshold. Attempting to reestablish connection with spacebridge")

            if hasattr(self, 'ping_call_id') and self.ping_call_id:
                self.ping_call_id.cancel()

            if hasattr(self, 'check_spacebridge_ping_id') and self.check_spacebridge_ping_id:
                self.check_spacebridge_ping_id.cancel()

            # Explicitly close websocket connection
            self.logger.debug("Closing websocket connection...{}".format(id(self)))
            self.sendClose()

        else:
            self.check_spacebridge_ping_id = asyncio.get_event_loop()\
                .call_later(self.SPACEBRIDGE_RECONNECT_THRESHOLD_SECONDS, self.check_spacebridge_pings)
        def check_spacebridge_pings():
            """
            Check if a ping was received from spacebridge within the last K seconds. If not, drop and reestablish
            the connection.
            """
            try:
                current_time = time_utils.get_current_timestamp()
                seconds_since_ping = current_time - self.last_spacebridge_ping

                self.logger.debug(
                    "Time since last spacebridge ping current_time=%s, last_spacebridge_ping=%s, "
                    "seconds_since_ping=%d seconds, self=%s" %
                    (current_time, self.last_spacebridge_ping,
                     seconds_since_ping, id(self)))
                if seconds_since_ping > self.SPACEBRIDGE_RECONNECT_THRESHOLD_SECONDS:
                    self.logger.info(
                        "Attempting to reestablish connection with spacebridge"
                    )
                    self.transport.loseConnection()

                self.check_spacebridge_ping_id = self.factory.reactor.callLater(
                    self.SPACEBRIDGE_RECONNECT_THRESHOLD_SECONDS,
                    check_spacebridge_pings)
            except:
                self.logger.exception("Exception checking spacebridge ping")
Exemplo n.º 3
0
    async def onPing(self, payload):
        """
        When receiving ping message from spacebridge
        """
        self.last_spacebridge_ping = time_utils.get_current_timestamp()
        self.logger.info("Received Ping from Spacebridge self={}, time={}".format(id(self), self.last_spacebridge_ping))
        self.sendPong()
        self.logger.info("Sent Pong")

        if self.websocket_context:
            try:
                await self.websocket_context.on_ping(payload, self)
            except Exception as e:
                self.logger.exception("Exception on websocket_ctx on_ping")
Exemplo n.º 4
0
    async def onOpen(self):
        """
        When websocket connection is opened, kick off monitoring tasks

        """
        self.ping_call_id = None
        self.check_spacebridge_ping_id = None
        self.last_spacebridge_ping = time_utils.get_current_timestamp()
        self.logger.info(
            "WebSocket connection open. self={}, current_time={}".format(id(self), self.last_spacebridge_ping))

        self.ping_spacebridge()
        self.check_spacebridge_pings()

        if self.parent_process_monitor:
            self.parent_process_monitor.monitor(self.logger, websocket_ctx=self.websocket_context, protocol=self)

        if self.websocket_context:
            try:
                await self.websocket_context.on_open(self)
            except Exception as e:
                self.logger.exception("Exception on websocket_ctx onopen")
    def onOpen(self):
        """ When connection is opened, start sending ping messages
        to Spacebridge every K seconds.
        """

        self.ping_call_id = None
        self.check_spacebridge_ping_id = None
        self.last_spacebridge_ping = time_utils.get_current_timestamp()
        self.logger.info(
            "WebSocket connection open. self=%s, current_time=%s" %
            (id(self), self.last_spacebridge_ping))

        def hello():
            """ Send ping every K seconds.
            """

            try:
                self.sendPing()
                self.logger.debug("Sent Ping")
            except Exception:
                self.logger.exception("Exception sending ping to spacebridge")
            self.ping_call_id = self.factory.reactor.callLater(
                self.PING_FREQUENCY_SECONDS, hello)

        def check_spacebridge_pings():
            """
            Check if a ping was received from spacebridge within the last K seconds. If not, drop and reestablish
            the connection.
            """
            try:
                current_time = time_utils.get_current_timestamp()
                seconds_since_ping = current_time - self.last_spacebridge_ping

                self.logger.debug(
                    "Time since last spacebridge ping current_time=%s, last_spacebridge_ping=%s, "
                    "seconds_since_ping=%d seconds, self=%s" %
                    (current_time, self.last_spacebridge_ping,
                     seconds_since_ping, id(self)))
                if seconds_since_ping > self.SPACEBRIDGE_RECONNECT_THRESHOLD_SECONDS:
                    self.logger.info(
                        "Attempting to reestablish connection with spacebridge"
                    )
                    self.transport.loseConnection()

                self.check_spacebridge_ping_id = self.factory.reactor.callLater(
                    self.SPACEBRIDGE_RECONNECT_THRESHOLD_SECONDS,
                    check_spacebridge_pings)
            except:
                self.logger.exception("Exception checking spacebridge ping")

        # start sending messages every second ..
        hello()
        check_spacebridge_pings()
        send_public_key_to_spacebridge(self)

        if self.parent_process_monitor:
            self.parent_process_monitor.monitor(self.logger,
                                                self.factory.reactor)

        # Start Cluster Monitor
        if self.cluster_monitor is not None:
            self.cluster_monitor.monitor(self.system_auth_header,
                                         self.factory.reactor)
 def onPing(self, payload):
     self.last_spacebridge_ping = time_utils.get_current_timestamp()
     self.logger.debug("Received Ping from Spacebridge self=%s, time=%s" %
                       (id(self), self.last_spacebridge_ping))
     self.sendPong()
     self.logger.debug("Sent Pong")