當前位置: 首頁>>代碼示例>>Python>>正文


Python Querier.on_response_received方法代碼示例

本文整理匯總了Python中querier.Querier.on_response_received方法的典型用法代碼示例。如果您正苦於以下問題:Python Querier.on_response_received方法的具體用法?Python Querier.on_response_received怎麽用?Python Querier.on_response_received使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在querier.Querier的用法示例。


在下文中一共展示了Querier.on_response_received方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: __init__

# 需要導入模塊: from querier import Querier [as 別名]
# 或者: from querier.Querier import on_response_received [as 別名]

#.........這裏部分代碼省略.........
                                         + maintenance_delay)
        # Auto-save routing table
        if current_time > self._next_save_state_ts:
            self.save_state()
            self._next_save_state_ts = current_time + SAVE_STATE_DELAY

        # Schedule next call
        delay = (min(self._next_maintenance_ts, self._next_save_state_ts)
                 - current_time)
        self._reactor.call_later(delay, self._main_loop)

    def _maintenance_lookup(self, target):
        self._lookup_m.maintenance_lookup(target)

    def _on_datagram_received(self, data, addr):
        try:
            msg = message.IncomingMsg(data, addr)
        except(message.MsgError):
            return # ignore message
        if msg.sender_id == self._my_id:
            logger.debug('Got a msg from myself:\n%r', msg)
            return
        
        if msg.type == message.QUERY:
            response_msg = self._get_response(msg)
            if response_msg:
                bencoded_response = response_msg.encode(msg.tid)
                self._reactor.sendto(bencoded_response, addr)

            maintenance_queries_to_send = self._routing_m.on_query_received(
                msg.sender_node)
            
        elif msg.type in (message.RESPONSE, message.ERROR):
            related_query = self._querier.on_response_received(msg, addr)
            if not related_query:
                # Query timed out or unrequested response
                return
            # lookup related tasks
            if related_query.lookup_obj:
                if msg.type == message.RESPONSE:
                    (lookup_queries_to_send,
                     peers,
                     num_parallel_queries,
                     lookup_done
                     ) = related_query.lookup_obj.on_response_received(
                        msg, msg.sender_node)
                else: #ERROR
                    peers = None # an error msg doesn't have peers
                    (lookup_queries_to_send,
                     num_parallel_queries,
                     lookup_done
                     ) = related_query.lookup_obj.on_error_received(
                        msg, msg.sender_node)
                self._send_queries(lookup_queries_to_send)
                
                if related_query.lookup_obj.callback_f:
                    
                    lookup_id = related_query.lookup_obj.lookup_id
                    if peers:
                        related_query.lookup_obj.callback_f(lookup_id, peers)
                    if lookup_done:
                        self._announce(related_query.lookup_obj)
                        related_query.lookup_obj.callback_f(lookup_id, None)
            # maintenance related tasks
            if msg.type == message.RESPONSE:
                maintenance_queries_to_send = \
開發者ID:futtre,項目名稱:pymdht,代碼行數:70,代碼來源:controller.py

示例2: setup

# 需要導入模塊: from querier import Querier [as 別名]
# 或者: from querier.Querier import on_response_received [as 別名]
class TestQuerier:

    def setup(self):
        global time
        time = querier.time = MockTime()
        self.querier = Querier(tc.CLIENT_ID)

    def test_generate_tids(self):
        #TODO: move to message
        num_tids = 1000
        if RUN_CPU_INTENSIVE_TESTS:
            num_tids =  pow(2, 16) + 2 #CPU intensive
        for i in xrange(num_tids):
            eq_(self.querier._next_tid(),
                chr(i%256)+chr((i/256)%256))

    def test_ping_with_reponse(self):
        # Client creates a query
        ping_msg = message.OutgoingPingQuery(tc.CLIENT_ID)
        q = Query(ping_msg, tc.SERVER_NODE)
        timeout_task = minitwisted.Task(TIMEOUT_DELAY, None)
        # Client registers query
        bencoded_msg = self.querier.register_query(q, timeout_task)
        # Client sends bencoded_msg
        time.sleep(1)
        # Server gets bencoded_msg and creates response
        ping_r_msg_out = message.OutgoingPingResponse(tc.SERVER_ID)
        bencoded_r = ping_r_msg_out.encode(q.tid)
        # The client receives the bencoded message
        ping_r_in = message.IncomingMsg(bencoded_r,
                                        tc.SERVER_ADDR)
        stored_q = self.querier.on_response_received(ping_r_in,
                                                     tc.SERVER_ADDR)
        assert stored_q is q

    def test_ping_with_timeout(self):
        # Client creates a query
        ping_msg = message.OutgoingPingQuery(tc.CLIENT_ID)
        q = Query(ping_msg, tc.SERVER_NODE)
        timeout_task = minitwisted.Task(TIMEOUT_DELAY, None)
        # Client registers query
        bencoded_msg = self.querier.register_query(q, timeout_task)
        # Client sends bencoded_msg
        time.sleep(3)
        # The server never responds and the timeout is triggered
        stored_q = self.querier.on_timeout(tc.SERVER_ADDR)
        assert stored_q is q

    def test_unsolicited_response(self):
        # Server creates unsolicited response
        # It might well be that the server responds using another port,
        # and therefore, the addr is not matched
        # TODO: consider accepting responses from a different port
        ping_r_msg_out = message.OutgoingPingResponse(tc.SERVER_ID)
        bencoded_r = ping_r_msg_out.encode('zz')
        # The client receives the bencoded message
        ping_r_in = message.IncomingMsg(bencoded_r,
                                        tc.SERVER_ADDR)
        stored_q = self.querier.on_response_received(ping_r_in,
                                                     tc.SERVER_ADDR)
        assert stored_q is None

    def test_response_with_different_tid(self):
        # Client creates a query
        ping_msg = message.OutgoingPingQuery(tc.CLIENT_ID)
        q = Query(ping_msg, tc.SERVER_NODE)
        timeout_task = minitwisted.Task(TIMEOUT_DELAY, None)
        # Client registers query
        bencoded_msg = self.querier.register_query(q, timeout_task)
        # Client sends bencoded_msg
        time.sleep(1)
        # Server gets bencoded_msg and creates response
        ping_r_msg_out = message.OutgoingPingResponse(tc.SERVER_ID)
        bencoded_r = ping_r_msg_out.encode('zz')
        # The client receives the bencoded message
        ping_r_in = message.IncomingMsg(bencoded_r,
                                        tc.SERVER_ADDR)
        stored_q = self.querier.on_response_received(ping_r_in,
                                                     tc.SERVER_ADDR)
        assert stored_q is None

    def test_error(self):
        # Client creates a query
        ping_msg = message.OutgoingPingQuery(tc.CLIENT_ID)
        q = Query(ping_msg, tc.SERVER_NODE)
        timeout_task = minitwisted.Task(TIMEOUT_DELAY, None)
        # Client registers query
        bencoded_msg = self.querier.register_query(q, timeout_task)
        # Client sends bencoded_msg
        time.sleep(1)
        # Server gets bencoded_msg and creates response
        ping_r_msg_out = message.OutgoingErrorMsg(message.GENERIC_E)
        bencoded_r = ping_r_msg_out.encode(tc.TID)
        # The client receives the bencoded message
        ping_r_in = message.IncomingMsg(bencoded_r,
                                        tc.SERVER_ADDR)
        stored_q = self.querier.on_error_received(ping_r_in,
                                                  tc.SERVER_ADDR)
        assert stored_q is None

#.........這裏部分代碼省略.........
開發者ID:csasm,項目名稱:Look-MLKademlia,代碼行數:103,代碼來源:test_querier.py

示例3: Controller

# 需要導入模塊: from querier import Querier [as 別名]
# 或者: from querier.Querier import on_response_received [as 別名]

#.........這裏部分代碼省略.........
            self._send_queries(queries_to_send)
            if maintenance_lookup_target:
                log_distance = maintenance_lookup_target.log_distance(self._my_id)
                bootstrap_nodes = self._routing_m.get_closest_rnodes(log_distance, None, True)
                lookup_obj = self._lookup_m.maintenance_lookup(maintenance_lookup_target)
                lookup_queries_to_send = lookup_obj.start(bootstrap_nodes)
                self._send_queries(lookup_queries_to_send)
            self._next_maintenance_ts = current_time + maintenance_delay
        if current_time > self._next_save_state_ts:
            self.save_state()
            self._next_save_state_ts = current_time + SAVE_STATE_DELAY
        delay = min(self._next_maintenance_ts, self._next_save_state_ts) - current_time
        self._reactor.call_later(delay, self._main_loop)

    def _maintenance_lookup(self, target):
        self._lookup_m.maintenance_lookup(target)

    def _on_datagram_received(self, data, addr):
        try:
            msg = message.IncomingMsg(data, addr)
        except message.MsgError:
            return

        if msg.type == message.QUERY:
            if msg.sender_id == self._my_id:
                logger.debug('Got a msg from myself:\n%r', msg)
                return
            response_msg = self._get_response(msg)
            if response_msg:
                bencoded_response = response_msg.encode(msg.tid)
                self._reactor.sendto(bencoded_response, addr)
            maintenance_queries_to_send = self._routing_m.on_query_received(msg.sender_node)
        elif msg.type == message.RESPONSE:
            related_query = self._querier.on_response_received(msg, addr)
            if not related_query:
                return
            if related_query.lookup_obj:
                if msg.type == message.RESPONSE:
                    lookup_queries_to_send, peers, num_parallel_queries, lookup_done = related_query.lookup_obj.on_response_received(msg, msg.sender_node)
                self._send_queries(lookup_queries_to_send)
                if related_query.lookup_obj.callback_f:
                    lookup_id = related_query.lookup_obj.lookup_id
                    if peers:
                        related_query.lookup_obj.callback_f(lookup_id, peers)
                    if lookup_done:
                        self._announce(related_query.lookup_obj)
                        related_query.lookup_obj.callback_f(lookup_id, None)
            maintenance_queries_to_send = self._routing_m.on_response_received(msg.sender_node, related_query.rtt, msg.all_nodes)
        elif msg.type == message.ERROR:
            related_query = self._querier.on_error_received(msg, addr)
            if not related_query:
                return
            if related_query.lookup_obj:
                peers = None
                lookup_queries_to_send, num_parallel_queries, lookup_done = related_query.lookup_obj.on_error_received(msg, addr)
                self._send_queries(lookup_queries_to_send)
            if related_query.lookup_obj.callback_f:
                lookup_id = related_query.lookup_obj.lookup_id
                if lookup_done:
                    self._announce(related_query.lookup_obj)
                    related_query.lookup_obj.callback_f(lookup_id, None)
            maintenance_queries_to_send = self._routing_m.on_error_received(addr)
        else:
            return
        self._send_queries(maintenance_queries_to_send)
開發者ID:salekseev,項目名稱:freestream,代碼行數:69,代碼來源:controller.py


注:本文中的querier.Querier.on_response_received方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。