- fix
[oweals/gnunet.git] / doc / testbed_test.c
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
10 struct GNUNET_TESTBED_Operation *dht_op;
11
12 struct GNUNET_DHT_Handle *dht_handle;
13
14 GNUNET_SCHEDULER_TaskIdentifier shutdown_tid;
15
16 struct MyContext
17 {
18   int ht_len;
19 } ctxt;
20
21 static int result;
22
23 static void 
24 shutdown_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
38 static void
39 service_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
53 static void *
54 dht_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
64 static void 
65 dht_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
75 static void
76 test_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
105 int
106 main (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 }