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
337
338
339
340
341
342
343
344
345
|
/***************************************************************************
* Copyright (C) 2005 by Joris Guisson *
* [email protected] *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
***************************************************************************/
#if 0
#include <util/log.h>
#include <util/timer.h>
#include <util/functions.h>
#include <torrent/globals.h>
#include <interfaces/functions.h>
#include "newchokealgorithm.h"
#include "peermanager.h"
#include "peer.h"
#include "packetwriter.h"
#include "peeruploader.h"
using namespace kt;
namespace bt
{
NewChokeAlgorithm::NewChokeAlgorithm(): ChokeAlgorithm()
{
round_state = 1;
}
NewChokeAlgorithm::~NewChokeAlgorithm()
{}
int RevDownloadRateCmp(Peer* a,Peer* b)
{
if (b->getDownloadRate() > a->getDownloadRate())
return 1;
else if (a->getDownloadRate() > b->getDownloadRate())
return -1;
else
return 0;
}
void NewChokeAlgorithm::doChokingLeechingState(PeerManager & pman,ChunkManager & cman,const kt::TorrentStats & stats)
{
Uint32 num_peers = pman.getNumConnectedPeers();
if (num_peers == 0)
return;
Uint32 now = GetCurrentTime();
Peer* poup = pman.findPeer(opt_unchoked_peer_id);
Peer* unchokers[] = {0,0,0,0};
// first find the planned optimistic unchoked peer if we are in the correct round
if (round_state == 1 || poup == 0)
{
opt_unchoked_peer_id = findPlannedOptimisticUnchokedPeer(pman);
poup = pman.findPeer(opt_unchoked_peer_id);
}
PeerPtrList peers,other;
// now get all the peers who are interested and have sent us a piece in the
// last 30 seconds
for (Uint32 i = 0;i < num_peers;i++)
{
Peer* p = pman.getPeer(i);
if (!p)
continue;
if (!p->isSeeder())
{
if (p->isInterested() && now - p->getTimeSinceLastPiece() <= 30000)
peers.append(p);
else
other.append(p);
}
else
{
p->choke();
}
}
// sort them using a reverse download rate compare
// so that the fastest downloaders are in front
peers.setCompareFunc(RevDownloadRateCmp);
peers.sort();
other.setCompareFunc(RevDownloadRateCmp);
other.sort();
// get the first tree and punt them in the unchokers
for (Uint32 i = 0;i < 3;i++)
{
if (i < peers.count())
{
unchokers[i] = peers.at(i);
}
}
// see if poup if part of the first 3
// and if necessary replace it
bool poup_in_unchokers = false;
Uint32 attempts = 0;
do
{
poup_in_unchokers = false;
for (Uint32 i = 0;i < 3;i++)
{
if (unchokers[i] != poup)
continue;
opt_unchoked_peer_id = findPlannedOptimisticUnchokedPeer(pman);
poup = pman.findPeer(opt_unchoked_peer_id);
poup_in_unchokers = true;
break;
}
// we don't want to keep trying this forever, so limit it to 5 atttempts
attempts++;
}while (poup_in_unchokers && attempts < 5);
unchokers[3] = poup;
Uint32 other_idx = 0;
Uint32 peers_idx = 3;
// unchoke the 4 unchokers
for (Uint32 i = 0;i < 4;i++)
{
if (!unchokers[i])
{
// pick some other peer to unchoke
unchokers[i] = peers.at(peers_idx++);
if (unchokers[i] == poup) // it must not be equal to the poup
unchokers[i] = peers.at(peers_idx++);
// nobody in the peers list, try the others list
if (!unchokers[i])
unchokers[i] = other.at(other_idx++);
}
if (unchokers[i])
unchokers[i]->getPacketWriter().sendUnchoke();
}
// choke the rest
for (Uint32 i = 0;i < num_peers;i++)
{
Peer* p = pman.getPeer(i);
if (p == unchokers[0] || p == unchokers[1] || p == unchokers[2] || p == unchokers[3])
continue;
if (p)
p->choke();
}
round_state++;
if (round_state > 3)
round_state = 1;
}
Uint32 NewChokeAlgorithm::findPlannedOptimisticUnchokedPeer(PeerManager& pman)
{
Uint32 num_peers = pman.getNumConnectedPeers();
if (num_peers == 0)
return UNDEFINED_ID;
// find a random peer that is choked and interested
Uint32 start = rand() % num_peers;
Uint32 i = (start + 1) % num_peers;
while (i != start)
{
Peer* p = pman.getPeer(i);
if (p && p->isChoked() && p->isInterested() && !p->isSeeder())
return p->getID();
i = (i + 1) % num_peers;
}
// we do not expect to have 4 billion peers
return 0xFFFFFFFF;
}
//////////////////////////////////////////////
int NChokeCmp(Peer* a,Peer* b)
{
Uint32 now = GetCurrentTime();
// if they have pending upload requests or they were unchoked in the last 20 seconds,
// they are category 1
bool a_first_class = a->getPeerUploader()->getNumRequests() > 0 ||
(now - a->getUnchokeTime() <= 20000);
bool b_first_class = b->getPeerUploader()->getNumRequests() > 0 ||
(now - b->getUnchokeTime() <= 20000);
if (a_first_class && !b_first_class)
{
// category 1 come first
return -1;
}
else if (!a_first_class && b_first_class)
{
// category 1 come first
return 1;
}
else
{
// use upload rate to differentiate peers of the same class
if (a->getUploadRate() > b->getUploadRate())
return -1;
else if (b->getUploadRate() > a->getUploadRate())
return 1;
else
return 0;
}
}
void NewChokeAlgorithm::doChokingSeedingState(PeerManager & pman,ChunkManager & cman,const kt::TorrentStats & stats)
{
Uint32 num_peers = pman.getNumConnectedPeers();
if (num_peers == 0)
return;
// first get all unchoked and interested peers
PeerPtrList peers,others;
for (Uint32 i = 0;i < num_peers;i++)
{
Peer* p = pman.getPeer(i);
if (!p)
continue;
if (!p->isSeeder())
{
if (!p->isChoked() && p->isInterested())
peers.append(p);
else
others.append(p);
}
else
{
p->choke();
}
}
// sort them
peers.setCompareFunc(NChokeCmp);
peers.sort();
others.setCompareFunc(NChokeCmp);
others.sort();
// first round so take the 4 first peers
if (round_state == 1)
{
Uint32 num_unchoked = 0;
for (Uint32 i = 0;i < peers.count();i++)
{
Peer* p = peers.at(i);
if (!p)
continue;
if (num_unchoked < 4)
{
p->getPacketWriter().sendUnchoke();
num_unchoked++;
}
else
p->choke();
}
// go over the other peers and unchoke, if we do not have enough
for (Uint32 i = 0;i < others.count();i++)
{
Peer* p = others.at(i);
if (!p)
continue;
if (num_unchoked < 4)
{
p->getPacketWriter().sendUnchoke();
num_unchoked++;
}
else
p->choke();
}
}
else
{
Uint32 rnd = 0;
if (peers.count() > 3)
rnd = 3 + rand() % (peers.count() - 3);
Uint32 num_unchoked = 0;
// take the first 3 and a random one
for (Uint32 i = 0;i < peers.count();i++)
{
Peer* p = peers.at(i);
if (!p)
continue;
if (num_unchoked < 4 || i == rnd)
{
p->getPacketWriter().sendUnchoke();
num_unchoked++;
}
else
p->choke();
}
// go over the other peers and unchoke, if we do not have enough
for (Uint32 i = 0;i < others.count();i++)
{
Peer* p = others.at(i);
if (!p)
continue;
if (num_unchoked < 4 || i == rnd)
{
p->getPacketWriter().sendUnchoke();
num_unchoked++;
}
else
p->choke();
}
}
round_state++;
if (round_state > 3)
round_state = 1;
}
}
#endif
|