2 This file is part of GNUnet.
3 (C) 2011 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file ats/gnunet-service-ats_addresses_simplistic.h
23 * @brief ats simplistic ressource assignment
24 * @author Matthias Wachs
25 * @author Christian Grothoff
28 #include "gnunet_util_lib.h"
29 #include "gnunet-service-ats_addresses.h"
30 #include "gnunet_statistics_service.h"
32 #define LOG(kind,...) GNUNET_log_from (kind, "ats-simplistic",__VA_ARGS__)
35 * ATS simplistic solver
37 * Assigns in and outbound bandwidth equally for all addresses in specific
38 * network type (WAN, LAN) based on configured in and outbound quota for this
41 * For each peer only a single is selected and marked as "active" in the address
46 * You have the networks WAN and LAN and quotas
47 * WAN_TOTAL_IN, WAN_TOTAL_OUT
48 * LAN_TOTAL_IN, LAN_TOTAL_OUT
50 * If you have x addresses in the network segment LAN, the quotas are
51 * QUOTA_PER_ADDRESS = LAN_TOTAL_OUT / x
53 * Quotas are automatically recalculated and reported back when addresses are
60 * A handle for the simplistic solver
62 struct GAS_SIMPLISTIC_Handle
68 struct GNUNET_STATISTICS_Handle *stats;
71 * Total number of addresses for solver
73 unsigned int total_addresses;
76 * Number of active addresses for solver
78 unsigned int active_addresses;
83 struct Network *network_entries;
88 unsigned int networks;
93 GAS_bandwidth_changed_cb bw_changed;
100 struct PreferenceClient *pc_head;
101 struct PreferenceClient *pc_tail;
112 * Network description
117 * Total inbound quota
120 unsigned long long total_quota_in;
123 * Total outbound quota
126 unsigned long long total_quota_out;
129 * Number of active addresses for this network
131 unsigned int active_addresses;
134 * Number of total addresses for this network
136 unsigned int total_addresses;
139 * String for statistics total addresses
144 * String for statistics active addresses
148 struct AddressWrapper *head;
149 struct AddressWrapper *tail;
152 struct AddressWrapper
154 struct AddressWrapper *next;
155 struct AddressWrapper *prev;
157 struct ATS_Address *addr;
161 struct PreferencePeer
163 struct PreferencePeer *next;
164 struct PreferencePeer *prev;
165 struct GNUNET_PeerIdentity id;
167 float f[GNUNET_ATS_PreferenceCount];
168 float f_rel[GNUNET_ATS_PreferenceCount];
172 struct PreferenceClient
174 struct PreferenceClient *prev;
175 struct PreferenceClient *next;
178 float f_total[GNUNET_ATS_PreferenceCount];
180 struct PreferencePeer *p_head;
181 struct PreferencePeer *p_tail;
186 * Get the prefered address for a specific peer
188 * @param solver the solver handle
189 * @param addresses the address hashmap containing all addresses
190 * @param peer the identity of the peer
192 const struct ATS_Address *
193 GAS_simplistic_get_preferred_address (void *solver,
194 struct GNUNET_CONTAINER_MultiHashMap * addresses,
195 const struct GNUNET_PeerIdentity *peer);
198 * Init the simplistic problem solving component
201 * network[i] contains the network type as type GNUNET_ATS_NetworkType[i]
202 * out_quota[i] contains outbound quota for network type i
203 * in_quota[i] contains inbound quota for network type i
206 * network = {GNUNET_ATS_NET_UNSPECIFIED, GNUNET_ATS_NET_LOOPBACK, GNUNET_ATS_NET_LAN, GNUNET_ATS_NET_WAN, GNUNET_ATS_NET_WLAN}
207 * network[2] == GNUNET_ATS_NET_LAN
208 * out_quota[2] == 65353
209 * in_quota[2] == 65353
211 * @param cfg configuration handle
212 * @param stats the GNUNET_STATISTICS handle
213 * @param network array of GNUNET_ATS_NetworkType with length dest_length
214 * @param out_quota array of outbound quotas
215 * @param in_quota array of outbound quota
216 * @param dest_length array length for quota arrays
217 * @param bw_changed_cb callback for changed bandwidth amounts
218 * @param bw_changed_cb_cls cls for callback
219 * @return handle for the solver on success, NULL on fail
222 GAS_simplistic_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
223 const struct GNUNET_STATISTICS_Handle *stats,
225 unsigned long long *out_quota,
226 unsigned long long *in_quota,
228 GAS_bandwidth_changed_cb bw_changed_cb,
229 void *bw_changed_cb_cls)
232 struct GAS_SIMPLISTIC_Handle *s = GNUNET_malloc (sizeof (struct GAS_SIMPLISTIC_Handle));
233 struct Network * cur;
234 char * net_str[GNUNET_ATS_NetworkTypeCount] = GNUNET_ATS_NetworkTypeString;
237 s->stats = (struct GNUNET_STATISTICS_Handle *) stats;
238 s->bw_changed = bw_changed_cb;
239 s->bw_changed_cls = bw_changed_cb_cls;
240 s->networks = dest_length;
241 s->network_entries = GNUNET_malloc (dest_length * sizeof (struct Network));
242 s->active_addresses = 0;
243 s->total_addresses = 0;
245 for (c = 0; c < dest_length; c++)
247 cur = &s->network_entries[c];
248 cur->total_addresses = 0;
249 cur->active_addresses = 0;
250 cur->type = network[c];
251 cur->total_quota_in = in_quota[c];
252 cur->total_quota_out = out_quota[c];
253 cur->desc = net_str[c];
254 GNUNET_asprintf (&cur->stat_total, "# ATS addresses %s total", cur->desc);
255 GNUNET_asprintf (&cur->stat_active, "# ATS active addresses %s total", cur->desc);
261 * Shutdown the simplistic problem solving component
263 * @param solver the respective handle to shutdown
266 GAS_simplistic_done (void *solver)
268 struct GAS_SIMPLISTIC_Handle *s = solver;
269 struct PreferenceClient *pc;
270 struct PreferenceClient *next_pc;
271 struct PreferencePeer *p;
272 struct PreferencePeer *next_p;
273 struct AddressWrapper *cur;
274 struct AddressWrapper *next;
276 GNUNET_assert (s != NULL);
278 for (c = 0; c < s->networks; c++)
280 if (s->network_entries[c].total_addresses > 0)
282 LOG (GNUNET_ERROR_TYPE_ERROR,
283 "Had %u addresses for network `%s' not deleted during shutdown\n",
284 s->network_entries[c].total_addresses,
285 s->network_entries[c].desc);
289 if (s->network_entries[c].active_addresses > 0)
291 LOG (GNUNET_ERROR_TYPE_ERROR,
292 "Had %u active addresses for network `%s' not deleted during shutdown\n",
293 s->network_entries[c].active_addresses,
294 s->network_entries[c].desc);
298 next = s->network_entries[c].head;
299 while (NULL != (cur = next))
302 GNUNET_CONTAINER_DLL_remove (s->network_entries[c].head,
303 s->network_entries[c].tail,
307 GNUNET_free (s->network_entries[c].stat_total);
308 GNUNET_free (s->network_entries[c].stat_active);
310 if (s->total_addresses > 0)
312 LOG (GNUNET_ERROR_TYPE_ERROR,
313 "Had %u addresses not deleted during shutdown\n",
317 if (s->active_addresses > 0)
319 LOG (GNUNET_ERROR_TYPE_ERROR,
320 "Had %u active addresses not deleted during shutdown\n",
321 s->active_addresses);
324 GNUNET_free (s->network_entries);
326 next_pc = s->pc_head;
327 while (NULL != (pc = next_pc))
330 GNUNET_CONTAINER_DLL_remove (s->pc_head, s->pc_tail, pc);
332 while (NULL != (p = next_p))
335 GNUNET_CONTAINER_DLL_remove (pc->p_head, pc->p_tail, p);
343 static unsigned long long int
344 calculate_new_quota (unsigned long long int total,
345 unsigned int addresses)
347 return (total / addresses);
351 * Test if bandwidth is available in this network
353 * @param s the solver handle
354 * @param net the network type to update
355 * @return GNUNET_YES or GNUNET_NO
359 bw_available_in_network (struct Network *net)
361 unsigned int na = net->active_addresses + 1;
362 uint32_t min_bw = ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
363 if (((net->total_quota_in / na) > min_bw) &&
364 ((net->total_quota_out / na) > min_bw))
366 LOG (GNUNET_ERROR_TYPE_DEBUG,
367 "Enough bandwidth available for %u active addresses in network `%s'\n",
373 LOG (GNUNET_ERROR_TYPE_DEBUG,
374 "Not enough bandwidth available for %u active addresses in network `%s'\n",
381 * Update the quotas for a network type
383 * @param s the solver handle
384 * @param net the network type to update
385 * @param address_except address excluded from notifcation, since we suggest
389 update_quota_per_network (struct GAS_SIMPLISTIC_Handle *s,
391 struct ATS_Address *address_except)
393 unsigned long long quota_in = 0;
394 unsigned long long quota_out = 0;
395 struct AddressWrapper *cur;
397 LOG (GNUNET_ERROR_TYPE_DEBUG,
398 "Recalculate quota for network type `%s' for %u addresses (in/out): %llu/%llu \n",
399 net->desc, net->active_addresses, quota_in, quota_out);
401 if (net->active_addresses == 0)
402 return; /* no addresses to update */
404 quota_in = calculate_new_quota (net->total_quota_in, net->active_addresses);
405 quota_out = calculate_new_quota (net->total_quota_out, net->active_addresses);
407 LOG (GNUNET_ERROR_TYPE_DEBUG,
408 "New per address quota for network type `%s' for %u addresses (in/out): %llu/%llu \n",
409 net->desc, net->active_addresses, quota_in, quota_out);
414 /* Compare to current bandwidth assigned */
415 if ((quota_in != ntohl(cur->addr->assigned_bw_in.value__)) ||
416 (quota_out != ntohl(cur->addr->assigned_bw_out.value__)))
418 cur->addr->assigned_bw_in.value__ = htonl (quota_in);
419 cur->addr->assigned_bw_out.value__ = htonl (quota_out);
420 /* Notify on change */
421 if ((GNUNET_YES == cur->addr->active) && (cur->addr != address_except))
422 s->bw_changed (s->bw_changed_cls, cur->addr);
429 addresse_increment (struct GAS_SIMPLISTIC_Handle *s,
434 if (GNUNET_YES == total)
436 s->total_addresses ++;
437 net->total_addresses ++;
438 GNUNET_STATISTICS_update (s->stats, "# ATS addresses total", 1, GNUNET_NO);
439 GNUNET_STATISTICS_update (s->stats, net->stat_total, 1, GNUNET_NO);
441 if (GNUNET_YES == active)
443 net->active_addresses ++;
444 s->active_addresses ++;
445 GNUNET_STATISTICS_update (s->stats, "# ATS active addresses total", 1, GNUNET_NO);
446 GNUNET_STATISTICS_update (s->stats, net->stat_active, 1, GNUNET_NO);
452 addresse_decrement (struct GAS_SIMPLISTIC_Handle *s,
458 if (GNUNET_YES == total)
460 if (s->total_addresses < 1)
467 s->total_addresses --;
468 GNUNET_STATISTICS_update (s->stats, "# ATS addresses total", -1, GNUNET_NO);
470 if (net->total_addresses < 1)
477 net->total_addresses --;
478 GNUNET_STATISTICS_update (s->stats, net->stat_total, -1, GNUNET_NO);
482 if (GNUNET_YES == active)
484 if (net->active_addresses < 1)
491 net->active_addresses --;
492 GNUNET_STATISTICS_update (s->stats, net->stat_active, -1, GNUNET_NO);
494 if (s->active_addresses < 1)
501 s->active_addresses --;
502 GNUNET_STATISTICS_update (s->stats, "# ATS addresses total", -1, GNUNET_NO);
510 * Add a single address to the solve
512 * @param solver the solver Handle
513 * @param addresses the address hashmap containing all addresses
514 * @param address the address to add
517 GAS_simplistic_address_add (void *solver, struct GNUNET_CONTAINER_MultiHashMap * addresses, struct ATS_Address *address)
519 struct GAS_SIMPLISTIC_Handle *s = solver;
520 struct Network *net = NULL;
521 struct AddressWrapper *aw = NULL;
522 GNUNET_assert (NULL != s);
524 for (c = 0; c < s->networks; c++)
526 net = &s->network_entries[c];
527 if (address->atsp_network_type == net->type)
536 aw = GNUNET_malloc (sizeof (struct AddressWrapper));
538 GNUNET_CONTAINER_DLL_insert (net->head, net->tail, aw);
539 addresse_increment (s, net, GNUNET_YES, GNUNET_NO);
540 aw->addr->solver_information = net;
543 LOG (GNUNET_ERROR_TYPE_DEBUG, "After adding address now total %u and active %u addresses in network `%s'\n",
544 net->total_addresses,
545 net->active_addresses,
550 * Remove an address from the solver
552 * @param solver the solver handle
553 * @param addresses the address hashmap containing all addresses
554 * @param address the address to remove
555 * @param session_only delete only session not whole address
558 GAS_simplistic_address_delete (void *solver,
559 struct GNUNET_CONTAINER_MultiHashMap * addresses,
560 struct ATS_Address *address, int session_only)
562 struct GAS_SIMPLISTIC_Handle *s = solver;
564 struct AddressWrapper *aw;
566 /* Remove an adress completely, we have to:
567 * - Remove from specific network
568 * - Decrease number of total addresses
570 * - decrease number of active addreses
574 net = (struct Network *) address->solver_information;
576 if (GNUNET_NO == session_only)
578 LOG (GNUNET_ERROR_TYPE_DEBUG, "Deleting %s address %p for peer `%s' from network `%s' (total: %u/ active: %u)\n",
579 (GNUNET_NO == address->active) ? "inactive" : "active",
580 address, GNUNET_i2s (&address->peer),
581 net->desc, net->total_addresses, net->active_addresses);
584 addresse_decrement (s, net, GNUNET_YES, GNUNET_NO);
585 for (aw = net->head; NULL != aw; aw = aw->next)
587 if (aw->addr == address)
595 GNUNET_CONTAINER_DLL_remove (net->head, net->tail, aw);
600 /* Remove session only: remove if active and update */
601 LOG (GNUNET_ERROR_TYPE_DEBUG, "Deleting %s session %p for peer `%s' from network `%s' (total: %u/ active: %u)\n",
602 (GNUNET_NO == address->active) ? "inactive" : "active",
603 address, GNUNET_i2s (&address->peer),
604 net->desc, net->total_addresses, net->active_addresses);
607 if (GNUNET_YES == address->active)
609 /* Address was active, remove from network and update quotas*/
610 address->active = GNUNET_NO;
611 if (GNUNET_SYSERR == addresse_decrement (s, net, GNUNET_NO, GNUNET_YES))
613 update_quota_per_network (s, net, NULL);
615 LOG (GNUNET_ERROR_TYPE_DEBUG, "After deleting address now total %u and active %u addresses in network `%s'\n",
616 net->total_addresses,
617 net->active_addresses,
622 static struct Network *
623 find_network (struct GAS_SIMPLISTIC_Handle *s, uint32_t type)
626 for (c = 0 ; c < s->networks; c++)
628 if (s->network_entries[c].type == type)
629 return &s->network_entries[c];
635 * Updates a single address in the solve
637 * @param solver the solver Handle
638 * @param addresses the address hashmap containing all addresses
639 * @param address the update address
640 * @param session the new session (if changed otherwise current)
641 * @param in_use the new address in use state (if changed otherwise current)
642 * @param atsi the latest ATS information
643 * @param atsi_count the atsi count
646 GAS_simplistic_address_update (void *solver,
647 struct GNUNET_CONTAINER_MultiHashMap *addresses,
648 struct ATS_Address *address,
651 const struct GNUNET_ATS_Information *atsi,
654 struct ATS_Address *new;
655 struct GAS_SIMPLISTIC_Handle *s = (struct GAS_SIMPLISTIC_Handle *) solver;
659 int save_active = GNUNET_NO;
660 struct Network *new_net = NULL;
661 for (i = 0; i < atsi_count; i++)
663 type = ntohl (atsi[i].type);
664 value = ntohl (atsi[i].value);
667 case GNUNET_ATS_UTILIZATION_UP:
668 //if (address->atsp_utilization_out.value__ != atsi[i].value)
671 case GNUNET_ATS_UTILIZATION_DOWN:
672 //if (address->atsp_utilization_in.value__ != atsi[i].value)
675 case GNUNET_ATS_QUALITY_NET_DELAY:
676 //if (address->atsp_latency.rel_value != value)
679 case GNUNET_ATS_QUALITY_NET_DISTANCE:
680 //if (address->atsp_distance != value)
683 case GNUNET_ATS_COST_WAN:
684 //if (address->atsp_cost_wan != value)
687 case GNUNET_ATS_COST_LAN:
688 //if (address->atsp_cost_lan != value)
691 case GNUNET_ATS_COST_WLAN:
692 //if (address->atsp_cost_wlan != value)
695 case GNUNET_ATS_NETWORK_TYPE:
696 if (address->atsp_network_type != value)
699 LOG (GNUNET_ERROR_TYPE_DEBUG, "Network type changed, moving %s address from `%s' to `%s'\n",
700 (GNUNET_YES == address->active) ? "active" : "inactive",
701 GNUNET_ATS_print_network_type(address->atsp_network_type),
702 GNUNET_ATS_print_network_type(value));
704 save_active = address->active;
705 /* remove from old network */
706 GAS_simplistic_address_delete (solver, addresses, address, GNUNET_NO);
708 /* set new network type */
709 address->atsp_network_type = value;
710 new_net = find_network (solver, value);
711 address->solver_information = new_net;
712 if (address->solver_information == NULL)
715 address->atsp_network_type = GNUNET_ATS_NET_UNSPECIFIED;
719 /* Add to new network and update*/
720 GAS_simplistic_address_add (solver, addresses, address);
721 if (GNUNET_YES == save_active)
723 /* check if bandwidth available in new network */
724 if (GNUNET_YES == (bw_available_in_network (new_net)))
726 /* Suggest updated address */
727 address->active = GNUNET_YES;
728 addresse_increment (s, new_net, GNUNET_NO, GNUNET_YES);
729 update_quota_per_network (solver, new_net, NULL);
733 LOG (GNUNET_ERROR_TYPE_DEBUG, "Not enough bandwidth in new network, suggesting alternative address ..\n");
735 /* Set old address to zero bw */
736 address->assigned_bw_in = GNUNET_BANDWIDTH_value_init (0);
737 address->assigned_bw_out = GNUNET_BANDWIDTH_value_init (0);
738 s->bw_changed (s->bw_changed_cls, address);
740 /* Find new address to suggest since no bandwidth in network*/
741 new = (struct ATS_Address *) GAS_simplistic_get_preferred_address (s, addresses, &address->peer);
744 /* Have an alternative address to suggest */
745 s->bw_changed (s->bw_changed_cls, new);
752 case GNUNET_ATS_ARRAY_TERMINATOR:
755 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
756 "Received unsupported ATS type %u\n", type);
763 if (address->session_id != session)
765 LOG (GNUNET_ERROR_TYPE_DEBUG,
766 "Session changed from %u to %u\n", address->session_id, session);
767 address->session_id = session;
769 if (address->used != in_use)
771 LOG (GNUNET_ERROR_TYPE_DEBUG,
772 "Usage changed from %u to %u\n", address->used, in_use);
773 address->used = in_use;
781 * Find a "good" address to use for a peer. If we already have an existing
782 * address, we stick to it. Otherwise, we pick by lowest distance and then
785 * @param cls the 'struct ATS_Address**' where we store the result
787 * @param value another 'struct ATS_Address*' to consider using
788 * @return GNUNET_OK (continue to iterate)
791 find_address_it (void *cls, const struct GNUNET_HashCode * key, void *value)
793 struct ATS_Address **previous_p = cls;
794 struct ATS_Address *current = (struct ATS_Address *) value;
795 struct ATS_Address *previous = *previous_p;
796 struct GNUNET_TIME_Absolute now;
797 struct Network *net = (struct Network *) current->solver_information;
799 now = GNUNET_TIME_absolute_get();
801 if (current->blocked_until.abs_value == GNUNET_TIME_absolute_max (now, current->blocked_until).abs_value)
803 /* This address is blocked for suggestion */
804 LOG (GNUNET_ERROR_TYPE_DEBUG,
805 "Address %p blocked for suggestion for %llu ms \n",
807 GNUNET_TIME_absolute_get_difference(now, current->blocked_until).rel_value);
811 if (GNUNET_NO == bw_available_in_network (net))
812 return GNUNET_OK; /* There's no bandwidth available in this network */
814 if (NULL != previous)
816 if ((0 == strcmp (previous->plugin, "tcp")) &&
817 (0 == strcmp (current->plugin, "tcp")))
819 if ((0 != previous->addr_len) &&
820 (0 == current->addr_len))
822 /* saved address was an outbound address, but we have an inbound address */
823 *previous_p = current;
826 if (0 == previous->addr_len)
828 /* saved address was an inbound address, so do not overwrite */
834 if (NULL == previous)
836 *previous_p = current;
839 if ((ntohl (previous->assigned_bw_in.value__) == 0) &&
840 (ntohl (current->assigned_bw_in.value__) > 0))
842 /* stick to existing connection */
843 *previous_p = current;
846 if (previous->atsp_distance > current->atsp_distance)
848 /* user shorter distance */
849 *previous_p = current;
852 if (previous->atsp_latency.rel_value > current->atsp_latency.rel_value)
854 /* user lower latency */
855 *previous_p = current;
863 find_active_address_it (void *cls, const struct GNUNET_HashCode * key, void *value)
865 struct ATS_Address * dest = (struct ATS_Address *) (*(struct ATS_Address **)cls);
866 struct ATS_Address * aa = (struct ATS_Address *) value;
868 if (GNUNET_YES == aa->active)
872 /* should never happen */
873 LOG (GNUNET_ERROR_TYPE_ERROR, "Multiple active addresses for peer `%s'\n", GNUNET_i2s (&aa->peer));
882 static struct ATS_Address *
883 find_active_address (void *solver,
884 struct GNUNET_CONTAINER_MultiHashMap * addresses,
885 const struct GNUNET_PeerIdentity *peer)
887 struct ATS_Address * dest = NULL;
889 GNUNET_CONTAINER_multihashmap_get_multiple(addresses,
891 &find_active_address_it, &dest);
896 * Get the prefered address for a specific peer
898 * @param solver the solver handle
899 * @param addresses the address hashmap containing all addresses
900 * @param peer the identity of the peer
902 const struct ATS_Address *
903 GAS_simplistic_get_preferred_address (void *solver,
904 struct GNUNET_CONTAINER_MultiHashMap * addresses,
905 const struct GNUNET_PeerIdentity *peer)
907 struct GAS_SIMPLISTIC_Handle *s = solver;
908 struct Network *net_prev;
909 struct Network *net_cur;
910 struct ATS_Address *cur;
911 struct ATS_Address *prev;
913 GNUNET_assert (s != NULL);
915 /* Get address with: stick to current address, lower distance, lower latency */
916 GNUNET_CONTAINER_multihashmap_get_multiple (addresses, &peer->hashPubKey,
917 &find_address_it, &cur);
920 LOG (GNUNET_ERROR_TYPE_DEBUG, "Cannot suggest address for peer `%s'\n", GNUNET_i2s (peer));
924 LOG (GNUNET_ERROR_TYPE_DEBUG, "Suggesting %s address %p for peer `%s'\n",
925 (GNUNET_NO == cur->active) ? "inactive" : "active",
926 cur, GNUNET_i2s (peer));
927 net_cur = (struct Network *) cur->solver_information;
928 if (GNUNET_YES == cur->active)
930 /* This address was selected previously, so no need to update quotas */
934 /* This address was not active, so we have to:
936 * - mark previous active address as not active
937 * - update quota for previous address network
938 * - update quota for this address network
941 prev = find_active_address (s, addresses, peer);
944 net_prev = (struct Network *) prev->solver_information;
945 prev->active = GNUNET_NO; /* No active any longer */
946 prev->assigned_bw_in = GNUNET_BANDWIDTH_value_init (0); /* no bw assigned */
947 prev->assigned_bw_out = GNUNET_BANDWIDTH_value_init (0); /* no bw assigned */
948 s->bw_changed (s->bw_changed_cls, prev); /* notify about bw change, REQUIRED? */
949 if (GNUNET_SYSERR == addresse_decrement (s, net_prev, GNUNET_NO, GNUNET_YES))
951 update_quota_per_network (s, net_prev, NULL);
954 if (GNUNET_NO == (bw_available_in_network (cur->solver_information)))
956 GNUNET_break (0); /* This should never happen*/
960 cur->active = GNUNET_YES;
961 addresse_increment(s, net_cur, GNUNET_NO, GNUNET_YES);
962 update_quota_per_network (s, net_cur, cur);
968 * Changes the preferences for a peer in the problem
970 * @param solver the solver handle
971 * @param client the client with this preference
972 * @param peer the peer to change the preference for
973 * @param kind the kind to change the preference
974 * @param score the score
977 GAS_simplistic_address_change_preference (void *solver,
979 const struct GNUNET_PeerIdentity *peer,
980 enum GNUNET_ATS_PreferenceKind kind,
983 struct GAS_SIMPLISTIC_Handle *s = solver;
984 struct PreferenceClient *cur;
985 struct PreferencePeer *p;
988 GNUNET_assert (NULL != solver);
989 GNUNET_assert (NULL != client);
990 GNUNET_assert (NULL != peer);
997 * Set of peers p_i in P
998 * Set of preference kinds k
999 * A preference value f_k_p_i with an unknown range
1002 * A client specific relative preference f_p_i_rel [1..2] for all peers
1004 * For every client c
1006 * For every preference kind k:
1008 * We remember for the preference f_p_i for each peer p_i.
1009 * We have a default preference value f_p_i = 0
1010 * We have a sum of all preferences f_t = sum (f_p_i)
1011 * So we can calculate a relative preference value fr_p_i:
1013 * f_k_p_i_rel = (f_t + f_p_i) / f_t
1014 * f_k_p_i_rel = [1..2]
1016 * f_p_i_rel = sum (f_k_p_i_rel) / #k
1021 LOG (GNUNET_ERROR_TYPE_DEBUG, "Client %p changes preference for peer `%s' %s %f\n",
1024 GNUNET_ATS_print_preference_type (kind),
1027 if (kind >= GNUNET_ATS_PreferenceCount)
1033 for (cur = s->pc_head; NULL != cur; cur = cur->next)
1035 if (client == cur->client)
1040 cur = GNUNET_malloc (sizeof (struct PreferenceClient));
1041 cur->client = client;
1042 GNUNET_CONTAINER_DLL_insert (s->pc_head, s->pc_tail, cur);
1045 for (p = cur->p_head; NULL != p; p = p->next)
1046 if (0 == memcmp (&p->id, peer, sizeof (p->id)))
1051 /* Add a new peer entry */
1052 p = GNUNET_malloc (sizeof (struct PreferencePeer));
1054 for (i = 0; i < GNUNET_ATS_PreferenceCount; i++)
1059 GNUNET_CONTAINER_DLL_insert (cur->p_head, cur->p_tail, p);
1063 case GNUNET_ATS_PREFERENCE_BANDWIDTH:
1064 case GNUNET_ATS_PREFERENCE_LATENCY:
1065 p->f[kind] = (p->f[kind] + score) / 2;
1067 case GNUNET_ATS_PREFERENCE_END:
1072 /* Recalcalculate total preference for kind*/
1073 cur->f_total[kind] = 0;
1074 for (p = cur->p_head; NULL != p; p = p->next)
1075 cur->f_total[kind] += p->f[kind];
1077 LOG (GNUNET_ERROR_TYPE_DEBUG, "Client %p has total preference for %s of %f\n",
1079 GNUNET_ATS_print_preference_type (kind),
1080 cur->f_total[kind]);
1082 /* Recalcalculate relative preference */
1083 for (p = cur->p_head; NULL != p; p = p->next)
1085 /* Calculate relative preference for specific kind */
1086 p->f_rel[kind] = (cur->f_total[kind] + p->f[kind]) / cur->f_total[kind];
1087 LOG (GNUNET_ERROR_TYPE_DEBUG, "Client %p: peer `%s' has relative preference for %s of %f\n",
1089 GNUNET_i2s (&p->id),
1090 GNUNET_ATS_print_preference_type (kind),
1093 /* Calculate peer relative preference
1094 * Start with i = 1 to exclude terminator */
1096 for (i = 1; i < GNUNET_ATS_PreferenceCount; i ++)
1098 p->f_rel_total += p->f_rel[i];
1100 p->f_rel_total /= GNUNET_ATS_PreferenceCount - 1.0;
1101 LOG (GNUNET_ERROR_TYPE_DEBUG, "Client %p: peer `%s' has total relative preference of %f\n",
1103 GNUNET_i2s (&p->id),
1108 /* end of gnunet-service-ats_addresses_simplistic.c */