本文整理汇总了Python中querier.Querier.get_related_query方法的典型用法代码示例。如果您正苦于以下问题:Python Querier.get_related_query方法的具体用法?Python Querier.get_related_query怎么用?Python Querier.get_related_query使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类querier.Querier
的用法示例。
在下文中一共展示了Querier.get_related_query方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: setup
# 需要导入模块: from querier import Querier [as 别名]
# 或者: from querier.Querier import get_related_query [as 别名]
class TestQuerier:
def setup(self):
time.mock_mode()
self.querier = Querier()#tc.CLIENT_ID)
def test_generate_tids(self):
#TODO: move to message
if RUN_CPU_INTENSIVE_TESTS:
num_tids = pow(2, 16) + 2 #CPU intensive
else:
num_tids = 1000
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 = clients_msg_f.outgoing_ping_query(tc.SERVER_NODE)
q = ping_msg
# Client registers query
timeout_ts, bencoded_msgs = self.querier.register_queries([q])
# Client sends bencoded_msg
# Server gets bencoded_msg and creates response
ping_r_msg_out = servers_msg_f.outgoing_ping_response(tc.CLIENT_NODE)
bencoded_r = ping_r_msg_out.stamp(ping_msg.tid)
time.sleep(1)
eq_(self.querier.get_timeout_queries()[1], [])
# The client receives the bencoded message (after 1 second)
ping_r_in = clients_msg_f.incoming_msg(
Datagram(bencoded_r, tc.SERVER_ADDR))
related_query = self.querier.get_related_query(ping_r_in)
assert related_query is ping_msg
def test_ping_with_timeout(self):
# Client creates a query
ping_msg = clients_msg_f.outgoing_ping_query(tc.SERVER_NODE)
q = ping_msg
# Client registers query
bencoded_msg = self.querier.register_queries([q])
# Client sends bencoded_msg
time.sleep(3)
# The server never responds and the timeout is triggered
timeout_queries = self.querier.get_timeout_queries()
eq_(len(timeout_queries[1]), 1)
assert timeout_queries[1][0] is ping_msg
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 = servers_msg_f.outgoing_ping_response(tc.CLIENT_NODE)
bencoded_r = ping_r_msg_out.stamp('zz')
# The client receives the bencoded message
ping_r_in = clients_msg_f.incoming_msg(
Datagram(bencoded_r, tc.SERVER_ADDR))
related_query = self.querier.get_related_query(ping_r_in)
assert related_query is None
def test_response_with_different_tid(self):
# Client creates a query
ping_msg = clients_msg_f.outgoing_ping_query(tc.SERVER_NODE)
q = ping_msg
# Client registers query
bencoded_msg = self.querier.register_queries([q])
# Client sends bencoded_msg
time.sleep(1)
# Server gets bencoded_msg and creates response
ping_r_msg_out = servers_msg_f.outgoing_ping_response(tc.CLIENT_NODE)
bencoded_r = ping_r_msg_out.stamp('zz')
# The client receives the bencoded message
ping_r_in = clients_msg_f.incoming_msg(
Datagram(bencoded_r, tc.SERVER_ADDR))
related_query = self.querier.get_related_query(ping_r_in)
assert related_query is None
def test_error_received(self):
# Client creates a query
msg = clients_msg_f.outgoing_ping_query(tc.SERVER_NODE)
q = msg
# Client registers query
bencoded_msg = self.querier.register_queries([q])
# Client sends bencoded_msg
time.sleep(1)
# Server gets bencoded_msg and creates response
ping_r_msg_out = servers_msg_f.outgoing_error(tc.CLIENT_NODE,
message.GENERIC_E)
bencoded_r = ping_r_msg_out.stamp(msg.tid)
# The client receives the bencoded message
ping_r_in = clients_msg_f.incoming_msg(
Datagram(bencoded_r, tc.SERVER_ADDR))
related_query = self.querier.get_related_query(ping_r_in)
assert related_query is msg
def test_many_queries(self):
# Client creates a query
msgs = [clients_msg_f.outgoing_ping_query(
tc.SERVER_NODE) for i in xrange(10)]
queries = msgs
#.........这里部分代码省略.........
示例2: __init__
# 需要导入模块: from querier import Querier [as 别名]
# 或者: from querier.Querier import get_related_query [as 别名]
#.........这里部分代码省略.........
lookup's handler will be called (see get\_peers above).
This method is designed to be used as minitwisted's networking handler.
"""
exp_queries_to_send = []
data = datagram.data
addr = datagram.addr
datagrams_to_send = []
try:
msg = self.msg_f.incoming_msg(datagram)
except(message.MsgError):
# ignore message
return self._next_main_loop_call_ts, datagrams_to_send
if msg.type == message.QUERY:
if msg.src_node.id == self._my_id:
logger.debug('Got a msg from myself:\n%r', msg)
return self._next_main_loop_call_ts, datagrams_to_send
#zinat: inform experimental_module
exp_queries_to_send = self._experimental_m.on_query_received(msg)
response_msg = self._responder.get_response(msg)
if response_msg:
bencoded_response = response_msg.stamp(msg.tid)
datagrams_to_send.append(
message.Datagram(bencoded_response, addr))
maintenance_queries_to_send = self._routing_m.on_query_received(
msg.src_node)
elif msg.type == message.RESPONSE:
related_query = self._querier.get_related_query(msg)
if not related_query:
# Query timed out or unrequested response
return self._next_main_loop_call_ts, datagrams_to_send
## zinat: if related_query.experimental_obj:
exp_queries_to_send = self._experimental_m.on_response_received(
msg, related_query)
#TODO: you need to get datagrams to be able to send messages (raul)
# lookup related tasks
if related_query.lookup_obj:
(lookup_queries_to_send,
peers,
num_parallel_queries,
lookup_done
) = related_query.lookup_obj.on_response_received(
msg, msg.src_node)
datagrams = self._register_queries(lookup_queries_to_send)
datagrams_to_send.extend(datagrams)
lookup_obj = related_query.lookup_obj
lookup_id = lookup_obj.lookup_id
callback_f = lookup_obj.callback_f
if peers:
self._add_cache_peers(lookup_obj.info_hash, peers)
if callable(callback_f):
callback_f(lookup_id, peers, msg.src_node)
if lookup_done:
if callable(callback_f):
callback_f(lookup_id, None, msg.src_node)
queries_to_send = self._announce(
related_query.lookup_obj)
datagrams = self._register_queries(
queries_to_send)