-
Notifications
You must be signed in to change notification settings - Fork 0
/
peer.py
42 lines (33 loc) · 1.27 KB
/
peer.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
33
34
35
36
37
38
39
40
41
42
#!/usr/bin/python
import random
from messages import Upload, Request
from util import even_split
class Peer:
def __init__(self, config, id, init_pieces, up_bandwidth):
self.conf = config
self.id = id
self.pieces = init_pieces[:]
# bandwidth measured in blocks-per-time-period
self.up_bw = round(up_bandwidth)
# This is an upper bound on the number of requests to send to
# each peer -- they can't possibly handle more than this in one round
self.max_requests = round(self.conf.max_up_bw / self.conf.blocks_per_piece + 1)
self.max_requests = int(min(self.max_requests, self.conf.num_pieces))
self.post_init()
def __repr__(self):
return "%s(id=%s pieces=%s up_bw=%d)" % (
self.__class__.__name__,
self.id, self.pieces, self.up_bw)
def update_pieces(self, new_pieces):
"""
Called by the sim when this peer gets new pieces. Using a function
so it's easy to add any extra processing...
"""
self.pieces = new_pieces
def requests(self, peers, history):
return []
def uploads(self, requests, peers, history):
return []
def post_init(self):
# Here to be overridden by child classes
pass