/*
This file is part of GNUnet.
- (C) 2011-2015 Christian Grothoff (and other contributing authors)
+ Copyright (C) 2011-2015 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
#define LOG(kind,...) GNUNET_log_from (kind, "ats-preferencesx",__VA_ARGS__)
+/**
+ *
+ */
#define PREF_AGING_INTERVAL GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
+/**
+ *
+ */
#define PREF_AGING_FACTOR 0.95
+/**
+ *
+ */
#define PREF_EPSILON 0.01
peer_count ++;
f_rel_total += p_cur->f_rel[kind];
}
-
}
}
rp->f_rel[kind] = DEFAULT_REL_PREFERENCE;
}
if (backup != rp->f_rel[kind])
- GAS_normalized_preference_changed (&rp->id, kind, rp->f_rel[kind]);
+ GAS_plugin_notify_preference_changed (&rp->id, kind,
+ rp->f_rel[kind]);
}
}
p->f_abs[kind] = score;
/* p->f_abs[kind] = (p->f_abs[kind] + score) / 2; */
p->next_aging[kind] = GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
- PREF_AGING_INTERVAL);
+ PREF_AGING_INTERVAL);
break;
case GNUNET_ATS_PREFERENCE_END:
break;
}
-
/**
- * Change the preference for a peer
+ * Normalize an updated preference value
*
- * @param client the client sending this request
- * @param peer the peer id
- * @param kind the preference kind to change
- * @param score_abs the new preference score
+ * @param client the client with this preference
+ * @param peer the peer to change the preference for
+ * @param kind the kind to change the preference
+ * @param score_abs the normalized score
*/
static void
-preference_change (struct GNUNET_SERVER_Client *client,
- const struct GNUNET_PeerIdentity *peer,
- enum GNUNET_ATS_PreferenceKind kind,
- float score_abs)
+normalize_preference (struct GNUNET_SERVER_Client *client,
+ const struct GNUNET_PeerIdentity *peer,
+ enum GNUNET_ATS_PreferenceKind kind,
+ float score_abs)
{
- if (GNUNET_NO ==
- GNUNET_CONTAINER_multipeermap_contains (GSA_addresses,
- peer))
+ struct PreferenceClient *c_cur;
+ struct PreferencePeer *p_cur;
+ struct PeerRelative *r_cur;
+ double old_value;
+ int i;
+
+ LOG (GNUNET_ERROR_TYPE_DEBUG,
+ "Client changes preference for peer `%s' for `%s' to %.2f\n",
+ GNUNET_i2s (peer),
+ GNUNET_ATS_print_preference_type (kind),
+ score_abs);
+
+ if (kind >= GNUNET_ATS_PreferenceCount)
{
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
- "Received CHANGE_PREFERENCE for unknown peer `%s'\n",
- GNUNET_i2s (peer));
+ GNUNET_break(0);
return;
}
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
- "Received CHANGE_PREFERENCE for peer `%s'\n",
- GNUNET_i2s (peer));
- GAS_plugin_update_preferences (client,
- peer,
- kind,
- score_abs);
+
+ /* Find preference client */
+ for (c_cur = pc_head; NULL != c_cur; c_cur = c_cur->next)
+ if (client == c_cur->client)
+ break;
+ /* Not found: create new preference client */
+ if (NULL == c_cur)
+ {
+ c_cur = GNUNET_new (struct PreferenceClient);
+ c_cur->client = client;
+ for (i = 0; i < GNUNET_ATS_PreferenceCount; i++)
+ {
+ c_cur->f_abs_sum[i] = DEFAULT_ABS_PREFERENCE;
+ c_cur->f_rel_sum[i] = DEFAULT_REL_PREFERENCE;
+ }
+ GNUNET_CONTAINER_DLL_insert (pc_head,
+ pc_tail,
+ c_cur);
+ LOG (GNUNET_ERROR_TYPE_DEBUG,
+ "Adding new client %p\n",
+ c_cur);
+ }
+
+ /* Find entry for peer */
+ for (p_cur = c_cur->p_head; NULL != p_cur; p_cur = p_cur->next)
+ if (0 == memcmp (&p_cur->id,
+ peer,
+ sizeof (p_cur->id)))
+ break;
+
+ /* Not found: create new peer entry */
+ if (NULL == p_cur)
+ {
+ p_cur = GNUNET_new (struct PreferencePeer);
+ p_cur->client = c_cur;
+ p_cur->id = *peer;
+ for (i = 0; i < GNUNET_ATS_PreferenceCount; i++)
+ {
+ /* Default value per peer absolute preference for a preference: 0 */
+ p_cur->f_abs[i] = DEFAULT_ABS_PREFERENCE;
+ /* Default value per peer relative preference for a quality: 1.0 */
+ p_cur->f_rel[i] = DEFAULT_REL_PREFERENCE;
+ p_cur->next_aging[i] = GNUNET_TIME_UNIT_FOREVER_ABS;
+ }
+ LOG (GNUNET_ERROR_TYPE_DEBUG,
+ "Adding new peer %p for client %p\n",
+ p_cur,
+ c_cur);
+ GNUNET_CONTAINER_DLL_insert (c_cur->p_head,
+ c_cur->p_tail,
+ p_cur);
+ }
+
+ /* Create struct for peer */
+ if (NULL ==
+ GNUNET_CONTAINER_multipeermap_get (preference_peers,
+ peer))
+ {
+ r_cur = GNUNET_new (struct PeerRelative);
+ r_cur->id = *peer;
+ for (i = 0; i < GNUNET_ATS_PreferenceCount; i++)
+ r_cur->f_rel[i] = DEFAULT_REL_PREFERENCE;
+ GNUNET_assert(GNUNET_OK ==
+ GNUNET_CONTAINER_multipeermap_put (preference_peers,
+ &r_cur->id,
+ r_cur,
+ GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
+ }
+
+ /* Update absolute value */
+ old_value = p_cur->f_abs[kind];
+ update_abs_preference (c_cur, p_cur, kind, score_abs);
+ if (p_cur->f_abs[kind] == old_value)
+ return;
+
+ GAS_plugin_solver_lock ();
+ run_preference_update (c_cur,
+ p_cur,
+ kind,
+ score_abs);
+ GAS_plugin_solver_unlock ();
+
+ if (NULL == aging_task)
+ aging_task = GNUNET_SCHEDULER_add_delayed (PREF_AGING_INTERVAL,
+ &preference_aging,
+ NULL);
}
uint32_t i;
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
- "Received `%s' message\n",
- "PREFERENCE_CHANGE");
+ "Received PREFERENCE_CHANGE message\n");
msize = ntohs (message->size);
if (msize < sizeof (struct ChangePreferenceMessage))
{
1, GNUNET_NO);
pi = (const struct PreferenceInformation *) &msg[1];
for (i = 0; i < nump; i++)
- preference_change (client,
- &msg->peer,
- (enum GNUNET_ATS_PreferenceKind)
- ntohl (pi[i].preference_kind),
- pi[i].preference_value);
+ normalize_preference (client,
+ &msg->peer,
+ (enum GNUNET_ATS_PreferenceKind)
+ ntohl (pi[i].preference_kind),
+ pi[i].preference_value);
GNUNET_SERVER_receive_done (client, GNUNET_OK);
}
while (NULL != (pc = next_pc))
{
next_pc = pc->next;
- GNUNET_CONTAINER_DLL_remove(pc_head, pc_tail, pc);
+ GNUNET_CONTAINER_DLL_remove (pc_head,
+ pc_tail,
+ pc);
free_client (pc);
}
GNUNET_CONTAINER_multipeermap_iterate (preference_peers,
}
-/**
- * Normalize an updated preference value
- *
- * @param client the client with this preference
- * @param peer the peer to change the preference for
- * @param kind the kind to change the preference
- * @param score_abs the normalized score
- */
-void
-GAS_normalization_normalize_preference (struct GNUNET_SERVER_Client *client,
- const struct GNUNET_PeerIdentity *peer,
- enum GNUNET_ATS_PreferenceKind kind,
- float score_abs)
-{
- struct PreferenceClient *c_cur;
- struct PreferencePeer *p_cur;
- struct PeerRelative *r_cur;
- double old_value;
- int i;
-
- LOG (GNUNET_ERROR_TYPE_DEBUG,
- "Client changes preference for peer `%s' for `%s' to %.2f\n",
- GNUNET_i2s (peer),
- GNUNET_ATS_print_preference_type (kind),
- score_abs);
-
- if (kind >= GNUNET_ATS_PreferenceCount)
- {
- GNUNET_break(0);
- return;
- }
-
- /* Find preference client */
- for (c_cur = pc_head; NULL != c_cur; c_cur = c_cur->next)
- {
- if (client == c_cur->client)
- break;
- }
- /* Not found: create new preference client */
- if (NULL == c_cur)
- {
- c_cur = GNUNET_new (struct PreferenceClient);
- c_cur->client = client;
- for (i = 0; i < GNUNET_ATS_PreferenceCount; i++)
- {
- c_cur->f_abs_sum[i] = DEFAULT_ABS_PREFERENCE;
- c_cur->f_rel_sum[i] = DEFAULT_REL_PREFERENCE;
- }
-
- GNUNET_CONTAINER_DLL_insert(pc_head, pc_tail, c_cur);
- LOG (GNUNET_ERROR_TYPE_DEBUG, "Adding new client %p \n", c_cur);
- }
-
- /* Find entry for peer */
- for (p_cur = c_cur->p_head; NULL != p_cur; p_cur = p_cur->next)
- if (0 == memcmp (&p_cur->id, peer, sizeof(p_cur->id)))
- break;
-
- /* Not found: create new peer entry */
- if (NULL == p_cur)
- {
- p_cur = GNUNET_new (struct PreferencePeer);
- p_cur->client = c_cur;
- p_cur->id = (*peer);
- for (i = 0; i < GNUNET_ATS_PreferenceCount; i++)
- {
- /* Default value per peer absolute preference for a preference: 0 */
- p_cur->f_abs[i] = DEFAULT_ABS_PREFERENCE;
- /* Default value per peer relative preference for a quality: 1.0 */
- p_cur->f_rel[i] = DEFAULT_REL_PREFERENCE;
- p_cur->next_aging[i] = GNUNET_TIME_UNIT_FOREVER_ABS;
- }
- LOG (GNUNET_ERROR_TYPE_DEBUG, "Adding new peer %p for client %p \n",
- p_cur, c_cur);
- GNUNET_CONTAINER_DLL_insert(c_cur->p_head, c_cur->p_tail, p_cur);
- }
-
- /* Create struct for peer */
- if (NULL == GNUNET_CONTAINER_multipeermap_get (preference_peers, peer))
- {
- r_cur = GNUNET_new (struct PeerRelative);
- r_cur->id = (*peer);
- for (i = 0; i < GNUNET_ATS_PreferenceCount; i++)
- r_cur->f_rel[i] = DEFAULT_REL_PREFERENCE;
- GNUNET_assert(
- GNUNET_OK == GNUNET_CONTAINER_multipeermap_put (preference_peers,
- &r_cur->id, r_cur, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
- }
-
- /* Update absolute value */
- old_value = p_cur->f_abs[kind];
- update_abs_preference (c_cur, p_cur, kind, score_abs);
- if (p_cur->f_abs[kind] == old_value)
- return;
-
- run_preference_update (c_cur, p_cur, kind, score_abs);
-
- /* Start aging task */
- if (NULL == aging_task)
- aging_task = GNUNET_SCHEDULER_add_delayed (PREF_AGING_INTERVAL,
- &preference_aging,
- NULL);
-
-}
-
-
/**
* Get the normalized preference values for a specific peer or
* the default values if
* default preferences if peer does not exist
*/
const double *
-GAS_normalization_get_preferences_by_peer (void *cls,
- const struct GNUNET_PeerIdentity *id)
+GAS_preference_get_by_peer (void *cls,
+ const struct GNUNET_PeerIdentity *id)
{
struct PeerRelative *rp;
* @param client the client
*/
void
-GAS_normalization_preference_client_disconnect (struct GNUNET_SERVER_Client *client)
+GAS_preference_client_disconnect (struct GNUNET_SERVER_Client *client)
{
struct PreferenceClient *c_cur;
- /* Find preference client */
for (c_cur = pc_head; NULL != c_cur; c_cur = c_cur->next)
- {
if (client == c_cur->client)
break;
- }
if (NULL == c_cur)
return;
-
- GNUNET_CONTAINER_DLL_remove(pc_head, pc_tail, c_cur);
+ GNUNET_CONTAINER_DLL_remove (pc_head,
+ pc_tail,
+ c_cur);
free_client (c_cur);
}
+
+
+/* end of gnunet-service-ats_preferences.c */