def start_flows(self): with self.rwlock.rlock(): snapshot = self.flows[:] for flow in snapshot: Trace.trace("hub: starting flow {}", flow.name) flow.start() Trace.debug("hub: started flow {}", flow.name)
def startup(): """Starts all the flows attached to the hub.""" Trace.debug("starting up") Hub.instance.setup() Hub.instance.start_flows() TimeFlow.get().reserve_repetition(Hub.heartbeat_event, \ timedelta(seconds=Config.heartbeat_interval)) Trace.info("started")
def detach(self, flow): """Detaches the specified flow from the hub.""" if flow is None or not isinstance(flow, Flow): raise TypeError() with self.rwlock.wlock(): if flow in self.flows: self.flows.remove(flow) Trace.debug("hub: detached flow {}", flow.name) return self
def attach(self, flow): """Attaches the specified flow to the hub.""" if flow is None or not isinstance(flow, Flow): raise TypeError() with self.rwlock.wlock(): if flow not in self.flows: self.flows.append(flow) Trace.debug("hub: attached flow {}", flow.name) return self
def stop_flows(self): with self.rwlock.rlock(): snapshot = self.flows[::-1] for flow in snapshot: try: Trace.trace("hub: stopping flow {}", flow.name) flow.stop() Trace.debug("hub: stopped flow {}", flow.name) except: pass
def on_receive(self, data): if self.has_heartbeat_strategy: self.heartbeat_strategy.on_receive() self.rx_buffer += data deserializer = Deserializer() while True: deserializer.buffer = self.rx_buffer deserializer.pos = 0 num_bytes, length, transformed = self.parse_header(deserializer) if num_bytes == 0: return if len(self.rx_buffer) < (length + num_bytes): return buffer = self.rx_buffer[num_bytes:num_bytes + length] self.rx_buffer = self.rx_buffer[num_bytes + length:] if self.has_channel_strategy and transformed: try: buffer = self.channel_strategy.after_receive(buffer) except Exception as ex: Trace.error("{} inverse transform error {}", self.link.name, ex) continue deserializer.buffer = buffer deserializer.pos = 0 type_id = deserializer.read_int32(None) event = self.link.create_event(type_id) if event is None: Trace.Error("unknown event type id {}", type_id) continue event.deserialize(deserializer) Trace.debug("{} received {}", self.link.name, event) processed = False if self.has_channel_strategy: processed = self.channel_strategy.process(event) elif not processed and self.has_heartbeat_strategy: processed = self.heartbeat_strategy.process(event) elif not processed: processed = self._process(event) if not processed: event._handle = self.handle Hub.post(event)
def on_heartbeat(self): link_strategy = self.session.link.heartbeat_strategy if link_strategy.outgoing_heartbeat_enabled: if self.has_sent: self.has_sent = False else: self.session.send(Hub.heartbeat_event) if link_strategy.incoming_heartbeat_enabled: if self.has_received: self.has_received = False self.successive_failure_count = 0 else: self.successive_failure_count = self.successive_failure_count + 1 if not self.marked and \ self.successive_failure_count > link_strategy.max_failure_count: Trace.debug("{} {} keepalive failure count {}", self.session.link.name, self.session.handle, self.successive_failure_count) return True
def shutdown(): """Stops all the flows attached to the hub.""" Trace.debug("shutting down") Hub.instance.stop_flows() Hub.instance.teardown() Trace.info("stopped")