aboutsummaryrefslogtreecommitdiff
path: root/doc/testbed_test.c
diff options
context:
space:
mode:
Diffstat (limited to 'doc/testbed_test.c')
-rw-r--r--doc/testbed_test.c112
1 files changed, 10 insertions, 102 deletions
diff --git a/doc/testbed_test.c b/doc/testbed_test.c
index 0ef0980af..1696234b0 100644
--- a/doc/testbed_test.c
+++ b/doc/testbed_test.c
@@ -4,65 +4,35 @@
4#include <gnunet/gnunet_testbed_service.h> 4#include <gnunet/gnunet_testbed_service.h>
5#include <gnunet/gnunet_dht_service.h> 5#include <gnunet/gnunet_dht_service.h>
6 6
7/* Number of peers we want to start */
8#define NUM_PEERS 20 7#define NUM_PEERS 20
9 8
10static struct GNUNET_TESTBED_Operation *dht_op; 9static struct GNUNET_TESTBED_Operation *dht_op;
11 10
12static struct GNUNET_DHT_Handle *dht_handle; 11static struct GNUNET_DHT_Handle *dht_handle;
13 12
14static struct GNUNET_SCHEDULER_Task * shutdown_tid;
15 13
16
17/**
18 * Closure to 'dht_ca' and 'dht_da' DHT adapters.
19 */
20struct MyContext 14struct MyContext
21{ 15{
22 /**
23 * Argument we pass to GNUNET_DHT_connect.
24 */
25 int ht_len; 16 int ht_len;
26} ctxt; 17} ctxt;
27 18
28 19
29/**
30 * Global result for testcase.
31 */
32static int result; 20static int result;
33 21
34 22
35/**
36 * Function run on CTRL-C or shutdown (i.e. success/timeout/etc.).
37 * Cleans up.
38 */
39static void 23static void
40shutdown_task (void *cls) 24shutdown_task (void *cls)
41{ 25{
42 shutdown_tid = NULL;
43 if (NULL != dht_op) 26 if (NULL != dht_op)
44 { 27 {
45 GNUNET_TESTBED_operation_done (dht_op); /* indirectly calls the dht_da() for closing 28 GNUNET_TESTBED_operation_done (dht_op);
46 down the connection to the DHT */
47 dht_op = NULL; 29 dht_op = NULL;
48 dht_handle = NULL; 30 dht_handle = NULL;
49 } 31 }
50 result = GNUNET_OK; 32 result = GNUNET_OK;
51 GNUNET_SCHEDULER_shutdown (); /* Also kills the testbed */
52} 33}
53 34
54 35
55/**
56 * This is where the test logic should be, at least that
57 * part of it that uses the DHT of peer "0".
58 *
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
64 * connect to the DHT.
65 */
66static void 36static void
67service_connect_comp (void *cls, 37service_connect_comp (void *cls,
68 struct GNUNET_TESTBED_Operation *op, 38 struct GNUNET_TESTBED_Operation *op,
@@ -71,72 +41,31 @@ service_connect_comp (void *cls,
71{ 41{
72 GNUNET_assert (op == dht_op); 42 GNUNET_assert (op == dht_op);
73 dht_handle = ca_result; 43 dht_handle = ca_result;
74 /* Service to DHT successful; here we'd usually do something 44 // Do work here...
75 with the DHT (ok, if successful) */ 45 GNUNET_SCHEDULER_shutdown ();
76
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);
82} 46}
83 47
84 48
85/**
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).
93 *
94 * @param cls closure
95 * @param cfg peer configuration (here: peer[0]
96 * @return NULL on error, otherwise some handle to access the
97 * subsystem
98 */
99static void * 49static void *
100dht_ca (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg) 50dht_ca (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg)
101{ 51{
102 struct MyContext *ctxt = cls; 52 struct MyContext *ctxt = cls;
103 53
104 /* Use the provided configuration to connect to service */
105 dht_handle = GNUNET_DHT_connect (cfg, ctxt->ht_len); 54 dht_handle = GNUNET_DHT_connect (cfg, ctxt->ht_len);
106 return dht_handle; 55 return dht_handle;
107} 56}
108 57
109 58
110/**
111 * Dual of 'dht_ca' to perform the 'disconnect'/cleanup operation
112 * once we no longer need to access this subsystem.
113 *
114 * @param cls closure
115 * @param op_result whatever we returned from 'dht_ca'
116 */
117static void 59static void
118dht_da (void *cls, void *op_result) 60dht_da (void *cls, void *op_result)
119{ 61{
120 struct MyContext *ctxt = cls; 62 struct MyContext *ctxt = cls;
121 63
122 /* Disconnect from DHT service */
123 GNUNET_DHT_disconnect ((struct GNUNET_DHT_Handle *) op_result); 64 GNUNET_DHT_disconnect ((struct GNUNET_DHT_Handle *) op_result);
124 dht_handle = NULL; 65 dht_handle = NULL;
125} 66}
126 67
127 68
128/**
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.
132 *
133 * @param cls closure
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
139 */
140static void 69static void
141test_master (void *cls, 70test_master (void *cls,
142 struct GNUNET_TESTBED_RunHandle *h, 71 struct GNUNET_TESTBED_RunHandle *h,
@@ -145,27 +74,12 @@ test_master (void *cls,
145 unsigned int links_succeeded, 74 unsigned int links_succeeded,
146 unsigned int links_failed) 75 unsigned int links_failed)
147{ 76{
148 /* Testbed is ready with peers running and connected in a pre-defined overlay
149 topology */
150
151 /* do something */
152 ctxt.ht_len = 10; 77 ctxt.ht_len = 10;
153
154 /* connect to a peers service */
155 dht_op = GNUNET_TESTBED_service_connect 78 dht_op = GNUNET_TESTBED_service_connect
156 (NULL, /* Closure for operation */ 79 (NULL, peers[0], "dht",
157 peers[0], /* The peer whose service to connect to */ 80 &service_connect_comp, NULL,
158 "dht", /* The name of the service */ 81 &dht_ca, &dht_da, &ctxt);
159 service_connect_comp, /* callback to call after a handle to service 82 GNUNET_SCHEDULER_add_shutdown (&shutdown_task, NULL);
160 is opened */
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);
169} 83}
170 84
171 85
@@ -176,15 +90,9 @@ main (int argc, char **argv)
176 90
177 result = GNUNET_SYSERR; 91 result = GNUNET_SYSERR;
178 ret = GNUNET_TESTBED_test_run 92 ret = GNUNET_TESTBED_test_run
179 ("awesome-test", /* test case name */ 93 ("awesome-test", "template.conf",
180 "template.conf", /* template configuration */ 94 NUM_PEERS, 0LL,
181 NUM_PEERS, /* number of peers to start */ 95 NULL, NULL, &test_master, NULL);
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
186 complete */
187 NULL); /* Closure for the test_master callback */
188 if ( (GNUNET_OK != ret) || (GNUNET_OK != result) ) 96 if ( (GNUNET_OK != ret) || (GNUNET_OK != result) )
189 return 1; 97 return 1;
190 return 0; 98 return 0;