forked from thomasballinger/bittorrent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
peerstrategy.py
33 lines (28 loc) · 1.1 KB
/
peerstrategy.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import logging
import msg
def keep_asking_strategy(peer):
if not peer.interested:
logging.info('%s sending interested', peer)
peer.send_msg(msg.Interested())
peer.interested = True
if not peer.choked:
while len(peer.outstanding_requests) < 10:
needed_piece = peer.torrent.get_needed_request(peer)
if needed_piece:
logging.info('torrent needed_piece: %s', repr(needed_piece))
peer.send_msg(needed_piece)
else:
break
if peer.torrent.piece_checked.count(1) == len(peer.torrent.piece_hashes):
for p in peer.torrent.peers:
p.strategy = cancel_all_strategy
def cancel_all_strategy(peer):
logging.info('file download complete')
peer.strategy = do_nothing_strategy
def do_nothing_strategy(peer):
pass
def wait_for_handshake_strategy(peer):
"""wait_for_handshake strategy is initially for peers not yet connected to a torrent"""
if peer.handshake:
logging.info('%s switching to do_nothing_strategy', peer)
peer.strategy = do_nothing_strategy