GNUnet  0.10.x
dht_test_lib.c
Go to the documentation of this file.
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 */
25 #include "platform.h"
26 #include "dht_test_lib.h"
27 
32 {
37 
42 
47 
52 
56  void *app_main_cls;
57 
61  unsigned int num_peers;
62 
63 };
64 
65 
76 static void *
78  const struct GNUNET_CONFIGURATION_Handle *cfg)
79 {
80  return GNUNET_DHT_connect (cfg, 16);
81 }
82 
83 
91 static void
93  void *op_result)
94 {
96 
98 }
99 
100 
110 static void
111 dht_connect_cb (void *cls,
113  void *ca_result,
114  const char *emsg)
115 {
116  struct GNUNET_DHT_TEST_Context *ctx = cls;
117 
118  if (NULL != emsg)
119  {
120  fprintf (stderr,
121  "Failed to connect to DHT service: %s\n",
122  emsg);
124  return;
125  }
126  for (unsigned int i=0;i<ctx->num_peers;i++)
127  if (op == ctx->ops[i])
128  ctx->dhts[i] = ca_result;
129  for (unsigned int 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,
134  ctx,
135  ctx->num_peers,
136  ctx->peers,
137  ctx->dhts);
138 }
139 
140 
146 void
148 {
149  for (unsigned int i=0;i<ctx->num_peers;i++)
151  GNUNET_free (ctx->ops);
152  GNUNET_free (ctx->dhts);
153  GNUNET_free (ctx);
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",
175  ctx,
178  ctx);
179 }
180 
181 
192 void
193 GNUNET_DHT_TEST_run (const char *testname,
194  const char *cfgname,
195  unsigned int num_peers,
197  void *tmain_cls)
198 {
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 /* end of dht_test_lib.c */
static void dht_test_run(void *cls, struct GNUNET_TESTBED_RunHandle *h, unsigned int num_peers, struct GNUNET_TESTBED_Peer **peers, unsigned int links_succeeded, unsigned int links_failed)
Definition: dht_test_lib.c:159
void GNUNET_DHT_TEST_cleanup(struct GNUNET_DHT_TEST_Context *ctx)
Clean up the testbed.
Definition: dht_test_lib.c:147
int GNUNET_TESTBED_test_run(const char *testname, const char *cfg_filename, unsigned int num_peers, uint64_t event_mask, GNUNET_TESTBED_ControllerCallback cc, void *cc_cls, GNUNET_TESTBED_TestMaster test_master, void *test_master_cls)
Convenience method for running a "simple" test on the local system with a single call from &#39;main&#39;...
unsigned int num_peers
Number of peers running, size of the arrays above.
Definition: dht_test_lib.c:61
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
library for writing DHT tests
static int op_result(struct GNUNET_OP_Handle *h, uint64_t op_id, int64_t result_code, const void *data, uint16_t data_size, void **ctx, uint8_t cancel)
Remove an operation, and call its result callback (unless it was cancelled).
Definition: op.c:244
#define GNUNET_new(type)
Allocate a struct or union of the given type.
Test context for a DHT Test.
Definition: dht_test_lib.c:31
void GNUNET_SCHEDULER_shutdown(void)
Request the shutdown of a scheduler.
Definition: scheduler.c:524
static void dht_disconnect_adapter(void *cls, void *op_result)
Adapter function called to destroy a connection to the DHT service.
Definition: dht_test_lib.c:92
static struct GNUNET_ARM_Handle * h
Connection with ARM.
Definition: gnunet-arm.c:94
static struct GNUNET_DNSSTUB_Context * ctx
Context for DNS resolution.
Connection to the DHT service.
Definition: dht_api.c:205
Opaque handle to an abstract operation to be executed by the testing framework.
void(* GNUNET_DHT_TEST_AppMain)(void *cls, struct GNUNET_DHT_TEST_Context *ctx, unsigned int num_peers, struct GNUNET_TESTBED_Peer **peers, struct GNUNET_DHT_Handle **dhts)
Main function of a DHT test.
Definition: dht_test_lib.h:54
static void tmain(void *cls, struct GNUNET_CADET_TEST_Context *ctx, unsigned int num_peers, struct GNUNET_TESTBED_Peer **testbed_peers, struct GNUNET_CADET_Handle **cadetes)
test main: start test when all peers are connected
static void * dht_connect_adapter(void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg)
Adapter function called to establish a connection to the DHT service.
Definition: dht_test_lib.c:77
struct GNUNET_TESTBED_Operation * GNUNET_TESTBED_service_connect(void *op_cls, struct GNUNET_TESTBED_Peer *peer, const char *service_name, GNUNET_TESTBED_ServiceConnectCompletionCallback cb, void *cb_cls, GNUNET_TESTBED_ConnectAdapter ca, GNUNET_TESTBED_DisconnectAdapter da, void *cada_cls)
Connect to a service offered by the given peer.
struct GNUNET_TESTBED_Peer ** peers
Array of running peers.
Definition: dht_test_lib.c:36
#define GNUNET_new_array(n, type)
Allocate a size n array with structs or unions of the given type.
void GNUNET_TESTBED_operation_done(struct GNUNET_TESTBED_Operation *operation)
This function is used to signal that the event information (struct GNUNET_TESTBED_EventInformation) f...
Definition: testbed_api.c:2046
static struct GNUNET_CONFIGURATION_Handle * cfg
Our configuration.
Definition: gnunet-arm.c:104
struct GNUNET_DHT_Handle * GNUNET_DHT_connect(const struct GNUNET_CONFIGURATION_Handle *cfg, unsigned int ht_len)
Initialize the connection with the DHT service.
Definition: dht_api.c:895
void GNUNET_DHT_disconnect(struct GNUNET_DHT_Handle *handle)
Shutdown connection with the DHT service.
Definition: dht_api.c:923
struct GNUNET_DHT_Handle ** dhts
Array of handles to the DHT for each peer.
Definition: dht_test_lib.c:41
void * app_main_cls
Closure for &#39;app_main&#39;.
Definition: dht_test_lib.c:56
configuration data
Definition: configuration.c:85
static void dht_connect_cb(void *cls, struct GNUNET_TESTBED_Operation *op, void *ca_result, const char *emsg)
Callback to be called when a service connect operation is completed.
Definition: dht_test_lib.c:111
void GNUNET_DHT_TEST_run(const char *testname, const char *cfgname, unsigned int num_peers, GNUNET_DHT_TEST_AppMain tmain, void *tmain_cls)
Run a test using the given name, configuration file and number of peers.
Definition: dht_test_lib.c:193
struct GNUNET_TESTBED_Operation ** ops
Operation associated with the connection to the DHT.
Definition: dht_test_lib.c:46
static struct GNUNET_ARM_Operation * op
Current operation.
Definition: gnunet-arm.c:139
A peer controlled by the testing framework.
static struct GNUNET_DHT_Handle * dht
Handle to the DHT.
GNUNET_DHT_TEST_AppMain app_main
Main function of the test to run once all DHTs are available.
Definition: dht_test_lib.c:51
#define GNUNET_free(ptr)
Wrapper around free.