本文整理汇总了Python中minitwisted.ThreadedReactor.sendto方法的典型用法代码示例。如果您正苦于以下问题:Python ThreadedReactor.sendto方法的具体用法?Python ThreadedReactor.sendto怎么用?Python ThreadedReactor.sendto使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类minitwisted.ThreadedReactor
的用法示例。
在下文中一共展示了ThreadedReactor.sendto方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _main_loop
# 需要导入模块: from minitwisted import ThreadedReactor [as 别名]
# 或者: from minitwisted.ThreadedReactor import sendto [as 别名]
class _TestSocketErrors:
def _main_loop(self):
return time.time() + tc.TASK_INTERVAL*10000, []
def _main_loop_send(self):
self.main_loop_send_called = True
logger.critical('main loop returns datagram!!!!')
return time.time() + tc.TASK_INTERVAL*10000, [DATAGRAM1]
def _callback(self, *args, **kwargs):
self.callback_fired = True
def _on_datagram_received(self, datagram):
return time.time() + 100, []
def setup(self):
self.main_loop_send_called = False
self.callback_fired = False
self.r = ThreadedReactor(self._main_loop_send, tc.CLIENT_PORT,
self._on_datagram_received)
self.r.s = _SocketErrorMock()
#self.r.listen_udp(tc.CLIENT_PORT, lambda x,y:None)
def test_sendto(self):
logger.critical('TESTING: IGNORE CRITICAL MESSAGE')
assert not self.main_loop_send_called
# self.r.start()
while not self.r.running:
time.sleep(tc.TASK_INTERVAL)
while not self.main_loop_send_called:
time.sleep(tc.TASK_INTERVAL)
assert self.r.s.error_raised
assert self.r.running # reactor doesn't crashed
def _test_recvfrom(self):
#self.r.start()
r2 = ThreadedReactor(self._main_loop, tc.CLIENT_PORT,
self._on_datagram_received,
task_interval=tc.TASK_INTERVAL)
r2.s = _SocketErrorMock()
assert not r2.running
# r2.start()
assert r2.running
logger.critical('TESTING: IGNORE CRITICAL MESSAGE')
# self.r will call recvfrom (which raises socket.error)
while not r2.s.error_raised:
time.sleep(tc.TASK_INTERVAL)
assert r2.running # the error is ignored
ok_(not self.callback_fired)
# r2.stop()
def _test_sendto_too_large_data_string(self):
logger.critical('TESTING: IGNORE CRITICAL MESSAGE')
self.r.sendto('z'*12345, tc.NO_ADDR)
def tear_down(self):
pass
示例2: test_recvfrom
# 需要导入模块: from minitwisted import ThreadedReactor [as 别名]
# 或者: from minitwisted.ThreadedReactor import sendto [as 别名]
def test_recvfrom(self):
self.r.start()
r2 = ThreadedReactor()
r2.listen_udp(tc.SERVER_ADDR[1], lambda x,y:None)
logger.critical('TESTING: IGNORE CRITICAL MESSAGE')
r2.sendto('z', tc.CLIENT_ADDR)
# self.r will call recvfrom (which raises socket.error)
time.sleep(tc.TASK_INTERVAL)
ok_(not self.callback_fired)
self.r.stop()
示例3: test_mock_threaded_reactor
# 需要导入模块: from minitwisted import ThreadedReactor [as 别名]
# 或者: from minitwisted.ThreadedReactor import sendto [as 别名]
def test_mock_threaded_reactor(self):
'''
Just making sure that the interface is the same
'''
r = ThreadedReactor(task_interval=.1)
rm = ThreadedReactorMock(task_interval=.1)
r.listen_udp(tc.CLIENT_ADDR[1], lambda x,y:None)
rm.listen_udp(tc.CLIENT_ADDR[1], lambda x,y:None)
r.start()
rm.start()
r.sendto(DATA, tc.CLIENT_ADDR)
rm.sendto(DATA, tc.CLIENT_ADDR)
r.call_later(.1, self._callback)
rm.call_later(.1, self._callback)
# time.sleep(.002)
r.stop()
rm.stop()
示例4: __init__
# 需要导入模块: from minitwisted import ThreadedReactor [as 别名]
# 或者: from minitwisted.ThreadedReactor import sendto [as 别名]
#.........这里部分代码省略.........
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)
# 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)
示例5: setup
# 需要导入模块: from minitwisted import ThreadedReactor [as 别名]
# 或者: from minitwisted.ThreadedReactor import sendto [as 别名]
class TestMinitwisted:
def setup(self):
global time
#TODO: mock time and socket
#time = minitwisted.time = MockTime()
#minitwisted.socket = MockSocket()
self.lock = threading.Lock()
self.datagrams_received = []
self.callback_order = []
self.client_r = ThreadedReactor(task_interval=tc.TASK_INTERVAL)
self.server_r = ThreadedReactor(task_interval=tc.TASK_INTERVAL)
self.client_s = self.client_r.listen_udp(tc.CLIENT_ADDR[1],
self.on_datagram_received)
self.server_s = self.server_r.listen_udp(tc.SERVER_ADDR[1],
self.on_datagram_received)
self.client_r.start()
self.server_r.start()
def test_listen_upd(self):
r = ThreadedReactor()
r.start()
logger.warning(''.join(
('TESTING LOGS ** IGNORE EXPECTED WARNING ** ',
'(udp_listen has not been called)')))
self.client_r.sendto(DATA, tc.SERVER_ADDR)
while 1: #waiting for data
with self.lock:
if self.datagrams_received:
break
time.sleep(tc.TASK_INTERVAL)
with self.lock:
first_datagram = self.datagrams_received.pop(0)
logger.debug('first_datagram: %s, %s' % (
first_datagram,
(DATA, tc.CLIENT_ADDR)))
assert first_datagram, (DATA, tc.CLIENT_ADDR)
r.stop()
def test_network_callback(self):
self.client_r.sendto(DATA, tc.SERVER_ADDR)
time.sleep(tc.TASK_INTERVAL)
with self.lock:
first_datagram = self.datagrams_received.pop(0)
logger.debug('first_datagram: %s, %s' % (
first_datagram,
(DATA, tc.CLIENT_ADDR)))
assert first_datagram, (DATA, tc.CLIENT_ADDR)
def test_block_flood(self):
from floodbarrier import MAX_PACKETS_PER_PERIOD as FLOOD_LIMIT
for _ in xrange(FLOOD_LIMIT):
self.client_r.sendto(DATA, tc.SERVER_ADDR)
for _ in xrange(10):
self.client_r.sendto(DATA, tc.SERVER_ADDR)
logger.warning(
"TESTING LOGS ** IGNORE EXPECTED WARNING **")
time.sleep(tc.TASK_INTERVAL)
return
######################################
with self.lock:
logger.debug('datagram processed: %d/%d' % (
len(self.datagrams_received),
FLOOD_LIMIT))
print len(self.datagrams_received)
assert len(self.datagrams_received) <= FLOOD_LIMIT
def test_call_later(self):
self.client_r.call_later(.13, self.callback_f, 1)
self.client_r.call_later(.11, self.callback_f, 2)
self.client_r.call_later(.01, self.callback_f, 3)
task4 = self.client_r.call_later(.01, self.callback_f, 4)
task4.cancel()
time.sleep(.03)
with self.lock:
logger.debug('callback_order: %s' % self.callback_order)
eq_(self.callback_order, [3])
self.callback_order = []
self.client_r.call_now(self.callback_f, 5)
time.sleep(.03)
with self.lock:
logger.debug('callback_order: %s' % self.callback_order)
eq_(self.callback_order, [5])
self.callback_order = []
task6 = self.client_r.call_later(.03, self.callback_f, 6)
task6.cancel()
time.sleep(.1)
with self.lock:
logger.debug('callback_order: %s' % self.callback_order)
eq_(self.callback_order, [2, 1])
def test_network_and_delayed(self):
self.client_r.call_later(.2, self.callback_f, 0)
self.client_r.call_now(self.callback_f, 1)
task2 = self.client_r.call_later(.2, self.callback_f, 2)
with self.lock:
eq_(self.callback_order, [])
time.sleep(.1)
#.........这里部分代码省略.........
示例6: Controller
# 需要导入模块: from minitwisted import ThreadedReactor [as 别名]
# 或者: from minitwisted.ThreadedReactor import sendto [as 别名]
#.........这里部分代码省略.........
current_time = time.time()
if current_time > self._next_maintenance_ts:
maintenance_delay, queries_to_send, maintenance_lookup_target = self._routing_m.do_maintenance()
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: