/*
This file is part of GNUnet.
- (C) 2011-2014 Christian Grothoff (and other contributing authors)
+ Copyright (C) 2011-2014 Christian Grothoff (and other contributing authors)
GNUnet is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published
*/
struct GAS_PROPORTIONAL_Handle
{
- struct GNUNET_ATS_PluginEnvironment *env;
/**
- * Statistics handle
+ * Our execution environment.
*/
- struct GNUNET_STATISTICS_Handle *stats;
+ struct GNUNET_ATS_PluginEnvironment *env;
/**
* Hashmap containing all valid addresses
*/
struct GNUNET_CONTAINER_MultiPeerMap *requests;
- /**
- * Bandwidth changed callback
- */
- GAS_bandwidth_changed_cb bw_changed;
-
- /**
- * Bandwidth changed callback cls
- */
- void *bw_changed_cls;
-
- /**
- * ATS function to get preferences
- */
- GAS_get_preferences get_preferences;
-
- /**
- * Closure for ATS function to get preferences
- */
- void *get_preferences_cls;
-
- /**
- * ATS function to get properties
- */
- GAS_get_properties get_properties;
-
- /**
- * Closure for ATS function to get properties
- */
- void *get_properties_cls;
-
/**
* Bulk lock
*/
/**
* ATS network type
*/
- unsigned int type;
+ enum GNUNET_ATS_Network_Type type;
/**
* Network description
void *
libgnunet_plugin_ats_proportional_done (void *cls)
{
- struct GAS_PROPORTIONAL_Handle *s = cls;
+ struct GNUNET_ATS_SolverFunctions *sf = cls;
+ struct GAS_PROPORTIONAL_Handle *s = sf->cls;
struct AddressWrapper *cur;
struct AddressWrapper *next;
int c;
if (GNUNET_YES != cur_address->addr->active)
continue;
- GNUNET_assert( NULL != (peer_relative_prefs = s->get_preferences (s->get_preferences_cls,
- &cur_address->addr->peer)));
+ GNUNET_assert( NULL != (peer_relative_prefs = s->env->get_preferences (s->env->cls,
+ &cur_address->addr->peer)));
relative_peer_prefence = 0.0;
relative_peer_prefence += peer_relative_prefs[GNUNET_ATS_PREFERENCE_BANDWIDTH];
sum_relative_peer_prefences += relative_peer_prefence;
if (GNUNET_YES == cur_address->addr->active)
{
GNUNET_assert( NULL != (peer_relative_prefs =
- s->get_preferences (s->get_preferences_cls, &cur_address->addr->peer)));
+ s->env->get_preferences (s->env->cls,
+ &cur_address->addr->peer)));
cur_pref = peer_relative_prefs[GNUNET_ATS_PREFERENCE_BANDWIDTH];
total_weight = net->active_addresses +
struct FindBestAddressCtx *ctx = cls;
struct ATS_Address *current = value;
struct ATS_Address *current_best = current;
- struct GNUNET_TIME_Absolute now;
struct AddressSolverInformation *asi;
struct GNUNET_TIME_Relative active_time;
struct GNUNET_TIME_Relative min_active_time;
- const double *norm_prop_cur;
- const double *norm_prop_best;
double best_delay;
double best_distance;
double cur_delay;
current_best = NULL;
asi = current->solver_information;
- now = GNUNET_TIME_absolute_get ();
-
- if ((current->active == GNUNET_NO)
- && (current->blocked_until.abs_value_us
- == GNUNET_TIME_absolute_max (now, current->blocked_until).abs_value_us))
- {
- /* This address is blocked for suggestion */
- LOG (GNUNET_ERROR_TYPE_DEBUG,
- "Address %p blocked for suggestion for %s \n",
- current,
- GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_difference (now, current->blocked_until),
- GNUNET_YES));
- return GNUNET_OK;
- }
if (NULL == asi)
{
GNUNET_break (0);
}
/* Now compare ATS information */
- norm_prop_cur = ctx->s->get_properties (ctx->s->get_properties_cls,
- (const struct ATS_Address *) current);
index = find_property_index (GNUNET_ATS_QUALITY_NET_DISTANCE);
- cur_distance = norm_prop_cur[index];
+ cur_distance = current->atsin[index].norm;
+ best_distance = ctx->best->atsin[index].norm;
index = find_property_index (GNUNET_ATS_QUALITY_NET_DELAY);
- cur_delay = norm_prop_cur[index];
-
- norm_prop_best = ctx->s->get_properties (ctx->s->get_properties_cls,
- (const struct ATS_Address *) ctx->best);
- index = find_property_index (GNUNET_ATS_QUALITY_NET_DISTANCE);
- best_distance = norm_prop_best[index];
- index = find_property_index (GNUNET_ATS_QUALITY_NET_DELAY);
- best_delay = norm_prop_best[index];
+ cur_delay = current->atsin[index].norm;
+ best_delay = ctx->best->atsin[index].norm;
/* user shorter distance */
if (cur_distance < best_distance)
const struct GNUNET_CONTAINER_MultiPeerMap * addresses,
const struct GNUNET_PeerIdentity *peer)
{
- static struct ATS_Address * dest = NULL;
+ static struct ATS_Address *dest;
dest = NULL;
GNUNET_CONTAINER_multipeermap_get_multiple (addresses, peer,
- &get_active_address_it, &dest);
+ &get_active_address_it,
+ &dest);
return dest;
}
* @return the network struct
*/
static struct Network *
-get_network (struct GAS_PROPORTIONAL_Handle *s, uint32_t type)
+get_network (struct GAS_PROPORTIONAL_Handle *s,
+ enum GNUNET_ATS_Network_Type type)
{
- int c;
- for (c = 0; c < s->network_count; c++)
+ if (type >= s->env->network_count)
{
- if (s->network_entries[c].type == type)
- return &s->network_entries[c];
+ GNUNET_break (0);
+ return NULL;
}
- return NULL ;
+ return &s->network_entries[type];
}
{
s->total_addresses++;
net->total_addresses++;
- GNUNET_STATISTICS_update (s->stats, "# ATS addresses total", 1, GNUNET_NO);
- GNUNET_STATISTICS_update (s->stats, net->stat_total, 1, GNUNET_NO);
+ GNUNET_STATISTICS_update (s->env->stats,
+ "# ATS addresses total", 1, GNUNET_NO);
+ GNUNET_STATISTICS_update (s->env->stats,
+ net->stat_total, 1, GNUNET_NO);
}
if (GNUNET_YES == active)
{
net->active_addresses++;
s->active_addresses++;
- GNUNET_STATISTICS_update (s->stats, "# ATS active addresses total", 1,
+ GNUNET_STATISTICS_update (s->env->stats,
+ "# ATS active addresses total", 1,
GNUNET_NO);
- GNUNET_STATISTICS_update (s->stats, net->stat_active, 1, GNUNET_NO);
+ GNUNET_STATISTICS_update (s->env->stats,
+ net->stat_active, 1, GNUNET_NO);
}
}
else
{
s->total_addresses--;
- GNUNET_STATISTICS_update (s->stats, "# ATS addresses total", -1,
+ GNUNET_STATISTICS_update (s->env->stats,
+ "# ATS addresses total", -1,
GNUNET_NO);
}
if (net->total_addresses < 1)
else
{
net->total_addresses--;
- GNUNET_STATISTICS_update (s->stats, net->stat_total, -1, GNUNET_NO);
+ GNUNET_STATISTICS_update (s->env->stats,
+ net->stat_total, -1, GNUNET_NO);
}
}
else
{
net->active_addresses--;
- GNUNET_STATISTICS_update (s->stats, net->stat_active, -1, GNUNET_NO);
+ GNUNET_STATISTICS_update (s->env->stats,
+ net->stat_active, -1, GNUNET_NO);
}
if (s->active_addresses < 1)
{
else
{
s->active_addresses--;
- GNUNET_STATISTICS_update (s->stats, "# ATS addresses total", -1,
+ GNUNET_STATISTICS_update (s->env->stats,
+ "# ATS addresses total", -1,
GNUNET_NO);
}
}
{
struct AddressWrapper *cur;
struct AddressSolverInformation *asi;
+
for (cur = net->head; NULL != cur; cur = cur->next)
{
asi = cur->addr->solver_information;
/* Notify on change */
if ((GNUNET_YES == cur->addr->active))
{
- s->bw_changed (s->bw_changed_cls, cur->addr);
+ s->env->bandwidth_changed_cb (s->env->cls,
+ cur->addr);
}
}
}
GNUNET_ATS_print_network_type(n->type),
n->active_addresses, n->total_addresses);
- if (NULL != s->env->info_cb)
- s->env->info_cb(s->env->info_cb_cls, GAS_OP_SOLVE_START,
- GAS_STAT_SUCCESS, GAS_INFO_PROP_SINGLE);
+ s->env->info_cb (s->env->cls,
+ GAS_OP_SOLVE_START,
+ GAS_STAT_SUCCESS,
+ GAS_INFO_PROP_SINGLE);
/* Distribute */
distribute_bandwidth(s, n);
- if (NULL != s->env->info_cb)
- s->env->info_cb(s->env->info_cb_cls, GAS_OP_SOLVE_STOP,
- GAS_STAT_SUCCESS, GAS_INFO_PROP_SINGLE);
- if (NULL != s->env->info_cb)
- s->env->info_cb(s->env->info_cb_cls, GAS_OP_SOLVE_UPDATE_NOTIFICATION_START,
- GAS_STAT_SUCCESS, GAS_INFO_PROP_SINGLE);
+ s->env->info_cb (s->env->cls,
+ GAS_OP_SOLVE_STOP,
+ GAS_STAT_SUCCESS,
+ GAS_INFO_PROP_SINGLE);
+ s->env->info_cb (s->env->cls,
+ GAS_OP_SOLVE_UPDATE_NOTIFICATION_START,
+ GAS_STAT_SUCCESS,
+ GAS_INFO_PROP_SINGLE);
/* Do propagation */
propagate_bandwidth (s, n);
- if (NULL != s->env->info_cb)
- s->env->info_cb(s->env->info_cb_cls, GAS_OP_SOLVE_UPDATE_NOTIFICATION_STOP,
- GAS_STAT_SUCCESS, GAS_INFO_PROP_SINGLE);
+ s->env->info_cb (s->env->cls,
+ GAS_OP_SOLVE_UPDATE_NOTIFICATION_STOP,
+ GAS_STAT_SUCCESS,
+ GAS_INFO_PROP_SINGLE);
}
else
{
int i;
- if (NULL != s->env->info_cb)
- s->env->info_cb(s->env->info_cb_cls, GAS_OP_SOLVE_START,
- GAS_STAT_SUCCESS, GAS_INFO_PROP_ALL);
+ s->env->info_cb (s->env->cls,
+ GAS_OP_SOLVE_START,
+ GAS_STAT_SUCCESS,
+ GAS_INFO_PROP_ALL);
for (i = 0; i < s->network_count; i++)
{
/* Distribute */
distribute_bandwidth(s, &s->network_entries[i]);
}
- if (NULL != s->env->info_cb)
- s->env->info_cb(s->env->info_cb_cls, GAS_OP_SOLVE_STOP,
- GAS_STAT_SUCCESS, GAS_INFO_PROP_ALL);
- if (NULL != s->env->info_cb)
- s->env->info_cb(s->env->info_cb_cls, GAS_OP_SOLVE_UPDATE_NOTIFICATION_START,
- GAS_STAT_SUCCESS, GAS_INFO_PROP_ALL);
+ s->env->info_cb (s->env->cls,
+ GAS_OP_SOLVE_STOP,
+ GAS_STAT_SUCCESS,
+ GAS_INFO_PROP_ALL);
+ s->env->info_cb (s->env->cls,
+ GAS_OP_SOLVE_UPDATE_NOTIFICATION_START,
+ GAS_STAT_SUCCESS,
+ GAS_INFO_PROP_ALL);
for (i = 0; i < s->network_count; i++)
{
/* Do propagation */
propagate_bandwidth(s, &s->network_entries[i]);
}
- if (NULL != s->env->info_cb)
- s->env->info_cb(s->env->info_cb_cls, GAS_OP_SOLVE_UPDATE_NOTIFICATION_STOP,
- GAS_STAT_SUCCESS, GAS_INFO_PROP_ALL);
+ s->env->info_cb (s->env->cls,
+ GAS_OP_SOLVE_UPDATE_NOTIFICATION_STOP,
+ GAS_STAT_SUCCESS,
+ GAS_INFO_PROP_ALL);
}
}
"Disconnecting peer `%s' with previous address %p\n",
GNUNET_i2s (peer),
current_address);
- s->bw_changed (s->bw_changed_cls,
- current_address);
+ s->env->bandwidth_changed_cb (s->env->cls,
+ current_address);
}
}
if (NULL == best_address)
*/
static void
GAS_proportional_address_preference_feedback (void *solver,
- void *application,
+ struct GNUNET_SERVER_Client *application,
const struct GNUNET_PeerIdentity *peer,
const struct GNUNET_TIME_Relative scope,
enum GNUNET_ATS_PreferenceKind kind,
"Disconnecting peer `%s' after deleting previous address %p\n",
GNUNET_i2s (&address->peer),
address);
- s->bw_changed (s->bw_changed_cls,
- address);
+ s->env->bandwidth_changed_cb (s->env->cls,
+ address);
}
}
if (GNUNET_NO == session_only)
static void
GAS_proportional_address_property_changed (void *solver,
struct ATS_Address *address,
- uint32_t type,
+ enum GNUNET_ATS_Property type,
uint32_t abs_value,
double rel_value)
{
}
}
-/**
- * Transport session for this address has changed
- *
- * NOTE: values in addresses are already updated
- *
- * @param solver solver handle
- * @param address the address
- * @param cur_session the current session
- * @param new_session the new session
- */
-static void
-GAS_proportional_address_session_changed (void *solver,
- struct ATS_Address *address,
- uint32_t cur_session,
- uint32_t new_session)
-{
- struct GAS_PROPORTIONAL_Handle *s = solver;
- struct ATS_Address *best_address;
- struct ATS_Address *active_address;
- struct AddressSolverInformation *asi;
-
- if (cur_session != new_session)
- {
- LOG (GNUNET_ERROR_TYPE_DEBUG,
- "Session changed from %u to %u\n",
- cur_session,
- new_session);
- }
-
- if (NULL == address->solver_information)
- {
- GNUNET_break (0);
- return;
- }
-
- if (GNUNET_NO ==
- GNUNET_CONTAINER_multipeermap_contains (s->requests, &address->peer))
- return; /* Peer is not requested */
-
- /* This peer is requested, find active and best address */
- active_address = get_active_address (s, s->addresses, &address->peer);
- best_address = update_active_address (s, &address->peer);
-
- if ((NULL != best_address) && ((NULL != active_address) &&
- (GNUNET_YES == address_eq (active_address, best_address))))
- {
- asi = best_address->solver_information;
- GNUNET_assert (NULL != asi);
-
- /* We sticked to the same address, therefore redistribute */
- distribute_bandwidth_in_network (s, asi->network);
- }
-}
-
-
-/**
- * Network scope for this address has changed
- *
- * NOTE: values in addresses are already updated
- *
- * @param solver solver handle
- * @param address the address
- * @param current_network the current network
- * @param new_network the new network
- */
-static void
-GAS_proportional_address_change_network (void *solver,
- struct ATS_Address *address,
- uint32_t current_network,
- uint32_t new_network)
-{
- struct GAS_PROPORTIONAL_Handle *s = solver;
- struct AddressSolverInformation *asi;
- int save_active = GNUNET_NO;
-
- if (current_network == new_network)
- {
- GNUNET_break(0);
- return;
- }
-
- asi = address->solver_information;
- if (NULL == asi)
- {
- GNUNET_break(0);
- return;
- }
-
- /* Network changed */
- LOG(GNUNET_ERROR_TYPE_DEBUG,
- "Network type changed, moving %s address from `%s' to `%s'\n",
- (GNUNET_YES == address->active) ? "active" : "inactive",
- GNUNET_ATS_print_network_type (current_network),
- GNUNET_ATS_print_network_type (new_network));
-
-
- /* Start bulk to prevent disconnect */
- GAS_proportional_bulk_start(s);
-
- save_active = address->active;
-
- /* Disable and assign no bandwidth */
- address->active = GNUNET_NO;
- address->assigned_bw_in = 0; /* no bandwidth assigned */
- address->assigned_bw_out = 0; /* no bandwidth assigned */
-
- /* Remove from old network */
- GAS_proportional_address_delete (solver, address, GNUNET_NO);
-
- /* Set new network type */
- if (NULL == get_network (solver, new_network))
- {
- /* Address changed to invalid network... */
- LOG(GNUNET_ERROR_TYPE_ERROR,
- _("Invalid network type `%u' `%s': Disconnect!\n"), new_network,
- GNUNET_ATS_print_network_type (new_network));
- s->bw_changed (s->bw_changed_cls, address);
- }
- else
- {
- /* Add to new network and update*/
- GAS_proportional_address_add (solver, address, new_network);
- }
- GAS_proportional_bulk_stop (s);
-
- if (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (s->requests, &address->peer))
- return; /* Peer is not requested */
-
- /* Find new address to suggest */
- if (GNUNET_YES == save_active)
- {
- /* No address available, therefore disconnect */
- if (NULL == update_active_address (s, &address->peer))
- s->bw_changed (s->bw_changed_cls, address);
- }
-
-}
/**
* Add a new single address to a network
static void
GAS_proportional_address_add (void *solver,
struct ATS_Address *address,
- uint32_t network)
+ enum GNUNET_ATS_Network_Type network)
{
struct GAS_PROPORTIONAL_Handle *s = solver;
- struct Network *net = NULL;
- struct AddressWrapper *aw = NULL;
+ struct Network *net;
+ struct AddressWrapper *aw;
struct AddressSolverInformation *asi;
- GNUNET_assert(NULL != s);
net = get_network (s, network);
if (NULL == net)
{
- GNUNET_break(0);
-
- LOG(GNUNET_ERROR_TYPE_ERROR,
- "Unknown network %u `%s' for new address %p for peer `%s'\n",
- network, GNUNET_ATS_print_network_type(network),
- address, GNUNET_i2s(&address->peer));
-
+ GNUNET_break (0);
+ LOG (GNUNET_ERROR_TYPE_ERROR,
+ "Unknown network %u `%s' for new address %p for peer `%s'\n",
+ network,
+ GNUNET_ATS_print_network_type (network),
+ address,
+ GNUNET_i2s (&address->peer));
return;
}
void *
libgnunet_plugin_ats_proportional_init (void *cls)
{
+ static struct GNUNET_ATS_SolverFunctions sf;
struct GNUNET_ATS_PluginEnvironment *env = cls;
struct GAS_PROPORTIONAL_Handle *s;
struct Network * cur;
GNUNET_assert (NULL != env->cfg);
GNUNET_assert (NULL != env->bandwidth_changed_cb);
GNUNET_assert (NULL != env->get_preferences);
- GNUNET_assert (NULL != env->get_property);
s = GNUNET_new (struct GAS_PROPORTIONAL_Handle);
s->env = env;
- env->sf.s_add = &GAS_proportional_address_add;
- env->sf.s_address_update_property = &GAS_proportional_address_property_changed;
- env->sf.s_address_update_session = &GAS_proportional_address_session_changed;
- env->sf.s_address_update_network = &GAS_proportional_address_change_network;
- env->sf.s_get = &GAS_proportional_get_preferred_address;
- env->sf.s_get_stop = &GAS_proportional_stop_get_preferred_address;
- env->sf.s_pref = &GAS_proportional_address_change_preference;
- env->sf.s_feedback = &GAS_proportional_address_preference_feedback;
- env->sf.s_del = &GAS_proportional_address_delete;
- env->sf.s_bulk_start = &GAS_proportional_bulk_start;
- env->sf.s_bulk_stop = &GAS_proportional_bulk_stop;
-
- s->stats = (struct GNUNET_STATISTICS_Handle *) env->stats;
- s->bw_changed = env->bandwidth_changed_cb;
- s->bw_changed_cls = env->bw_changed_cb_cls;
- s->get_preferences = env->get_preferences;
- s->get_preferences_cls = env->get_preference_cls;
- s->get_properties = env->get_property;
- s->get_properties_cls = env->get_property_cls;
- s->network_count = env->network_count;
+ sf.cls = s;
+ sf.s_add = &GAS_proportional_address_add;
+ sf.s_address_update_property = &GAS_proportional_address_property_changed;
+ sf.s_get = &GAS_proportional_get_preferred_address;
+ sf.s_get_stop = &GAS_proportional_stop_get_preferred_address;
+ sf.s_pref = &GAS_proportional_address_change_preference;
+ sf.s_feedback = &GAS_proportional_address_preference_feedback;
+ sf.s_del = &GAS_proportional_address_delete;
+ sf.s_bulk_start = &GAS_proportional_bulk_start;
+ sf.s_bulk_stop = &GAS_proportional_bulk_stop;
+
s->network_entries = GNUNET_malloc (env->network_count * sizeof (struct Network));
/* Init */
for (c = 0; c < env->network_count; c++)
{
cur = &s->network_entries[c];
- cur->type = env->networks[c];
+ cur->type = c;
cur->total_quota_in = env->in_quota[c];
cur->total_quota_out = env->out_quota[c];
cur->desc = GNUNET_ATS_print_network_type (c);
cur->total_quota_in,
cur->total_quota_out);
}
- return s;
+ return &sf;
}