本文整理汇总了Python中querier.Querier.on_error_received方法的典型用法代码示例。如果您正苦于以下问题:Python Querier.on_error_received方法的具体用法?Python Querier.on_error_received怎么用?Python Querier.on_error_received使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类querier.Querier
的用法示例。
在下文中一共展示了Querier.on_error_received方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: from querier import Querier [as 别名]
# 或者: from querier.Querier import on_error_received [as 别名]
#.........这里部分代码省略.........
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:
# 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)
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
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:
# Query timed out or unrequested response
return
# lookup related tasks
if related_query.lookup_obj:
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, 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 related tasks
maintenance_queries_to_send = \
self._routing_m.on_error_received(addr)
else: # unknown type
return
self._send_queries(maintenance_queries_to_send)
def _get_response(self, msg):
if msg.query == message.PING:
return message.OutgoingPingResponse(self._my_id)
elif msg.query == message.FIND_NODE:
log_distance = msg.target.log_distance(self._my_id)
rnodes = self._routing_m.get_closest_rnodes(log_distance,
示例2: setup
# 需要导入模块: from querier import Querier [as 别名]
# 或者: from querier.Querier import on_error_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
#.........这里部分代码省略.........
示例3: Controller
# 需要导入模块: from querier import Querier [as 别名]
# 或者: from querier.Querier import on_error_received [as 别名]
#.........这里部分代码省略.........
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)
def _get_response(self, msg):
if msg.query == message.PING:
return message.OutgoingPingResponse(self._my_id)
if msg.query == message.FIND_NODE:
log_distance = msg.target.log_distance(self._my_id)
rnodes = self._routing_m.get_closest_rnodes(log_distance, NUM_NODES, False)
return message.OutgoingFindNodeResponse(self._my_id, rnodes)
if msg.query == message.GET_PEERS:
token = self._token_m.get()
log_distance = msg.info_hash.log_distance(self._my_id)
rnodes = self._routing_m.get_closest_rnodes(log_distance, NUM_NODES, False)
peers = self._tracker.get(msg.info_hash)
if peers:
logger.debug('RESPONDING with PEERS:\n%r' % peers)
return message.OutgoingGetPeersResponse(self._my_id, token, nodes=rnodes, peers=peers)
if msg.query == message.ANNOUNCE_PEER:
peer_addr = (msg.sender_addr[0], msg.bt_port)
self._tracker.put(msg.info_hash, peer_addr)
return message.OutgoingAnnouncePeerResponse(self._my_id)
logger.debug('Invalid QUERY: %r' % msg.query)
def _on_response_received(self, msg):
pass
def _on_timeout(self, addr):
related_query = self._querier.on_timeout(addr)
if not related_query:
return
if related_query.lookup_obj:
lookup_queries_to_send, num_parallel_queries, lookup_done = related_query.lookup_obj.on_timeout(related_query.dstnode)
self._send_queries(lookup_queries_to_send)
if lookup_done and related_query.lookup_obj.callback_f:
self._announce(related_query.lookup_obj)
lookup_id = related_query.lookup_obj.lookup_id
related_query.lookup_obj.callback_f(lookup_id, None)
maintenance_queries_to_send = self._routing_m.on_timeout(related_query.dstnode)
self._send_queries(maintenance_queries_to_send)
def _announce(self, lookup_obj):
queries_to_send, announce_to_myself = lookup_obj.announce()
self._send_queries(queries_to_send)
def _send_queries(self, queries_to_send, lookup_obj = None):
if queries_to_send is None:
return
for query in queries_to_send:
timeout_task = self._reactor.call_later(TIMEOUT_DELAY, self._on_timeout, query.dstnode.addr)
bencoded_query = self._querier.register_query(query, timeout_task)
self._reactor.sendto(bencoded_query, query.dstnode.addr)