aboutsummaryrefslogtreecommitdiff
path: root/doc/testbed_test.c
diff options
context:
space:
mode:
authorSree Harsha Totakura <totakura@in.tum.de>2013-04-14 21:36:14 +0000
committerSree Harsha Totakura <totakura@in.tum.de>2013-04-14 21:36:14 +0000
commit5b6a9fcaadf0a8be03367367625fdad7e9340998 (patch)
tree46eb2ff4ef9d3ae371518d17a918638bb5c44a72 /doc/testbed_test.c
parent6a957aea5b660cb8a7eae9ea0b5c9e1300caf23b (diff)
downloadgnunet-5b6a9fcaadf0a8be03367367625fdad7e9340998.tar.gz
gnunet-5b6a9fcaadf0a8be03367367625fdad7e9340998.zip
- input source for lstlisting in c tutorial
Diffstat (limited to 'doc/testbed_test.c')
-rw-r--r--doc/testbed_test.c124
1 files changed, 124 insertions, 0 deletions
diff --git a/doc/testbed_test.c b/doc/testbed_test.c
new file mode 100644
index 000000000..593c3c703
--- /dev/null
+++ b/doc/testbed_test.c
@@ -0,0 +1,124 @@
1#include <unistd.h>
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>
6
7/* Number of peers we want to start */
8#define NUM_PEERS 20
9
10struct GNUNET_TESTBED_Operation *dht_op;
11
12struct GNUNET_DHT_Handle *dht_handle;
13
14GNUNET_SCHEDULER_TaskIdentifier shutdown_tid;
15
16struct MyContext
17{
18 int ht_len;
19} ctxt;
20
21static int result;
22
23static void
24shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
25{
26 shutdown_tid = GNUNET_SCHEDULER_NO_TASK;
27 if (NULL != dht_op)
28 {
29 GNUNET_TESTBED_operation_done (dht_op); /* calls the dht_da() for closing
30 down the connection */
31 dht_op = NULL;
32 }
33 result = GNUNET_OK;
34 GNUNET_SCHEDULER_shutdown (); /* Also kills the testbed */
35}
36
37
38static void
39service_connect_comp (void *cls,
40 struct GNUNET_TESTBED_Operation *op,
41 void *ca_result,
42 const char *emsg)
43{
44 /* Service to DHT successful; do something */
45
46 GNUNET_SCHEDULER_cancel (shutdown_tid);
47 GNUNET_SCHEDULER_add_delayed
48 (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10),
49 &shutdown_task, NULL);
50}
51
52
53static void *
54dht_ca (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg)
55{
56 struct MyContext *ctxt = cls;
57
58 /* Use the provided configuration to connect to service */
59 dht_handle = GNUNET_DHT_connect (cfg, ctxt->ht_len);
60 return dht_handle;
61}
62
63
64static void
65dht_da (void *cls, void *op_result)
66{
67 struct MyContext *ctxt = cls;
68
69 /* Disconnect from DHT service */
70 GNUNET_DHT_disconnect ((struct GNUNET_DHT_Handle *) op_result);
71 ctxt->ht_len = 0;
72 dht_handle = NULL;
73}
74
75static void
76test_master (void *cls, unsigned int num_peers,
77 struct GNUNET_TESTBED_Peer **peers,
78 unsigned int links_succeeeded,
79 unsigned int links_failed)
80{
81 /* Testbed is ready with peers running and connected in a pre-defined overlay
82 topology */
83
84 /* do something */
85 ctxt.ht_len = 10;
86
87 /* connect to a peers service */
88 dht_op = GNUNET_TESTBED_service_connect
89 (NULL, /* Closure for operation */
90 peers[0], /* The peer whose service to connect to */
91 "dht", /* The name of the service */
92 service_connect_comp, /* callback to call after a handle to service
93 is opened */
94 NULL, /* closure for the above callback */
95 dht_ca, /* callback to call with peer's configuration;
96 this should open the needed service connection */
97 dht_da, /* callback to be called when closing the
98 opened service connection */
99 &ctxt); /* closure for the above two callbacks */
100 shutdown_tid = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
101 &shutdown_task, NULL);
102}
103
104
105int
106main (int argc, char **argv)
107{
108 int ret;
109
110 result = GNUNET_SYSERR;
111 ret = GNUNET_TESTBED_test_run
112 ("awesome-test", /* test case name */
113 "template.conf", /* template configuration */
114 NUM_PEERS, /* number of peers to start */
115 0LL, /* Event mask - set to 0 for no event notifications */
116 NULL, /* Controller event callback */
117 NULL, /* Closure for controller event callback */
118 &test_master, /* continuation callback to be called when testbed setup is
119 complete */
120 NULL); /* Closure for the test_master callback */
121 if ( (GNUNET_OK != ret) || (GNUNET_OK != result) )
122 return 1;
123 return 0;
124}