-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathbt_selector_random.c
221 lines (183 loc) · 3.98 KB
/
bt_selector_random.c
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
/**
* Copyright (c) 2011, Willem-Hendrik Thiart
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*
* @file
* @brief Select a random piece to download
* @author Willem Thiart himself@willemthiart.com
* @version 0.1
*/
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "bt.h"
#include "linked_list_queue.h"
#include "linked_list_hashmap.h"
#include "bag.h"
/* random */
typedef struct
{
hashmap_t *peers;
/* pieces that are candidates for polling */
//hashmap_t *p_candidates;
/* pieces that we've polled */
hashmap_t *p_polled;
/* number of pieces to download */
int npieces;
} random_t;
/* peer */
typedef struct
{
//hashmap_t *have_pieces;
bag_t *p_candidates;
} peer_t;
static unsigned long __peer_hash(
const void *obj
)
{
return (unsigned long) obj;
}
static long __peer_compare(
const void *obj,
const void *other
)
{
return obj - other;
}
static int __cmp_piece(
const void *i1,
const void *i2,
const void *ckr
)
{
return i2 - i1;
}
void *bt_random_selector_new(
const int npieces
)
{
random_t *rf;
rf = calloc(1, sizeof(random_t));
rf->npieces = npieces;
rf->peers = hashmap_new(__peer_hash, __peer_compare, 17);
rf->p_polled = hashmap_new(__peer_hash, __peer_compare, 17);
return rf;
}
void bt_random_selector_free(
void *r
)
{
// hashmap_free(rf->peers);
// bag_free(rf->p_candidates);
// hashmap_free(rf->p_polled);
// free(rf);
}
void bt_random_selector_remove_peer(
void *r,
void *peer
)
{
random_t *rf = r;
peer_t *pr;
if ((pr = hashmap_remove(rf->peers, peer)))
{
// hashmap_free(pr->have_pieces);
free(pr);
}
}
void bt_random_selector_add_peer(
void *r,
void *peer
)
{
random_t *rf = r;
peer_t *pr;
/* make sure not to add duplicates */
if ((pr = hashmap_get(rf->peers, peer)))
return;
pr = calloc(1,sizeof(peer_t));
pr->p_candidates = bag_new();
// pr->have_pieces = hashmap_new(__peer_hash, __peer_compare, 11);
hashmap_put(rf->peers, peer, pr);
}
void bt_random_selector_giveback_piece(
void *r,
void* peer,
int piece_idx
)
{
random_t *rf = r;
peer_t *pr;
hashmap_remove(rf->p_polled, (void *) (long) piece_idx + 1);
if (peer)
{
pr = hashmap_get(rf->peers, peer);
assert(pr);
bag_put(pr->p_candidates, (void *) (long) piece_idx + 1);
}
}
void bt_random_selector_have_piece(
void *r,
int piece_idx
)
{
random_t *rf = r;
assert(rf);
assert(rf->p_polled);
hashmap_put(rf->p_polled, (void *) (long) piece_idx + 1, (void *) (long) piece_idx + 1);
}
void bt_random_selector_peer_have_piece(
void *r,
void *peer,
const int piece_idx
)
{
random_t *rf = r;
peer_t *pr;
void* p;
/* get the peer */
pr = hashmap_get(rf->peers, peer);
assert(pr);
if (!(p = hashmap_get(rf->p_polled, (void *) (long) piece_idx + 1)))
{
bag_put(pr->p_candidates, (void *) (long) piece_idx + 1);
}
}
int bt_random_selector_get_npeers(void *r)
{
random_t *rf = r;
return hashmap_count(rf->peers);
}
int bt_random_selector_get_npieces(void *r)
{
random_t *rf = r;
return rf->npieces;
}
int bt_random_selector_poll_best_piece(
void *r,
const void *peer
)
{
random_t *rf = r;
peer_t *pr;
int piece_idx;
if (!(pr = hashmap_get(rf->peers, peer)))
{
return -1;
}
/* get a random piece that the client might have */
while (0 < bag_count(pr->p_candidates))
{
piece_idx = ((unsigned long int)bag_take(pr->p_candidates)) - 1;
/* don't poll if it has been polled already */
if (!(hashmap_get(rf->p_polled, (void *) (long) piece_idx + 1)))
{
void* i = (void *) ((long) piece_idx + 1);
hashmap_put(rf->p_polled, i, i);
return piece_idx;
}
}
return -1;
}