def receive(self, sock, recvthread): """Receive new connection """ output.vvdbg("Receiving packet on UDP socket "+str(sock), self.__class__.__name__) data, addr = sock.recvfrom(self.maxlen) self.scheduler.post_event(message(sock, data, addr))
def run(self): """Main loop """ while self.running: if (len(self._events) == 0): #Sleep for some time if nothing to do time.sleep(self.sleep) continue else: while (self._events.has_event_ready()): (t, event) = self._events.get_next() if ((t-time.time()) > self.tolerance): output.warn("Event "+event.name+" scheduled for "+str(t)+\ " is being run at time "+str(time.time()), self.__class__.__name__) else: output.vvdbg("Event "+event.name+" scheduled for "+str(t)+\ " is being run at time "+str(time.time()), self.__class__.__name__) self._dispatch_event(event) ntime = self.sleep if (len(self._events) > 0): ntime = self._events.get_next_time() - time.time() time.sleep(min(ntime, self.sleep))
def add(self, event, clock): """Add event @param event event to add @param clock time to dispatch (based on time.time()) """ p = bisect.bisect(self.__time, clock) bisect.insort(self.__time, clock) self.__events.insert(p, event) output.vvdbg(str(event)+" inserted for dispatch at time "+str(clock), self.__class__.__name__)
def receive(self, sock, recvthread): """Receive new connection """ output.vvdbg("Receiving packet on raw socket "+str(sock), self.__class__.__name__) data = sock.recv(self.maxlen) if (self.handler == None): self.scheduler.post_event(message(sock, data)) else: self.scheduler.post_event(yapc.priv_callback(self.handler, message(sock, data)))
def post_event(self, event, clock): """Post event @param event event to post @param clock time to post event @return success status """ if (clock > (time.time()+self.sleep)): self._events.add(event, clock) output.vvdbg("Added event "+event.name+" for time "+str(clock), self.__class__.__name__) return True else: output.warn("Cannot add event "+event.name+\ " shorter than "+str(self.sleep)+\ " before execution time", self.__class__.__name__) return False
def _dispatch_event(self, event): """Dispatch next event @param event event to dispatch """ #Dispatch event if (isinstance(event, yapc.priv_callback)): self.__handle_event(event.handler, event) output.vvdbg("Event "+event.name+" dispatched to "+ event.handler.__class__.__name__, self.__class__.__name__) else: try: for handler in self._processors[event.name]: output.vvdbg("Dispatch "+event.name+\ " to "+handler.__class__.__name__, self.__class__.__name__) if (not self.__handle_event(handler, event)): break except KeyError: #No handler, so pass output.vdbg("Event "+str(event.name)+" does not have handler", self.__class__.__name__)