Exemplo n.º 1
0
    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
Exemplo n.º 2
0
 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)
Exemplo n.º 3
0
 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)
Exemplo n.º 4
0
 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
Exemplo n.º 5
0
 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
Exemplo n.º 6
0
 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
Exemplo n.º 7
0
 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
Exemplo n.º 8
0
 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)
Exemplo n.º 9
0
    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
Exemplo n.º 10
0
    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
Exemplo n.º 11
0
 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)
Exemplo n.º 12
0
 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)
Exemplo n.º 13
0
    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)
Exemplo n.º 14
0
    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()
Exemplo n.º 15
0
 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
Exemplo n.º 16
0
    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
Exemplo n.º 17
0
def reset():
    """Reset all message and other history"""
    Framework.reset()
    TimerManager.reset()
    History.reset()
Exemplo n.º 18
0
 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]
Exemplo n.º 19
0
def reset():
    """Reset all message and other history"""
    Framework.reset()
    TimerManager.reset()
    History.reset()
Exemplo n.º 20
0
 def _work_to_do(cls):
     if cls.queue:
         return True
     if TimerManager.pending_count() > 0:
         return True
     return False
Exemplo n.º 21
0
 def initialize_timers(self):
     self.timer_manager = TimerManager(
         self.windows[0], self.timers)  # XXX: passing window in a nasty way
Exemplo n.º 22
0
 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]
Exemplo n.º 23
0
    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
Exemplo n.º 24
0
    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
Exemplo n.º 25
0
 def remove_req_timer(cls, reqmsg):
     if reqmsg in cls.pending_timers:
         TimerManager.cancel_timer(cls.pending_timers[reqmsg])
         del cls.pending_timers[reqmsg]