def startStream(self, msg, request_handler): """Get this stream rolling!""" connection = self.channel_set.connection_manager.getConnection( msg.headers.get(msg.FLEX_CLIENT_ID_HEADER)) if self.channel_set.notify_connections is True: poller = None else: # Call _notify multiple times if polling. poller = PeriodicCallback(None, float(self.poll_interval) / 1000, IOLoop.instance()) # Handle new message. def _notify(): if connection.connected is False: if poller is not None: poller.stop() connection.unSetNotifyFunc() if request_handler.request.connection.stream.closed() is False: msg = messaging.StreamingMessage.getDisconnectMsg() self.sendMsgs((msg, ), request_handler) request_handler.finish() return msgs = self.channel_set.subscription_manager.pollConnection( connection) if len(msgs) > 0: self.sendMsgs(msgs, request_handler) connection.setNotifyFunc(_notify) if poller is not None: poller.callback = _notify poller.start() # Handle dropped connection. def _connectionLost(): if poller is not None: poller.stop() self.channel_set.disconnect(connection) connection.unSetNotifyFunc() request_handler.request.connection.stream.set_close_callback( _connectionLost) # Send acknowledge message response = msg.acknowledge() response.body = connection.id self.sendMsgs((response, ), request_handler) request_handler.write( chr(messaging.StreamingMessage.NULL_BYTE) * self.KICKSTART_BYTES) request_handler.flush() self.startBeat(connection, request_handler)
def startBeat(self, connection, request_handler): beater = PeriodicCallback(None, self.heart_interval, IOLoop.instance()) def _beat(): if connection.connected is False: beater.stop() else: request_handler.write(chr(messaging.StreamingMessage.NULL_BYTE)) request_handler.flush() beater.callback = _beat beater.start()
def startBeat(self, connection, request_handler): beater = PeriodicCallback(None, self.heart_interval, IOLoop.instance()) def _beat(): if connection.connected is False: beater.stop() else: request_handler.write(chr( messaging.StreamingMessage.NULL_BYTE)) request_handler.flush() beater.callback = _beat beater.start()
def startStream(self, msg, request_handler): """Get this stream rolling!""" connection = self.channel_set.connection_manager.getConnection(msg.headers.get(msg.FLEX_CLIENT_ID_HEADER)) if self.channel_set.notify_connections is True: poller = None else: # Call _notify multiple times if polling. poller = PeriodicCallback(None, self.poll_interval, IOLoop.instance()) # Handle new message. def _notify(): if connection.connected is False: if poller is not None: poller.stop() connection.unSetNotifyFunc() if request_handler.request.connection.stream.closed() is False: msg = messaging.StreamingMessage.getDisconnectMsg() self.sendMsgs((msg,), request_handler) request_handler.finish() return msgs = self.channel_set.subscription_manager.pollConnection(connection) if len(msgs) > 0: self.sendMsgs(msgs, request_handler) connection.setNotifyFunc(_notify) if poller is not None: poller.callback = _notify poller.start() # Handle dropped connection. def _connectionLost(): if poller is not None: poller.stop() self.channel_set.disconnect(connection) connection.unSetNotifyFunc() request_handler.request.connection.stream.set_close_callback(_connectionLost) # Send acknowledge message response = msg.acknowledge() response.body = connection.id self.sendMsgs((response,), request_handler) request_handler.write(chr(messaging.StreamingMessage.NULL_BYTE) * self.KICKSTART_BYTES) request_handler.flush() self.startBeat(connection, request_handler)
def _pollForMessage(self, packet, message, connection): """Overridden to be non-blocking.""" request = self.setupPollRequest(packet) # Polls for messages every self.poll_interval poller = PeriodicCallback(None, float(self.poll_interval) / 1000, IOLoop.instance()) def _timeout(): # Executed when timeout is reached. poller.stop() messages = self.channel_set.subscription_manager.pollConnection( connection) self.finishPoll(request, packet, message, messages) if self.wait_interval > -1: timeout_call = IOLoop.instance().add_timeout( time.time() + float(self.wait_interval) / 1000, _timeout) else: timeout_call = None # Timeout if client drops connection. request.request.connection.stream.set_close_callback(_timeout) def _poll(): messages = self.channel_set.subscription_manager.pollConnection( connection) if len(messages) > 0: poller.stop() if timeout_call is not None: # Disable time out callback IOLoop.instance().remove_timeout(timeout_call) self.finishPoll(request, packet, message, messages) poller.callback = _poll poller.start() return ()
def _pollForMessage(self, packet, message, connection): """Overridden to be non-blocking.""" request = self.setupPollRequest(packet) # Polls for messages every self.poll_interval poller = PeriodicCallback(None, self.poll_interval, IOLoop.instance()) def _timeout(): # Executed when timeout is reached. poller.stop() messages = self.channel_set.subscription_manager.pollConnection(connection) self.finishPoll(request, packet, message, messages) if self.wait_interval > -1: timeout_call = IOLoop.instance().add_timeout( time.time() + self.wait_interval, _timeout) else: timeout_call = None # Timeout if client drops connection. request.request.connection.stream.set_close_callback(_timeout) def _poll(): messages = self.channel_set.subscription_manager.pollConnection(connection) if len(messages) > 0: poller.stop() if timeout_call is not None: # Disable time out callback IOLoop.instance().remove_timeout(timeout_call) self.finishPoll(request, packet, message, messages) poller.callback = _poll poller.start() return ()