-remove debug message
[oweals/gnunet.git] / src / dht / dht_test_lib.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2012 GNUnet e.V.
4
5      GNUnet is free software: you can redistribute it and/or modify it
6      under the terms of the GNU Affero General Public License as published
7      by the Free Software Foundation, either version 3 of the License,
8      or (at your option) any later version.
9
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      Affero General Public License for more details.
14
15      You should have received a copy of the GNU Affero General Public License
16      along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18      SPDX-License-Identifier: AGPL3.0-or-later
19  */
20 /**
21  * @file dht/dht_test_lib.c
22  * @author Christian Grothoff
23  * @brief library for writing DHT tests
24  */
25 #include "platform.h"
26 #include "dht_test_lib.h"
27
28 /**
29  * Test context for a DHT Test.
30  */
31 struct GNUNET_DHT_TEST_Context
32 {
33   /**
34    * Array of running peers.
35    */
36   struct GNUNET_TESTBED_Peer **peers;
37
38   /**
39    * Array of handles to the DHT for each peer.
40    */
41   struct GNUNET_DHT_Handle **dhts;
42
43   /**
44    * Operation associated with the connection to the DHT.
45    */
46   struct GNUNET_TESTBED_Operation **ops;
47
48   /**
49    * Main function of the test to run once all DHTs are available.
50    */
51   GNUNET_DHT_TEST_AppMain app_main;
52
53   /**
54    * Closure for 'app_main'.
55    */
56   void *app_main_cls;
57
58   /**
59    * Number of peers running, size of the arrays above.
60    */
61   unsigned int num_peers;
62 };
63
64
65 /**
66  * Adapter function called to establish a connection to
67  * the DHT service.
68  *
69  * @param cls closure
70  * @param cfg configuration of the peer to connect to; will be available until
71  *          GNUNET_TESTBED_operation_done() is called on the operation returned
72  *          from GNUNET_TESTBED_service_connect()
73  * @return service handle to return in 'op_result', NULL on error
74  */
75 static void *
76 dht_connect_adapter (void *cls,
77                      const struct GNUNET_CONFIGURATION_Handle *cfg)
78 {
79   return GNUNET_DHT_connect (cfg, 16);
80 }
81
82
83 /**
84  * Adapter function called to destroy a connection to
85  * the DHT service.
86  *
87  * @param cls closure
88  * @param op_result service handle returned from the connect adapter
89  */
90 static void
91 dht_disconnect_adapter (void *cls,
92                         void *op_result)
93 {
94   struct GNUNET_DHT_Handle *dht = op_result;
95
96   GNUNET_DHT_disconnect (dht);
97 }
98
99
100 /**
101  * Callback to be called when a service connect operation is completed
102  *
103  * @param cls the callback closure from functions generating an operation
104  * @param op the operation that has been finished
105  * @param ca_result the service handle returned from GNUNET_TESTBED_ConnectAdapter()
106  * @param emsg error message in case the operation has failed; will be NULL if
107  *          operation has executed successfully.
108  */
109 static void
110 dht_connect_cb (void *cls,
111                 struct GNUNET_TESTBED_Operation *op,
112                 void *ca_result,
113                 const char *emsg)
114 {
115   struct GNUNET_DHT_TEST_Context *ctx = cls;
116
117   if (NULL != emsg)
118   {
119     fprintf (stderr,
120              "Failed to connect to DHT service: %s\n",
121              emsg);
122     GNUNET_SCHEDULER_shutdown ();
123     return;
124   }
125   for (unsigned int i = 0; i < ctx->num_peers; i++)
126     if (op == ctx->ops[i])
127       ctx->dhts[i] = ca_result;
128   for (unsigned int i = 0; i < ctx->num_peers; i++)
129     if (NULL == ctx->dhts[i])
130       return;
131   /* still some DHT connections missing */
132   /* all DHT connections ready! */
133   ctx->app_main (ctx->app_main_cls,
134                  ctx,
135                  ctx->num_peers,
136                  ctx->peers,
137                  ctx->dhts);
138 }
139
140
141 /**
142  * Clean up the testbed.
143  *
144  * @param ctx handle for the testbed
145  */
146 void
147 GNUNET_DHT_TEST_cleanup (struct GNUNET_DHT_TEST_Context *ctx)
148 {
149   for (unsigned int i = 0; i < ctx->num_peers; i++)
150     GNUNET_TESTBED_operation_done (ctx->ops[i]);
151   GNUNET_free (ctx->ops);
152   GNUNET_free (ctx->dhts);
153   GNUNET_free (ctx);
154   GNUNET_SCHEDULER_shutdown ();
155 }
156
157
158 static void
159 dht_test_run (void *cls,
160               struct GNUNET_TESTBED_RunHandle *h,
161               unsigned int num_peers,
162               struct GNUNET_TESTBED_Peer **peers,
163               unsigned int links_succeeded,
164               unsigned int links_failed)
165 {
166   struct GNUNET_DHT_TEST_Context *ctx = cls;
167
168   GNUNET_assert (num_peers == ctx->num_peers);
169   ctx->peers = peers;
170   for (unsigned int i = 0; i < num_peers; i++)
171     ctx->ops[i] = GNUNET_TESTBED_service_connect (ctx,
172                                                   peers[i],
173                                                   "dht",
174                                                   &dht_connect_cb,
175                                                   ctx,
176                                                   &dht_connect_adapter,
177                                                   &dht_disconnect_adapter,
178                                                   ctx);
179 }
180
181
182 /**
183  * Run a test using the given name, configuration file and number of
184  * peers.
185  *
186  * @param testname name of the test (for logging)
187  * @param cfgname name of the configuration file
188  * @param num_peers number of peers to start
189  * @param tmain main function to run once the testbed is ready
190  * @param tmain_cls closure for 'tmain'
191  */
192 void
193 GNUNET_DHT_TEST_run (const char *testname,
194                      const char *cfgname,
195                      unsigned int num_peers,
196                      GNUNET_DHT_TEST_AppMain tmain,
197                      void *tmain_cls)
198 {
199   struct GNUNET_DHT_TEST_Context *ctx;
200
201   ctx = GNUNET_new (struct GNUNET_DHT_TEST_Context);
202   ctx->num_peers = num_peers;
203   ctx->ops = GNUNET_new_array (num_peers,
204                                struct GNUNET_TESTBED_Operation *);
205   ctx->dhts = GNUNET_new_array (num_peers,
206                                 struct GNUNET_DHT_Handle *);
207   ctx->app_main = tmain;
208   ctx->app_main_cls = tmain_cls;
209   (void) GNUNET_TESTBED_test_run (testname,
210                                   cfgname,
211                                   num_peers,
212                                   0LL, NULL, NULL,
213                                   &dht_test_run, ctx);
214 }
215
216
217 /* end of dht_test_lib.c */