aboutsummaryrefslogtreecommitdiff
path: root/src/dht/dht_test_lib.c
blob: 4c1bd3057e0e737de372f2f2d02896a4f0865d8c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/*
     This file is part of GNUnet.
     Copyright (C) 2012 GNUnet e.V.

     GNUnet is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published
     by the Free Software Foundation; either version 3, or (at your
     option) any later version.

     GNUnet is distributed in the hope that it will be useful, but
     WITHOUT ANY WARRANTY; without even the implied warranty of
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     General Public License for more details.

     You should have received a copy of the GNU General Public License
     along with GNUnet; see the file COPYING.  If not, write to the
     Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
     Boston, MA 02110-1301, USA.
*/
/**
 * @file dht/dht_test_lib.c
 * @author Christian Grothoff
 * @brief library for writing DHT tests
 */
#include "platform.h"
#include "dht_test_lib.h"

/**
 * Test context for a DHT Test.
 */
struct GNUNET_DHT_TEST_Context
{
  /**
   * Array of running peers.
   */
  struct GNUNET_TESTBED_Peer **peers;

  /**
   * Array of handles to the DHT for each peer.
   */
  struct GNUNET_DHT_Handle **dhts;

  /**
   * Operation associated with the connection to the DHT.
   */
  struct GNUNET_TESTBED_Operation **ops;

  /**
   * Main function of the test to run once all DHTs are available.
   */
  GNUNET_DHT_TEST_AppMain app_main;

  /**
   * Closure for 'app_main'.
   */
  void *app_main_cls;

  /**
   * Number of peers running, size of the arrays above.
   */
  unsigned int num_peers;

};


/**
 * Adapter function called to establish a connection to
 * the DHT service.
 *
 * @param cls closure
 * @param cfg configuration of the peer to connect to; will be available until
 *          GNUNET_TESTBED_operation_done() is called on the operation returned
 *          from GNUNET_TESTBED_service_connect()
 * @return service handle to return in 'op_result', NULL on error
 */
static void *
dht_connect_adapter (void *cls,
		     const struct GNUNET_CONFIGURATION_Handle *cfg)
{
  return GNUNET_DHT_connect (cfg, 16);
}


/**
 * Adapter function called to destroy a connection to
 * the DHT service.
 *
 * @param cls closure
 * @param op_result service handle returned from the connect adapter
 */
static void
dht_disconnect_adapter (void *cls,
			void *op_result)
{
  struct GNUNET_DHT_Handle *dht = op_result;

  GNUNET_DHT_disconnect (dht);
}


/**
 * Callback to be called when a service connect operation is completed
 *
 * @param cls the callback closure from functions generating an operation
 * @param op the operation that has been finished
 * @param ca_result the service handle returned from GNUNET_TESTBED_ConnectAdapter()
 * @param emsg error message in case the operation has failed; will be NULL if
 *          operation has executed successfully.
 */
static void
dht_connect_cb (void *cls,
		struct GNUNET_TESTBED_Operation *op,
		void *ca_result,
		const char *emsg)
{
  struct GNUNET_DHT_TEST_Context *ctx = cls;
  unsigned int i;

  if (NULL != emsg)
  {
    fprintf (stderr,
             "Failed to connect to DHT service: %s\n",
	     emsg);
    GNUNET_SCHEDULER_shutdown ();
    return;
  }
  for (i=0;i<ctx->num_peers;i++)
    if (op == ctx->ops[i])
      ctx->dhts[i] = ca_result;
  for (i=0;i<ctx->num_peers;i++)
    if (NULL == ctx->dhts[i])
      return; /* still some DHT connections missing */
  /* all DHT connections ready! */
  ctx->app_main (ctx->app_main_cls,
		 ctx,
		 ctx->num_peers,
		 ctx->peers,
		 ctx->dhts);
}


/**
 * Clean up the testbed.
 *
 * @param ctx handle for the testbed
 */
void
GNUNET_DHT_TEST_cleanup (struct GNUNET_DHT_TEST_Context *ctx)
{
  unsigned int i;

  for (i=0;i<ctx->num_peers;i++)
    GNUNET_TESTBED_operation_done (ctx->ops[i]);
  GNUNET_free (ctx->ops);
  GNUNET_free (ctx->dhts);
  GNUNET_free (ctx);
  GNUNET_SCHEDULER_shutdown ();
}


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)
{
  struct GNUNET_DHT_TEST_Context *ctx = cls;
  unsigned int i;

  GNUNET_assert (num_peers == ctx->num_peers);
  ctx->peers = peers;
  for (i=0;i<num_peers;i++)
    ctx->ops[i] = GNUNET_TESTBED_service_connect (ctx,
						  peers[i],
						  "dht",
						  &dht_connect_cb,
						  ctx,
						  &dht_connect_adapter,
						  &dht_disconnect_adapter,
						  ctx);
}


/**
 * Run a test using the given name, configuration file and number of
 * peers.
 *
 * @param testname name of the test (for logging)
 * @param cfgname name of the configuration file
 * @param num_peers number of peers to start
 * @param tmain main function to run once the testbed is ready
 * @param tmain_cls closure for 'tmain'
 */
void
GNUNET_DHT_TEST_run (const char *testname,
		     const char *cfgname,
		     unsigned int num_peers,
		     GNUNET_DHT_TEST_AppMain tmain,
		     void *tmain_cls)
{
  struct GNUNET_DHT_TEST_Context *ctx;

  ctx = GNUNET_new (struct GNUNET_DHT_TEST_Context);
  ctx->num_peers = num_peers;
  ctx->ops = GNUNET_new_array (num_peers,
                               struct GNUNET_TESTBED_Operation *);
  ctx->dhts = GNUNET_new_array (num_peers,
                                struct GNUNET_DHT_Handle *);
  ctx->app_main = tmain;
  ctx->app_main_cls = tmain_cls;
  (void) GNUNET_TESTBED_test_run (testname,
                                  cfgname,
                                  num_peers,
                                  0LL, NULL, NULL,
                                  &dht_test_run, ctx);
}

/* end of dht_test_lib.c */