def render_subscriber(self, request, response, compressed_headers): """ Renders the subscribe message for an event. @param request: request object (Cherrypy) @param response: response object (Cherrypy) @note: see Cherrypy documentation for further info about request and response attributes and methods. """ # print "EventController.render_subscriber" # print "request: " + str(request) # print "compressed_headers: " + str(compressed_headers) log.debug('Receiving subscribe request') request_status = self._validate_subscribe_request(request, compressed_headers) # print "request_status: " + str(request_status) if request_status == 200: timeout = int(compressed_headers['timeout'].split("-")[-1]) callback = compressed_headers['callback'][1:-1] log.debug('Subscriber callback: %s' % callback) subscriber = Subscriber(self.service, timeout, callback, request.server_protocol, self.event_reload_time, self.force_event_reload) response_body = self._get_subscribe_response(request, response, subscriber) self.subscribers.append(subscriber) eventing_variables = {} for var_name, state_var in self.service.get_variables().items(): # only event variables whose send events are set to yes # TODO: check if we need to do this change anywhere else if state_var.send_events: eventing_variables[var_name] = state_var.get_value() EventMessage(subscriber, eventing_variables, 1, "") # Try to unsubscribe after the timeout t_call = ThreadedCall(self._auto_remove_subscriber, None, None, None, None, timeout * 1.1, "uuid:" + str(subscriber.subscription_id)) reactor.add_after_stop_func(t_call.stop) t_call.start() log.debug('Subscribe success') return response_body else: return self._build_error(request_status, request, response)
def _auto_remove_subscriber(self, sid): subscriber = self._find_subscriber(sid) if not subscriber: #Already unsubscribe return time_delta = datetime.now() - subscriber.timestamp if time_delta.seconds > subscriber.subscription_duration: log.debug('Subscriber sid:%s timeout' % str(subscriber.subscription_id)) self._remove_subscriber(subscriber) else: subscriber.timestamp = datetime.now() # Try to unsubscribe after the timeout t_call = ThreadedCall(self._auto_remove_subscriber, None, None, None, None, subscriber.subscription_duration * 1.1, sid) reactor.add_after_stop_func(t_call.stop) t_call.start()
def render_subscriber(self, request, response, compressed_headers): """ Renders the subscribe message for an event. @param request: request object @param response: response object """ log.debug('Receiving subscribe request') request_status = self._validate_subscribe_request(request, compressed_headers) if request_status == 200: timeout = int(compressed_headers['timeout'].split("-")[-1]) callback = compressed_headers['callback'][1:-1] log.debug('Subscriber callback: %s' % callback) subscriber = Subscriber(self.service, timeout, callback, request.server_protocol, self.event_reload_time, self.force_event_reload) response_body = self._get_subscribe_response(request, response, subscriber) self.subscribers.append(subscriber) eventing_variables = {} for var_name, state_var in list(self.service.get_variables().items()): eventing_variables[var_name] = state_var.get_value() EventMessage(subscriber, eventing_variables, 1, "") # Try to unsubscribe after the timeout t_call = ThreadedCall(self._auto_remove_subscriber, None, None, None, None, timeout * 1.1, "uuid:" + str(subscriber.subscription_id)) reactor.add_after_stop_func(t_call.stop) t_call.start() log.debug('Subscribe success') return response_body else: return self._build_error(request_status, request, response)
def render_subscriber(self, request, response, compressed_headers): """ Renders the subscribe message for an event. @param request: request object @param response: response object """ log.debug('Receiving subscribe request') request_status = self._validate_subscribe_request( request, compressed_headers) if request_status == 200: timeout = int(compressed_headers['timeout'].split("-")[-1]) callback = compressed_headers['callback'][1:-1] log.debug('Subscriber callback: %s' % callback) subscriber = Subscriber(self.service, timeout, callback, request.server_protocol, self.event_reload_time, self.force_event_reload) response_body = self._get_subscribe_response( request, response, subscriber) self.subscribers.append(subscriber) eventing_variables = {} for var_name, state_var in self.service.get_variables().items(): eventing_variables[var_name] = state_var.get_value() EventMessage(subscriber, eventing_variables, 1, "") # Try to unsubscribe after the timeout t_call = ThreadedCall(self._auto_remove_subscriber, None, None, None, None, timeout * 1.1, "uuid:" + str(subscriber.subscription_id)) reactor.add_after_stop_func(t_call.stop) t_call.start() log.debug('Subscribe success') return response_body else: return self._build_error(request_status, request, response)
def _auto_renew(self): renew_delay = int(self.service.event_timeout) - 10 if renew_delay <= 0: renew_delay = int(self.service.event_timeout) - 0.5 t_call = ThreadedCall(self._renew, delay=renew_delay) t_call.start()