streamers / topology.c @ 8bda7a99
History | View | Annotate | Download (12.6 KB)
1 | 7f591208 | Csaba Kiraly | /*
|
---|---|---|---|
2 | * Copyright (c) 2010 Csaba Kiraly
|
||
3 | * Copyright (c) 2010 Luca Abeni
|
||
4 | *
|
||
5 | * This is free software; see gpl-3.0.txt
|
||
6 | */
|
||
7 | 2f846ddd | Csaba Kiraly | #include <stdint.h> |
8 | fcb5c29b | Csaba Kiraly | #include <stdio.h> |
9 | 2f846ddd | Csaba Kiraly | #include <sys/time.h> |
10 | #include <time.h> |
||
11 | 7e39164f | MarcoBiazzini | #include <stdlib.h> |
12 | c2d8fe2d | Csaba Kiraly | #include <string.h> |
13 | 2f846ddd | Csaba Kiraly | |
14 | 7e39164f | MarcoBiazzini | #include <math.h> |
15 | 74a5d4ae | CsabaKiraly | #include <net_helper.h> |
16 | 2f846ddd | Csaba Kiraly | #include <peerset.h> |
17 | #include <peer.h> |
||
18 | 33d23b91 | MarcoBiazzini | #include <grapes_msg_types.h> |
19 | 2f846ddd | Csaba Kiraly | #include <topmanager.h> |
20 | 7e39164f | MarcoBiazzini | #include <tman.h> |
21 | 2f846ddd | Csaba Kiraly | |
22 | 63ebb93d | CsabaKiraly | #include "compatibility/timer.h" |
23 | |||
24 | 2f846ddd | Csaba Kiraly | #include "topology.h" |
25 | 132f8b40 | Csaba Kiraly | #include "streaming.h" |
26 | 4729fe2d | Csaba Kiraly | #include "dbg.h" |
27 | 4bf91643 | Csaba Kiraly | #include "measures.h" |
28 | 0877a9f6 | Csaba Kiraly | #include "streamer.h" |
29 | 4bf91643 | Csaba Kiraly | |
30 | a4933dfb | Csaba Kiraly | #define MIN(A,B) (((A) < (B)) ? (A) : (B))
|
31 | 31ba5789 | Csaba Kiraly | |
32 | 9818ca86 | Csaba Kiraly | double desired_bw = 0; |
33 | 970bd24a | napawine | double desired_rtt = 0.2; |
34 | acbc9155 | Csaba Kiraly | double alpha_target = 0.5; |
35 | |||
36 | 20663df3 | Csaba Kiraly | int NEIGHBORHOOD_TARGET_SIZE = 20; |
37 | 52c2cd41 | Csaba Kiraly | double NEIGHBORHOOD_ROTATE_RATIO = 1.0; |
38 | 01a25012 | MarcoBiazzini | #define TMAN_MAX_IDLE 10 |
39 | 821cc939 | CsabaKiraly | #define TMAN_LOG_EVERY 1000 |
40 | d78e9c6a | Csaba Kiraly | |
41 | fcb5c29b | Csaba Kiraly | static struct peerset *pset; |
42 | 9610d648 | Csaba Kiraly | static struct timeval tout_bmap = {10, 0}; |
43 | 7e39164f | MarcoBiazzini | static int counter = 0; |
44 | static int simpleRanker (const void *tin, const void *p1in, const void *p2in); |
||
45 | static tmanRankingFunction rankFunct = simpleRanker;
|
||
46 | e4247dda | Csaba Kiraly | |
47 | cc0cd459 | Csaba Kiraly | struct metadata {
|
48 | d2514625 | Csaba Kiraly | uint16_t cb_size; |
49 | 9d55b3b7 | Csaba Kiraly | uint16_t cps; |
50 | b1d41fdb | Csaba Kiraly | float capacity;
|
51 | 5be2e90d | Csaba Kiraly | float recv_delay;
|
52 | e4247dda | Csaba Kiraly | } __attribute__((packed)); |
53 | |||
54 | cc0cd459 | Csaba Kiraly | static struct metadata my_metadata; |
55 | 7e39164f | MarcoBiazzini | static int cnt = 0; |
56 | static struct nodeID *me = NULL; |
||
57 | 33d23b91 | MarcoBiazzini | static unsigned char mTypes[] = {MSG_TYPE_TOPOLOGY,MSG_TYPE_TMAN}; |
58 | 9ffd84bb | MarcoBiazzini | static struct nodeID ** neighbors; |
59 | 7e39164f | MarcoBiazzini | |
60 | static void update_metadata(void) { |
||
61 | b88c9f4a | Csaba Kiraly | my_metadata.cb_size = am_i_source() ? 0 : get_cb_size();
|
62 | 5be2e90d | Csaba Kiraly | my_metadata.recv_delay = get_receive_delay(); |
63 | 9d55b3b7 | Csaba Kiraly | my_metadata.cps = get_chunks_per_sec(); |
64 | b1d41fdb | Csaba Kiraly | my_metadata.capacity = get_capacity(); |
65 | 7e39164f | MarcoBiazzini | } |
66 | |||
67 | static int simpleRanker (const void *tin, const void *p1in, const void *p2in) { |
||
68 | |||
69 | double t,p1,p2;
|
||
70 | t = *((const double *)tin); |
||
71 | p1 = *((const double *)p1in); |
||
72 | p2 = *((const double *)p2in); |
||
73 | |||
74 | if (isnan(t) || (isnan(p1) && isnan(p2))) return 0; |
||
75 | else if (isnan(p1)) return 2; |
||
76 | else if (isnan(p2)) return 1; |
||
77 | else return (fabs(t-p1) == fabs(t-p2))?0:(fabs(t-p1) < fabs(t-p2))?1:2; |
||
78 | |||
79 | } |
||
80 | |||
81 | int topologyInit(struct nodeID *myID, const char *config) |
||
82 | { |
||
83 | 33d23b91 | MarcoBiazzini | int i;
|
84 | for (i=0;i<2;i++) |
||
85 | bind_msg_type(mTypes[i]); |
||
86 | 7e39164f | MarcoBiazzini | update_metadata(); |
87 | me = myID; |
||
88 | return (topInit(myID, &my_metadata, sizeof(my_metadata), config) && tmanInit(myID,&my_metadata, sizeof(my_metadata),rankFunct,0)); |
||
89 | } |
||
90 | |||
91 | void topologyShutdown(void) |
||
92 | { |
||
93 | } |
||
94 | |||
95 | int topoAddNeighbour(struct nodeID *neighbour, void *metadata, int metadata_size) |
||
96 | { |
||
97 | // TODO: check this!! Just to use this function to bootstrap ncast...
|
||
98 | cc0cd459 | Csaba Kiraly | struct metadata m = {0}; //TODO: check what metadata option should mean |
99 | |||
100 | 7e39164f | MarcoBiazzini | if (counter < TMAN_MAX_IDLE)
|
101 | cc0cd459 | Csaba Kiraly | return topAddNeighbour(neighbour,&m,sizeof(m)); |
102 | else return tmanAddNeighbour(neighbour,&m,sizeof(m)); |
||
103 | 7e39164f | MarcoBiazzini | } |
104 | |||
105 | static int topoParseData(const uint8_t *buff, int len) |
||
106 | { |
||
107 | int res = -1,ncs = 0,msize; |
||
108 | const struct nodeID **n; const void *m; |
||
109 | if (!buff || buff[0] == MSG_TYPE_TOPOLOGY) { |
||
110 | res = topParseData(buff,len); |
||
111 | c65ac153 | Csaba Kiraly | // if (counter <= TMAN_MAX_IDLE)
|
112 | // counter++;
|
||
113 | 7e39164f | MarcoBiazzini | } |
114 | if (counter >= TMAN_MAX_IDLE && (!buff || buff[0] == MSG_TYPE_TMAN)) |
||
115 | { |
||
116 | n = topGetNeighbourhood(&ncs); |
||
117 | if (ncs) {
|
||
118 | m = topGetMetadata(&msize); |
||
119 | res = tmanParseData(buff,len,n,ncs,m,msize); |
||
120 | } |
||
121 | } |
||
122 | return res;
|
||
123 | } |
||
124 | |||
125 | static const struct nodeID **topoGetNeighbourhood(int *n) |
||
126 | { |
||
127 | int i; double d; |
||
128 | if (counter > TMAN_MAX_IDLE) {
|
||
129 | 9ffd84bb | MarcoBiazzini | uint8_t *mdata; int msize;
|
130 | 7e39164f | MarcoBiazzini | *n = tmanGetNeighbourhoodSize(); |
131 | 9ffd84bb | MarcoBiazzini | if (neighbors) free(neighbors);
|
132 | 7e39164f | MarcoBiazzini | neighbors = calloc(*n,sizeof(struct nodeID *)); |
133 | tmanGetMetadata(&msize); |
||
134 | mdata = calloc(*n,msize); |
||
135 | tmanGivePeers(*n,neighbors,(void *)mdata);
|
||
136 | |||
137 | 0711317b | CsabaKiraly | if (cnt % TMAN_LOG_EVERY == 0) { |
138 | 5be2e90d | Csaba Kiraly | fprintf(stderr,"abouttopublish,%s,%s,,Tman_chunk_delay,%f\n",node_addr(me),node_addr(me),my_metadata.recv_delay);
|
139 | 7e39164f | MarcoBiazzini | for (i=0;i<(*n) && i<NEIGHBORHOOD_TARGET_SIZE;i++) { |
140 | d = *((double *)(mdata+i*msize));
|
||
141 | fprintf(stderr,"abouttopublish,%s,",node_addr(me));
|
||
142 | fprintf(stderr,"%s,,Tman_chunk_delay,%f\n",node_addr(neighbors[i]),d);
|
||
143 | } |
||
144 | fprintf(stderr,"abouttopublish,%s,%s,,Tman_neighborhood_size,%d\n\n",node_addr(me),node_addr(me),*n);
|
||
145 | } |
||
146 | |||
147 | free(mdata); |
||
148 | return (const struct nodeID **)neighbors; |
||
149 | } |
||
150 | else
|
||
151 | return topGetNeighbourhood(n);
|
||
152 | } |
||
153 | 2f846ddd | Csaba Kiraly | |
154 | 685c4dd0 | MarcoBiazzini | static void topoAddToBL (struct nodeID *id) |
155 | { |
||
156 | if (counter >= TMAN_MAX_IDLE)
|
||
157 | tmanAddToBlackList(id); |
||
158 | // else
|
||
159 | topAddToBlackList(id); |
||
160 | } |
||
161 | fcb5c29b | Csaba Kiraly | |
162 | cc0cd459 | Csaba Kiraly | void add_peer(const struct nodeID *id, const struct metadata *m) |
163 | fcb5c29b | Csaba Kiraly | { |
164 | b88c9f4a | Csaba Kiraly | dprintf("Adding %s to neighbourhood! cb_size:%d\n", node_addr(id), m?m->cb_size:-1); |
165 | fcb5c29b | Csaba Kiraly | peerset_add_peer(pset, id); |
166 | b88c9f4a | Csaba Kiraly | if (m) peerset_get_peer(pset, id)->cb_size = m->cb_size;
|
167 | b1d41fdb | Csaba Kiraly | if (m) peerset_get_peer(pset, id)->capacity = m->capacity;
|
168 | 4bf91643 | Csaba Kiraly | /* add measures here */
|
169 | add_measures(id); |
||
170 | b0225995 | Csaba Kiraly | send_bmap(id); |
171 | fcb5c29b | Csaba Kiraly | } |
172 | |||
173 | 89ccef52 | Csaba Kiraly | void remove_peer(const struct nodeID *id) |
174 | fcb5c29b | Csaba Kiraly | { |
175 | dprintf("Removing %s from neighbourhood!\n", node_addr(id));
|
||
176 | 4bf91643 | Csaba Kiraly | /* add measures here */
|
177 | delete_measures(id); |
||
178 | fcb5c29b | Csaba Kiraly | peerset_remove_peer(pset, id); |
179 | } |
||
180 | |||
181 | a26a0456 | Csaba Kiraly | //get the rtt. Currenly only MONL version is supported
|
182 | 89ccef52 | Csaba Kiraly | static double get_rtt_of(const struct nodeID* n){ |
183 | acbc9155 | Csaba Kiraly | #ifdef MONL
|
184 | 896704e9 | Csaba Kiraly | return get_rtt(n);
|
185 | acbc9155 | Csaba Kiraly | #else
|
186 | a26a0456 | Csaba Kiraly | return NAN;
|
187 | acbc9155 | Csaba Kiraly | #endif
|
188 | } |
||
189 | |||
190 | 9818ca86 | Csaba Kiraly | //get the declared capacity of a node
|
191 | static double get_capacity_of(const struct nodeID* n){ |
||
192 | struct peer *p = peerset_get_peer(pset, n);
|
||
193 | if (p) {
|
||
194 | return p->capacity;
|
||
195 | } |
||
196 | |||
197 | return NAN;
|
||
198 | } |
||
199 | |||
200 | a26a0456 | Csaba Kiraly | //returns: 1:yes 0:no -1:unknown
|
201 | 89ccef52 | Csaba Kiraly | int desiredness(const struct nodeID* n) { |
202 | a26a0456 | Csaba Kiraly | double rtt = get_rtt_of(n);
|
203 | 9818ca86 | Csaba Kiraly | double bw = get_capacity_of(n);
|
204 | |||
205 | if ((isnan(rtt) && finite(desired_rtt)) || (isnan(bw) && desired_bw > 0)) { |
||
206 | return -1; |
||
207 | } else if ((isnan(rtt) || rtt <= desired_rtt) && (isnan(bw) || bw >= desired_bw)) { |
||
208 | return 1; |
||
209 | } |
||
210 | a26a0456 | Csaba Kiraly | |
211 | 9818ca86 | Csaba Kiraly | return 0; |
212 | a26a0456 | Csaba Kiraly | } |
213 | |||
214 | 89ccef52 | Csaba Kiraly | bool is_desired(const struct nodeID* n) { |
215 | a9e55854 | Csaba Kiraly | return (desiredness(n) == 1); |
216 | } |
||
217 | |||
218 | c2d8fe2d | Csaba Kiraly | // The usual shuffle
|
219 | static void shuffle(void *base, size_t nmemb, size_t size) { |
||
220 | int i;
|
||
221 | unsigned char t[size]; |
||
222 | unsigned char* b = base; |
||
223 | |||
224 | for (i = nmemb - 1; i > 0; i--) { |
||
225 | int newpos = (rand()/(RAND_MAX + 1.0)) * (i + 1); |
||
226 | memcpy(t, b + size * newpos, size); |
||
227 | memmove(b + size * newpos, b + size * i, size); |
||
228 | memcpy(b + size * i, t, size); |
||
229 | } |
||
230 | } |
||
231 | |||
232 | 89ccef52 | Csaba Kiraly | static void nidset_shuffle(const struct nodeID **base, size_t nmemb) { |
233 | c2d8fe2d | Csaba Kiraly | shuffle(base, nmemb, sizeof(struct nodeID *)); |
234 | } |
||
235 | |||
236 | 89ccef52 | Csaba Kiraly | static int nidset_filter(const struct nodeID **dst, size_t *dst_size, const struct nodeID **src, size_t src_size, bool(*f)(const struct nodeID *)) { |
237 | c2d8fe2d | Csaba Kiraly | size_t i; |
238 | size_t max_size = *dst_size; |
||
239 | *dst_size = 0;
|
||
240 | |||
241 | for (i = 0; i < src_size; i++) { |
||
242 | if (f(src[i])) {
|
||
243 | if (*dst_size < max_size) {
|
||
244 | dst[(*dst_size)++] = src[i]; |
||
245 | } else {
|
||
246 | return -1; |
||
247 | } |
||
248 | } |
||
249 | } |
||
250 | |||
251 | return 0; |
||
252 | } |
||
253 | |||
254 | // B \ A
|
||
255 | 89ccef52 | Csaba Kiraly | static int nidset_complement(const struct nodeID **dst, size_t *dst_size, const struct nodeID **bs, size_t bs_size, const struct nodeID **as, size_t as_size) { |
256 | c2d8fe2d | Csaba Kiraly | size_t i, j; |
257 | size_t max_size = *dst_size; |
||
258 | *dst_size = 0;
|
||
259 | |||
260 | for (i = 0; i < bs_size; i++) { |
||
261 | for (j = 0; j < as_size; j++) { |
||
262 | fe4dc798 | Csaba Kiraly | if (nodeid_equal(bs[i], as[j])) {
|
263 | c2d8fe2d | Csaba Kiraly | break;
|
264 | } |
||
265 | } |
||
266 | if (j >= as_size) {
|
||
267 | if (*dst_size < max_size) {
|
||
268 | dst[(*dst_size)++] = bs[i]; |
||
269 | } else {
|
||
270 | return -1; |
||
271 | } |
||
272 | } |
||
273 | } |
||
274 | |||
275 | return 0; |
||
276 | } |
||
277 | |||
278 | c7ebbd34 | Csaba Kiraly | static bool nidset_find(size_t *i, const struct nodeID **ids, size_t ids_size, const struct nodeID *id) { |
279 | for (*i = 0; *i < ids_size; (*i)++) { |
||
280 | fe4dc798 | Csaba Kiraly | if (nodeid_equal(ids[*i],id)) {
|
281 | c7ebbd34 | Csaba Kiraly | return true; |
282 | } |
||
283 | } |
||
284 | return false; |
||
285 | } |
||
286 | |||
287 | 89ccef52 | Csaba Kiraly | static int nidset_add(const struct nodeID **dst, size_t *dst_size, const struct nodeID **as, size_t as_size, const struct nodeID **bs, size_t bs_size) { |
288 | e216399f | Csaba Kiraly | int r;
|
289 | c2d8fe2d | Csaba Kiraly | size_t i; |
290 | size_t max_size = *dst_size; |
||
291 | |||
292 | i = MIN(as_size, max_size); |
||
293 | memcpy(dst, as, i * sizeof(struct nodeID*)); |
||
294 | *dst_size = i; |
||
295 | if (i < as_size) return -1; |
||
296 | |||
297 | e216399f | Csaba Kiraly | max_size -= *dst_size; |
298 | r = nidset_complement(dst + *dst_size, &max_size, bs, bs_size, as, as_size); |
||
299 | *dst_size += max_size; |
||
300 | c2d8fe2d | Csaba Kiraly | |
301 | e216399f | Csaba Kiraly | return r;
|
302 | c2d8fe2d | Csaba Kiraly | } |
303 | |||
304 | 89ccef52 | Csaba Kiraly | static int nidset_add_i(const struct nodeID **dst, size_t *dst_size, size_t max_size, const struct nodeID **as, size_t as_size) { |
305 | e216399f | Csaba Kiraly | int r;
|
306 | c2d8fe2d | Csaba Kiraly | |
307 | e216399f | Csaba Kiraly | max_size -= *dst_size; |
308 | r = nidset_complement(dst + *dst_size, &max_size, as, as_size, dst, *dst_size); |
||
309 | *dst_size += max_size; |
||
310 | c2d8fe2d | Csaba Kiraly | |
311 | e216399f | Csaba Kiraly | return r;
|
312 | c2d8fe2d | Csaba Kiraly | } |
313 | |||
314 | 2f846ddd | Csaba Kiraly | // currently it just makes the peerset grow
|
315 | 74a5d4ae | CsabaKiraly | void update_peers(struct nodeID *from, const uint8_t *buff, int len) |
316 | 2f846ddd | Csaba Kiraly | { |
317 | 6b2924d4 | Csaba Kiraly | int n_ids, metasize, i, newids_size, max_ids;
|
318 | static const struct nodeID **newids; |
||
319 | cc0cd459 | Csaba Kiraly | static const struct metadata *metas; |
320 | 2f846ddd | Csaba Kiraly | struct peer *peers;
|
321 | struct timeval tnow, told;
|
||
322 | |||
323 | eac0d4ce | Csaba Kiraly | if timerisset(&tout_bmap) {
|
324 | gettimeofday(&tnow, NULL);
|
||
325 | timersub(&tnow, &tout_bmap, &told); |
||
326 | peers = peerset_get_peers(pset); |
||
327 | for (i = 0; i < peerset_size(pset); i++) { |
||
328 | if ( (!timerisset(&peers[i].bmap_timestamp) && timercmp(&peers[i].creation_timestamp, &told, <) ) ||
|
||
329 | ( timerisset(&peers[i].bmap_timestamp) && timercmp(&peers[i].bmap_timestamp, &told, <) ) ) { |
||
330 | 825df88c | Csaba Kiraly | fprintf(stderr,"Topo: dropping inactive %s (peers:%d)\n", node_addr(peers[i].id), peerset_size(pset));
|
331 | eac0d4ce | Csaba Kiraly | //if (peerset_size(pset) > 1) { // avoid dropping our last link to the world
|
332 | topoAddToBL(peers[i].id); |
||
333 | remove_peer(peers[i--].id); |
||
334 | //}
|
||
335 | } |
||
336 | } |
||
337 | } |
||
338 | |||
339 | 8a49328f | CsabaKiraly | if (cnt++ % 100 == 0) { |
340 | 7e39164f | MarcoBiazzini | update_metadata(); |
341 | 59ad7d83 | Csaba Kiraly | if (counter > TMAN_MAX_IDLE) {
|
342 | 7e39164f | MarcoBiazzini | tmanChangeMetadata(&my_metadata,sizeof(my_metadata));
|
343 | 59ad7d83 | Csaba Kiraly | } |
344 | 7e39164f | MarcoBiazzini | } |
345 | |||
346 | topoParseData(buff, len); |
||
347 | de351684 | Csaba Kiraly | |
348 | 6828703c | Csaba Kiraly | if (!buff) {
|
349 | reg_neigh_size(peerset_size(pset)); |
||
350 | return;
|
||
351 | } |
||
352 | de351684 | Csaba Kiraly | |
353 | e0f05cb2 | napawine | peers = peerset_get_peers(pset); |
354 | acbc9155 | Csaba Kiraly | n_ids = peerset_size(pset); |
355 | 6b2924d4 | Csaba Kiraly | newids = topoGetNeighbourhood(&newids_size); //TODO handle both tman and topo
|
356 | metas = topGetMetadata(&metasize); //TODO: check metasize
|
||
357 | max_ids = n_ids + newids_size; |
||
358 | 825df88c | Csaba Kiraly | fprintf(stderr,"Topo modify start peers:%d candidates:%d\n", n_ids, newids_size);
|
359 | d8a205cd | Csaba Kiraly | { |
360 | int desired_part;
|
||
361 | 6b2924d4 | Csaba Kiraly | const struct nodeID *oldids[max_ids], *nodeids[max_ids], *desireds[max_ids], *selecteds[max_ids], *others[max_ids], *toadds[max_ids], *toremoves[max_ids]; |
362 | size_t oldids_size, nodeids_size, desireds_size, selecteds_size, others_size, toadds_size, toremoves_size; |
||
363 | dff739fc | Csaba Kiraly | nodeids_size = desireds_size = selecteds_size = others_size = toadds_size = toremoves_size = max_ids; |
364 | acbc9155 | Csaba Kiraly | |
365 | 6b2924d4 | Csaba Kiraly | for (i = 0, oldids_size = 0; i < peerset_size(pset); i++) { |
366 | oldids[oldids_size++] = peers[i].id; |
||
367 | fprintf(stderr," %s - RTT: %f\n", node_addr(peers[i].id) , get_rtt_of(peers[i].id));
|
||
368 | acbc9155 | Csaba Kiraly | } |
369 | d8a205cd | Csaba Kiraly | |
370 | 6b2924d4 | Csaba Kiraly | |
371 | //compose list of nodeids
|
||
372 | nidset_add(nodeids, &nodeids_size, oldids, oldids_size, newids, newids_size); |
||
373 | |||
374 | d8a205cd | Csaba Kiraly | // select the alpha_target portion of desired peers
|
375 | 2167040b | Csaba Kiraly | desired_part = (1 - alpha_target) * NEIGHBORHOOD_TARGET_SIZE;
|
376 | d8a205cd | Csaba Kiraly | nidset_filter(desireds, &desireds_size, nodeids, nodeids_size, is_desired); |
377 | nidset_shuffle(desireds, desireds_size); |
||
378 | selecteds_size = MIN(desireds_size,desired_part); |
||
379 | memcpy(selecteds, desireds, selecteds_size * sizeof(selecteds[0])); |
||
380 | |||
381 | // random from the rest
|
||
382 | nidset_complement(others, &others_size, nodeids, nodeids_size, selecteds, selecteds_size); |
||
383 | nidset_shuffle(others, others_size); |
||
384 | fe5d7b85 | Csaba Kiraly | nidset_add_i(selecteds, &selecteds_size, max_ids, others, NEIGHBORHOOD_TARGET_SIZE ? MIN(others_size, NEIGHBORHOOD_TARGET_SIZE - selecteds_size) : others_size); |
385 | d8a205cd | Csaba Kiraly | |
386 | 8bda7a99 | Csaba Kiraly | fprintf(stderr,"Topo modify (from:%ld sel:%ld) - desired: %ld of %ld (target:%d sel:%ld); random: from %ld (sel:%ld)\n",
|
387 | (long)nodeids_size, (long)selecteds_size, (long)desireds_size, (long)nodeids_size, desired_part, (long) MIN(desireds_size,desired_part), (long)others_size, (long)selecteds_size - MIN(desireds_size, desired_part)); |
||
388 | 6b2924d4 | Csaba Kiraly | // add new ones
|
389 | nidset_complement(toadds, &toadds_size, selecteds, selecteds_size, oldids, oldids_size); |
||
390 | for (i = 0; i < toadds_size; i++) { |
||
391 | size_t j; |
||
392 | //searching for the metadata
|
||
393 | if (nidset_find(&j, newids, newids_size, toadds[i])) {
|
||
394 | fprintf(stderr," adding %s\n", node_addr(toadds[i]));
|
||
395 | add_peer(newids[j], &metas[j]); |
||
396 | } else {
|
||
397 | fprintf(stderr," Error: missing metadataadding for %s\n", node_addr(toadds[i]));
|
||
398 | } |
||
399 | } |
||
400 | |||
401 | d8a205cd | Csaba Kiraly | // finally, remove those not needed
|
402 | fprintf(stderr,"Topo remove start (peers:%d)\n", n_ids);
|
||
403 | nidset_complement(toremoves, &toremoves_size, nodeids, nodeids_size, selecteds, selecteds_size); |
||
404 | for (i = 0; i < toremoves_size; i++) { |
||
405 | fprintf(stderr," removing %s\n", node_addr(toremoves[i]));
|
||
406 | remove_peer(toremoves[i]); |
||
407 | acbc9155 | Csaba Kiraly | } |
408 | d8a205cd | Csaba Kiraly | fprintf(stderr,"Topo remove end\n");
|
409 | 52c2cd41 | Csaba Kiraly | } |
410 | |||
411 | e4ef30e3 | Csaba Kiraly | reg_neigh_size(peerset_size(pset)); |
412 | 2f846ddd | Csaba Kiraly | } |
413 | fcb5c29b | Csaba Kiraly | |
414 | struct peer *nodeid_to_peer(const struct nodeID* id, int reg) |
||
415 | { |
||
416 | struct peer *p = peerset_get_peer(pset, id);
|
||
417 | if (!p) {
|
||
418 | cb6be841 | Csaba Kiraly | //fprintf(stderr,"warning: received message from unknown peer: %s!%s\n",node_addr(id), reg ? " Adding it to pset." : "");
|
419 | fcb5c29b | Csaba Kiraly | if (reg) {
|
420 | cc0cd459 | Csaba Kiraly | add_peer(id,NULL);
|
421 | 825df88c | Csaba Kiraly | fprintf(stderr,"Topo: ext adding %s (peers:%d)\n", node_addr(id), peerset_size(pset));
|
422 | fcb5c29b | Csaba Kiraly | p = peerset_get_peer(pset,id); |
423 | } |
||
424 | } |
||
425 | |||
426 | return p;
|
||
427 | } |
||
428 | |||
429 | 7e39164f | MarcoBiazzini | int peers_init(void) |
430 | fcb5c29b | Csaba Kiraly | { |
431 | fprintf(stderr,"peers_init\n");
|
||
432 | pset = peerset_init(0);
|
||
433 | 0bb2425e | Csaba Kiraly | return pset ? 1 : 0; |
434 | fcb5c29b | Csaba Kiraly | } |
435 | |||
436 | 7e39164f | MarcoBiazzini | struct peerset *get_peers(void) |
437 | fcb5c29b | Csaba Kiraly | { |
438 | return pset;
|
||
439 | } |