-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhistory.py
104 lines (82 loc) · 3.05 KB
/
history.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#!/usr/bin/python
import copy
import pprint
class AgentHistory:
"""
History available to a single peer
history.downloads: [[Download objects for round]] (one sublist for each round)
All the downloads _to_ this agent.
history.uploads: [[Upload objects for round]] (one sublist for each round)
All the downloads _from_ this agent.
"""
def __init__(self, peer_id, downloads, uploads):
"""
Pull out just the info for peer_id.
"""
self.uploads = uploads
self.downloads = downloads
self.peer_id = peer_id
def last_round(self):
return len(self.downloads)-1
def current_round(self):
""" 0 is the first """
return len(self.downloads)
def __repr__(self):
return "AgentHistory(downloads=%s, uploads=%s)" % (
pprint.pformat(self.downloads),
pprint.pformat(self.uploads))
class History:
"""History of the whole sim"""
def __init__(self, peer_ids, upload_rates):
"""
uploads:
dict : peer_id -> [[uploads] -- one list per round]
downloads:
dict : peer_id -> [[downloads] -- one list per round]
Keep track of the uploads _from_ and downloads _to_ the
specified peer id.
"""
self.upload_rates = upload_rates # peer_id -> up_bw
self.peer_ids = peer_ids[:]
self.round_done = dict() # peer_id -> round finished
self.downloads = dict((pid, []) for pid in peer_ids)
self.uploads = dict((pid, []) for pid in peer_ids)
def update(self, dls, ups):
"""
dls: dict : peer_id -> [downloads] -- downloads for this round
ups: dict : peer_id -> [uploads] -- uploads for this round
append these downloads to to the history
"""
for pid in self.peer_ids:
self.downloads[pid].append(dls[pid])
self.uploads[pid].append(ups[pid])
def peer_is_done(self, round, peer_id):
# Only save the _first_ round where we hear this
if peer_id not in self.round_done:
self.round_done[peer_id] = round
def peer_history(self, peer_id):
return AgentHistory(peer_id, self.downloads[peer_id], self.uploads[peer_id])
def last_round(self):
"""index of the last completed round"""
p = self.peer_ids[0]
return len(self.downloads[p])-1
def pretty_for_round(self, r):
s = "\nRound %s:\n" % r
for peer_id in self.peer_ids:
ds = self.downloads[peer_id][r]
stringify = lambda d: "%s downloaded %d blocks of piece %d from %s\n" % (
peer_id, d.blocks, d.piece, d.from_id)
s += "".join(map(stringify, ds))
return s
def pretty(self):
s = "History\n"
for r in range(self.last_round()+1):
s += self.pretty_for_round(r)
return s
def __repr__(self):
return """History(
uploads=%s
downloads=%s
)""" % (
pprint.pformat(self.uploads),
pprint.pformat(self.downloads))