forked from arnaumartifont/GoTorrent-Python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBitTorrent.py
More file actions
executable file
·336 lines (239 loc) · 7.66 KB
/
BitTorrent.py
File metadata and controls
executable file
·336 lines (239 loc) · 7.66 KB
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
from pyactor.context import set_context, create_host, sleep, shutdown, interval, later
#import plotly.plotly as py
#import plotly.graph_objs as go
#import plotly
import random
import copy
#tell = assincronous
#ask = sincronous
#ref = sending reference between machines
#pull = ask for info
#push = nofify with info
class Peer(object):
_tell = ["pushGossip","pullGossip","push","init_start","stop_interval","announce"]
_ask = ["initArray","get_file","pull","chunk_rounds"]
def __init__(self):
self.torrentFile = []
self.torrentPeers = {}
self.chunks = []
def initArray(self,Object):
self.torrentFile = Object
def announce(self,tracker,torrent):
tracker.announce(torrent,self.proxy,11)
def get_file(self):
return self.torrentFile
def chunk_rounds(self):
return self.chunks
def pull(self,chunk_id):
return self.torrentFile[chunk_id]
def push(self,chunk_id, chunk_data):
if self.torrentFile[chunk_id] == None:
if chunk_data != None:
self.torrentFile[chunk_id] = chunk_data
print "PUSH: "+self.id +" has received "+str(chunk_data)+ " --->"+ str(self.torrentFile)
def pushGossip(self,torrent): #deliver
try:
self.torrentPeers = t.getPeers(torrent)
except Exception:
pass
self.chunks.append(sum(x is not None for x in self.torrentFile))
if self.torrentPeers != None:
if self.proxy in self.torrentPeers:
randomPeers = random.sample(self.torrentPeers, 2)
if self.proxy in randomPeers:
newPeers = copy.copy(self.torrentPeers)
newPeers.pop(self.proxy)
randomPeers = random.sample(newPeers, 2)
for peer in randomPeers:
sleep(0.2)
# PUSH - GOSSIP method:(self send chunk to random peer of randoms)
randomPosition = random.randint(0, len(self.torrentFile)-1)
peer.push(randomPosition, self.torrentFile[randomPosition])
def pullGossip(self,torrent): #ask
self.peersNoneChunks = []
self.position = 0
for chunk in self.torrentFile:
if chunk == None:
self.peersNoneChunks.append(self.position)
self.position = self.position + 1
if self.position != 0:
try:
self.torrentPeers = t.getPeers(torrent)
except Exception:
pass
if self.torrentPeers != None:
self.peersNoneChunks = []
self.position = 0
if self.proxy in self.torrentPeers:
randomPeers = random.sample(self.torrentPeers, 2)
if self.proxy in randomPeers:
newPeers = copy.copy(self.torrentPeers)
newPeers.pop(self.proxy)
randomPeers = random.sample(newPeers, 2)
for randomPeer in randomPeers:
self.position = 0
for chunk in self.torrentFile:
if chunk == None:
self.peersNoneChunks.append(self.position)
self.position = self.position + 1
if len(self.peersNoneChunks) > 0:
randomPosition = random.sample(self.peersNoneChunks, 1)
# PULL - GOSSIP method: (self ask for random not None chunk to random peer)
chunk = randomPeer.pull(randomPosition[0],future = True) #use a FUTURE, for not get None chunk
sleep(0.3)
if chunk.done():
try:
if chunk.result() != None:
file = self.torrentFile
file[randomPosition[0]] = chunk.result()
print "PULL: " + self.id + " has received "+str(chunk.result())+" --->"+ str(self.torrentFile)
except Exception, e:
print e
#INTERVALS:
def init_start(self,torrent):
self.peerAnounce = interval(h, 6, self.proxy, "announce", t,torrent)
self.peerPush = interval(h, 1, self.proxy, "pushGossip",torrent)
if self.proxy != ps:
self.peerPull = interval(h, 1, self.proxy, "pullGossip",torrent)
later(60, self.proxy, "stop_interval")
def stop_interval(self):
print self.id+": stopping interval"
if self.proxy != ps:
self.peerPull.set()
self.peerPush.set()
self.peerAnounce.set()
class Tracker(object):
_tell = ["announce","trackerTimeCheck","init_start","stop_interval"]
_ask = ["getPeers"]
def __init__(self):
self.torrents = {}
def getPeers(self,torrent_id):
return self.torrents.get(torrent_id)
def announce(self,torrent_id,peer,time):
self.torrents.setdefault(torrent_id, {}).update({peer:time}) # {torrent_id: [peer:time , peer2:time]...}
def trackerTimeCheck(self,torrent):
deadPeers = {}
if self.torrents.get(torrent) != None:
for peer in self.torrents.get(torrent):
self.announce(torrent, peer, self.torrents.get(torrent)[peer] - 1)
if self.torrents.get(torrent)[peer] < 1:
peer.stop_interval()
deadPeers.setdefault(torrent, []).append(peer)
for torrent in deadPeers.keys():
for peer in deadPeers.get(torrent):
self.torrents.get(torrent).pop(peer,None)
print "KICKED:" + peer.get_id()
peer.stop_interval()
#INTERVALS:
def init_start(self,torrent):
self.timeCheck = interval(h,1, self.proxy, "trackerTimeCheck",torrent)
later(60, self.proxy, "stop_interval")
def stop_interval(self):
print "Tracker: stopping interval"
self.timeCheck.set()
if __name__ == '__main__':
set_context()
h = create_host()
peersList = []
t = h.spawn("tracker1",Tracker)
t.init_start("movie")
ps = h.spawn("Seed", Peer)
ps.initArray(["G", "O", "T", "O", "R", "R", "E", "N", "T"])
ps.init_start("movie")
for x in range(1,6):
p = h.spawn("peer" + str(x), Peer)
p.initArray([None, None, None, None, None, None, None, None, None])
p.init_start("movie")
peersList.append(p)
sleep(0.1)
sleep(65)
print "----------------------------"
for peer in t.getPeers("movie"):
if peer.get_id() != "Seed":
print peer.get_id() + str(peer.get_file())
'''
ps = h.spawn("peerSeed", Peer)
p1 = h.spawn("peer1", Peer)
p2 = h.spawn("peer2", Peer)
p3 = h.spawn("peer3", Peer)
p4 = h.spawn("peer4", Peer)
p5 = h.spawn("peer5", Peer)
ps.initArray(["G", "O", "T", "O", "R", "R", "E", "N", "T"])
p1.initArray([None, None, None, None, None, None, None, None, None])
p2.initArray([None, None, None, None, None, None, None, None, None])
p3.initArray([None, None, None, None, None, None, None, None, None])
p4.initArray([None, None, None, None, None, None, None, None, None])
p5.initArray([None, None, None, None, None, None, None, None, None])
ps.init_start("movie")
sleep(0.1)
p1.init_start("movie")
sleep(0.1)
p2.init_start("movie")
sleep(0.1)
p3.init_start("movie")
sleep(0.1)
p4.init_start("movie")
sleep(0.1)
p5.init_start("movie")
sleep(0.1)
t.init_start("movie")
print "-----------"
print "p1: " + str(p1.get_file())
print "p1: " + str(p1.chunk_rounds())
print "p2: " + str(p2.get_file())
print "p2: " + str(p2.chunk_rounds())
print "p3: " + str(p3.get_file())
print "p3: " + str(p3.chunk_rounds())
print "p4: " + str(p4.get_file())
print "p4: " + str(p4.chunk_rounds())
print "p5: " + str(p5.get_file())
print "p5: " + str(p5.chunk_rounds())
'''
'''
plotly.tools.set_credentials_file(username='ArnauMarti', api_key='UZWivMWZggFo2kO5WezX')
x1 = list(range(len(p1.chunk_rounds())))
y1 = p1.chunk_rounds()
x2 = list(range(len(p2.chunk_rounds())))
y2 = p2.chunk_rounds()
x3 = list(range(len(p3.chunk_rounds())))
y3 = p3.chunk_rounds()
x4 = list(range(len(p4.chunk_rounds())))
y4 = p4.chunk_rounds()
x5 = list(range(len(p5.chunk_rounds())))
y5 = p5.chunk_rounds()
# Create a trace
trace0 = go.Scatter(
x=x1,
y=y1,
name = "peer1"
)
trace1 = go.Scatter(
x=x2,
y=y2,
name = "peer2"
)
trace2 = go.Scatter(
x=x3,
y=y3,
name = "peer3"
)
trace3 = go.Scatter(
x=x4,
y=y4,
name = "peer4"
)
trace4 = go.Scatter(
x=x5,
y=y5,
name = "peer5"
)
data = [trace0, trace1, trace2,trace3,trace4]
# Edit the layout
layout = dict(title='pull/push-2',
xaxis=dict(title='Time'),
yaxis=dict(title='Chunk'),
)
fig = dict(data=data, layout=layout)
py.iplot(fig, filename='PullPush2')
'''
shutdown()