aboutsummaryrefslogtreecommitdiff
path: root/src/rps
diff options
context:
space:
mode:
authorJulius Bünger <buenger@mytum.de>2015-11-30 23:06:19 +0000
committerJulius Bünger <buenger@mytum.de>2015-11-30 23:06:19 +0000
commit1a3f487d25259afe2dacd784c7e088a307113b62 (patch)
treef51264e258656dc4aa41a170597252be2d55e9d5 /src/rps
parent392baa82fc240d76b31a353c2d729a6f83f10b2c (diff)
downloadgnunet-1a3f487d25259afe2dacd784c7e088a307113b62.tar.gz
gnunet-1a3f487d25259afe2dacd784c7e088a307113b62.zip
-using "view" helper in rps service
Signed-off-by: Julius Bünger <buenger@mytum.de>
Diffstat (limited to 'src/rps')
-rw-r--r--src/rps/Makefile.am1
-rw-r--r--src/rps/gnunet-service-rps.c173
2 files changed, 36 insertions, 138 deletions
diff --git a/src/rps/Makefile.am b/src/rps/Makefile.am
index c0b60ee0b..d5a081a2b 100644
--- a/src/rps/Makefile.am
+++ b/src/rps/Makefile.am
@@ -50,6 +50,7 @@ gnunet_service_rps_SOURCES = \
50 gnunet-service-rps_sampler_elem.h gnunet-service-rps_sampler_elem.c \ 50 gnunet-service-rps_sampler_elem.h gnunet-service-rps_sampler_elem.c \
51 gnunet-service-rps_sampler.h gnunet-service-rps_sampler.c \ 51 gnunet-service-rps_sampler.h gnunet-service-rps_sampler.c \
52 gnunet-service-rps_peers.h gnunet-service-rps_peers.c \ 52 gnunet-service-rps_peers.h gnunet-service-rps_peers.c \
53 gnunet-service-rps_view.h gnunet-service-rps_view.c \
53 rps-test_util.h rps-test_util.c \ 54 rps-test_util.h rps-test_util.c \
54 gnunet-service-rps.c 55 gnunet-service-rps.c
55 56
diff --git a/src/rps/gnunet-service-rps.c b/src/rps/gnunet-service-rps.c
index ba6dbee3e..0073ad9bd 100644
--- a/src/rps/gnunet-service-rps.c
+++ b/src/rps/gnunet-service-rps.c
@@ -30,6 +30,7 @@
30#include "gnunet_nse_service.h" 30#include "gnunet_nse_service.h"
31#include "rps.h" 31#include "rps.h"
32#include "gnunet-service-rps_peers.h" 32#include "gnunet-service-rps_peers.h"
33#include "gnunet-service-rps_view.h"
33#include "rps-test_util.h" 34#include "rps-test_util.h"
34 35
35#include "gnunet-service-rps_sampler.h" 36#include "gnunet-service-rps_sampler.h"
@@ -306,19 +307,6 @@ static struct GNUNET_CONTAINER_MultiPeerMap *peer_map;
306 */ 307 */
307static char *file_name_view_log; 308static char *file_name_view_log;
308 309
309/**
310 * The "local view" containing peers we learned from gossip and history
311 */
312static struct GNUNET_CONTAINER_MultiPeerMap *view;
313
314/**
315 * An array containing the peers of the local view.
316 *
317 * This is created every time we send a pull reply if it has changed since the
318 * last pull reply we sent.
319 */
320static struct GNUNET_PeerIdentity *view_array;
321
322 310
323/** 311/**
324 * The size of sampler we need to be able to satisfy the client's need of 312 * The size of sampler we need to be able to satisfy the client's need of
@@ -953,27 +941,7 @@ insert_in_pull_map_scheduled (const struct PeerContext *peer_ctx)
953 void 941 void
954insert_in_view (void *cls, const struct GNUNET_PeerIdentity *peer) 942insert_in_view (void *cls, const struct GNUNET_PeerIdentity *peer)
955{ 943{
956 if (GNUNET_YES == GNUNET_CONTAINER_multipeermap_contains (view, peer)) 944 View_put (peer);
957 return;
958 LOG (GNUNET_ERROR_TYPE_DEBUG,
959 "Going to put %s into view\n",
960 GNUNET_i2s (peer));
961 if (GNUNET_YES != GNUNET_CONTAINER_multipeermap_put (view,
962 peer,
963 NULL,
964 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
965 {
966 LOG (GNUNET_ERROR_TYPE_WARNING,
967 "Failed to put peer into view. (insert_in_view)\n");
968 }
969 if (NULL != view_array)
970 {
971 GNUNET_free (view_array);
972 view_array = NULL;
973 }
974 if (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (peer_map, peer))
975 create_peer_ctx (peer);
976 (void) get_channel (peer);
977} 945}
978 946
979/** 947/**
@@ -1033,11 +1001,9 @@ hist_update (void *cls, struct GNUNET_PeerIdentity *ids, uint32_t num_peers)
1033{ 1001{
1034 unsigned int i; 1002 unsigned int i;
1035 1003
1036 for (i = 0; i < GNUNET_MIN ( 1004 for (i = 0; i < num_peers; i++)
1037 sampler_size_est_need - GNUNET_CONTAINER_multipeermap_size (view),
1038 num_peers); i++)
1039 { 1005 {
1040 insert_in_view (NULL, &ids[i]); 1006 View_put (&ids[i]);
1041 to_file (file_name_view_log, 1007 to_file (file_name_view_log,
1042 "+%s\t(hist)", 1008 "+%s\t(hist)",
1043 GNUNET_i2s_full (ids)); 1009 GNUNET_i2s_full (ids));
@@ -1296,7 +1262,7 @@ new_peer_id (const struct GNUNET_PeerIdentity *peer_id)
1296 "Got peer_id %s (at %p, view size: %u)\n", 1262 "Got peer_id %s (at %p, view size: %u)\n",
1297 GNUNET_i2s (peer_id), 1263 GNUNET_i2s (peer_id),
1298 peer_id, 1264 peer_id,
1299 GNUNET_CONTAINER_multipeermap_size (view)); 1265 View_size ());
1300 1266
1301 /* if the seed peer is already know, skip context creation */ 1267 /* if the seed peer is already know, skip context creation */
1302 if (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (peer_map, peer_id)) 1268 if (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (peer_map, peer_id))
@@ -1679,58 +1645,6 @@ handle_peer_push (void *cls,
1679 1645
1680 1646
1681/** 1647/**
1682 * Iterator over hash map entries.
1683 * Called from #generate_view_array and writes every peer id into #view_array.
1684 *
1685 * @param cls closure - the pointer to the counter
1686 * @param key current public key
1687 * @param value value in the hash map
1688 * @return #GNUNET_YES if we should continue to
1689 * iterate,
1690 * #GNUNET_NO if not.
1691 */
1692static int
1693dump_id_to_view_array (void *cls,
1694 const struct GNUNET_PeerIdentity *key,
1695 void *value)
1696{
1697 unsigned int *i = (unsigned int *) cls;
1698
1699 view_array[(*i)++] = *key;
1700 return GNUNET_YES;
1701}
1702
1703
1704/**
1705 * Makes sure the view_array is filled with the peer ids currently in #view.
1706 * Called from within #do_round before sending pushes and pulls and from
1707 * #handle_peer_pull_request when a reply is sent.
1708 */
1709static void
1710generate_view_array (unsigned int view_size)
1711{
1712 unsigned int *i;
1713 int ret;
1714
1715 if (NULL == view_array)
1716 {
1717 view_array = GNUNET_new_array (view_size,
1718 struct GNUNET_PeerIdentity);
1719 i = GNUNET_new (unsigned int);
1720 *i = 0;
1721
1722 ret = GNUNET_CONTAINER_multipeermap_iterate (view,
1723 dump_id_to_view_array,
1724 i);
1725 GNUNET_assert (view_size == ret);
1726 GNUNET_assert (view_size == *i);
1727
1728 GNUNET_free (i);
1729 }
1730}
1731
1732
1733/**
1734 * Handle PULL REQUEST request message from another peer. 1648 * Handle PULL REQUEST request message from another peer.
1735 * 1649 *
1736 * Reply with the view of PeerIDs. 1650 * Reply with the view of PeerIDs.
@@ -1747,7 +1661,7 @@ handle_peer_pull_request (void *cls,
1747 const struct GNUNET_MessageHeader *msg) 1661 const struct GNUNET_MessageHeader *msg)
1748{ 1662{
1749 struct GNUNET_PeerIdentity *peer; 1663 struct GNUNET_PeerIdentity *peer;
1750 unsigned int view_size; 1664 const struct GNUNET_PeerIdentity *view_array;
1751 1665
1752 peer = (struct GNUNET_PeerIdentity *) 1666 peer = (struct GNUNET_PeerIdentity *)
1753 GNUNET_CADET_channel_get_info (channel, 1667 GNUNET_CADET_channel_get_info (channel,
@@ -1774,10 +1688,9 @@ handle_peer_pull_request (void *cls,
1774 } 1688 }
1775 #endif /* ENABLE_MALICIOUS */ 1689 #endif /* ENABLE_MALICIOUS */
1776 1690
1777 view_size = GNUNET_CONTAINER_multipeermap_size (view); 1691 view_array = View_get_as_array ();
1778 generate_view_array (view_size);
1779 1692
1780 send_pull_reply (peer, view_array, view_size); 1693 send_pull_reply (peer, view_array, View_size ());
1781 1694
1782 GNUNET_CADET_receive_done (channel); 1695 GNUNET_CADET_receive_done (channel);
1783 return GNUNET_OK; 1696 return GNUNET_OK;
@@ -2315,7 +2228,7 @@ do_round (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2315 LOG (GNUNET_ERROR_TYPE_DEBUG, "Going to execute next round.\n"); 2228 LOG (GNUNET_ERROR_TYPE_DEBUG, "Going to execute next round.\n");
2316 2229
2317 uint32_t i; 2230 uint32_t i;
2318 unsigned int view_size; 2231 const struct GNUNET_PeerIdentity *view_array;
2319 unsigned int *permut; 2232 unsigned int *permut;
2320 unsigned int a_peers; /* Number of peers we send pushes to */ 2233 unsigned int a_peers; /* Number of peers we send pushes to */
2321 unsigned int b_peers; /* Number of peers we send pull requests to */ 2234 unsigned int b_peers; /* Number of peers we send pull requests to */
@@ -2330,9 +2243,8 @@ do_round (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2330 "Printing view:\n"); 2243 "Printing view:\n");
2331 to_file (file_name_view_log, 2244 to_file (file_name_view_log,
2332 "___ new round ___"); 2245 "___ new round ___");
2333 view_size = GNUNET_CONTAINER_multipeermap_size (view); 2246 view_array = View_get_as_array ();
2334 generate_view_array (view_size); 2247 for (i = 0; i < View_size (); i++)
2335 for (i = 0 ; i < view_size ; i++)
2336 { 2248 {
2337 LOG (GNUNET_ERROR_TYPE_DEBUG, 2249 LOG (GNUNET_ERROR_TYPE_DEBUG,
2338 "\t%s\n", GNUNET_i2s (&view_array[i])); 2250 "\t%s\n", GNUNET_i2s (&view_array[i]));
@@ -2343,17 +2255,17 @@ do_round (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2343 2255
2344 2256
2345 /* Send pushes and pull requests */ 2257 /* Send pushes and pull requests */
2346 if (0 < view_size) 2258 if (0 < View_size ())
2347 { 2259 {
2348 permut = GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_STRONG, 2260 permut = GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_STRONG,
2349 (unsigned int) view_size); 2261 View_size ());
2350 2262
2351 /* Send PUSHes */ 2263 /* Send PUSHes */
2352 a_peers = ceil (alpha * view_size); 2264 a_peers = ceil (alpha * View_size ());
2353 2265
2354 LOG (GNUNET_ERROR_TYPE_DEBUG, 2266 LOG (GNUNET_ERROR_TYPE_DEBUG,
2355 "Going to send pushes to %u (ceil (%f * %u)) peers.\n", 2267 "Going to send pushes to %u (ceil (%f * %u)) peers.\n",
2356 a_peers, alpha, view_size); 2268 a_peers, alpha, View_size ());
2357 for (i = 0; i < a_peers; i++) 2269 for (i = 0; i < a_peers; i++)
2358 { 2270 {
2359 peer = view_array[permut[i]]; 2271 peer = view_array[permut[i]];
@@ -2364,17 +2276,17 @@ do_round (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2364 } 2276 }
2365 2277
2366 /* Send PULL requests */ 2278 /* Send PULL requests */
2367 b_peers = ceil (beta * view_size); 2279 b_peers = ceil (beta * View_size ());
2368 first_border = a_peers; 2280 first_border = a_peers;
2369 second_border = a_peers + b_peers; 2281 second_border = a_peers + b_peers;
2370 if (second_border > view_size) 2282 if (second_border > View_size ())
2371 { 2283 {
2372 first_border = view_size - b_peers; 2284 first_border = View_size () - b_peers;
2373 second_border = view_size; 2285 second_border = View_size ();
2374 } 2286 }
2375 LOG (GNUNET_ERROR_TYPE_DEBUG, 2287 LOG (GNUNET_ERROR_TYPE_DEBUG,
2376 "Going to send pulls to %u (ceil (%f * %u)) peers.\n", 2288 "Going to send pulls to %u (ceil (%f * %u)) peers.\n",
2377 b_peers, beta, view_size); 2289 b_peers, beta, View_size ());
2378 for (i = first_border; i < second_border; i++) 2290 for (i = first_border; i < second_border; i++)
2379 { 2291 {
2380 peer = view_array[permut[i]]; 2292 peer = view_array[permut[i]];
@@ -2394,7 +2306,7 @@ do_round (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2394 /* Update view */ 2306 /* Update view */
2395 /* TODO see how many peers are in push-/pull- list! */ 2307 /* TODO see how many peers are in push-/pull- list! */
2396 2308
2397 if ((CustomPeerMap_size (push_map) <= alpha * view_size) && 2309 if ((CustomPeerMap_size (push_map) <= alpha * View_size ()) &&
2398 (0 < CustomPeerMap_size (push_map)) && 2310 (0 < CustomPeerMap_size (push_map)) &&
2399 (0 < CustomPeerMap_size (pull_map))) 2311 (0 < CustomPeerMap_size (pull_map)))
2400 { /* If conditions for update are fulfilled, update */ 2312 { /* If conditions for update are fulfilled, update */
@@ -2406,14 +2318,13 @@ do_round (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2406 2318
2407 peers_to_clean = NULL; 2319 peers_to_clean = NULL;
2408 peers_to_clean_size = 0; 2320 peers_to_clean_size = 0;
2409 GNUNET_array_grow (peers_to_clean, peers_to_clean_size, view_size); 2321 GNUNET_array_grow (peers_to_clean, peers_to_clean_size, View_size ());
2410 memcpy (peers_to_clean, 2322 memcpy (peers_to_clean,
2411 view_array, 2323 view_array,
2412 view_size * sizeof (struct GNUNET_PeerIdentity)); 2324 View_size () * sizeof (struct GNUNET_PeerIdentity));
2413 2325
2414 /* Seems like recreating is the easiest way of emptying the peermap */ 2326 /* Seems like recreating is the easiest way of emptying the peermap */
2415 GNUNET_CONTAINER_multipeermap_destroy (view); 2327 View_clear ();
2416 view = GNUNET_CONTAINER_multipeermap_create (view_size, GNUNET_NO);
2417 to_file (file_name_view_log, 2328 to_file (file_name_view_log,
2418 "--- emptied ---"); 2329 "--- emptied ---");
2419 2330
@@ -2425,17 +2336,12 @@ do_round (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2425 final_size = second_border + 2336 final_size = second_border +
2426 ceil ((1 - (alpha + beta)) * sampler_size_est_need); 2337 ceil ((1 - (alpha + beta)) * sampler_size_est_need);
2427 2338
2428 GNUNET_array_grow (view_array, view_size, second_border);
2429
2430 /* Update view with peers received through PUSHes */ 2339 /* Update view with peers received through PUSHes */
2431 permut = GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_STRONG, 2340 permut = GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_STRONG,
2432 CustomPeerMap_size (push_map)); 2341 CustomPeerMap_size (push_map));
2433 for (i = 0; i < first_border; i++) 2342 for (i = 0; i < first_border; i++)
2434 { 2343 {
2435 view_array[i] = *CustomPeerMap_get_peer_by_index (push_map, permut[i]); 2344 View_put (CustomPeerMap_get_peer_by_index (push_map, permut[i]));
2436 GNUNET_CONTAINER_multipeermap_put (view, &view_array[i], NULL,
2437 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
2438
2439 to_file (file_name_view_log, 2345 to_file (file_name_view_log,
2440 "+%s\t(push list)", 2346 "+%s\t(push list)",
2441 GNUNET_i2s_full (&view_array[i])); 2347 GNUNET_i2s_full (&view_array[i]));
@@ -2449,11 +2355,8 @@ do_round (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2449 CustomPeerMap_size (pull_map)); 2355 CustomPeerMap_size (pull_map));
2450 for (i = first_border; i < second_border; i++) 2356 for (i = first_border; i < second_border; i++)
2451 { 2357 {
2452 view_array[i] = 2358 View_put (CustomPeerMap_get_peer_by_index (pull_map,
2453 *CustomPeerMap_get_peer_by_index (pull_map, permut[i - first_border]); 2359 permut[i - first_border]));
2454 GNUNET_CONTAINER_multipeermap_put (view, &view_array[i], NULL,
2455 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
2456
2457 to_file (file_name_view_log, 2360 to_file (file_name_view_log,
2458 "+%s\t(pull list)", 2361 "+%s\t(pull list)",
2459 GNUNET_i2s_full (&view_array[i])); 2362 GNUNET_i2s_full (&view_array[i]));
@@ -2470,7 +2373,7 @@ do_round (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2470 num_hist_update_tasks = final_size - second_border; 2373 num_hist_update_tasks = final_size - second_border;
2471 // TODO change the peer_flags accordingly 2374 // TODO change the peer_flags accordingly
2472 2375
2473 for (i = 0; i < view_size; i++) 2376 for (i = 0; i < View_size (); i++)
2474 rem_from_list (&peers_to_clean, &peers_to_clean_size, &view_array[i]); 2377 rem_from_list (&peers_to_clean, &peers_to_clean_size, &view_array[i]);
2475 2378
2476 /* Clean peers that were removed from the view */ 2379 /* Clean peers that were removed from the view */
@@ -2496,8 +2399,8 @@ do_round (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2496 CustomPeerMap_size (push_map), 2399 CustomPeerMap_size (push_map),
2497 CustomPeerMap_size (pull_map), 2400 CustomPeerMap_size (pull_map),
2498 alpha, 2401 alpha,
2499 view_size, 2402 View_size (),
2500 alpha * view_size); 2403 alpha * View_size ());
2501 2404
2502 /* Update samplers */ 2405 /* Update samplers */
2503 for (i = 0; i < CustomPeerMap_size (push_map); i++) 2406 for (i = 0; i < CustomPeerMap_size (push_map); i++)
@@ -2629,17 +2532,12 @@ peer_remove_cb (void *cls, const struct GNUNET_PeerIdentity *key, void *value)
2629 unset_peer_flag (peer_ctx, PULL_REPLY_PENDING); 2532 unset_peer_flag (peer_ctx, PULL_REPLY_PENDING);
2630 2533
2631 /* Remove peer from view */ 2534 /* Remove peer from view */
2632 if (GNUNET_CONTAINER_multipeermap_contains (view, key)) 2535 if (View_contains_peer (key))
2633 { 2536 {
2634 to_file (file_name_view_log, 2537 to_file (file_name_view_log,
2635 "-%s\t(cleanup channel, other peer)", 2538 "-%s\t(cleanup channel, other peer)",
2636 GNUNET_i2s_full (key)); 2539 GNUNET_i2s_full (key));
2637 GNUNET_CONTAINER_multipeermap_remove_all (view, key); 2540 View_remove_peer (key);
2638 if (NULL != view_array)
2639 {
2640 GNUNET_free (view_array);
2641 view_array = NULL;
2642 }
2643 } 2541 }
2644 2542
2645 /* Remove from push and pull lists */ 2543 /* Remove from push and pull lists */
@@ -2708,7 +2606,7 @@ peer_clean (const struct GNUNET_PeerIdentity *peer)
2708 /* struct GNUNET_CADET_Channel *channel; */ 2606 /* struct GNUNET_CADET_Channel *channel; */
2709 2607
2710 if ( (0 == RPS_sampler_count_id (prot_sampler, peer)) && 2608 if ( (0 == RPS_sampler_count_id (prot_sampler, peer)) &&
2711 (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (view, peer)) && 2609 (GNUNET_NO == View_contains_peer (peer)) &&
2712 (GNUNET_NO == CustomPeerMap_contains_peer (push_map, peer)) && 2610 (GNUNET_NO == CustomPeerMap_contains_peer (push_map, peer)) &&
2713 (GNUNET_NO == CustomPeerMap_contains_peer (pull_map, peer)) && 2611 (GNUNET_NO == CustomPeerMap_contains_peer (pull_map, peer)) &&
2714 (GNUNET_YES == GNUNET_CONTAINER_multipeermap_contains (peer_map, peer)) ) 2612 (GNUNET_YES == GNUNET_CONTAINER_multipeermap_contains (peer_map, peer)) )
@@ -2768,8 +2666,7 @@ shutdown_task (void *cls,
2768 GNUNET_break (0 == GNUNET_CONTAINER_multipeermap_size (peer_map)); 2666 GNUNET_break (0 == GNUNET_CONTAINER_multipeermap_size (peer_map));
2769 GNUNET_CADET_disconnect (cadet_handle); 2667 GNUNET_CADET_disconnect (cadet_handle);
2770 GNUNET_CONTAINER_multipeermap_destroy (peer_map); 2668 GNUNET_CONTAINER_multipeermap_destroy (peer_map);
2771 GNUNET_CONTAINER_multipeermap_destroy (view); 2669 View_destroy ();
2772 view = NULL;
2773 CustomPeerMap_destroy (push_map); 2670 CustomPeerMap_destroy (push_map);
2774 CustomPeerMap_destroy (pull_map); 2671 CustomPeerMap_destroy (pull_map);
2775 #ifdef ENABLE_MALICIOUS 2672 #ifdef ENABLE_MALICIOUS
@@ -3044,7 +2941,7 @@ run (void *cls,
3044 LOG (GNUNET_ERROR_TYPE_DEBUG, "INITSIZE is %" PRIu64 "\n", sampler_size_est_need); 2941 LOG (GNUNET_ERROR_TYPE_DEBUG, "INITSIZE is %" PRIu64 "\n", sampler_size_est_need);
3045 2942
3046 2943
3047 view = GNUNET_CONTAINER_multipeermap_create (4, GNUNET_NO); 2944 View_create (4);
3048 2945
3049 /* file_name_view_log */ 2946 /* file_name_view_log */
3050 if (GNUNET_OK != GNUNET_DISK_directory_create ("/tmp/rps/")) 2947 if (GNUNET_OK != GNUNET_DISK_directory_create ("/tmp/rps/"))