diff options
Diffstat (limited to 'src/ats/gnunet-service-ats_preferences.c')
-rw-r--r-- | src/ats/gnunet-service-ats_preferences.c | 496 |
1 files changed, 242 insertions, 254 deletions
diff --git a/src/ats/gnunet-service-ats_preferences.c b/src/ats/gnunet-service-ats_preferences.c index 26929ea7c..0a12faaae 100644 --- a/src/ats/gnunet-service-ats_preferences.c +++ b/src/ats/gnunet-service-ats_preferences.c | |||
@@ -11,12 +11,12 @@ | |||
11 | WITHOUT ANY WARRANTY; without even the implied warranty of | 11 | WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
13 | Affero General Public License for more details. | 13 | Affero General Public License for more details. |
14 | 14 | ||
15 | You should have received a copy of the GNU Affero General Public License | 15 | You should have received a copy of the GNU Affero General Public License |
16 | along with this program. If not, see <http://www.gnu.org/licenses/>. | 16 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
17 | 17 | ||
18 | SPDX-License-Identifier: AGPL3.0-or-later | 18 | SPDX-License-Identifier: AGPL3.0-or-later |
19 | */ | 19 | */ |
20 | /** | 20 | /** |
21 | * @file ats/gnunet-service-ats_preferences.c | 21 | * @file ats/gnunet-service-ats_preferences.c |
22 | * @brief manage preferences expressed by clients | 22 | * @brief manage preferences expressed by clients |
@@ -32,12 +32,12 @@ | |||
32 | #include "gnunet-service-ats_reservations.h" | 32 | #include "gnunet-service-ats_reservations.h" |
33 | #include "ats.h" | 33 | #include "ats.h" |
34 | 34 | ||
35 | #define LOG(kind,...) GNUNET_log_from (kind, "ats-preferences",__VA_ARGS__) | 35 | #define LOG(kind, ...) GNUNET_log_from(kind, "ats-preferences", __VA_ARGS__) |
36 | 36 | ||
37 | /** | 37 | /** |
38 | * How frequently do we age preference values? | 38 | * How frequently do we age preference values? |
39 | */ | 39 | */ |
40 | #define PREF_AGING_INTERVAL GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10) | 40 | #define PREF_AGING_INTERVAL GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 10) |
41 | 41 | ||
42 | /** | 42 | /** |
43 | * By which factor do we age preferences expressed during | 43 | * By which factor do we age preferences expressed during |
@@ -56,8 +56,7 @@ | |||
56 | /** | 56 | /** |
57 | * Relative preferences for a peer. | 57 | * Relative preferences for a peer. |
58 | */ | 58 | */ |
59 | struct PeerRelative | 59 | struct PeerRelative { |
60 | { | ||
61 | /** | 60 | /** |
62 | * Array of relative preference values, to be indexed by | 61 | * Array of relative preference values, to be indexed by |
63 | * an `enum GNUNET_ATS_PreferenceKind`. | 62 | * an `enum GNUNET_ATS_PreferenceKind`. |
@@ -83,8 +82,7 @@ static struct PeerRelative defvalues; | |||
83 | /** | 82 | /** |
84 | * Preference information per peer and client. | 83 | * Preference information per peer and client. |
85 | */ | 84 | */ |
86 | struct PreferencePeer | 85 | struct PreferencePeer { |
87 | { | ||
88 | /** | 86 | /** |
89 | * Next in DLL of preference entries for the same client. | 87 | * Next in DLL of preference entries for the same client. |
90 | */ | 88 | */ |
@@ -107,7 +105,6 @@ struct PreferencePeer | |||
107 | * client scored other peers. | 105 | * client scored other peers. |
108 | */ | 106 | */ |
109 | double f_rel[GNUNET_ATS_PREFERENCE_END]; | 107 | double f_rel[GNUNET_ATS_PREFERENCE_END]; |
110 | |||
111 | }; | 108 | }; |
112 | 109 | ||
113 | 110 | ||
@@ -116,9 +113,7 @@ struct PreferencePeer | |||
116 | * for peers. This is the information we keep track of for each | 113 | * for peers. This is the information we keep track of for each |
117 | * such client. | 114 | * such client. |
118 | */ | 115 | */ |
119 | struct PreferenceClient | 116 | struct PreferenceClient { |
120 | { | ||
121 | |||
122 | /** | 117 | /** |
123 | * Next in client list | 118 | * Next in client list |
124 | */ | 119 | */ |
@@ -145,7 +140,6 @@ struct PreferenceClient | |||
145 | * peers as expressed by this client. | 140 | * peers as expressed by this client. |
146 | */ | 141 | */ |
147 | double f_abs_sum[GNUNET_ATS_PREFERENCE_END]; | 142 | double f_abs_sum[GNUNET_ATS_PREFERENCE_END]; |
148 | |||
149 | }; | 143 | }; |
150 | 144 | ||
151 | 145 | ||
@@ -175,8 +169,7 @@ static struct GNUNET_SCHEDULER_Task *aging_task; | |||
175 | /** | 169 | /** |
176 | * Closure for #sum_relative_preferences(). | 170 | * Closure for #sum_relative_preferences(). |
177 | */ | 171 | */ |
178 | struct SumContext | 172 | struct SumContext { |
179 | { | ||
180 | /** | 173 | /** |
181 | * Where to accumulate the result. | 174 | * Where to accumulate the result. |
182 | */ | 175 | */ |
@@ -200,9 +193,9 @@ struct SumContext | |||
200 | * @return #GNUNET_OK | 193 | * @return #GNUNET_OK |
201 | */ | 194 | */ |
202 | static int | 195 | static int |
203 | sum_relative_preferences (void *cls, | 196 | sum_relative_preferences(void *cls, |
204 | const struct GNUNET_PeerIdentity *peer, | 197 | const struct GNUNET_PeerIdentity *peer, |
205 | void *value) | 198 | void *value) |
206 | { | 199 | { |
207 | struct SumContext *sum_ctx = cls; | 200 | struct SumContext *sum_ctx = cls; |
208 | struct PreferencePeer *p_cur = value; | 201 | struct PreferencePeer *p_cur = value; |
@@ -221,8 +214,8 @@ sum_relative_preferences (void *cls, | |||
221 | * @return the new relative preference | 214 | * @return the new relative preference |
222 | */ | 215 | */ |
223 | static void | 216 | static void |
224 | update_relative_values_for_peer (const struct GNUNET_PeerIdentity *id, | 217 | update_relative_values_for_peer(const struct GNUNET_PeerIdentity *id, |
225 | enum GNUNET_ATS_PreferenceKind kind) | 218 | enum GNUNET_ATS_PreferenceKind kind) |
226 | { | 219 | { |
227 | struct PreferenceClient *c_cur; | 220 | struct PreferenceClient *c_cur; |
228 | struct SumContext sum_ctx; | 221 | struct SumContext sum_ctx; |
@@ -231,25 +224,25 @@ update_relative_values_for_peer (const struct GNUNET_PeerIdentity *id, | |||
231 | sum_ctx.f_rel_total = 0.0; | 224 | sum_ctx.f_rel_total = 0.0; |
232 | sum_ctx.kind = kind; | 225 | sum_ctx.kind = kind; |
233 | for (c_cur = pc_head; NULL != c_cur; c_cur = c_cur->next) | 226 | for (c_cur = pc_head; NULL != c_cur; c_cur = c_cur->next) |
234 | GNUNET_CONTAINER_multipeermap_get_multiple (c_cur->peer2pref, | 227 | GNUNET_CONTAINER_multipeermap_get_multiple(c_cur->peer2pref, |
235 | id, | 228 | id, |
236 | &sum_relative_preferences, | 229 | &sum_relative_preferences, |
237 | &sum_ctx); | 230 | &sum_ctx); |
238 | LOG (GNUNET_ERROR_TYPE_DEBUG, | 231 | LOG(GNUNET_ERROR_TYPE_DEBUG, |
239 | "Total relative preference for peer `%s' for `%s' is %.3f\n", | 232 | "Total relative preference for peer `%s' for `%s' is %.3f\n", |
240 | GNUNET_i2s (id), | 233 | GNUNET_i2s(id), |
241 | GNUNET_ATS_print_preference_type (kind), | 234 | GNUNET_ATS_print_preference_type(kind), |
242 | sum_ctx.f_rel_total); | 235 | sum_ctx.f_rel_total); |
243 | rp = GNUNET_CONTAINER_multipeermap_get (preference_peers, | 236 | rp = GNUNET_CONTAINER_multipeermap_get(preference_peers, |
244 | id); | 237 | id); |
245 | GNUNET_assert (NULL != rp); | 238 | GNUNET_assert(NULL != rp); |
246 | if (rp->f_rel[kind] != sum_ctx.f_rel_total) | 239 | if (rp->f_rel[kind] != sum_ctx.f_rel_total) |
247 | { | 240 | { |
248 | rp->f_rel[kind] = sum_ctx.f_rel_total; | 241 | rp->f_rel[kind] = sum_ctx.f_rel_total; |
249 | GAS_plugin_notify_preference_changed (id, | 242 | GAS_plugin_notify_preference_changed(id, |
250 | kind, | 243 | kind, |
251 | rp->f_rel[kind]); | 244 | rp->f_rel[kind]); |
252 | } | 245 | } |
253 | } | 246 | } |
254 | 247 | ||
255 | 248 | ||
@@ -262,17 +255,17 @@ update_relative_values_for_peer (const struct GNUNET_PeerIdentity *id, | |||
262 | * @return #GNUNET_OK to continue | 255 | * @return #GNUNET_OK to continue |
263 | */ | 256 | */ |
264 | static int | 257 | static int |
265 | free_peer (void *cls, | 258 | free_peer(void *cls, |
266 | const struct GNUNET_PeerIdentity *key, | 259 | const struct GNUNET_PeerIdentity *key, |
267 | void *value) | 260 | void *value) |
268 | { | 261 | { |
269 | struct PeerRelative *rp = value; | 262 | struct PeerRelative *rp = value; |
270 | 263 | ||
271 | GNUNET_assert (GNUNET_YES == | 264 | GNUNET_assert(GNUNET_YES == |
272 | GNUNET_CONTAINER_multipeermap_remove (preference_peers, | 265 | GNUNET_CONTAINER_multipeermap_remove(preference_peers, |
273 | key, | 266 | key, |
274 | value)); | 267 | value)); |
275 | GNUNET_free (rp); | 268 | GNUNET_free(rp); |
276 | return GNUNET_OK; | 269 | return GNUNET_OK; |
277 | } | 270 | } |
278 | 271 | ||
@@ -286,30 +279,30 @@ free_peer (void *cls, | |||
286 | * @return #GNUNET_OK (continue to iterate) | 279 | * @return #GNUNET_OK (continue to iterate) |
287 | */ | 280 | */ |
288 | static int | 281 | static int |
289 | free_preference (void *cls, | 282 | free_preference(void *cls, |
290 | const struct GNUNET_PeerIdentity *key, | 283 | const struct GNUNET_PeerIdentity *key, |
291 | void *value) | 284 | void *value) |
292 | { | 285 | { |
293 | struct PreferenceClient *pc = cls; | 286 | struct PreferenceClient *pc = cls; |
294 | struct PreferencePeer *p = value; | 287 | struct PreferencePeer *p = value; |
295 | struct PeerRelative *pr; | 288 | struct PeerRelative *pr; |
296 | 289 | ||
297 | GNUNET_assert (GNUNET_OK == | 290 | GNUNET_assert(GNUNET_OK == |
298 | GNUNET_CONTAINER_multipeermap_remove (pc->peer2pref, | 291 | GNUNET_CONTAINER_multipeermap_remove(pc->peer2pref, |
299 | key, | 292 | key, |
300 | p)); | 293 | p)); |
301 | GNUNET_free (p); | 294 | GNUNET_free(p); |
302 | pr = GNUNET_CONTAINER_multipeermap_get (preference_peers, | 295 | pr = GNUNET_CONTAINER_multipeermap_get(preference_peers, |
303 | key); | 296 | key); |
304 | GNUNET_assert (NULL != pr); | 297 | GNUNET_assert(NULL != pr); |
305 | GNUNET_assert (pr->num_clients > 0); | 298 | GNUNET_assert(pr->num_clients > 0); |
306 | pr->num_clients--; | 299 | pr->num_clients--; |
307 | if (0 == pr->num_clients) | 300 | if (0 == pr->num_clients) |
308 | { | 301 | { |
309 | free_peer (NULL, | 302 | free_peer(NULL, |
310 | key, | 303 | key, |
311 | pr); | 304 | pr); |
312 | } | 305 | } |
313 | return GNUNET_OK; | 306 | return GNUNET_OK; |
314 | } | 307 | } |
315 | 308 | ||
@@ -317,8 +310,7 @@ free_preference (void *cls, | |||
317 | /** | 310 | /** |
318 | * Closure for #age_values(). | 311 | * Closure for #age_values(). |
319 | */ | 312 | */ |
320 | struct AgeContext | 313 | struct AgeContext { |
321 | { | ||
322 | /** | 314 | /** |
323 | * Counter of values remaining to update, incremented for each value | 315 | * Counter of values remaining to update, incremented for each value |
324 | * changed (to a new non-zero value). | 316 | * changed (to a new non-zero value). |
@@ -329,7 +321,6 @@ struct AgeContext | |||
329 | * Client we are currently aging values for. | 321 | * Client we are currently aging values for. |
330 | */ | 322 | */ |
331 | struct PreferenceClient *cur_client; | 323 | struct PreferenceClient *cur_client; |
332 | |||
333 | }; | 324 | }; |
334 | 325 | ||
335 | 326 | ||
@@ -342,9 +333,9 @@ struct AgeContext | |||
342 | * @return #GNUNET_OK (continue to iterate) | 333 | * @return #GNUNET_OK (continue to iterate) |
343 | */ | 334 | */ |
344 | static int | 335 | static int |
345 | age_values (void *cls, | 336 | age_values(void *cls, |
346 | const struct GNUNET_PeerIdentity *peer, | 337 | const struct GNUNET_PeerIdentity *peer, |
347 | void *value) | 338 | void *value) |
348 | { | 339 | { |
349 | struct AgeContext *ac = cls; | 340 | struct AgeContext *ac = cls; |
350 | struct PreferencePeer *p = value; | 341 | struct PreferencePeer *p = value; |
@@ -353,32 +344,32 @@ age_values (void *cls, | |||
353 | 344 | ||
354 | dead = GNUNET_YES; | 345 | dead = GNUNET_YES; |
355 | for (i = 0; i < GNUNET_ATS_PREFERENCE_END; i++) | 346 | for (i = 0; i < GNUNET_ATS_PREFERENCE_END; i++) |
356 | { | ||
357 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
358 | "Aging preference for peer `%s'\n", | ||
359 | GNUNET_i2s (peer)); | ||
360 | if (p->f_abs[i] > DEFAULT_ABS_PREFERENCE) | ||
361 | p->f_abs[i] *= PREF_AGING_FACTOR; | ||
362 | if (p->f_abs[i] <= DEFAULT_ABS_PREFERENCE + PREF_EPSILON) | ||
363 | { | 347 | { |
364 | p->f_abs[i] = DEFAULT_ABS_PREFERENCE; | 348 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, |
365 | p->f_rel[i] = DEFAULT_REL_PREFERENCE; | 349 | "Aging preference for peer `%s'\n", |
366 | update_relative_values_for_peer (peer, | 350 | GNUNET_i2s(peer)); |
367 | i); | 351 | if (p->f_abs[i] > DEFAULT_ABS_PREFERENCE) |
352 | p->f_abs[i] *= PREF_AGING_FACTOR; | ||
353 | if (p->f_abs[i] <= DEFAULT_ABS_PREFERENCE + PREF_EPSILON) | ||
354 | { | ||
355 | p->f_abs[i] = DEFAULT_ABS_PREFERENCE; | ||
356 | p->f_rel[i] = DEFAULT_REL_PREFERENCE; | ||
357 | update_relative_values_for_peer(peer, | ||
358 | i); | ||
359 | } | ||
360 | else | ||
361 | { | ||
362 | ac->values_to_update++; | ||
363 | dead = GNUNET_NO; | ||
364 | } | ||
368 | } | 365 | } |
369 | else | 366 | if (GNUNET_YES == dead) |
370 | { | 367 | { |
371 | ac->values_to_update++; | 368 | /* all preferences are zero, remove this entry */ |
372 | dead = GNUNET_NO; | 369 | free_preference(ac->cur_client, |
370 | peer, | ||
371 | p); | ||
373 | } | 372 | } |
374 | } | ||
375 | if (GNUNET_YES == dead) | ||
376 | { | ||
377 | /* all preferences are zero, remove this entry */ | ||
378 | free_preference (ac->cur_client, | ||
379 | peer, | ||
380 | p); | ||
381 | } | ||
382 | return GNUNET_OK; | 373 | return GNUNET_OK; |
383 | } | 374 | } |
384 | 375 | ||
@@ -389,41 +380,40 @@ age_values (void *cls, | |||
389 | * @param cls unused | 380 | * @param cls unused |
390 | */ | 381 | */ |
391 | static void | 382 | static void |
392 | preference_aging (void *cls) | 383 | preference_aging(void *cls) |
393 | { | 384 | { |
394 | struct AgeContext ac; | 385 | struct AgeContext ac; |
395 | 386 | ||
396 | aging_task = NULL; | 387 | aging_task = NULL; |
397 | GAS_plugin_solver_lock (); | 388 | GAS_plugin_solver_lock(); |
398 | ac.values_to_update = 0; | 389 | ac.values_to_update = 0; |
399 | for (ac.cur_client = pc_head; NULL != ac.cur_client; ac.cur_client = ac.cur_client->next) | 390 | for (ac.cur_client = pc_head; NULL != ac.cur_client; ac.cur_client = ac.cur_client->next) |
400 | GNUNET_CONTAINER_multipeermap_iterate (ac.cur_client->peer2pref, | 391 | GNUNET_CONTAINER_multipeermap_iterate(ac.cur_client->peer2pref, |
401 | &age_values, | 392 | &age_values, |
402 | &ac); | 393 | &ac); |
403 | GAS_plugin_solver_unlock (); | 394 | GAS_plugin_solver_unlock(); |
404 | if (ac.values_to_update > 0) | 395 | if (ac.values_to_update > 0) |
405 | { | 396 | { |
406 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | 397 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, |
407 | "Rescheduling aging task due to %u elements remaining to age\n", | 398 | "Rescheduling aging task due to %u elements remaining to age\n", |
408 | ac.values_to_update); | 399 | ac.values_to_update); |
409 | if (NULL == aging_task) | 400 | if (NULL == aging_task) |
410 | aging_task = GNUNET_SCHEDULER_add_delayed (PREF_AGING_INTERVAL, | 401 | aging_task = GNUNET_SCHEDULER_add_delayed(PREF_AGING_INTERVAL, |
411 | &preference_aging, | 402 | &preference_aging, |
412 | NULL); | 403 | NULL); |
413 | } | 404 | } |
414 | else | 405 | else |
415 | { | 406 | { |
416 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | 407 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, |
417 | "No values to age left, not rescheduling aging task\n"); | 408 | "No values to age left, not rescheduling aging task\n"); |
418 | } | 409 | } |
419 | } | 410 | } |
420 | 411 | ||
421 | 412 | ||
422 | /** | 413 | /** |
423 | * Closure for #update_rel_sum() and #update_abs_sum(). | 414 | * Closure for #update_rel_sum() and #update_abs_sum(). |
424 | */ | 415 | */ |
425 | struct UpdateContext | 416 | struct UpdateContext { |
426 | { | ||
427 | /** | 417 | /** |
428 | * Preference client with the sum of all absolute scores. | 418 | * Preference client with the sum of all absolute scores. |
429 | */ | 419 | */ |
@@ -433,7 +423,6 @@ struct UpdateContext | |||
433 | * Which kind are we updating? | 423 | * Which kind are we updating? |
434 | */ | 424 | */ |
435 | enum GNUNET_ATS_PreferenceKind kind; | 425 | enum GNUNET_ATS_PreferenceKind kind; |
436 | |||
437 | }; | 426 | }; |
438 | 427 | ||
439 | 428 | ||
@@ -447,9 +436,9 @@ struct UpdateContext | |||
447 | * @return #GNUNET_OK (continue to iterate) | 436 | * @return #GNUNET_OK (continue to iterate) |
448 | */ | 437 | */ |
449 | static int | 438 | static int |
450 | update_abs_sum (void *cls, | 439 | update_abs_sum(void *cls, |
451 | const struct GNUNET_PeerIdentity *peer, | 440 | const struct GNUNET_PeerIdentity *peer, |
452 | void *value) | 441 | void *value) |
453 | { | 442 | { |
454 | struct UpdateContext *uc = cls; | 443 | struct UpdateContext *uc = cls; |
455 | struct PreferencePeer *p_cur = value; | 444 | struct PreferencePeer *p_cur = value; |
@@ -469,19 +458,19 @@ update_abs_sum (void *cls, | |||
469 | * @return #GNUNET_OK (continue to iterate) | 458 | * @return #GNUNET_OK (continue to iterate) |
470 | */ | 459 | */ |
471 | static int | 460 | static int |
472 | update_rel_sum (void *cls, | 461 | update_rel_sum(void *cls, |
473 | const struct GNUNET_PeerIdentity *peer, | 462 | const struct GNUNET_PeerIdentity *peer, |
474 | void *value) | 463 | void *value) |
475 | { | 464 | { |
476 | struct UpdateContext *uc = cls; | 465 | struct UpdateContext *uc = cls; |
477 | struct PreferencePeer *p_cur = value; | 466 | struct PreferencePeer *p_cur = value; |
478 | 467 | ||
479 | p_cur->f_rel[uc->kind] = p_cur->f_abs[uc->kind] / uc->pc->f_abs_sum[uc->kind]; | 468 | p_cur->f_rel[uc->kind] = p_cur->f_abs[uc->kind] / uc->pc->f_abs_sum[uc->kind]; |
480 | LOG (GNUNET_ERROR_TYPE_DEBUG, | 469 | LOG(GNUNET_ERROR_TYPE_DEBUG, |
481 | "Client has relative preference for %s for peer `%s' of %.3f\n", | 470 | "Client has relative preference for %s for peer `%s' of %.3f\n", |
482 | GNUNET_ATS_print_preference_type (uc->kind), | 471 | GNUNET_ATS_print_preference_type(uc->kind), |
483 | GNUNET_i2s (peer), | 472 | GNUNET_i2s(peer), |
484 | p_cur->f_rel[uc->kind]); | 473 | p_cur->f_rel[uc->kind]); |
485 | return GNUNET_OK; | 474 | return GNUNET_OK; |
486 | } | 475 | } |
487 | 476 | ||
@@ -494,8 +483,8 @@ update_rel_sum (void *cls, | |||
494 | * @return the result | 483 | * @return the result |
495 | */ | 484 | */ |
496 | static void | 485 | static void |
497 | recalculate_relative_preferences (struct PreferenceClient *c, | 486 | recalculate_relative_preferences(struct PreferenceClient *c, |
498 | enum GNUNET_ATS_PreferenceKind kind) | 487 | enum GNUNET_ATS_PreferenceKind kind) |
499 | { | 488 | { |
500 | struct UpdateContext uc; | 489 | struct UpdateContext uc; |
501 | 490 | ||
@@ -505,18 +494,18 @@ recalculate_relative_preferences (struct PreferenceClient *c, | |||
505 | c->f_abs_sum[kind] = 0.0; | 494 | c->f_abs_sum[kind] = 0.0; |
506 | 495 | ||
507 | /* For all peers: calculate sum of absolute preferences */ | 496 | /* For all peers: calculate sum of absolute preferences */ |
508 | GNUNET_CONTAINER_multipeermap_iterate (c->peer2pref, | 497 | GNUNET_CONTAINER_multipeermap_iterate(c->peer2pref, |
509 | &update_abs_sum, | 498 | &update_abs_sum, |
510 | &uc); | 499 | &uc); |
511 | LOG (GNUNET_ERROR_TYPE_DEBUG, | 500 | LOG(GNUNET_ERROR_TYPE_DEBUG, |
512 | "Client has sum of total preferences for %s of %.3f\n", | 501 | "Client has sum of total preferences for %s of %.3f\n", |
513 | GNUNET_ATS_print_preference_type (kind), | 502 | GNUNET_ATS_print_preference_type(kind), |
514 | c->f_abs_sum[kind]); | 503 | c->f_abs_sum[kind]); |
515 | 504 | ||
516 | /* For all peers: calculate relative preference */ | 505 | /* For all peers: calculate relative preference */ |
517 | GNUNET_CONTAINER_multipeermap_iterate (c->peer2pref, | 506 | GNUNET_CONTAINER_multipeermap_iterate(c->peer2pref, |
518 | &update_rel_sum, | 507 | &update_rel_sum, |
519 | &uc); | 508 | &uc); |
520 | } | 509 | } |
521 | 510 | ||
522 | 511 | ||
@@ -531,14 +520,14 @@ recalculate_relative_preferences (struct PreferenceClient *c, | |||
531 | * @param value a `struct PeerRelative`, unused | 520 | * @param value a `struct PeerRelative`, unused |
532 | */ | 521 | */ |
533 | static int | 522 | static int |
534 | update_iterator (void *cls, | 523 | update_iterator(void *cls, |
535 | const struct GNUNET_PeerIdentity *key, | 524 | const struct GNUNET_PeerIdentity *key, |
536 | void *value) | 525 | void *value) |
537 | { | 526 | { |
538 | enum GNUNET_ATS_PreferenceKind *kind = cls; | 527 | enum GNUNET_ATS_PreferenceKind *kind = cls; |
539 | 528 | ||
540 | update_relative_values_for_peer (key, | 529 | update_relative_values_for_peer(key, |
541 | *kind); | 530 | *kind); |
542 | return GNUNET_OK; | 531 | return GNUNET_OK; |
543 | } | 532 | } |
544 | 533 | ||
@@ -553,10 +542,10 @@ update_iterator (void *cls, | |||
553 | * @param score_abs the normalized score | 542 | * @param score_abs the normalized score |
554 | */ | 543 | */ |
555 | static void | 544 | static void |
556 | update_preference (struct GNUNET_SERVICE_Client *client, | 545 | update_preference(struct GNUNET_SERVICE_Client *client, |
557 | const struct GNUNET_PeerIdentity *peer, | 546 | const struct GNUNET_PeerIdentity *peer, |
558 | enum GNUNET_ATS_PreferenceKind kind, | 547 | enum GNUNET_ATS_PreferenceKind kind, |
559 | float score_abs) | 548 | float score_abs) |
560 | { | 549 | { |
561 | struct PreferenceClient *c_cur; | 550 | struct PreferenceClient *c_cur; |
562 | struct PreferencePeer *p_cur; | 551 | struct PreferencePeer *p_cur; |
@@ -564,15 +553,15 @@ update_preference (struct GNUNET_SERVICE_Client *client, | |||
564 | unsigned int i; | 553 | unsigned int i; |
565 | 554 | ||
566 | if (kind >= GNUNET_ATS_PREFERENCE_END) | 555 | if (kind >= GNUNET_ATS_PREFERENCE_END) |
567 | { | 556 | { |
568 | GNUNET_break(0); | 557 | GNUNET_break(0); |
569 | return; | 558 | return; |
570 | } | 559 | } |
571 | LOG (GNUNET_ERROR_TYPE_DEBUG, | 560 | LOG(GNUNET_ERROR_TYPE_DEBUG, |
572 | "Client changes preference for peer `%s' for `%s' to %.2f\n", | 561 | "Client changes preference for peer `%s' for `%s' to %.2f\n", |
573 | GNUNET_i2s (peer), | 562 | GNUNET_i2s(peer), |
574 | GNUNET_ATS_print_preference_type (kind), | 563 | GNUNET_ATS_print_preference_type(kind), |
575 | score_abs); | 564 | score_abs); |
576 | 565 | ||
577 | /* Find preference client */ | 566 | /* Find preference client */ |
578 | for (c_cur = pc_head; NULL != c_cur; c_cur = c_cur->next) | 567 | for (c_cur = pc_head; NULL != c_cur; c_cur = c_cur->next) |
@@ -580,66 +569,66 @@ update_preference (struct GNUNET_SERVICE_Client *client, | |||
580 | break; | 569 | break; |
581 | /* Not found: create new preference client */ | 570 | /* Not found: create new preference client */ |
582 | if (NULL == c_cur) | 571 | if (NULL == c_cur) |
583 | { | 572 | { |
584 | c_cur = GNUNET_new (struct PreferenceClient); | 573 | c_cur = GNUNET_new(struct PreferenceClient); |
585 | c_cur->client = client; | 574 | c_cur->client = client; |
586 | c_cur->peer2pref = GNUNET_CONTAINER_multipeermap_create (16, | 575 | c_cur->peer2pref = GNUNET_CONTAINER_multipeermap_create(16, |
587 | GNUNET_NO); | 576 | GNUNET_NO); |
588 | for (i = 0; i < GNUNET_ATS_PREFERENCE_END; i++) | 577 | for (i = 0; i < GNUNET_ATS_PREFERENCE_END; i++) |
589 | c_cur->f_abs_sum[i] = DEFAULT_ABS_PREFERENCE; | 578 | c_cur->f_abs_sum[i] = DEFAULT_ABS_PREFERENCE; |
590 | GNUNET_CONTAINER_DLL_insert (pc_head, | 579 | GNUNET_CONTAINER_DLL_insert(pc_head, |
591 | pc_tail, | 580 | pc_tail, |
592 | c_cur); | 581 | c_cur); |
593 | } | 582 | } |
594 | 583 | ||
595 | /* check global peer entry exists */ | 584 | /* check global peer entry exists */ |
596 | if (NULL == | 585 | if (NULL == |
597 | (r_cur = GNUNET_CONTAINER_multipeermap_get (preference_peers, | 586 | (r_cur = GNUNET_CONTAINER_multipeermap_get(preference_peers, |
598 | peer))) | 587 | peer))) |
599 | { | 588 | { |
600 | /* Create struct for peer */ | 589 | /* Create struct for peer */ |
601 | r_cur = GNUNET_new (struct PeerRelative); | 590 | r_cur = GNUNET_new(struct PeerRelative); |
602 | for (i = 0; i < GNUNET_ATS_PREFERENCE_END; i++) | 591 | for (i = 0; i < GNUNET_ATS_PREFERENCE_END; i++) |
603 | r_cur->f_rel[i] = DEFAULT_REL_PREFERENCE; | 592 | r_cur->f_rel[i] = DEFAULT_REL_PREFERENCE; |
604 | GNUNET_assert (GNUNET_OK == | 593 | GNUNET_assert(GNUNET_OK == |
605 | GNUNET_CONTAINER_multipeermap_put (preference_peers, | 594 | GNUNET_CONTAINER_multipeermap_put(preference_peers, |
606 | peer, | 595 | peer, |
607 | r_cur, | 596 | r_cur, |
608 | GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)); | 597 | GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)); |
609 | } | 598 | } |
610 | 599 | ||
611 | /* Find entry for peer */ | 600 | /* Find entry for peer */ |
612 | p_cur = GNUNET_CONTAINER_multipeermap_get (c_cur->peer2pref, | 601 | p_cur = GNUNET_CONTAINER_multipeermap_get(c_cur->peer2pref, |
613 | peer); | 602 | peer); |
614 | if (NULL == p_cur) | 603 | if (NULL == p_cur) |
615 | { | ||
616 | /* Not found: create new peer entry */ | ||
617 | p_cur = GNUNET_new (struct PreferencePeer); | ||
618 | for (i = 0; i < GNUNET_ATS_PREFERENCE_END; i++) | ||
619 | { | 604 | { |
620 | /* Default value per peer absolute preference for a preference*/ | 605 | /* Not found: create new peer entry */ |
621 | p_cur->f_abs[i] = DEFAULT_ABS_PREFERENCE; | 606 | p_cur = GNUNET_new(struct PreferencePeer); |
622 | /* Default value per peer relative preference for a quality */ | 607 | for (i = 0; i < GNUNET_ATS_PREFERENCE_END; i++) |
623 | p_cur->f_rel[i] = DEFAULT_REL_PREFERENCE; | 608 | { |
624 | } | 609 | /* Default value per peer absolute preference for a preference*/ |
625 | GNUNET_assert (GNUNET_YES == | 610 | p_cur->f_abs[i] = DEFAULT_ABS_PREFERENCE; |
626 | GNUNET_CONTAINER_multipeermap_put (c_cur->peer2pref, | 611 | /* Default value per peer relative preference for a quality */ |
612 | p_cur->f_rel[i] = DEFAULT_REL_PREFERENCE; | ||
613 | } | ||
614 | GNUNET_assert(GNUNET_YES == | ||
615 | GNUNET_CONTAINER_multipeermap_put(c_cur->peer2pref, | ||
627 | peer, | 616 | peer, |
628 | p_cur, | 617 | p_cur, |
629 | GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)); | 618 | GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)); |
630 | r_cur->num_clients++; | 619 | r_cur->num_clients++; |
631 | } | 620 | } |
632 | 621 | ||
633 | p_cur->f_abs[kind] += score_abs; | 622 | p_cur->f_abs[kind] += score_abs; |
634 | recalculate_relative_preferences (c_cur, kind); | 623 | recalculate_relative_preferences(c_cur, kind); |
635 | GNUNET_CONTAINER_multipeermap_iterate (preference_peers, | 624 | GNUNET_CONTAINER_multipeermap_iterate(preference_peers, |
636 | &update_iterator, | 625 | &update_iterator, |
637 | &kind); | 626 | &kind); |
638 | 627 | ||
639 | if (NULL == aging_task) | 628 | if (NULL == aging_task) |
640 | aging_task = GNUNET_SCHEDULER_add_delayed (PREF_AGING_INTERVAL, | 629 | aging_task = GNUNET_SCHEDULER_add_delayed(PREF_AGING_INTERVAL, |
641 | &preference_aging, | 630 | &preference_aging, |
642 | NULL); | 631 | NULL); |
643 | } | 632 | } |
644 | 633 | ||
645 | 634 | ||
@@ -650,28 +639,28 @@ update_preference (struct GNUNET_SERVICE_Client *client, | |||
650 | * @param msg the request message | 639 | * @param msg the request message |
651 | */ | 640 | */ |
652 | void | 641 | void |
653 | GAS_handle_preference_change (struct GNUNET_SERVICE_Client *client, | 642 | GAS_handle_preference_change(struct GNUNET_SERVICE_Client *client, |
654 | const struct ChangePreferenceMessage *msg) | 643 | const struct ChangePreferenceMessage *msg) |
655 | { | 644 | { |
656 | const struct PreferenceInformation *pi; | 645 | const struct PreferenceInformation *pi; |
657 | uint32_t nump; | 646 | uint32_t nump; |
658 | 647 | ||
659 | nump = ntohl (msg->num_preferences); | 648 | nump = ntohl(msg->num_preferences); |
660 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | 649 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, |
661 | "Received PREFERENCE_CHANGE message for peer `%s'\n", | 650 | "Received PREFERENCE_CHANGE message for peer `%s'\n", |
662 | GNUNET_i2s (&msg->peer)); | 651 | GNUNET_i2s(&msg->peer)); |
663 | GNUNET_STATISTICS_update (GSA_stats, | 652 | GNUNET_STATISTICS_update(GSA_stats, |
664 | "# preference change requests processed", | 653 | "# preference change requests processed", |
665 | 1, | 654 | 1, |
666 | GNUNET_NO); | 655 | GNUNET_NO); |
667 | pi = (const struct PreferenceInformation *) &msg[1]; | 656 | pi = (const struct PreferenceInformation *)&msg[1]; |
668 | GAS_plugin_solver_lock (); | 657 | GAS_plugin_solver_lock(); |
669 | for (uint32_t i = 0; i < nump; i++) | 658 | for (uint32_t i = 0; i < nump; i++) |
670 | update_preference (client, | 659 | update_preference(client, |
671 | &msg->peer, | 660 | &msg->peer, |
672 | (enum GNUNET_ATS_PreferenceKind) ntohl (pi[i].preference_kind), | 661 | (enum GNUNET_ATS_PreferenceKind)ntohl(pi[i].preference_kind), |
673 | pi[i].preference_value); | 662 | pi[i].preference_value); |
674 | GAS_plugin_solver_unlock (); | 663 | GAS_plugin_solver_unlock(); |
675 | } | 664 | } |
676 | 665 | ||
677 | 666 | ||
@@ -679,12 +668,12 @@ GAS_handle_preference_change (struct GNUNET_SERVICE_Client *client, | |||
679 | * Initialize preferences subsystem. | 668 | * Initialize preferences subsystem. |
680 | */ | 669 | */ |
681 | void | 670 | void |
682 | GAS_preference_init () | 671 | GAS_preference_init() |
683 | { | 672 | { |
684 | unsigned int i; | 673 | unsigned int i; |
685 | 674 | ||
686 | preference_peers = GNUNET_CONTAINER_multipeermap_create (16, | 675 | preference_peers = GNUNET_CONTAINER_multipeermap_create(16, |
687 | GNUNET_NO); | 676 | GNUNET_NO); |
688 | for (i = 0; i < GNUNET_ATS_PREFERENCE_END; i++) | 677 | for (i = 0; i < GNUNET_ATS_PREFERENCE_END; i++) |
689 | defvalues.f_rel[i] = DEFAULT_REL_PREFERENCE; | 678 | defvalues.f_rel[i] = DEFAULT_REL_PREFERENCE; |
690 | } | 679 | } |
@@ -694,34 +683,33 @@ GAS_preference_init () | |||
694 | * Shutdown preferences subsystem. | 683 | * Shutdown preferences subsystem. |
695 | */ | 684 | */ |
696 | void | 685 | void |
697 | GAS_preference_done () | 686 | GAS_preference_done() |
698 | { | 687 | { |
699 | struct PreferenceClient *pc; | 688 | struct PreferenceClient *pc; |
700 | struct PreferenceClient *next_pc; | 689 | struct PreferenceClient *next_pc; |
701 | 690 | ||
702 | if (NULL != aging_task) | 691 | if (NULL != aging_task) |
703 | { | 692 | { |
704 | GNUNET_SCHEDULER_cancel (aging_task); | 693 | GNUNET_SCHEDULER_cancel(aging_task); |
705 | aging_task = NULL; | 694 | aging_task = NULL; |
706 | } | 695 | } |
707 | next_pc = pc_head; | 696 | next_pc = pc_head; |
708 | while (NULL != (pc = next_pc)) | 697 | while (NULL != (pc = next_pc)) |
709 | { | 698 | { |
710 | next_pc = pc->next; | 699 | next_pc = pc->next; |
711 | GNUNET_CONTAINER_DLL_remove (pc_head, | 700 | GNUNET_CONTAINER_DLL_remove(pc_head, |
712 | pc_tail, | 701 | pc_tail, |
713 | pc); | 702 | pc); |
714 | GNUNET_CONTAINER_multipeermap_iterate (pc->peer2pref, | 703 | GNUNET_CONTAINER_multipeermap_iterate(pc->peer2pref, |
715 | &free_preference, | 704 | &free_preference, |
716 | pc); | 705 | pc); |
717 | GNUNET_CONTAINER_multipeermap_destroy (pc->peer2pref); | 706 | GNUNET_CONTAINER_multipeermap_destroy(pc->peer2pref); |
718 | GNUNET_free (pc); | 707 | GNUNET_free(pc); |
719 | } | 708 | } |
720 | GNUNET_CONTAINER_multipeermap_iterate (preference_peers, | 709 | GNUNET_CONTAINER_multipeermap_iterate(preference_peers, |
721 | &free_peer, | 710 | &free_peer, |
722 | NULL); | 711 | NULL); |
723 | GNUNET_CONTAINER_multipeermap_destroy (preference_peers); | 712 | GNUNET_CONTAINER_multipeermap_destroy(preference_peers); |
724 | |||
725 | } | 713 | } |
726 | 714 | ||
727 | 715 | ||
@@ -735,17 +723,17 @@ GAS_preference_done () | |||
735 | * default preferences if peer does not exist | 723 | * default preferences if peer does not exist |
736 | */ | 724 | */ |
737 | const double * | 725 | const double * |
738 | GAS_preference_get_by_peer (void *cls, | 726 | GAS_preference_get_by_peer(void *cls, |
739 | const struct GNUNET_PeerIdentity *id) | 727 | const struct GNUNET_PeerIdentity *id) |
740 | { | 728 | { |
741 | struct PeerRelative *rp; | 729 | struct PeerRelative *rp; |
742 | 730 | ||
743 | if (NULL == | 731 | if (NULL == |
744 | (rp = GNUNET_CONTAINER_multipeermap_get (preference_peers, | 732 | (rp = GNUNET_CONTAINER_multipeermap_get(preference_peers, |
745 | id))) | 733 | id))) |
746 | { | 734 | { |
747 | return defvalues.f_rel; | 735 | return defvalues.f_rel; |
748 | } | 736 | } |
749 | return rp->f_rel; | 737 | return rp->f_rel; |
750 | } | 738 | } |
751 | 739 | ||
@@ -756,7 +744,7 @@ GAS_preference_get_by_peer (void *cls, | |||
756 | * @param client the client | 744 | * @param client the client |
757 | */ | 745 | */ |
758 | void | 746 | void |
759 | GAS_preference_client_disconnect (struct GNUNET_SERVICE_Client *client) | 747 | GAS_preference_client_disconnect(struct GNUNET_SERVICE_Client *client) |
760 | { | 748 | { |
761 | struct PreferenceClient *c_cur; | 749 | struct PreferenceClient *c_cur; |
762 | 750 | ||
@@ -765,14 +753,14 @@ GAS_preference_client_disconnect (struct GNUNET_SERVICE_Client *client) | |||
765 | break; | 753 | break; |
766 | if (NULL == c_cur) | 754 | if (NULL == c_cur) |
767 | return; | 755 | return; |
768 | GNUNET_CONTAINER_DLL_remove (pc_head, | 756 | GNUNET_CONTAINER_DLL_remove(pc_head, |
769 | pc_tail, | 757 | pc_tail, |
770 | c_cur); | 758 | c_cur); |
771 | GNUNET_CONTAINER_multipeermap_iterate (c_cur->peer2pref, | 759 | GNUNET_CONTAINER_multipeermap_iterate(c_cur->peer2pref, |
772 | &free_preference, | 760 | &free_preference, |
773 | c_cur); | 761 | c_cur); |
774 | GNUNET_CONTAINER_multipeermap_destroy (c_cur->peer2pref); | 762 | GNUNET_CONTAINER_multipeermap_destroy(c_cur->peer2pref); |
775 | GNUNET_free (c_cur); | 763 | GNUNET_free(c_cur); |
776 | } | 764 | } |
777 | 765 | ||
778 | 766 | ||