def schedule(cls, msgs_to_process=None, timers_to_process=None): if msgs_to_process is None: msgs_to_process = 32768 if timers_to_process is None: timers_to_process = 32768 while cls._work_to_do(): while cls.queue: msg = cls.queue.popleft() print(msg) if isinstance(msg, ResponseMessage): try: reqmsg = msg.response_to.original_msg except: reqmsg = msg.response_to cls.remove_req_timer(reqmsg) msg.to_node.rcvmsg(msg) msgs_to_process -= 1 if msgs_to_process == 0: return if TimerManager.pending_count() > 0 and timers_to_process > 0: TimerManager.pop_timer() timers_to_process -= 1 if timers_to_process == 0: return
def retry_failed_node(self, _): # Permanently repeating timer if self.failed_nodes: node = self.failed_nodes.pop(0) # Send a test message to the oldest failed node pingmsg = PingReq(self, node) Framework.send_message(pingmsg) # Restart the timer TimerManager.start_timer(self, reason="retry", priority=15, callback=self.retry_failed_node)
def cancel_timers_to(cls, destnode): """Cancel all pending-request timers destined for the given node. Returns a list of the request messages whose timers have been cancelled.""" failed_requests = [] for reqmsg in cls.pending_timers.keys(): if reqmsg.to_node == destnode: TimerManager.cancel_timer(cls.pending_timers[reqmsg]) del cls.pending_timers[reqmsg] failed_requests.append(reqmsg) return failed_requests
def _work_to_do(cls): """Indicate whether there is work to do""" if cls.queue: return True if TimerManager.pending_count() > 0: return True return False
def send_message(cls, msg, expect_reply=True): cls.queue.append(msg) if (expect_reply and not isinstance(msg, ResponseMessage) and 'rsp_timer_pop' in msg.from_node.__class__.__dict__ and callable(msg.from_node.__class__.__dict__['rsp_timer_pop'])): cls.pending_timers[msg] = TimerManager.start_timer( msg.from_node, reason=msg, callback=Framework.rsp_timer_pop)
def schedule(cls, msgs_to_process=None, timers_to_process=None): """Schedule given number of pending messages""" if msgs_to_process is None: msgs_to_process = 32768 if timers_to_process is None: timers_to_process = 32768 while cls._work_to_do(): _logger.info( "Start of schedule: %d (limit %d) pending messages, %d (limit %d) pending timers", len(cls.queue), msgs_to_process, TimerManager.pending_count(), timers_to_process) # Process all the queued up messages (which may enqueue more along the way) while cls.queue: msg = cls.queue.popleft() if msg.to_node.failed: _logger.info("Drop %s->%s: %s as destination down", msg.from_node, msg.to_node, msg) History.add("drop", msg) elif not Framework.reachable(msg.from_node, msg.to_node): _logger.info("Drop %s->%s: %s as route down", msg.from_node, msg.to_node, msg) History.add("cut", msg) else: _logger.info("Dequeue %s->%s: %s", msg.from_node, msg.to_node, msg) if isinstance(msg, ResponseMessage): # figure out the original request this is a response to try: reqmsg = msg.response_to.original_msg except Exception: reqmsg = msg.response_to # cancel any timer associated with the original request cls.remove_req_timer(reqmsg) History.add("deliver", msg) msg.to_node.rcvmsg(msg) msgs_to_process = msgs_to_process - 1 if msgs_to_process == 0: return # No pending messages; potentially pop a (single) timer if TimerManager.pending_count() > 0 and timers_to_process > 0: # Pop the first pending timer; this may enqueue work TimerManager.pop_timer() timers_to_process = timers_to_process - 1 if timers_to_process == 0: return
def schedule(cls, msgs_to_process=None, timers_to_process=None): """Schedule given number of pending messages""" if msgs_to_process is None: msgs_to_process = 32768 if timers_to_process is None: timers_to_process = 32768 while cls._work_to_do(): _logger.info("Start of schedule: %d (limit %d) pending messages, %d (limit %d) pending timers", len(cls.queue), msgs_to_process, TimerManager.pending_count(), timers_to_process) # Process all the queued up messages (which may enqueue more along the way) while cls.queue: #modify msg, con = cls.queue.popleft() ############################### if msg.to_node.failed: _logger.info("Drop %s->%s: %s as destination down", msg.from_node, msg.to_node, msg) History.add("drop", msg) elif not Framework.reachable(msg.from_node, msg.to_node): _logger.info("Drop %s->%s: %s as route down", msg.from_node, msg.to_node, msg) History.add("cut", msg) else: _logger.info("Dequeue %s->%s: %s", msg.from_node, msg.to_node, msg) if isinstance(msg, ResponseMessage): # figure out the original request this is a response to try: reqmsg = msg.response_to.original_msg except Exception: reqmsg = msg.response_to # cancel any timer associated with the original request cls.remove_req_timer(reqmsg) History.add("deliver", msg) msgs_to_process = msgs_to_process - 1 if msgs_to_process == 0: return # No pending messages; potentially pop a (single) timer if TimerManager.pending_count() > 0 and timers_to_process > 0: # Pop the first pending timer; this may enqueue work TimerManager.pop_timer() timers_to_process = timers_to_process - 1 if timers_to_process == 0: return
def send_message(cls, msg, expect_reply=True): """Send a message""" _logger.info("Enqueue %s->%s: %s", msg.from_node, msg.to_node, msg) cls.queue.append(msg) History.add("send", msg) # Automatically run timers for request messages if the sender can cope # with retry timer pops if (expect_reply and not isinstance(msg, ResponseMessage) and 'rsp_timer_pop' in msg.from_node.__class__.__dict__ and callable(msg.from_node.__class__.__dict__['rsp_timer_pop'])): cls.pending_timers[msg] = TimerManager.start_timer( msg.from_node, reason=msg, callback=Framework.rsp_timer_pop)
def send_message(cls, msg, expect_reply=True): """Send a message""" _logger.info("Enqueue %s->%s: %s", msg.from_node, msg.to_node, msg) cls.queue.append(msg) History.add("send", msg) # Automatically run timers for request messages if the sender can cope # with retry timer pops if (expect_reply and not isinstance(msg, ResponseMessage) and 'rsp_timer_pop' in msg.from_node.__class__.__dict__ and callable(msg.from_node.__class__.__dict__['rsp_timer_pop'])): cls.pending_timers[msg] = TimerManager.start_timer(msg.from_node, reason=msg, callback=Framework.rsp_timer_pop)
def retry_failed_node(self): # Permanently repeating timer #modified while True: #################### gevent.sleep(5) # print 'sleeping...' if self.failed_nodes: if len(self.failed_nodes) < 1: continue # print "self.failed_nodes: ",self.failed_nodes node = self.failed_nodes.pop(0) # Send a test message to the oldest failed node pingmsg = PingReq(self.addr, node) #modified con = self.connections[self.servers.index(node)] result = Framework.send_message(pingmsg, con) if result is False and node not in self.failed_nodes: self.failed_nodes.append(node) ############################ # Restart the timer TimerManager.start_timer(self, reason="retry", priority=15, callback=None)#self.retry_failed_node)
def __init__(self, derpbot, host, port, username): self.host = host self.port = port self.username = username self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.log = logging.getLogger('connection') self.packlog = logging.getLogger('packets') self.packhandler = loghandler.PacketHandler(self.packlog) self.buffer = '' self.last_sent_packet = time.time() self.timers = TimerManager.get()
def send_message(cls, msg, con, expect_reply=True): """receive a message""" _logger.info("Enqueue %s->%s: %s", msg.from_node, msg.to_node, msg) cls.queue.append(msg) History.add("receive", msg) # Automatically run timers for request messages if the sender can cope # with retry timer pops if (expect_reply and not isinstance(msg, ResponseMessage) and 'rsp_timer_pop' in msg.from_node.__class__.__dict__ and callable(msg.from_node.__class__.__dict__['rsp_timer_pop'])): cls.pending_timers[msg] = TimerManager.start_timer(msg.from_node, reason=msg, callback=Framework.rsp_timer_pop) #modified try: pmsg = pickle.dumps(msg) return con.rcvmsg(pmsg) except zerorpc.TimeoutExpired: print "time out when", msg.from_node, "calls", msg.to_node return False
def __init__(self): Singleton.__init__(self) loghandler.RootHandler(self) host, port = config.server self.conn = Connection(self, host, port, config.username) self.packhandler = self.conn.packhandler self.timers = TimerManager.get() self.storage = Storage.load() self.x = self.y = self.z = 0 self.stance = 0 self.yaw = 180 self.pitch = 180 self.grounded = 1 self.health = 0 self.players = {} # by eid self.path = None
def reset(): """Reset all message and other history""" Framework.reset() TimerManager.reset() History.reset()
def remove_req_timer(cls, reqmsg): if reqmsg in cls.pending_timers: # Cancel request timer as we've seen a response TimerManager.cancel_timer(cls.pending_timers[reqmsg]) del cls.pending_timers[reqmsg]
def _work_to_do(cls): if cls.queue: return True if TimerManager.pending_count() > 0: return True return False
def initialize_timers(self): self.timer_manager = TimerManager( self.windows[0], self.timers) # XXX: passing window in a nasty way
def test_Parser(self): # test header data = self._head1.marshal() head = MsgCSLogin().unmarshal(data) self.assertEqual(self._head1.name, head.name) self.assertEqual(self._head1.icon, head.icon) data = self._head2.marshal() head = MsgCSMoveto().unmarshal(data) self.assertEqual(self._head2.x, head.x) self.assertEqual(self._head2.y, head.y) # test dispatcher msg = MsgService() msg.sid = 100 msg.cid = 10 self.assertEqual(self._dispatcher.dispatch(msg, 'client1'), 'client1') msg.cid = 20 self.assertEqual(self._dispatcher.dispatch(msg, 'client2'), 'client2') # test network host = SimpleHost() host.startup(2000) sock = NetStream() last = time.time() sock.connect('127.0.0.1', 2000) stat = 0 last = time.time() sock.nodelay(1) while 1: time.sleep(0.1) host.process() sock.process() if stat == 0: if sock.status() == conf.NET_STATE_ESTABLISHED: stat = 1 data = cPickle.dumps((stat, 'Hello, world !!'), -1) sock.send(data) last = time.time() elif stat == 1: if time.time() - last >= 2.0: stat = 2 data = cPickle.dumps((stat, 'exit'), -1) sock.send(data) event, wparam, data = host.read() if event < 0: continue if event == conf.NET_CONNECTION_DATA: client_stat, message = cPickle.loads(data) host.send_client(wparam, 'RE: ' + message) if client_stat == 1: self.assertEqual(message, 'Hello, world !!') elif client_stat == 2: self.assertEqual(message, 'exit') host.close_client(wparam) host.shutdown() break # test timer TimerManager.add_repeat_timer(0.15, self.addCount) last = time.time() while 1: time.sleep(0.01) TimerManager.scheduler() if time.time() - last > 1.0: break self.assertEqual(self.count, 6) return
def schedule(cls, msgs_to_process=None, timers_to_process=None): """Schedule given number of pending messages""" if msgs_to_process is None: msgs_to_process = 32768 if timers_to_process is None: timers_to_process = 32768 while cls._work_to_do(): _logger.info("Start of schedule: %d (limit %d) pending messages, %d (limit %d) pending timers", len(cls.queue), msgs_to_process, TimerManager.pending_count(), timers_to_process) # Process all the queued up messages (which may enqueue more along the way) while cls.queue: msg = cls.queue.popleft() if msg.to_node in cls.block: _logger.info("Drop %s->%s: %s as destination down", msg.from_node, msg.to_node, msg) History.add("drop", msg) else: try: c = zerorpc.Client(timeout=1) c.connect('tcp://' + msg.to_node) except zerorpc.TimeoutExpired: _logger.info("Drop %s->%s: %s as destination down", msg.from_node, msg.to_node, msg) History.add("drop", msg) continue if not Framework.reachable(msg.from_node, msg.to_node): _logger.info("Drop %s->%s: %s as route down", msg.from_node, msg.to_node, msg) History.add("cut", msg) _logger.info("Dequeue %s->%s: %s", msg.from_node, msg.to_node, msg) elif isinstance(msg, ResponseMessage): # figure out the original request this is a response to try: reqmsg = msg.response_to.original_msg except Exception: reqmsg = msg.response_to # cancel any timer associated with the original request cls.remove_req_timer(reqmsg) History.add("deliver", msg) m = pickle.dumps(msg) try: c.rcvmsg(m) c.close() except: print 'time out' cls.queue.append(msg) print cls.block for node in cls.nodeList: if node != msg.to_node: bmsg = BlockRsp(from_node=msg.from_node, to_node=node, key=msg.to_node, msg_id=None) cls.queue.append(bmsg) msgs_to_process = msgs_to_process - 1 if msgs_to_process == 0: return # No pending messages; potentially pop a (single) timer if TimerManager.pending_count() > 0 and timers_to_process > 0: # Pop the first pending timer; this may enqueue work TimerManager.pop_timer() timers_to_process = timers_to_process - 1 if timers_to_process == 0: return
def remove_req_timer(cls, reqmsg): if reqmsg in cls.pending_timers: TimerManager.cancel_timer(cls.pending_timers[reqmsg]) del cls.pending_timers[reqmsg]