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 GNUNET_PeerIdentity id;
165 float prefs[GNUNET_ATS_PreferenceCount];
168 struct PreferenceClient
170 struct PreferenceClient *prev;
171 struct PreferenceClient *next;
174 float prefs[GNUNET_ATS_PreferenceCount];
176 struct GNUNET_CONTAINER_MultiHashMap *peers;
181 * Get the prefered address for a specific peer
183 * @param solver the solver handle
184 * @param addresses the address hashmap containing all addresses
185 * @param peer the identity of the peer
187 const struct ATS_Address *
188 GAS_simplistic_get_preferred_address (void *solver,
189 struct GNUNET_CONTAINER_MultiHashMap * addresses,
190 const struct GNUNET_PeerIdentity *peer);
193 * Init the simplistic problem solving component
196 * network[i] contains the network type as type GNUNET_ATS_NetworkType[i]
197 * out_quota[i] contains outbound quota for network type i
198 * in_quota[i] contains inbound quota for network type i
201 * network = {GNUNET_ATS_NET_UNSPECIFIED, GNUNET_ATS_NET_LOOPBACK, GNUNET_ATS_NET_LAN, GNUNET_ATS_NET_WAN, GNUNET_ATS_NET_WLAN}
202 * network[2] == GNUNET_ATS_NET_LAN
203 * out_quota[2] == 65353
204 * in_quota[2] == 65353
206 * @param cfg configuration handle
207 * @param stats the GNUNET_STATISTICS handle
208 * @param network array of GNUNET_ATS_NetworkType with length dest_length
209 * @param out_quota array of outbound quotas
210 * @param in_quota array of outbound quota
211 * @param dest_length array length for quota arrays
212 * @param bw_changed_cb callback for changed bandwidth amounts
213 * @param bw_changed_cb_cls cls for callback
214 * @return handle for the solver on success, NULL on fail
217 GAS_simplistic_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
218 const struct GNUNET_STATISTICS_Handle *stats,
220 unsigned long long *out_quota,
221 unsigned long long *in_quota,
223 GAS_bandwidth_changed_cb bw_changed_cb,
224 void *bw_changed_cb_cls)
227 struct GAS_SIMPLISTIC_Handle *s = GNUNET_malloc (sizeof (struct GAS_SIMPLISTIC_Handle));
228 struct Network * cur;
229 char * net_str[GNUNET_ATS_NetworkTypeCount] = GNUNET_ATS_NetworkTypeString;
232 s->stats = (struct GNUNET_STATISTICS_Handle *) stats;
233 s->bw_changed = bw_changed_cb;
234 s->bw_changed_cls = bw_changed_cb_cls;
235 s->networks = dest_length;
236 s->network_entries = GNUNET_malloc (dest_length * sizeof (struct Network));
237 s->active_addresses = 0;
238 s->total_addresses = 0;
240 for (c = 0; c < dest_length; c++)
242 cur = &s->network_entries[c];
243 cur->total_addresses = 0;
244 cur->active_addresses = 0;
245 cur->type = network[c];
246 cur->total_quota_in = in_quota[c];
247 cur->total_quota_out = out_quota[c];
248 cur->desc = net_str[c];
249 GNUNET_asprintf (&cur->stat_total, "# ATS addresses %s total", cur->desc);
250 GNUNET_asprintf (&cur->stat_active, "# ATS active addresses %s total", cur->desc);
257 delete_peers (void *cls,
258 const struct GNUNET_HashCode * key,
261 struct GNUNET_CONTAINER_MultiHashMap *m = cls;
262 struct PreferencePeer *p = (struct PreferencePeer *) value;
263 GNUNET_CONTAINER_multihashmap_remove (m, &p->id.hashPubKey, p);
269 * Shutdown the simplistic problem solving component
271 * @param solver the respective handle to shutdown
274 GAS_simplistic_done (void *solver)
276 struct GAS_SIMPLISTIC_Handle *s = solver;
277 struct PreferenceClient *pc;
278 struct PreferenceClient *next_pc;
279 struct AddressWrapper *cur;
280 struct AddressWrapper *next;
282 GNUNET_assert (s != NULL);
284 for (c = 0; c < s->networks; c++)
286 if (s->network_entries[c].total_addresses > 0)
288 LOG (GNUNET_ERROR_TYPE_ERROR,
289 "Had %u addresses for network `%s' not deleted during shutdown\n",
290 s->network_entries[c].total_addresses,
291 s->network_entries[c].desc);
295 if (s->network_entries[c].active_addresses > 0)
297 LOG (GNUNET_ERROR_TYPE_ERROR,
298 "Had %u active addresses for network `%s' not deleted during shutdown\n",
299 s->network_entries[c].active_addresses,
300 s->network_entries[c].desc);
304 next = s->network_entries[c].head;
305 while (NULL != (cur = next))
308 GNUNET_CONTAINER_DLL_remove (s->network_entries[c].head,
309 s->network_entries[c].tail,
313 GNUNET_free (s->network_entries[c].stat_total);
314 GNUNET_free (s->network_entries[c].stat_active);
316 if (s->total_addresses > 0)
318 LOG (GNUNET_ERROR_TYPE_ERROR,
319 "Had %u addresses not deleted during shutdown\n",
323 if (s->active_addresses > 0)
325 LOG (GNUNET_ERROR_TYPE_ERROR,
326 "Had %u active addresses not deleted during shutdown\n",
327 s->active_addresses);
330 GNUNET_free (s->network_entries);
332 next_pc = s->pc_head;
333 while (NULL != (pc = next_pc))
336 GNUNET_CONTAINER_DLL_remove (s->pc_head, s->pc_tail, pc);
337 GNUNET_CONTAINER_multihashmap_iterate (pc->peers, &delete_peers, pc->peers);
338 GNUNET_CONTAINER_multihashmap_destroy (pc->peers);
344 static unsigned long long int
345 calculate_new_quota (unsigned long long int total,
346 unsigned int addresses)
348 return (total / addresses);
352 * Test if bandwidth is available in this network
354 * @param s the solver handle
355 * @param net the network type to update
356 * @return GNUNET_YES or GNUNET_NO
360 bw_available_in_network (struct Network *net)
362 unsigned int na = net->active_addresses + 1;
363 uint32_t min_bw = ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
364 if ((calculate_new_quota (net->total_quota_in, na) > min_bw) &&
365 (calculate_new_quota (net->total_quota_out, na) > min_bw))
367 LOG (GNUNET_ERROR_TYPE_DEBUG,
368 "Enough bandwidth available for %u active addresses in network `%s'\n",
374 LOG (GNUNET_ERROR_TYPE_DEBUG,
375 "Not enough bandwidth available for %u active addresses in network `%s'\n",
382 * Update the quotas for a network type
384 * @param s the solver handle
385 * @param net the network type to update
386 * @param address_except address excluded from notifcation, since we suggest
390 update_quota_per_network (struct GAS_SIMPLISTIC_Handle *s,
392 struct ATS_Address *address_except)
394 unsigned long long quota_in = 0;
395 unsigned long long quota_out = 0;
396 struct AddressWrapper *cur;
398 LOG (GNUNET_ERROR_TYPE_DEBUG,
399 "Recalculate quota for network type `%s' for %u addresses (in/out): %llu/%llu \n",
400 net->desc, net->active_addresses, quota_in, quota_out);
402 if (net->active_addresses == 0)
403 return; /* no addresses to update */
405 quota_in = calculate_new_quota (net->total_quota_in, net->active_addresses);
406 quota_out = calculate_new_quota (net->total_quota_out, net->active_addresses);
408 LOG (GNUNET_ERROR_TYPE_DEBUG,
409 "New per address quota for network type `%s' for %u addresses (in/out): %llu/%llu \n",
410 net->desc, net->active_addresses, quota_in, quota_out);
415 /* Compare to current bandwidth assigned */
416 if ((quota_in != ntohl(cur->addr->assigned_bw_in.value__)) ||
417 (quota_out != ntohl(cur->addr->assigned_bw_out.value__)))
419 cur->addr->assigned_bw_in.value__ = htonl (quota_in);
420 cur->addr->assigned_bw_out.value__ = htonl (quota_out);
421 /* Notify on change */
422 if ((GNUNET_YES == cur->addr->active) && (cur->addr != address_except))
423 s->bw_changed (s->bw_changed_cls, cur->addr);
430 addresse_increment (struct GAS_SIMPLISTIC_Handle *s,
435 if (GNUNET_YES == total)
437 s->total_addresses ++;
438 net->total_addresses ++;
439 GNUNET_STATISTICS_update (s->stats, "# ATS addresses total", 1, GNUNET_NO);
440 GNUNET_STATISTICS_update (s->stats, net->stat_total, 1, GNUNET_NO);
442 if (GNUNET_YES == active)
444 net->active_addresses ++;
445 s->active_addresses ++;
446 GNUNET_STATISTICS_update (s->stats, "# ATS active addresses total", 1, GNUNET_NO);
447 GNUNET_STATISTICS_update (s->stats, net->stat_active, 1, GNUNET_NO);
453 addresse_decrement (struct GAS_SIMPLISTIC_Handle *s,
459 if (GNUNET_YES == total)
461 if (s->total_addresses < 1)
468 s->total_addresses --;
469 GNUNET_STATISTICS_update (s->stats, "# ATS addresses total", -1, GNUNET_NO);
471 if (net->total_addresses < 1)
478 net->total_addresses --;
479 GNUNET_STATISTICS_update (s->stats, net->stat_total, -1, GNUNET_NO);
483 if (GNUNET_YES == active)
485 if (net->active_addresses < 1)
492 net->active_addresses --;
493 GNUNET_STATISTICS_update (s->stats, net->stat_active, -1, GNUNET_NO);
495 if (s->active_addresses < 1)
502 s->active_addresses --;
503 GNUNET_STATISTICS_update (s->stats, "# ATS addresses total", -1, GNUNET_NO);
511 * Add a single address to the solve
513 * @param solver the solver Handle
514 * @param addresses the address hashmap containing all addresses
515 * @param address the address to add
518 GAS_simplistic_address_add (void *solver, struct GNUNET_CONTAINER_MultiHashMap * addresses, struct ATS_Address *address)
520 struct GAS_SIMPLISTIC_Handle *s = solver;
521 struct Network *net = NULL;
522 struct AddressWrapper *aw = NULL;
523 GNUNET_assert (NULL != s);
525 for (c = 0; c < s->networks; c++)
527 net = &s->network_entries[c];
528 if (address->atsp_network_type == net->type)
537 aw = GNUNET_malloc (sizeof (struct AddressWrapper));
539 GNUNET_CONTAINER_DLL_insert (net->head, net->tail, aw);
540 addresse_increment (s, net, GNUNET_YES, GNUNET_NO);
541 aw->addr->solver_information = net;
544 LOG (GNUNET_ERROR_TYPE_DEBUG, "After adding address now total %u and active %u addresses in network `%s'\n",
545 net->total_addresses,
546 net->active_addresses,
551 * Remove an address from the solver
553 * @param solver the solver handle
554 * @param addresses the address hashmap containing all addresses
555 * @param address the address to remove
556 * @param session_only delete only session not whole address
559 GAS_simplistic_address_delete (void *solver,
560 struct GNUNET_CONTAINER_MultiHashMap * addresses,
561 struct ATS_Address *address, int session_only)
563 struct GAS_SIMPLISTIC_Handle *s = solver;
565 struct AddressWrapper *aw;
567 /* Remove an adress completely, we have to:
568 * - Remove from specific network
569 * - Decrease number of total addresses
571 * - decrease number of active addreses
575 net = (struct Network *) address->solver_information;
577 if (GNUNET_NO == session_only)
579 LOG (GNUNET_ERROR_TYPE_DEBUG, "Deleting %s address %p for peer `%s' from network `%s' (total: %u/ active: %u)\n",
580 (GNUNET_NO == address->active) ? "inactive" : "active",
581 address, GNUNET_i2s (&address->peer),
582 net->desc, net->total_addresses, net->active_addresses);
585 addresse_decrement (s, net, GNUNET_YES, GNUNET_NO);
586 for (aw = net->head; NULL != aw; aw = aw->next)
588 if (aw->addr == address)
596 GNUNET_CONTAINER_DLL_remove (net->head, net->tail, aw);
601 /* Remove session only: remove if active and update */
602 LOG (GNUNET_ERROR_TYPE_DEBUG, "Deleting %s session %p for peer `%s' from network `%s' (total: %u/ active: %u)\n",
603 (GNUNET_NO == address->active) ? "inactive" : "active",
604 address, GNUNET_i2s (&address->peer),
605 net->desc, net->total_addresses, net->active_addresses);
608 if (GNUNET_YES == address->active)
610 /* Address was active, remove from network and update quotas*/
611 address->active = GNUNET_NO;
612 if (GNUNET_SYSERR == addresse_decrement (s, net, GNUNET_NO, GNUNET_YES))
614 update_quota_per_network (s, net, NULL);
616 LOG (GNUNET_ERROR_TYPE_DEBUG, "After deleting address now total %u and active %u addresses in network `%s'\n",
617 net->total_addresses,
618 net->active_addresses,
623 static struct Network *
624 find_network (struct GAS_SIMPLISTIC_Handle *s, uint32_t type)
627 for (c = 0 ; c < s->networks; c++)
629 if (s->network_entries[c].type == type)
630 return &s->network_entries[c];
636 * Updates a single address in the solve
638 * @param solver the solver Handle
639 * @param addresses the address hashmap containing all addresses
640 * @param address the update address
641 * @param session the new session (if changed otherwise current)
642 * @param in_use the new address in use state (if changed otherwise current)
643 * @param atsi the latest ATS information
644 * @param atsi_count the atsi count
647 GAS_simplistic_address_update (void *solver,
648 struct GNUNET_CONTAINER_MultiHashMap *addresses,
649 struct ATS_Address *address,
652 const struct GNUNET_ATS_Information *atsi,
655 struct ATS_Address *new;
656 struct GAS_SIMPLISTIC_Handle *s = (struct GAS_SIMPLISTIC_Handle *) solver;
660 int save_active = GNUNET_NO;
661 struct Network *new_net = NULL;
662 for (i = 0; i < atsi_count; i++)
664 type = ntohl (atsi[i].type);
665 value = ntohl (atsi[i].value);
668 case GNUNET_ATS_UTILIZATION_UP:
669 //if (address->atsp_utilization_out.value__ != atsi[i].value)
672 case GNUNET_ATS_UTILIZATION_DOWN:
673 //if (address->atsp_utilization_in.value__ != atsi[i].value)
676 case GNUNET_ATS_QUALITY_NET_DELAY:
677 //if (address->atsp_latency.rel_value != value)
680 case GNUNET_ATS_QUALITY_NET_DISTANCE:
681 //if (address->atsp_distance != value)
684 case GNUNET_ATS_COST_WAN:
685 //if (address->atsp_cost_wan != value)
688 case GNUNET_ATS_COST_LAN:
689 //if (address->atsp_cost_lan != value)
692 case GNUNET_ATS_COST_WLAN:
693 //if (address->atsp_cost_wlan != value)
696 case GNUNET_ATS_NETWORK_TYPE:
697 if (address->atsp_network_type != value)
700 LOG (GNUNET_ERROR_TYPE_DEBUG, "Network type changed, moving %s address from `%s' to `%s'\n",
701 (GNUNET_YES == address->active) ? "active" : "inactive",
702 GNUNET_ATS_print_network_type(address->atsp_network_type),
703 GNUNET_ATS_print_network_type(value));
705 save_active = address->active;
706 /* remove from old network */
707 GAS_simplistic_address_delete (solver, addresses, address, GNUNET_NO);
709 /* set new network type */
710 address->atsp_network_type = value;
711 new_net = find_network (solver, value);
712 address->solver_information = new_net;
713 if (address->solver_information == NULL)
716 address->atsp_network_type = GNUNET_ATS_NET_UNSPECIFIED;
720 /* restore active state, add to new network and update*/
721 address->active = save_active;
722 GAS_simplistic_address_add (solver, addresses, address);
723 if (GNUNET_YES == save_active)
725 /* check if bandwidth available*/
726 if (GNUNET_NO == (bw_available_in_network (new_net)))
728 /* Find new address to suggest since no bandwidth in network*/
729 new = (struct ATS_Address *) GAS_simplistic_get_preferred_address (s, addresses, &address->peer);
732 /* Have an alternative address to suggest */
733 s->bw_changed (s->bw_changed_cls, address);
738 addresse_increment (s, new_net, GNUNET_NO, GNUNET_YES);
739 update_quota_per_network (solver, new_net, NULL);
743 case GNUNET_ATS_ARRAY_TERMINATOR:
746 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
747 "Received unsupported ATS type %u\n", type);
754 if (address->session_id != session)
756 LOG (GNUNET_ERROR_TYPE_DEBUG,
757 "Session changed from %u to %u\n", address->session_id, session);
758 address->session_id = session;
760 if (address->used != in_use)
762 LOG (GNUNET_ERROR_TYPE_DEBUG,
763 "Usage changed from %u to %u\n", address->used, in_use);
764 address->used = in_use;
772 * Find a "good" address to use for a peer. If we already have an existing
773 * address, we stick to it. Otherwise, we pick by lowest distance and then
776 * @param cls the 'struct ATS_Address**' where we store the result
778 * @param value another 'struct ATS_Address*' to consider using
779 * @return GNUNET_OK (continue to iterate)
782 find_address_it (void *cls, const struct GNUNET_HashCode * key, void *value)
784 struct ATS_Address **previous_p = cls;
785 struct ATS_Address *current = (struct ATS_Address *) value;
786 struct ATS_Address *previous = *previous_p;
787 struct GNUNET_TIME_Absolute now;
788 struct Network *net = (struct Network *) current->solver_information;
790 now = GNUNET_TIME_absolute_get();
792 if (current->blocked_until.abs_value == GNUNET_TIME_absolute_max (now, current->blocked_until).abs_value)
794 /* This address is blocked for suggestion */
795 LOG (GNUNET_ERROR_TYPE_DEBUG,
796 "Address %p blocked for suggestion for %llu ms \n",
798 GNUNET_TIME_absolute_get_difference(now, current->blocked_until).rel_value);
802 if (GNUNET_NO == bw_available_in_network (net))
803 return GNUNET_OK; /* There's no bandwidth available in this network */
805 if (NULL != previous)
807 if ((0 == strcmp (previous->plugin, "tcp")) &&
808 (0 == strcmp (current->plugin, "tcp")))
810 if ((0 != previous->addr_len) &&
811 (0 == current->addr_len))
813 /* saved address was an outbound address, but we have an inbound address */
814 *previous_p = current;
817 if (0 == previous->addr_len)
819 /* saved address was an inbound address, so do not overwrite */
825 if (NULL == previous)
827 *previous_p = current;
830 if ((ntohl (previous->assigned_bw_in.value__) == 0) &&
831 (ntohl (current->assigned_bw_in.value__) > 0))
833 /* stick to existing connection */
834 *previous_p = current;
837 if (previous->atsp_distance > current->atsp_distance)
839 /* user shorter distance */
840 *previous_p = current;
843 if (previous->atsp_latency.rel_value > current->atsp_latency.rel_value)
845 /* user lower latency */
846 *previous_p = current;
854 find_active_address_it (void *cls, const struct GNUNET_HashCode * key, void *value)
856 struct ATS_Address * dest = (struct ATS_Address *) (*(struct ATS_Address **)cls);
857 struct ATS_Address * aa = (struct ATS_Address *) value;
859 if (GNUNET_YES == aa->active)
863 /* should never happen */
864 LOG (GNUNET_ERROR_TYPE_ERROR, "Multiple active addresses for peer `%s'\n", GNUNET_i2s (&aa->peer));
873 static struct ATS_Address *
874 find_active_address (void *solver,
875 struct GNUNET_CONTAINER_MultiHashMap * addresses,
876 const struct GNUNET_PeerIdentity *peer)
878 struct ATS_Address * dest = NULL;
880 GNUNET_CONTAINER_multihashmap_get_multiple(addresses,
882 &find_active_address_it, &dest);
887 * Get the prefered address for a specific peer
889 * @param solver the solver handle
890 * @param addresses the address hashmap containing all addresses
891 * @param peer the identity of the peer
893 const struct ATS_Address *
894 GAS_simplistic_get_preferred_address (void *solver,
895 struct GNUNET_CONTAINER_MultiHashMap * addresses,
896 const struct GNUNET_PeerIdentity *peer)
898 struct GAS_SIMPLISTIC_Handle *s = solver;
899 struct Network *net_prev;
900 struct Network *net_cur;
901 struct ATS_Address *cur;
902 struct ATS_Address *prev;
904 GNUNET_assert (s != NULL);
906 /* Get address with: stick to current address, lower distance, lower latency */
907 GNUNET_CONTAINER_multihashmap_get_multiple (addresses, &peer->hashPubKey,
908 &find_address_it, &cur);
911 LOG (GNUNET_ERROR_TYPE_DEBUG, "Cannot suggest address for peer `%s'\n", GNUNET_i2s (peer));
915 LOG (GNUNET_ERROR_TYPE_DEBUG, "Suggesting %s address %p for peer `%s'\n",
916 (GNUNET_NO == cur->active) ? "inactive" : "active",
917 cur, GNUNET_i2s (peer));
918 net_cur = (struct Network *) cur->solver_information;
919 if (GNUNET_YES == cur->active)
921 /* This address was selected previously, so no need to update quotas */
925 /* This address was not active, so we have to:
927 * - mark previous active address as not active
928 * - update quota for previous address network
929 * - update quota for this address network
932 prev = find_active_address (s, addresses, peer);
935 net_prev = (struct Network *) prev->solver_information;
936 prev->active = GNUNET_NO; /* No active any longer */
937 prev->assigned_bw_in = GNUNET_BANDWIDTH_value_init (0); /* no bw assigned */
938 prev->assigned_bw_out = GNUNET_BANDWIDTH_value_init (0); /* no bw assigned */
939 s->bw_changed (s->bw_changed_cls, prev); /* notify about bw change, REQUIRED? */
940 if (GNUNET_SYSERR == addresse_decrement (s, net_prev, GNUNET_NO, GNUNET_YES))
942 update_quota_per_network (s, net_prev, NULL);
945 if (GNUNET_NO == (bw_available_in_network (cur->solver_information)))
947 GNUNET_break (0); /* This should never happen*/
951 cur->active = GNUNET_YES;
952 addresse_increment(s, net_cur, GNUNET_NO, GNUNET_YES);
953 update_quota_per_network (s, net_cur, cur);
960 update_pref_value (struct PreferenceClient *cur, int kind, float score)
962 float res = (cur->prefs[kind] + score) / 2; /* change update algorithm here */;
963 LOG (GNUNET_ERROR_TYPE_DEBUG, "Client %p changes preference for peer type %s from %f to %f\n",
965 GNUNET_ATS_print_preference_type (kind),
966 cur->prefs[kind], res);
971 * Changes the preferences for a peer in the problem
973 * @param solver the solver handle
974 * @param client the client with this preference
975 * @param peer the peer to change the preference for
976 * @param kind the kind to change the preference
977 * @param score the score
980 GAS_simplistic_address_change_preference (void *solver,
982 const struct GNUNET_PeerIdentity *peer,
983 enum GNUNET_ATS_PreferenceKind kind,
986 struct GAS_SIMPLISTIC_Handle *s = solver;
987 struct PreferenceClient *cur;
988 struct PreferencePeer *p;
991 GNUNET_assert (NULL != solver);
992 GNUNET_assert (NULL != client);
993 GNUNET_assert (NULL != peer);
995 LOG (GNUNET_ERROR_TYPE_DEBUG, "Client %p changes preference for peer `%s' %s %f\n",
998 GNUNET_ATS_print_preference_type (kind),
1001 if (kind >= GNUNET_ATS_PreferenceCount)
1007 for (cur = s->pc_head; NULL != cur; cur = cur->next)
1009 if (client == cur->client)
1014 cur = GNUNET_malloc (sizeof (struct PreferenceClient));
1015 cur->client = client;
1016 cur->peers = GNUNET_CONTAINER_multihashmap_create (10, GNUNET_NO);
1017 GNUNET_CONTAINER_DLL_insert (s->pc_head, s->pc_tail, cur);
1020 p = GNUNET_CONTAINER_multihashmap_get (cur->peers, &peer->hashPubKey);
1023 p = GNUNET_malloc (sizeof (struct PreferencePeer));
1025 for (i = 0; i < GNUNET_ATS_PreferenceCount; i++)
1027 GNUNET_CONTAINER_multihashmap_put (cur->peers,
1030 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1034 case GNUNET_ATS_PREFERENCE_BANDWIDTH:
1035 case GNUNET_ATS_PREFERENCE_LATENCY:
1036 p->prefs[kind] = update_pref_value (cur, kind, score);
1038 case GNUNET_ATS_PREFERENCE_END:
1046 /* end of gnunet-service-ats_addresses_simplistic.c */