diff options
author | Nathan S. Evans <evans@in.tum.de> | 2010-08-03 08:22:04 +0000 |
---|---|---|
committer | Nathan S. Evans <evans@in.tum.de> | 2010-08-03 08:22:04 +0000 |
commit | 6805357073d73c41c8712c8e852f3a95030bad89 (patch) | |
tree | a3c627aba3c84adcfeb15460b216fca2641294c0 /src | |
parent | 0b1207500e87f664ac45f1bdea94e99e2c68cf4d (diff) | |
download | gnunet-6805357073d73c41c8712c8e852f3a95030bad89.tar.gz gnunet-6805357073d73c41c8712c8e852f3a95030bad89.zip |
testing function to get current topology from peer group
Diffstat (limited to 'src')
-rw-r--r-- | src/testing/testing_group.c | 135 |
1 files changed, 135 insertions, 0 deletions
diff --git a/src/testing/testing_group.c b/src/testing/testing_group.c index 4de4da697..36734ba48 100644 --- a/src/testing/testing_group.c +++ b/src/testing/testing_group.c | |||
@@ -28,6 +28,7 @@ | |||
28 | #include "platform.h" | 28 | #include "platform.h" |
29 | #include "gnunet_arm_service.h" | 29 | #include "gnunet_arm_service.h" |
30 | #include "gnunet_testing_lib.h" | 30 | #include "gnunet_testing_lib.h" |
31 | #include "gnunet_core_service.h" | ||
31 | 32 | ||
32 | #define VERBOSE_TESTING GNUNET_NO | 33 | #define VERBOSE_TESTING GNUNET_NO |
33 | 34 | ||
@@ -304,6 +305,39 @@ struct HostData | |||
304 | uint16_t minport; | 305 | uint16_t minport; |
305 | }; | 306 | }; |
306 | 307 | ||
308 | struct TopologyIterateContext | ||
309 | { | ||
310 | /** | ||
311 | * Callback for notifying of two connected peers. | ||
312 | */ | ||
313 | GNUNET_TESTING_NotifyTopology topology_cb; | ||
314 | |||
315 | /** | ||
316 | * Closure for topology_cb | ||
317 | */ | ||
318 | void *cls; | ||
319 | |||
320 | /** | ||
321 | * Number of peers currently connected to. | ||
322 | */ | ||
323 | unsigned int connected; | ||
324 | |||
325 | /** | ||
326 | * Number of peers we have finished iterating. | ||
327 | */ | ||
328 | unsigned int completed; | ||
329 | |||
330 | /** | ||
331 | * Number of peers total. | ||
332 | */ | ||
333 | unsigned int total; | ||
334 | }; | ||
335 | |||
336 | struct TopologyCoreContext | ||
337 | { | ||
338 | struct TopologyIterateContext *topology_context; | ||
339 | struct GNUNET_TESTING_Daemon *daemon; | ||
340 | }; | ||
307 | 341 | ||
308 | /** | 342 | /** |
309 | * Handle to a group of GNUnet peers. | 343 | * Handle to a group of GNUnet peers. |
@@ -1946,6 +1980,13 @@ static void internal_connect_notify (void *cls, | |||
1946 | } | 1980 | } |
1947 | 1981 | ||
1948 | 1982 | ||
1983 | /** | ||
1984 | * Either delay a connection (because there are too many outstanding) | ||
1985 | * or schedule it for right now. | ||
1986 | * | ||
1987 | * @param cls a connection context | ||
1988 | * @param tc the task runtime context | ||
1989 | */ | ||
1949 | static void schedule_connect(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) | 1990 | static void schedule_connect(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) |
1950 | { | 1991 | { |
1951 | struct ConnectContext *connect_context = cls; | 1992 | struct ConnectContext *connect_context = cls; |
@@ -2707,6 +2748,100 @@ perform_dfs (struct GNUNET_TESTING_PeerGroup *pg, unsigned int num) | |||
2707 | } | 2748 | } |
2708 | 2749 | ||
2709 | /** | 2750 | /** |
2751 | * Internal callback for topology information for a particular peer. | ||
2752 | */ | ||
2753 | static void | ||
2754 | internal_topology_callback(void *cls, | ||
2755 | const struct GNUNET_PeerIdentity *peer, | ||
2756 | struct GNUNET_TIME_Relative latency, uint32_t distance) | ||
2757 | { | ||
2758 | struct TopologyCoreContext *core_ctx = cls; | ||
2759 | struct TopologyIterateContext *iter_ctx = core_ctx->topology_context; | ||
2760 | |||
2761 | if (peer == NULL) /* Either finished, or something went wrongo */ | ||
2762 | { | ||
2763 | iter_ctx->completed++; | ||
2764 | iter_ctx->connected--; | ||
2765 | } | ||
2766 | else | ||
2767 | { | ||
2768 | iter_ctx->topology_cb(iter_ctx->cls, &core_ctx->daemon->id, peer, latency, distance, NULL); | ||
2769 | } | ||
2770 | |||
2771 | if (iter_ctx->completed == iter_ctx->total) | ||
2772 | { | ||
2773 | iter_ctx->topology_cb(iter_ctx->cls, NULL, NULL, GNUNET_TIME_relative_get_zero(), 0, NULL); | ||
2774 | GNUNET_free(iter_ctx); | ||
2775 | GNUNET_free(core_ctx); | ||
2776 | } | ||
2777 | } | ||
2778 | |||
2779 | |||
2780 | /** | ||
2781 | * Check running topology iteration tasks, if below max start a new one, otherwise | ||
2782 | * schedule for some time in the future. | ||
2783 | */ | ||
2784 | static void | ||
2785 | schedule_get_topology(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) | ||
2786 | { | ||
2787 | struct TopologyCoreContext *core_context = cls; | ||
2788 | |||
2789 | if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN) | ||
2790 | return; | ||
2791 | |||
2792 | if (core_context->topology_context->connected > MAX_OUTSTANDING_CONNECTIONS) | ||
2793 | { | ||
2794 | #if VERBOSE_TESTING > 2 | ||
2795 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
2796 | _("Delaying connect, we have too many outstanding connections!\n")); | ||
2797 | #endif | ||
2798 | GNUNET_SCHEDULER_add_delayed(core_context->daemon->sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 100), &schedule_connect, core_context); | ||
2799 | } | ||
2800 | else | ||
2801 | { | ||
2802 | #if VERBOSE_TESTING > 2 | ||
2803 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
2804 | _("Creating connection, outstanding_connections is %d\n"), outstanding_connects); | ||
2805 | #endif | ||
2806 | core_context->topology_context->connected++; | ||
2807 | if (GNUNET_OK != GNUNET_CORE_iterate_peers (core_context->daemon->sched, core_context->daemon->cfg, &internal_topology_callback, core_context)) | ||
2808 | internal_topology_callback(core_context, NULL, GNUNET_TIME_relative_get_zero(), 0); | ||
2809 | |||
2810 | } | ||
2811 | } | ||
2812 | |||
2813 | /** | ||
2814 | * Iterate over all (running) peers in the peer group, retrieve | ||
2815 | * all connections that each currently has. | ||
2816 | */ | ||
2817 | void | ||
2818 | GNUNET_TESTING_get_topology (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_NotifyTopology cb, void *cls) | ||
2819 | { | ||
2820 | struct TopologyIterateContext *topology_context; | ||
2821 | struct TopologyCoreContext *core_ctx; | ||
2822 | unsigned int i; | ||
2823 | unsigned int total_count; | ||
2824 | |||
2825 | topology_context = GNUNET_malloc(sizeof(struct TopologyIterateContext)); | ||
2826 | topology_context->topology_cb = cb; | ||
2827 | topology_context->cls = cls; | ||
2828 | total_count = 0; | ||
2829 | for (i = 0; i < pg->total; i++) | ||
2830 | { | ||
2831 | if (pg->peers[i].daemon->running == GNUNET_YES) | ||
2832 | { | ||
2833 | core_ctx = GNUNET_malloc(sizeof(struct TopologyCoreContext)); | ||
2834 | core_ctx->daemon = pg->peers[i].daemon; | ||
2835 | core_ctx->topology_context = topology_context; | ||
2836 | GNUNET_SCHEDULER_add_now(pg->sched, &schedule_get_topology, core_ctx); | ||
2837 | total_count++; | ||
2838 | } | ||
2839 | } | ||
2840 | topology_context->total = total_count; | ||
2841 | return; | ||
2842 | } | ||
2843 | |||
2844 | /** | ||
2710 | * There are many ways to connect peers that are supported by this function. | 2845 | * There are many ways to connect peers that are supported by this function. |
2711 | * To connect peers in the same topology that was created via the | 2846 | * To connect peers in the same topology that was created via the |
2712 | * GNUNET_TESTING_create_topology, the topology variable must be set to | 2847 | * GNUNET_TESTING_create_topology, the topology variable must be set to |