X-Git-Url: https://git.librecmc.org/?a=blobdiff_plain;f=src%2Fats%2Fats_api.c;h=6cd8a82ba288c5e17bc7fd8ab909630dbad6681d;hb=502af2167f7c218366666ca4944bd7cc54b5b19a;hp=9400b1f304ae0668ab0f579e94c9b87c929007e6;hpb=03af5a603b7cc53432249d5854cd412aa90dde0d;p=oweals%2Fgnunet.git diff --git a/src/ats/ats_api.c b/src/ats/ats_api.c index 9400b1f30..6cd8a82ba 100644 --- a/src/ats/ats_api.c +++ b/src/ats/ats_api.c @@ -67,7 +67,7 @@ struct AllocationRecord /** * Number of bytes in plugin_addr. */ - size_t plugin_addr_len; + size_t plugin_addr_len; /** * Number of entries in 'ats'. @@ -105,12 +105,12 @@ struct GNUNET_ATS_SuggestionContext /** * Global ATS handle. - */ + */ struct GNUNET_ATS_Handle *atc; /** * Which peer are we monitoring? - */ + */ struct GNUNET_PeerIdentity target; }; @@ -169,13 +169,11 @@ struct GNUNET_ATS_Handle * @return GNUNET_YES (continue iteration) */ static int -count_connections (void *cls, - const GNUNET_HashCode *key, - void *value) +count_connections (void *cls, const GNUNET_HashCode * key, void *value) { unsigned int *ac = cls; struct AllocationRecord *ar = value; - + if (GNUNET_YES == ar->connected) (*ac)++; return GNUNET_YES; @@ -208,35 +206,29 @@ struct SetBandwidthContext * @return GNUNET_YES (continue iteration) */ static int -set_bw_connections (void *cls, - const GNUNET_HashCode *key, - void *value) +set_bw_connections (void *cls, const GNUNET_HashCode * key, void *value) { struct SetBandwidthContext *sbc = cls; struct AllocationRecord *ar = value; - + if (GNUNET_YES == ar->connected) - { - ar->bandwidth = sbc->bw; - sbc->atc->alloc_cb (sbc->atc->alloc_cb_cls, - (const struct GNUNET_PeerIdentity*) key, - ar->plugin_name, - ar->session, - ar->plugin_addr, - ar->plugin_addr_len, - ar->bandwidth); - } - else if (ntohl(ar->bandwidth.value__) > 0) - { - ar->bandwidth = GNUNET_BANDWIDTH_value_init (0); - sbc->atc->alloc_cb (sbc->atc->alloc_cb_cls, - (const struct GNUNET_PeerIdentity*) key, - ar->plugin_name, - ar->session, - ar->plugin_addr, - ar->plugin_addr_len, - ar->bandwidth); - } + { + ar->bandwidth = sbc->bw; + sbc->atc->alloc_cb (sbc->atc->alloc_cb_cls, + (const struct GNUNET_PeerIdentity *) key, + ar->plugin_name, + ar->session, + ar->plugin_addr, ar->plugin_addr_len, ar->bandwidth); + } + else if (ntohl (ar->bandwidth.value__) > 0) + { + ar->bandwidth = GNUNET_BANDWIDTH_value_init (0); + sbc->atc->alloc_cb (sbc->atc->alloc_cb_cls, + (const struct GNUNET_PeerIdentity *) key, + ar->plugin_name, + ar->session, + ar->plugin_addr, ar->plugin_addr_len, ar->bandwidth); + } return GNUNET_YES; } @@ -246,10 +238,9 @@ set_bw_connections (void *cls, * * @param cls the 'struct GNUNET_ATS_Handle' * @param tc scheduler context - */ + */ static void -update_bandwidth_task (void *cls, - const struct GNUNET_SCHEDULER_TaskContext *tc) +update_bandwidth_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) { struct GNUNET_ATS_Handle *atc = cls; unsigned int ac; @@ -257,14 +248,10 @@ update_bandwidth_task (void *cls, atc->ba_task = GNUNET_SCHEDULER_NO_TASK; /* FIXME: update calculations NICELY; what follows is a naive version */ - GNUNET_CONTAINER_multihashmap_iterate (atc->peers, - &count_connections, - &ac); + GNUNET_CONTAINER_multihashmap_iterate (atc->peers, &count_connections, &ac); bwc.atc = atc; bwc.bw = GNUNET_BANDWIDTH_value_init (atc->total_bps / ac); - GNUNET_CONTAINER_multihashmap_iterate (atc->peers, - &set_bw_connections, - &bwc); + GNUNET_CONTAINER_multihashmap_iterate (atc->peers, &set_bw_connections, &bwc); } @@ -276,12 +263,11 @@ update_bandwidth_task (void *cls, */ static void update_bandwidth_assignment (struct GNUNET_ATS_Handle *atc, - struct AllocationRecord *change) + struct AllocationRecord *change) { /* FIXME: based on the 'change', update the LP-problem... */ if (atc->ba_task == GNUNET_SCHEDULER_NO_TASK) - atc->ba_task = GNUNET_SCHEDULER_add_now (&update_bandwidth_task, - atc); + atc->ba_task = GNUNET_SCHEDULER_add_now (&update_bandwidth_task, atc); } @@ -294,21 +280,19 @@ update_bandwidth_assignment (struct GNUNET_ATS_Handle *atc, * @return GNUNET_NO if we're done, GNUNET_YES if we did not suggest an address yet */ static int -suggest_address (void *cls, - const GNUNET_HashCode *key, - void *value) +suggest_address (void *cls, const GNUNET_HashCode * key, void *value) { struct GNUNET_ATS_SuggestionContext *asc = cls; struct AllocationRecord *ar = value; - + /* trivial strategy: pick first available address... */ asc->cb (asc->cb_cls, - &asc->target, - ar->plugin_name, - ar->plugin_addr, - ar->plugin_addr_len, - GNUNET_BANDWIDTH_value_init (asc->atc->total_bps / 32), - ar->ats, ar->ats_count); + &asc->target, + ar->plugin_name, + ar->plugin_addr, + ar->plugin_addr_len, + GNUNET_BANDWIDTH_value_init (asc->atc->total_bps / 32), + ar->ats, ar->ats_count); asc->cb = NULL; return GNUNET_NO; } @@ -325,9 +309,9 @@ suggest_address (void *cls, */ struct GNUNET_ATS_SuggestionContext * GNUNET_ATS_suggest_address (struct GNUNET_ATS_Handle *atc, - const struct GNUNET_PeerIdentity *peer, - GNUNET_ATS_AddressSuggestionCallback cb, - void *cb_cls) + const struct GNUNET_PeerIdentity *peer, + GNUNET_ATS_AddressSuggestionCallback cb, + void *cb_cls) { struct GNUNET_ATS_SuggestionContext *asc; @@ -338,17 +322,16 @@ GNUNET_ATS_suggest_address (struct GNUNET_ATS_Handle *atc, asc->target = *peer; GNUNET_CONTAINER_multihashmap_get_multiple (atc->peers, &peer->hashPubKey, - &suggest_address, - asc); + &suggest_address, asc); if (NULL == asc->cb) - { - GNUNET_free (asc); - return NULL; - } + { + GNUNET_free (asc); + return NULL; + } GNUNET_CONTAINER_multihashmap_put (atc->notify_map, - &peer->hashPubKey, - asc, - GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE); + &peer->hashPubKey, + asc, + GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE); return asc; } @@ -362,9 +345,9 @@ void GNUNET_ATS_suggest_address_cancel (struct GNUNET_ATS_SuggestionContext *asc) { GNUNET_assert (GNUNET_OK == - GNUNET_CONTAINER_multihashmap_remove (asc->atc->notify_map, - &asc->target.hashPubKey, - asc)); + GNUNET_CONTAINER_multihashmap_remove (asc->atc->notify_map, + &asc->target.hashPubKey, + asc)); GNUNET_free (asc); } @@ -379,8 +362,8 @@ GNUNET_ATS_suggest_address_cancel (struct GNUNET_ATS_SuggestionContext *asc) */ struct GNUNET_ATS_Handle * GNUNET_ATS_init (const struct GNUNET_CONFIGURATION_Handle *cfg, - GNUNET_TRANSPORT_ATS_AllocationNotification alloc_cb, - void *alloc_cb_cls) + GNUNET_TRANSPORT_ATS_AllocationNotification alloc_cb, + void *alloc_cb_cls) { struct GNUNET_ATS_Handle *atc; @@ -390,9 +373,8 @@ GNUNET_ATS_init (const struct GNUNET_CONFIGURATION_Handle *cfg, atc->alloc_cb_cls = alloc_cb_cls; atc->peers = GNUNET_CONTAINER_multihashmap_create (256); GNUNET_CONFIGURATION_get_value_number (cfg, - "core", - "TOTAL_QUOTA_OUT", - &atc->total_bps); + "core", + "TOTAL_QUOTA_OUT", &atc->total_bps); return atc; } @@ -406,9 +388,7 @@ GNUNET_ATS_init (const struct GNUNET_CONFIGURATION_Handle *cfg, * @return GNUNET_OK (continue to iterate) */ static int -destroy_allocation_record (void *cls, - const GNUNET_HashCode *key, - void *value) +destroy_allocation_record (void *cls, const GNUNET_HashCode * key, void *value) { struct AllocationRecord *ar = value; @@ -428,13 +408,12 @@ void GNUNET_ATS_shutdown (struct GNUNET_ATS_Handle *atc) { if (GNUNET_SCHEDULER_NO_TASK != atc->ba_task) - { - GNUNET_SCHEDULER_cancel (atc->ba_task); - atc->ba_task = GNUNET_SCHEDULER_NO_TASK; - } + { + GNUNET_SCHEDULER_cancel (atc->ba_task); + atc->ba_task = GNUNET_SCHEDULER_NO_TASK; + } GNUNET_CONTAINER_multihashmap_iterate (atc->peers, - &destroy_allocation_record, - NULL); + &destroy_allocation_record, NULL); GNUNET_CONTAINER_multihashmap_destroy (atc->peers); GNUNET_assert (GNUNET_CONTAINER_multihashmap_size (atc->notify_map) == 0); GNUNET_CONTAINER_multihashmap_destroy (atc->notify_map); @@ -470,9 +449,7 @@ struct UpdateSessionContext * GNUNET_NO if the record do match and 'old' was updated */ static int -update_session (void *cls, - const GNUNET_HashCode *key, - void *value) +update_session (void *cls, const GNUNET_HashCode * key, void *value) { struct UpdateSessionContext *usc = cls; struct AllocationRecord *arnew = usc->arnew; @@ -481,32 +458,30 @@ update_session (void *cls, if (0 != strcmp (arnew->plugin_name, arold->plugin_name)) return GNUNET_YES; - if ( (arnew->session == arold->session) || - ( (arold->session == NULL) && - (arold->plugin_addr_len == arnew->plugin_addr_len) && - (0 == memcmp (arold->plugin_addr, - arnew->plugin_addr, - arnew->plugin_addr_len)) ) ) + if ((arnew->session == arold->session) || + ((arold->session == NULL) && + (arold->plugin_addr_len == arnew->plugin_addr_len) && + (0 == memcmp (arold->plugin_addr, + arnew->plugin_addr, arnew->plugin_addr_len)))) + { + change = GNUNET_NO; + /* records match */ + if (arnew->session != arold->session) + { + arold->session = arnew->session; + change = GNUNET_YES; + } + if ((arnew->connected == GNUNET_YES) && (arold->connected == GNUNET_NO)) { - change = GNUNET_NO; - /* records match */ - if (arnew->session != arold->session) - { - arold->session = arnew->session; - change = GNUNET_YES; - } - if ( (arnew->connected == GNUNET_YES) && - (arold->connected == GNUNET_NO) ) - { - arold->connected = GNUNET_YES; - change = GNUNET_YES; - } - // FIXME: merge ats arrays of (arold, arnew); - - if (GNUNET_YES == change) - update_bandwidth_assignment (usc->atc, arold); - return GNUNET_NO; + arold->connected = GNUNET_YES; + change = GNUNET_YES; } + // FIXME: merge ats arrays of (arold, arnew); + + if (GNUNET_YES == change) + update_bandwidth_assignment (usc->atc, arold); + return GNUNET_NO; + } return GNUNET_YES; } @@ -523,11 +498,11 @@ update_session (void *cls, */ static struct AllocationRecord * create_allocation_record (const char *plugin_name, - struct Session *session, - const void *plugin_addr, - size_t plugin_addr_len, - const struct GNUNET_TRANSPORT_ATS_Information *ats, - uint32_t ats_count) + struct Session *session, + const void *plugin_addr, + size_t plugin_addr_len, + const struct GNUNET_TRANSPORT_ATS_Information *ats, + uint32_t ats_count) { struct AllocationRecord *ar; @@ -537,12 +512,9 @@ create_allocation_record (const char *plugin_name, memcpy (&ar[1], plugin_addr, plugin_addr_len); ar->session = session; ar->plugin_addr_len = plugin_addr_len; - GNUNET_array_grow (ar->ats, - ar->ats_count, - ats_count); - memcpy (ar->ats, - ats, - ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information)); + GNUNET_array_grow (ar->ats, ar->ats_count, ats_count); + memcpy (ar->ats, + ats, ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information)); return ar; } @@ -556,18 +528,16 @@ create_allocation_record (const char *plugin_name, * @return GNUNET_OK (continue to iterate) */ static int -disconnect_peer (void *cls, - const GNUNET_HashCode *key, - void *value) +disconnect_peer (void *cls, const GNUNET_HashCode * key, void *value) { struct GNUNET_ATS_Handle *atc = cls; struct AllocationRecord *ar = value; if (GNUNET_YES == ar->connected) - { - ar->connected = GNUNET_NO; - update_bandwidth_assignment (atc, ar); - } + { + ar->connected = GNUNET_NO; + update_bandwidth_assignment (atc, ar); + } return GNUNET_OK; } @@ -588,42 +558,36 @@ disconnect_peer (void *cls, */ void GNUNET_ATS_peer_connect (struct GNUNET_ATS_Handle *atc, - const struct GNUNET_PeerIdentity *peer, - const char *plugin_name, - struct Session *session, - const void *plugin_addr, - size_t plugin_addr_len, - const struct GNUNET_TRANSPORT_ATS_Information *ats, - uint32_t ats_count) + const struct GNUNET_PeerIdentity *peer, + const char *plugin_name, + struct Session *session, + const void *plugin_addr, + size_t plugin_addr_len, + const struct GNUNET_TRANSPORT_ATS_Information *ats, + uint32_t ats_count) { struct AllocationRecord *ar; struct UpdateSessionContext usc; (void) GNUNET_CONTAINER_multihashmap_iterate (atc->peers, - &disconnect_peer, - atc); + &disconnect_peer, atc); ar = create_allocation_record (plugin_name, - session, - plugin_addr, - plugin_addr_len, - ats, - ats_count); + session, + plugin_addr, plugin_addr_len, ats, ats_count); ar->connected = GNUNET_YES; usc.atc = atc; usc.arnew = ar; if (GNUNET_SYSERR == - GNUNET_CONTAINER_multihashmap_iterate (atc->peers, - &update_session, - &usc)) - { - destroy_allocation_record (NULL, &peer->hashPubKey, ar); - return; - } + GNUNET_CONTAINER_multihashmap_iterate (atc->peers, &update_session, &usc)) + { + destroy_allocation_record (NULL, &peer->hashPubKey, ar); + return; + } GNUNET_assert (GNUNET_OK == - GNUNET_CONTAINER_multihashmap_put (atc->peers, - &peer->hashPubKey, - ar, - GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE)); + GNUNET_CONTAINER_multihashmap_put (atc->peers, + &peer->hashPubKey, + ar, + GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE)); } @@ -637,12 +601,11 @@ GNUNET_ATS_peer_connect (struct GNUNET_ATS_Handle *atc, */ void GNUNET_ATS_peer_disconnect (struct GNUNET_ATS_Handle *atc, - const struct GNUNET_PeerIdentity *peer) + const struct GNUNET_PeerIdentity *peer) { (void) GNUNET_CONTAINER_multihashmap_get_multiple (atc->peers, - &peer->hashPubKey, - &disconnect_peer, - atc); + &peer->hashPubKey, + &disconnect_peer, atc); } @@ -672,9 +635,7 @@ struct SessionDestroyContext * @return GNUNET_OK (continue to iterate) */ static int -destroy_session (void *cls, - const GNUNET_HashCode *key, - void *value) +destroy_session (void *cls, const GNUNET_HashCode * key, void *value) { struct SessionDestroyContext *sdc = cls; struct AllocationRecord *ar = value; @@ -685,14 +646,13 @@ destroy_session (void *cls, if (ar->plugin_addr != NULL) return GNUNET_OK; GNUNET_assert (GNUNET_OK == - GNUNET_CONTAINER_multihashmap_remove (sdc->atc->peers, - key, - ar)); - if (GNUNET_YES == ar->connected); - { - /* FIXME: is this supposed to be allowed? What to do then? */ - GNUNET_break (0); - } + GNUNET_CONTAINER_multihashmap_remove (sdc->atc->peers, + key, ar)); + if (GNUNET_YES == ar->connected) ; + { + /* FIXME: is this supposed to be allowed? What to do then? */ + GNUNET_break (0); + } destroy_allocation_record (NULL, key, ar); return GNUNET_OK; } @@ -707,16 +667,15 @@ destroy_session (void *cls, */ void GNUNET_ATS_session_destroyed (struct GNUNET_ATS_Handle *atc, - const struct GNUNET_PeerIdentity *peer, - const struct Session *session) + const struct GNUNET_PeerIdentity *peer, + const struct Session *session) { struct SessionDestroyContext sdc; sdc.atc = atc; sdc.session = session; (void) GNUNET_CONTAINER_multihashmap_iterate (atc->peers, - &destroy_session, - &sdc); + &destroy_session, &sdc); } @@ -729,20 +688,18 @@ GNUNET_ATS_session_destroyed (struct GNUNET_ATS_Handle *atc, * @return GNUNET_YES (continue to iterate) */ static int -notify_valid (void *cls, - const GNUNET_HashCode *key, - void *value) +notify_valid (void *cls, const GNUNET_HashCode * key, void *value) { struct AllocationRecord *ar = cls; struct GNUNET_ATS_SuggestionContext *asc = value; asc->cb (asc->cb_cls, - &asc->target, - ar->plugin_name, - ar->plugin_addr, - ar->plugin_addr_len, - GNUNET_BANDWIDTH_value_init (asc->atc->total_bps / 32), - ar->ats, ar->ats_count); + &asc->target, + ar->plugin_name, + ar->plugin_addr, + ar->plugin_addr_len, + GNUNET_BANDWIDTH_value_init (asc->atc->total_bps / 32), + ar->ats, ar->ats_count); GNUNET_ATS_suggest_address_cancel (asc); return GNUNET_OK; } @@ -768,43 +725,37 @@ notify_valid (void *cls, */ void GNUNET_ATS_address_update (struct GNUNET_ATS_Handle *atc, - const struct GNUNET_PeerIdentity *peer, - struct GNUNET_TIME_Absolute valid_until, - const char *plugin_name, - struct Session *session, - const void *plugin_addr, - size_t plugin_addr_len, - const struct GNUNET_TRANSPORT_ATS_Information *ats, - uint32_t ats_count) + const struct GNUNET_PeerIdentity *peer, + struct GNUNET_TIME_Absolute valid_until, + const char *plugin_name, + struct Session *session, + const void *plugin_addr, + size_t plugin_addr_len, + const struct GNUNET_TRANSPORT_ATS_Information *ats, + uint32_t ats_count) { struct AllocationRecord *ar; struct UpdateSessionContext usc; - ar = create_allocation_record (plugin_name, - session, - plugin_addr, - plugin_addr_len, - ats, - ats_count); + ar = create_allocation_record (plugin_name, + session, + plugin_addr, plugin_addr_len, ats, ats_count); usc.atc = atc; - usc.arnew = ar; + usc.arnew = ar; if (GNUNET_SYSERR == - GNUNET_CONTAINER_multihashmap_iterate (atc->peers, - &update_session, - &usc)) - { - destroy_allocation_record (NULL, &peer->hashPubKey, ar); - return; - } + GNUNET_CONTAINER_multihashmap_iterate (atc->peers, &update_session, &usc)) + { + destroy_allocation_record (NULL, &peer->hashPubKey, ar); + return; + } GNUNET_assert (GNUNET_OK == - GNUNET_CONTAINER_multihashmap_put (atc->peers, - &peer->hashPubKey, - ar, - GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE)); + GNUNET_CONTAINER_multihashmap_put (atc->peers, + &peer->hashPubKey, + ar, + GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE)); GNUNET_CONTAINER_multihashmap_get_multiple (atc->notify_map, - &peer->hashPubKey, - ¬ify_valid, - ar); + &peer->hashPubKey, + ¬ify_valid, ar); } /* end of file gnunet-service-transport_ats.c */