diff options
Diffstat (limited to 'doc/old/tutorial/examples/testbed_test.c')
-rw-r--r-- | doc/old/tutorial/examples/testbed_test.c | 100 |
1 files changed, 100 insertions, 0 deletions
diff --git a/doc/old/tutorial/examples/testbed_test.c b/doc/old/tutorial/examples/testbed_test.c new file mode 100644 index 000000000..1a8c25655 --- /dev/null +++ b/doc/old/tutorial/examples/testbed_test.c | |||
@@ -0,0 +1,100 @@ | |||
1 | #include "platform.h" | ||
2 | #include <unistd.h> | ||
3 | #include <gnunet/platform.h> | ||
4 | #include <gnunet/gnunet_util_lib.h> | ||
5 | #include <gnunet/gnunet_testbed_service.h> | ||
6 | #include <gnunet/gnunet_dht_service.h> | ||
7 | |||
8 | #define NUM_PEERS 20 | ||
9 | |||
10 | static struct GNUNET_TESTBED_Operation *dht_op; | ||
11 | |||
12 | static struct GNUNET_DHT_Handle *dht_handle; | ||
13 | |||
14 | |||
15 | struct MyContext | ||
16 | { | ||
17 | int ht_len; | ||
18 | } ctxt; | ||
19 | |||
20 | |||
21 | static int result; | ||
22 | |||
23 | |||
24 | static void | ||
25 | shutdown_task (void *cls) | ||
26 | { | ||
27 | if (NULL != dht_op) | ||
28 | { | ||
29 | GNUNET_TESTBED_operation_done (dht_op); | ||
30 | dht_op = NULL; | ||
31 | dht_handle = NULL; | ||
32 | } | ||
33 | result = GNUNET_OK; | ||
34 | } | ||
35 | |||
36 | |||
37 | static void | ||
38 | service_connect_comp (void *cls, | ||
39 | struct GNUNET_TESTBED_Operation *op, | ||
40 | void *ca_result, | ||
41 | const char *emsg) | ||
42 | { | ||
43 | GNUNET_assert (op == dht_op); | ||
44 | dht_handle = ca_result; | ||
45 | // Do work here... | ||
46 | GNUNET_SCHEDULER_shutdown (); | ||
47 | } | ||
48 | |||
49 | |||
50 | static void * | ||
51 | dht_ca (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg) | ||
52 | { | ||
53 | struct MyContext *ctxt = cls; | ||
54 | |||
55 | dht_handle = GNUNET_DHT_connect (cfg, ctxt->ht_len); | ||
56 | return dht_handle; | ||
57 | } | ||
58 | |||
59 | |||
60 | static void | ||
61 | dht_da (void *cls, void *op_result) | ||
62 | { | ||
63 | struct MyContext *ctxt = cls; | ||
64 | |||
65 | GNUNET_DHT_disconnect ((struct GNUNET_DHT_Handle *) op_result); | ||
66 | dht_handle = NULL; | ||
67 | } | ||
68 | |||
69 | |||
70 | static void | ||
71 | test_master (void *cls, | ||
72 | struct GNUNET_TESTBED_RunHandle *h, | ||
73 | unsigned int num_peers, | ||
74 | struct GNUNET_TESTBED_Peer **peers, | ||
75 | unsigned int links_succeeded, | ||
76 | unsigned int links_failed) | ||
77 | { | ||
78 | ctxt.ht_len = 10; | ||
79 | dht_op = GNUNET_TESTBED_service_connect | ||
80 | (NULL, peers[0], "dht", | ||
81 | &service_connect_comp, NULL, | ||
82 | &dht_ca, &dht_da, &ctxt); | ||
83 | GNUNET_SCHEDULER_add_shutdown (&shutdown_task, NULL); | ||
84 | } | ||
85 | |||
86 | |||
87 | int | ||
88 | main (int argc, char **argv) | ||
89 | { | ||
90 | int ret; | ||
91 | |||
92 | result = GNUNET_SYSERR; | ||
93 | ret = GNUNET_TESTBED_test_run | ||
94 | ("awesome-test", "template.conf", | ||
95 | NUM_PEERS, 0LL, | ||
96 | NULL, NULL, &test_master, NULL); | ||
97 | if ( (GNUNET_OK != ret) || (GNUNET_OK != result) ) | ||
98 | return 1; | ||
99 | return 0; | ||
100 | } | ||