本文整理汇总了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 = \
示例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
#.........这里部分代码省略.........
示例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)