forked from libswift/libswift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
avail.cpp
320 lines (266 loc) · 9.09 KB
/
avail.cpp
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
/*
* availability.h
* Tree keeping track of the availability of each bin in a swarm
*
* Created by Riccardo Petrocco
* Copyright 2009-2012 Delft University of Technology. All rights reserved.
*
*/
#include "swift.h"
#include <cassert>
#define DEBUG
using namespace swift;
#define DEBUGAVAILABILITY 0
void Availability::setParams(int connections)
{
if (DEBUGAVAILABILITY)
fprintf(stderr, "Availability: setting parameters => connections:%d\n", connections);
connections_=connections;
initRarity();
}
void Availability::setBin(bin_t bin, int idx)
{
assert(idx>=0 && idx < connections_);
assert(!bin.is_none());
if (DEBUGAVAILABILITY) {
fprintf(stderr, "Availability: search bin %s [%llu] on rarity %d [", bin.str().c_str(), bin.toUInt(), idx);
if (rarity_[idx]->is_empty())
fprintf(stderr, "empty] ");
else if (rarity_[idx]->is_filled())
fprintf(stderr, "full] ");
else
fprintf(stderr, "partial] ");
}
// if the bin is full at this rarity level, set it to empty
// and propagate the info to higher idx of the array
// (idx = availability of content)
if (rarity_[idx]->is_empty(bin)) {
// if we reached index 0 and the bin is empty
// it means it's out of our binmap => extend the root bin
if (idx==0) {
if (DEBUGAVAILABILITY)
fprintf(stderr, "empty => set the bin in the binmap with rarity 1.\n");
rarity_[1]->set(bin);
assert(rarity_[1]->is_filled(bin));
return;
} else {
if (DEBUGAVAILABILITY)
fprintf(stderr, "empty => search further..\n");
setBin(bin, idx-1);
}
} else if (rarity_[idx]->is_filled(bin)) {
if (DEBUGAVAILABILITY)
fprintf(stderr, "full => set bin in the binmap with rarity %d\n", idx+1);
if (idx==connections_-1)
return;
assert(rarity_[idx+1]->is_empty(bin));
rarity_[idx]->reset(bin);
rarity_[idx+1]->set(bin);
} else {
assert(!rarity_[idx]->is_empty());
if (DEBUGAVAILABILITY)
fprintf(stderr, "partially full!\n");
setBin(bin.left(), idx);
setBin(bin.right(), idx);
}
return;
}
void Availability::addBinmap(binmap_t * binmap)
{
if (DEBUGAVAILABILITY)
dprintf("%s Availability adding binmap ",tintstr());
if (binmap->is_filled()) {
bool complete = true;
for (int idx=0; idx<connections_; idx++) {
bin_t b = binmap_t::find_complement(*binmap, *rarity_[idx], 0);
if (!b.is_none()) {
complete = false;
break;
}
}
if (complete) {
if (DEBUGAVAILABILITY)
dprintf("of a seeder.\n");
// merge binmaps of max availability
bin_t b;
b = binmap_t::find_complement(*rarity_[connections_-1],*rarity_[connections_-2],0);
while (b!=bin_t::NONE) {
rarity_[connections_-1]->set(b);
b = binmap_t::find_complement(*rarity_[connections_-1],*rarity_[connections_-2],0);
}
for (int i=connections_-2; i>0; i--) {
rarity_[i] = rarity_[i-1];
}
return;
}
}
if (!binmap->is_empty()) {
if (DEBUGAVAILABILITY)
dprintf("of a leecher.\n");
bin_t tmp_b;
binmap_t tmp_bm;
tmp_b = binmap_t::find_complement(tmp_bm, *binmap, 0);
while (tmp_b != bin_t::NONE) {
setBin(tmp_b,connections_-1);
//binmap_t::copy(tmp_bm, *binmap, tmp_b);
tmp_bm.set(tmp_b);
tmp_b = binmap_t::find_complement(tmp_bm, *binmap, 0);
}
//status();
}
return;
}
void Availability::set(uint32_t channel_id, binmap_t& binmap, bin_t target)
{
if (DEBUGAVAILABILITY)
fprintf(stderr, "%s #%" PRIu32 " Availability -> setting %s (%llu)\n",tintstr(),channel_id,target.str().c_str(),
target.toUInt());
if (false)
for (int i=0; i<connections_; i++)
fprintf(stderr, "%d\t%p\t%s\n", i, rarity_[i], rarity_[i]->is_empty()?"empty":"something there");
// this functin is called BEFORE the target bin is set in the channel's binmap
if (!binmap.is_filled(target)) {
binmap_t tmp;
bin_t bin;
//binmap_t::copy(tmp, binmap, target);
tmp.set(target);
// find newly acked bins
do {
bin = binmap_t::find_complement(binmap, tmp, target, 0);
if (!bin.is_none()) {
if (DEBUGAVAILABILITY)
fprintf(stderr, "Availability: new bin = %s [%llu]\n", bin.str().c_str(), bin.toUInt());
setBin(bin, connections_-1);
tmp.reset(bin);
}
} while (!bin.is_none());
}
return;
}
void Availability::find_empty(binmap_t& binmap, bin_t range)
{
if (binmap.is_empty(range)) {
setBin(range, connections_-1);
} else {
if (range.is_base())
return;
if (!binmap.is_filled(range.left())) {
find_empty(binmap, range.to_left());
}
if (!binmap.is_filled(range.right()))
find_empty(binmap, range.to_right());
}
}
// remove the binmap from the rarity array
void Availability::removeBinmap(uint32_t channel_id, binmap_t& binmap)
{
if (DEBUGAVAILABILITY)
fprintf(stderr, "%s #%" PRIu32 " Availability -> removing peer ",tintstr(),channel_id);
// if it's a complete binmap of the file (or of our current knowledge)
// just move the binmaps down 1 idx in the rarity array
if (binmap.is_filled()) {
bool complete = true;
for (int idx=0; idx<connections_; idx++) {
bin_t b = binmap_t::find_complement(binmap, *rarity_[idx], 0);
if (!b.is_none()) {
complete = false;
break;
}
}
// basically change index of the array with precautions for first and last element
if (complete) {
if (DEBUGAVAILABILITY)
fprintf(stderr, "(seeder).\n");
// merge binmaps of availability 1 and 0
bin_t b;
b = binmap_t::find_complement(*rarity_[0],*rarity_[1],0);
while (b!=bin_t::NONE) {
rarity_[0]->set(b);
b = binmap_t::find_complement(*rarity_[0],*rarity_[1],0);
}
for (int i=1; i<connections_-1; i++) {
rarity_[i] = rarity_[i+1];
}
return;
}
}
if (!binmap.is_empty()) {
if (DEBUGAVAILABILITY)
fprintf(stderr, "(leecher).\n");
bin_t tmp_b;
binmap_t tmp_bm;
tmp_b = binmap_t::find_complement(tmp_bm, binmap, 0);
while (!tmp_b.is_none()) {
removeBin(tmp_b, connections_-1);
tmp_bm.set(tmp_b);
tmp_b = binmap_t::find_complement(tmp_bm, binmap, 0);
}
}
return;
}
void Availability::removeBin(bin_t bin, int idx)
{
if (idx==0) {
if (DEBUGAVAILABILITY)
fprintf(stderr, "Availability: already at idx 0\n");
return;
}
if (DEBUGAVAILABILITY)
fprintf(stderr, "Availability: search bin %s [%llu] on rarity %d [%s] ", bin.str().c_str(), bin.toUInt(), idx,
rarity_[idx]->is_empty()?"empty":"full");
// if the bin is full at this rarity level, set it to empty
// and propagate the info to higher idx of the array
// (idx = availability of content)
if (rarity_[idx]->is_empty(bin)) {
// if we reached index 0 and the bin is empty
// it means it's out of our binmap => extend the root bin
if (idx==0) {
if (DEBUGAVAILABILITY)
fprintf(stderr, "empty => extend root of binmap with rarity 1.\n");
//rarity_[0]->extend_if_needed(bin);
rarity_[1]->set(bin);
return;
} else {
if (DEBUGAVAILABILITY)
fprintf(stderr, "empty => search further..\n");
removeBin(bin, idx-1);
}
} else if (rarity_[idx]->is_filled(bin)) {
if (DEBUGAVAILABILITY)
fprintf(stderr, "full!!\n");
if (idx==connections_-1)
return;
assert(rarity_[idx-1]->is_empty(bin));
rarity_[idx]->reset(bin);
rarity_[idx-1]->set(bin);
} else {
assert(!rarity_[idx]->is_empty());
if (DEBUGAVAILABILITY)
fprintf(stderr, "partially full!\n");
removeBin(bin.left(), idx);
removeBin(bin.right(), idx);
}
return;
}
bin_t Availability::getRarest(const bin_t range, int width)
{
// TODO
return bin_t::NONE;
}
void Availability::status() const
{
for (int i=0; i<connections_; i++)
fprintf(stderr, "%d\t%p\t%s\n", i, rarity_[i], rarity_[i]->is_empty()?"empty":"full");
return;
}
void Availability::initRarity()
{
assert(connections_>0);
rarity_ = new binmap_t*[connections_];
for (int i=0; i<connections_; i++) {
rarity_[i]=new binmap_t();
}
if (DEBUGAVAILABILITY)
status();
return;
}