2 This file is part of GNUnet.
3 Copyright (C) 2012 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
21 * @file dht/dht_test_lib.c
22 * @author Christian Grothoff
23 * @brief library for writing DHT tests
26 #include "dht_test_lib.h"
29 * Test context for a DHT Test.
31 struct GNUNET_DHT_TEST_Context
34 * Array of running peers.
36 struct GNUNET_TESTBED_Peer **peers;
39 * Array of handles to the DHT for each peer.
41 struct GNUNET_DHT_Handle **dhts;
44 * Operation associated with the connection to the DHT.
46 struct GNUNET_TESTBED_Operation **ops;
49 * Main function of the test to run once all DHTs are available.
51 GNUNET_DHT_TEST_AppMain app_main;
54 * Closure for 'app_main'.
59 * Number of peers running, size of the arrays above.
61 unsigned int num_peers;
67 * Adapter function called to establish a connection to
71 * @param cfg configuration of the peer to connect to; will be available until
72 * GNUNET_TESTBED_operation_done() is called on the operation returned
73 * from GNUNET_TESTBED_service_connect()
74 * @return service handle to return in 'op_result', NULL on error
77 dht_connect_adapter (void *cls,
78 const struct GNUNET_CONFIGURATION_Handle *cfg)
80 return GNUNET_DHT_connect (cfg, 16);
85 * Adapter function called to destroy a connection to
89 * @param op_result service handle returned from the connect adapter
92 dht_disconnect_adapter (void *cls,
95 struct GNUNET_DHT_Handle *dht = op_result;
97 GNUNET_DHT_disconnect (dht);
102 * Callback to be called when a service connect operation is completed
104 * @param cls the callback closure from functions generating an operation
105 * @param op the operation that has been finished
106 * @param ca_result the service handle returned from GNUNET_TESTBED_ConnectAdapter()
107 * @param emsg error message in case the operation has failed; will be NULL if
108 * operation has executed successfully.
111 dht_connect_cb (void *cls,
112 struct GNUNET_TESTBED_Operation *op,
116 struct GNUNET_DHT_TEST_Context *ctx = cls;
121 fprintf (stderr, "Failed to connect to DHT service: %s\n",
123 GNUNET_SCHEDULER_shutdown ();
126 for (i=0;i<ctx->num_peers;i++)
127 if (op == ctx->ops[i])
128 ctx->dhts[i] = ca_result;
129 for (i=0;i<ctx->num_peers;i++)
130 if (NULL == ctx->dhts[i])
131 return; /* still some DHT connections missing */
132 /* all DHT connections ready! */
133 ctx->app_main (ctx->app_main_cls,
142 * Clean up the testbed.
144 * @param ctx handle for the testbed
147 GNUNET_DHT_TEST_cleanup (struct GNUNET_DHT_TEST_Context *ctx)
151 for (i=0;i<ctx->num_peers;i++)
152 GNUNET_TESTBED_operation_done (ctx->ops[i]);
153 GNUNET_free (ctx->ops);
154 GNUNET_free (ctx->dhts);
156 GNUNET_SCHEDULER_shutdown ();
161 dht_test_run (void *cls,
162 struct GNUNET_TESTBED_RunHandle *h,
163 unsigned int num_peers,
164 struct GNUNET_TESTBED_Peer **peers,
165 unsigned int links_succeeded,
166 unsigned int links_failed)
168 struct GNUNET_DHT_TEST_Context *ctx = cls;
171 GNUNET_assert (num_peers == ctx->num_peers);
173 for (i=0;i<num_peers;i++)
174 ctx->ops[i] = GNUNET_TESTBED_service_connect (ctx,
179 &dht_connect_adapter,
180 &dht_disconnect_adapter,
186 * Run a test using the given name, configuration file and number of
189 * @param testname name of the test (for logging)
190 * @param cfgname name of the configuration file
191 * @param num_peers number of peers to start
192 * @param tmain main function to run once the testbed is ready
193 * @param tmain_cls closure for 'tmain'
196 GNUNET_DHT_TEST_run (const char *testname,
198 unsigned int num_peers,
199 GNUNET_DHT_TEST_AppMain tmain,
202 struct GNUNET_DHT_TEST_Context *ctx;
204 ctx = GNUNET_new (struct GNUNET_DHT_TEST_Context);
205 ctx->num_peers = num_peers;
206 ctx->ops = GNUNET_malloc (num_peers * sizeof (struct GNUNET_TESTBED_Operation *));
207 ctx->dhts = GNUNET_malloc (num_peers * sizeof (struct GNUNET_DHT_Handle *));
208 ctx->app_main = tmain;
209 ctx->app_main_cls = tmain_cls;
210 (void) GNUNET_TESTBED_test_run (testname,
217 /* end of dht_test_lib.c */