aboutsummaryrefslogtreecommitdiff
path: root/src/ats/gnunet-service-ats_addresses.c
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2014-12-15 18:09:16 +0000
committerChristian Grothoff <christian@grothoff.org>2014-12-15 18:09:16 +0000
commitd80d181ca46d0804af85bb33f16caa4c8eb00f9a (patch)
tree887188d35de602da8793241bae716a998f5702d6 /src/ats/gnunet-service-ats_addresses.c
parent8978db474d26bef8fd7b023baeb5b8e3cb5224c6 (diff)
downloadgnunet-d80d181ca46d0804af85bb33f16caa4c8eb00f9a.tar.gz
gnunet-d80d181ca46d0804af85bb33f16caa4c8eb00f9a.zip
removing dead code, improving comments, simplifying iteration logic, making iteration over all addresses also faster
Diffstat (limited to 'src/ats/gnunet-service-ats_addresses.c')
-rw-r--r--src/ats/gnunet-service-ats_addresses.c207
1 files changed, 90 insertions, 117 deletions
diff --git a/src/ats/gnunet-service-ats_addresses.c b/src/ats/gnunet-service-ats_addresses.c
index 70224e67a..768b13076 100644
--- a/src/ats/gnunet-service-ats_addresses.c
+++ b/src/ats/gnunet-service-ats_addresses.c
@@ -1616,16 +1616,16 @@ find_preference_client (struct GAS_Addresses_Handle *handle, void *client)
1616 return NULL; 1616 return NULL;
1617} 1617}
1618 1618
1619
1619/** 1620/**
1620 * A performance client disconnected 1621 * A performance client disconnected
1621 * 1622 *
1622 * @param handle address handle 1623 * @param handle address handle
1623 * @param client the client 1624 * @param client the client
1624 */ 1625 */
1625
1626void 1626void
1627GAS_addresses_preference_client_disconnect (struct GAS_Addresses_Handle *handle, 1627GAS_addresses_preference_client_disconnect (struct GAS_Addresses_Handle *handle,
1628 void *client) 1628 void *client)
1629{ 1629{
1630 struct GAS_Addresses_Preference_Clients * pc; 1630 struct GAS_Addresses_Preference_Clients * pc;
1631 if (NULL != (pc = find_preference_client (handle, client))) 1631 if (NULL != (pc = find_preference_client (handle, client)))
@@ -1640,6 +1640,7 @@ GAS_addresses_preference_client_disconnect (struct GAS_Addresses_Handle *handle,
1640 GAS_normalization_preference_client_disconnect (client); 1640 GAS_normalization_preference_client_disconnect (client);
1641} 1641}
1642 1642
1643
1643/** 1644/**
1644 * Change the preference for a peer 1645 * Change the preference for a peer
1645 * 1646 *
@@ -1679,7 +1680,10 @@ GAS_addresses_preference_change (struct GAS_Addresses_Handle *handle,
1679 GNUNET_CONTAINER_DLL_insert (handle->preference_clients_head, 1680 GNUNET_CONTAINER_DLL_insert (handle->preference_clients_head,
1680 handle->preference_clients_tail, pc); 1681 handle->preference_clients_tail, pc);
1681 handle->pref_clients ++; 1682 handle->pref_clients ++;
1682 GNUNET_STATISTICS_set (handle->stat, "# active performance clients", handle->pref_clients, GNUNET_NO); 1683 GNUNET_STATISTICS_set (handle->stat,
1684 "# active performance clients",
1685 handle->pref_clients,
1686 GNUNET_NO);
1683 } 1687 }
1684 1688
1685 handle->env.sf.s_bulk_start (handle->solver); 1689 handle->env.sf.s_bulk_start (handle->solver);
@@ -1688,6 +1692,7 @@ GAS_addresses_preference_change (struct GAS_Addresses_Handle *handle,
1688 handle->env.sf.s_bulk_stop (handle->solver); 1692 handle->env.sf.s_bulk_stop (handle->solver);
1689} 1693}
1690 1694
1695
1691/** 1696/**
1692 * Change the preference for a peer 1697 * Change the preference for a peer
1693 * 1698 *
@@ -1700,9 +1705,11 @@ GAS_addresses_preference_change (struct GAS_Addresses_Handle *handle,
1700 */ 1705 */
1701void 1706void
1702GAS_addresses_preference_feedback (struct GAS_Addresses_Handle *handle, 1707GAS_addresses_preference_feedback (struct GAS_Addresses_Handle *handle,
1703 void *application, const struct GNUNET_PeerIdentity *peer, 1708 void *application,
1704 const struct GNUNET_TIME_Relative scope, 1709 const struct GNUNET_PeerIdentity *peer,
1705 enum GNUNET_ATS_PreferenceKind kind, float score_abs) 1710 const struct GNUNET_TIME_Relative scope,
1711 enum GNUNET_ATS_PreferenceKind kind,
1712 float score_abs)
1706{ 1713{
1707 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 1714 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1708 "Received `%s' for peer `%s' for client %p\n", "PREFERENCE FEEDBACK", 1715 "Received `%s' for peer `%s' for client %p\n", "PREFERENCE FEEDBACK",
@@ -1725,6 +1732,7 @@ GAS_addresses_preference_feedback (struct GAS_Addresses_Handle *handle,
1725 score_abs); 1732 score_abs);
1726} 1733}
1727 1734
1735
1728/** 1736/**
1729 * Load quotas for networks from configuration 1737 * Load quotas for networks from configuration
1730 * 1738 *
@@ -1850,6 +1858,7 @@ load_quotas (const struct GNUNET_CONFIGURATION_Handle *cfg,
1850 return GNUNET_ATS_NetworkTypeCount; 1858 return GNUNET_ATS_NetworkTypeCount;
1851} 1859}
1852 1860
1861
1853/** 1862/**
1854 * Callback for solver to notify about assignment changes 1863 * Callback for solver to notify about assignment changes
1855 * 1864 *
@@ -1945,7 +1954,7 @@ bandwidth_changed_cb (void *cls, struct ATS_Address *address)
1945 */ 1954 */
1946struct GAS_Addresses_Handle * 1955struct GAS_Addresses_Handle *
1947GAS_addresses_init (const struct GNUNET_CONFIGURATION_Handle *cfg, 1956GAS_addresses_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
1948 const struct GNUNET_STATISTICS_Handle *stats) 1957 const struct GNUNET_STATISTICS_Handle *stats)
1949{ 1958{
1950 struct GAS_Addresses_Handle *ah; 1959 struct GAS_Addresses_Handle *ah;
1951 unsigned long long quotas_in[GNUNET_ATS_NetworkTypeCount]; 1960 unsigned long long quotas_in[GNUNET_ATS_NetworkTypeCount];
@@ -1964,11 +1973,11 @@ GAS_addresses_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
1964 GNUNET_assert(NULL != ah->addresses); 1973 GNUNET_assert(NULL != ah->addresses);
1965 1974
1966 /* Figure out configured solution method */ 1975 /* Figure out configured solution method */
1967 if (GNUNET_SYSERR 1976 if (GNUNET_SYSERR ==
1968 == GNUNET_CONFIGURATION_get_value_string (cfg, "ats", "MODE", &mode_str)) 1977 GNUNET_CONFIGURATION_get_value_string (cfg, "ats", "MODE", &mode_str))
1969 { 1978 {
1970 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, 1979 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
1971 "No resource assignment method configured, using proportional approach\n"); 1980 "No resource assignment method configured, using proportional approach\n");
1972 ah->ats_mode = MODE_PROPORTIONAL; 1981 ah->ats_mode = MODE_PROPORTIONAL;
1973 } 1982 }
1974 else 1983 else
@@ -1982,8 +1991,8 @@ GAS_addresses_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
1982 ah->ats_mode = MODE_MLP; 1991 ah->ats_mode = MODE_MLP;
1983#if !HAVE_LIBGLPK 1992#if !HAVE_LIBGLPK
1984 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, 1993 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1985 "Assignment method `%s' configured, but GLPK is not available, please install \n", 1994 "Assignment method `%s' configured, but GLPK is not available, please install \n",
1986 mode_str); 1995 mode_str);
1987 ah->ats_mode = MODE_PROPORTIONAL; 1996 ah->ats_mode = MODE_PROPORTIONAL;
1988#endif 1997#endif
1989 } 1998 }
@@ -1992,8 +2001,8 @@ GAS_addresses_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
1992 else 2001 else
1993 { 2002 {
1994 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, 2003 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1995 "Invalid resource assignment method `%s' configured, using proportional approach\n", 2004 "Invalid resource assignment method `%s' configured, using proportional approach\n",
1996 mode_str); 2005 mode_str);
1997 ah->ats_mode = MODE_PROPORTIONAL; 2006 ah->ats_mode = MODE_PROPORTIONAL;
1998 } 2007 }
1999 GNUNET_free(mode_str); 2008 GNUNET_free(mode_str);
@@ -2035,11 +2044,18 @@ GAS_addresses_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
2035 plugin_short = NULL; 2044 plugin_short = NULL;
2036 break; 2045 break;
2037 } 2046 }
2038 GNUNET_asprintf (&ah->plugin, "libgnunet_plugin_ats_%s", plugin_short); 2047 GNUNET_asprintf (&ah->plugin,
2039 GNUNET_log(GNUNET_ERROR_TYPE_INFO, _("Initializing solver `%s '`%s'\n"), plugin_short, ah->plugin); 2048 "libgnunet_plugin_ats_%s",
2040 if (NULL == (ah->solver = GNUNET_PLUGIN_load (ah->plugin, &ah->env))) 2049 plugin_short);
2050 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
2051 _("Initializing solver `%s '`%s'\n"),
2052 plugin_short,
2053 ah->plugin);
2054 if (NULL == (ah->solver = GNUNET_PLUGIN_load (ah->plugin, &ah->env)))
2041 { 2055 {
2042 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Failed to initialize solver `%s'!\n"), ah->plugin); 2056 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2057 _("Failed to initialize solver `%s'!\n"),
2058 ah->plugin);
2043 return NULL; 2059 return NULL;
2044 } 2060 }
2045 2061
@@ -2058,11 +2074,12 @@ GAS_addresses_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
2058 2074
2059 2075
2060 GAS_normalization_start (&normalized_preference_changed_cb, ah, 2076 GAS_normalization_start (&normalized_preference_changed_cb, ah,
2061 &normalized_property_changed_cb, ah); 2077 &normalized_property_changed_cb, ah);
2062 2078
2063 if (NULL == ah->solver) 2079 if (NULL == ah->solver)
2064 { 2080 {
2065 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Failed to initialize solver!\n")); 2081 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2082 _("Failed to initialize solver!\n"));
2066 GNUNET_free(ah); 2083 GNUNET_free(ah);
2067 return NULL ; 2084 return NULL ;
2068 } 2085 }
@@ -2075,6 +2092,7 @@ GAS_addresses_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
2075 return ah; 2092 return ah;
2076} 2093}
2077 2094
2095
2078/** 2096/**
2079 * Destroy all addresses iterator 2097 * Destroy all addresses iterator
2080 * 2098 *
@@ -2151,21 +2169,27 @@ GAS_addresses_done (struct GAS_Addresses_Handle *handle)
2151 handle->addresses = NULL; 2169 handle->addresses = NULL;
2152 while (NULL != (cur = handle->pending_requests_head)) 2170 while (NULL != (cur = handle->pending_requests_head))
2153 { 2171 {
2154 GNUNET_CONTAINER_DLL_remove(handle->pending_requests_head, handle->pending_requests_tail, cur); 2172 GNUNET_CONTAINER_DLL_remove (handle->pending_requests_head,
2173 handle->pending_requests_tail,
2174 cur);
2155 GNUNET_free(cur); 2175 GNUNET_free(cur);
2156 } 2176 }
2157 2177
2158 while (NULL != (pcur = handle->preference_clients_head)) 2178 while (NULL != (pcur = handle->preference_clients_head))
2159 { 2179 {
2160 GNUNET_CONTAINER_DLL_remove (handle->preference_clients_head, 2180 GNUNET_CONTAINER_DLL_remove (handle->preference_clients_head,
2161 handle->preference_clients_tail, pcur); 2181 handle->preference_clients_tail,
2182 pcur);
2162 GNUNET_assert (handle->pref_clients > 0); 2183 GNUNET_assert (handle->pref_clients > 0);
2163 handle->pref_clients --; 2184 handle->pref_clients --;
2164 GNUNET_STATISTICS_set (handle->stat, "# active performance clients", handle->pref_clients, GNUNET_NO); 2185 GNUNET_STATISTICS_set (handle->stat,
2186 "# active performance clients",
2187 handle->pref_clients,
2188 GNUNET_NO);
2165 GNUNET_free (pcur); 2189 GNUNET_free (pcur);
2166 } 2190 }
2167 2191 GNUNET_PLUGIN_unload (handle->plugin,
2168 GNUNET_PLUGIN_unload (handle->plugin, handle->solver); 2192 handle->solver);
2169 GNUNET_free (handle->plugin); 2193 GNUNET_free (handle->plugin);
2170 GNUNET_free(handle); 2194 GNUNET_free(handle);
2171 /* Stop configured solution method */ 2195 /* Stop configured solution method */
@@ -2173,76 +2197,19 @@ GAS_addresses_done (struct GAS_Addresses_Handle *handle)
2173} 2197}
2174 2198
2175 2199
2176struct PeerIteratorContext
2177{
2178 GNUNET_ATS_Peer_Iterator it;
2179 void *it_cls;
2180 struct GNUNET_CONTAINER_MultiPeerMap *peers_returned;
2181};
2182
2183
2184/** 2200/**
2185 * Iterator to iterate over all peers 2201 * Closure for #peerinfo_it().
2186 *
2187 * @param cls a PeerIteratorContext
2188 * @param key the peer id
2189 * @param value the ATS_address
2190 * @return #GNUNET_OK to continue
2191 */ 2202 */
2192static int
2193peer_it (void *cls,
2194 const struct GNUNET_PeerIdentity *key,
2195 void *value)
2196{
2197 struct PeerIteratorContext *ip_ctx = cls;
2198
2199 if (GNUNET_NO ==
2200 GNUNET_CONTAINER_multipeermap_contains (ip_ctx->peers_returned, key))
2201 {
2202 GNUNET_CONTAINER_multipeermap_put (ip_ctx->peers_returned, key, NULL,
2203 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
2204 ip_ctx->it (ip_ctx->it_cls, key);
2205 }
2206
2207 return GNUNET_OK;
2208}
2209
2210/**
2211 * Return information all peers currently known to ATS
2212 *
2213 * @param handle the address handle to use
2214 * @param p_it the iterator to call for every peer
2215 * @param p_it_cls the closure for the iterator
2216 */
2217void
2218GAS_addresses_iterate_peers (struct GAS_Addresses_Handle *handle,
2219 GNUNET_ATS_Peer_Iterator p_it, void *p_it_cls)
2220{
2221 struct PeerIteratorContext ip_ctx;
2222 unsigned int size;
2223
2224 if (NULL == p_it)
2225 return;
2226 GNUNET_assert(NULL != handle->addresses);
2227
2228 size = GNUNET_CONTAINER_multipeermap_size (handle->addresses);
2229 if (0 != size)
2230 {
2231 ip_ctx.it = p_it;
2232 ip_ctx.it_cls = p_it_cls;
2233 ip_ctx.peers_returned = GNUNET_CONTAINER_multipeermap_create (size,
2234 GNUNET_NO);
2235 GNUNET_CONTAINER_multipeermap_iterate (handle->addresses,
2236 &peer_it,
2237 &ip_ctx);
2238 GNUNET_CONTAINER_multipeermap_destroy (ip_ctx.peers_returned);
2239 }
2240 p_it (p_it_cls, NULL );
2241}
2242
2243struct PeerInfoIteratorContext 2203struct PeerInfoIteratorContext
2244{ 2204{
2205 /**
2206 * Function to call for each address.
2207 */
2245 GNUNET_ATS_PeerInfo_Iterator it; 2208 GNUNET_ATS_PeerInfo_Iterator it;
2209
2210 /**
2211 * Closure for @e it.
2212 */
2246 void *it_cls; 2213 void *it_cls;
2247}; 2214};
2248 2215
@@ -2263,13 +2230,15 @@ peerinfo_it (void *cls,
2263 struct PeerInfoIteratorContext *pi_ctx = cls; 2230 struct PeerInfoIteratorContext *pi_ctx = cls;
2264 struct ATS_Address *addr = value; 2231 struct ATS_Address *addr = value;
2265 2232
2266 if (NULL != pi_ctx->it) 2233 pi_ctx->it (pi_ctx->it_cls,
2267 { 2234 &addr->peer,
2268 pi_ctx->it (pi_ctx->it_cls, &addr->peer, addr->plugin, addr->addr, 2235 addr->plugin,
2269 addr->addr_len, addr->active, addr->atsi, addr->atsi_count, 2236 addr->addr,
2270 GNUNET_BANDWIDTH_value_init (addr->assigned_bw_out), 2237 addr->addr_len,
2271 GNUNET_BANDWIDTH_value_init (addr->assigned_bw_in)); 2238 addr->active,
2272 } 2239 addr->atsi, addr->atsi_count,
2240 GNUNET_BANDWIDTH_value_init (addr->assigned_bw_out),
2241 GNUNET_BANDWIDTH_value_init (addr->assigned_bw_in));
2273 return GNUNET_YES; 2242 return GNUNET_YES;
2274} 2243}
2275 2244
@@ -2278,36 +2247,40 @@ peerinfo_it (void *cls,
2278 * Return information all peers currently known to ATS 2247 * Return information all peers currently known to ATS
2279 * 2248 *
2280 * @param handle the address handle to use 2249 * @param handle the address handle to use
2281 * @param peer the respective peer 2250 * @param peer the respective peer, NULL for 'all' peers
2282 * @param pi_it the iterator to call for every peer 2251 * @param pi_it the iterator to call for every peer
2283 * @param pi_it_cls the closure for the iterator 2252 * @param pi_it_cls the closure for @a pi_it
2284 */ 2253 */
2285void 2254void
2286GAS_addresses_get_peer_info (struct GAS_Addresses_Handle *handle, 2255GAS_addresses_get_peer_info (struct GAS_Addresses_Handle *handle,
2287 const struct GNUNET_PeerIdentity *peer, GNUNET_ATS_PeerInfo_Iterator pi_it, 2256 const struct GNUNET_PeerIdentity *peer,
2288 void *pi_it_cls) 2257 GNUNET_ATS_PeerInfo_Iterator pi_it,
2258 void *pi_it_cls)
2289{ 2259{
2290 struct PeerInfoIteratorContext pi_ctx; 2260 struct PeerInfoIteratorContext pi_ctx;
2291 2261
2292 GNUNET_assert(NULL != peer);
2293 GNUNET_assert(NULL != handle->addresses);
2294 if (NULL == pi_it) 2262 if (NULL == pi_it)
2295 return; /* does not make sense without callback */ 2263 {
2296 2264 /* does not make sense without callback */
2265 GNUNET_break (0);
2266 return;
2267 }
2297 pi_ctx.it = pi_it; 2268 pi_ctx.it = pi_it;
2298 pi_ctx.it_cls = pi_it_cls; 2269 pi_ctx.it_cls = pi_it_cls;
2299 GNUNET_CONTAINER_multipeermap_get_multiple (handle->addresses, 2270 if (NULL == peer)
2300 peer, 2271 GNUNET_CONTAINER_multipeermap_iterate (handle->addresses,
2301 &peerinfo_it, &pi_ctx); 2272 &peerinfo_it,
2302 2273 &pi_ctx);
2303 if (NULL != pi_it) 2274 else
2304 pi_it (pi_it_cls, 2275 GNUNET_CONTAINER_multipeermap_get_multiple (handle->addresses,
2305 NULL, NULL, NULL, 0, 2276 peer,
2306 GNUNET_NO, 2277 &peerinfo_it, &pi_ctx);
2307 NULL, 0, 2278 pi_it (pi_it_cls,
2308 zero_bw, 2279 NULL, NULL, NULL, 0,
2309 zero_bw); 2280 GNUNET_NO,
2310 2281 NULL, 0,
2282 zero_bw,
2283 zero_bw);
2311} 2284}
2312 2285
2313/* end of gnunet-service-ats_addresses.c */ 2286/* end of gnunet-service-ats_addresses.c */