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