2 #include <gnunet/platform.h>
3 #include <gnunet/gnunet_util_lib.h>
4 #include <gnunet/gnunet_testbed_service.h>
5 #include <gnunet/gnunet_dht_service.h>
7 /* Number of peers we want to start */
10 static struct GNUNET_TESTBED_Operation *dht_op;
12 static struct GNUNET_DHT_Handle *dht_handle;
14 static GNUNET_SCHEDULER_TaskIdentifier shutdown_tid;
18 * Closure to 'dht_ca' and 'dht_da' DHT adapters.
23 * Argument we pass to GNUNET_DHT_connect.
30 * Global result for testcase.
36 * Function run on CTRL-C or shutdown (i.e. success/timeout/etc.).
40 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
42 shutdown_tid = GNUNET_SCHEDULER_NO_TASK;
45 GNUNET_TESTBED_operation_done (dht_op); /* indirectly calls the dht_da() for closing
46 down the connection to the DHT */
51 GNUNET_SCHEDULER_shutdown (); /* Also kills the testbed */
56 * This is where the test logic should be, at least that
57 * part of it that uses the DHT of peer "0".
59 * @param cls closure, for the example: NULL
60 * @param op should be equal to "dht_op"
61 * @param ca_result result of the connect operation, the
62 * connection to the DHT service
63 * @param emsg error message, if testbed somehow failed to
67 service_connect_comp (void *cls,
68 struct GNUNET_TESTBED_Operation *op,
72 GNUNET_assert (op == dht_op);
73 dht_handle = ca_result;
74 /* Service to DHT successful; here we'd usually do something
75 with the DHT (ok, if successful) */
77 /* for now, just indiscriminately terminate after 10s */
78 GNUNET_SCHEDULER_cancel (shutdown_tid);
79 shutdown_tid = GNUNET_SCHEDULER_add_delayed
80 (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10),
81 &shutdown_task, NULL);
86 * Testbed has provided us with the configuration to access one
87 * of the peers and it is time to do "some" connect operation to
88 * "some" subsystem of the peer. For this example, we connect
89 * to the DHT subsystem. Testbed doesn't know which subsystem,
90 * so we need these adapters to do the actual connecting (and
91 * possibly pass additional options to the subsystem connect
92 * function, such as the "ht_len" argument for the DHT).
95 * @param cfg peer configuration (here: peer[0]
96 * @return NULL on error, otherwise some handle to access the
100 dht_ca (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg)
102 struct MyContext *ctxt = cls;
104 /* Use the provided configuration to connect to service */
105 dht_handle = GNUNET_DHT_connect (cfg, ctxt->ht_len);
111 * Dual of 'dht_ca' to perform the 'disconnect'/cleanup operation
112 * once we no longer need to access this subsystem.
115 * @param op_result whatever we returned from 'dht_ca'
118 dht_da (void *cls, void *op_result)
120 struct MyContext *ctxt = cls;
122 /* Disconnect from DHT service */
123 GNUNET_DHT_disconnect ((struct GNUNET_DHT_Handle *) op_result);
129 * Main function inovked from TESTBED once all of the
130 * peers are up and running. This one then connects
131 * just to the DHT service of peer 0.
134 * @param h the run handle
135 * @param peers started peers for the test
136 * @param num_peers size of the 'peers' array
137 * @param links_succeeded number of links between peers that were created
138 * @param links_failed number of links testbed was unable to establish
141 test_master (void *cls,
142 struct GNUNET_TESTBED_RunHandle *h,
143 unsigned int num_peers,
144 struct GNUNET_TESTBED_Peer **peers,
145 unsigned int links_succeeded,
146 unsigned int links_failed)
148 /* Testbed is ready with peers running and connected in a pre-defined overlay
154 /* connect to a peers service */
155 dht_op = GNUNET_TESTBED_service_connect
156 (NULL, /* Closure for operation */
157 peers[0], /* The peer whose service to connect to */
158 "dht", /* The name of the service */
159 service_connect_comp, /* callback to call after a handle to service
161 NULL, /* closure for the above callback */
162 dht_ca, /* callback to call with peer's configuration;
163 this should open the needed service connection */
164 dht_da, /* callback to be called when closing the
165 opened service connection */
166 &ctxt); /* closure for the above two callbacks */
167 shutdown_tid = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
168 &shutdown_task, NULL);
173 main (int argc, char **argv)
177 result = GNUNET_SYSERR;
178 ret = GNUNET_TESTBED_test_run
179 ("awesome-test", /* test case name */
180 "template.conf", /* template configuration */
181 NUM_PEERS, /* number of peers to start */
182 0LL, /* Event mask - set to 0 for no event notifications */
183 NULL, /* Controller event callback */
184 NULL, /* Closure for controller event callback */
185 &test_master, /* continuation callback to be called when testbed setup is
187 NULL); /* Closure for the test_master callback */
188 if ( (GNUNET_OK != ret) || (GNUNET_OK != result) )