- give out run handle through which master controller's handle can be retrieved
[oweals/gnunet.git] / src / testbed / test_testbed_api_testbed_run.c
1 /*
2   This file is part of GNUnet
3   (C) 2008--2013 Christian Grothoff (and other contributing authors)
4
5   GNUnet is free software; you can redistribute it and/or modify
6   it under the terms of the GNU General Public License as published
7   by the Free Software Foundation; either version 3, or (at your
8   option) any later version.
9
10   GNUnet is distributed in the hope that it will be useful, but
11   WITHOUT ANY WARRANTY; without even the implied warranty of
12   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13   General Public License for more details.
14
15   You should have received a copy of the GNU General Public License
16   along with GNUnet; see the file COPYING.  If not, write to the
17   Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18   Boston, MA 02111-1307, USA.
19 */
20
21 /**
22  * @file testbed/test_testbed_api_testbed_run.c
23  * @brief Test cases for testing high-level testbed management
24  * @author Sree Harsha Totakura <sreeharsha@totakura.in>
25  */
26
27 #include "platform.h"
28 #include "gnunet_common.h"
29 #include "gnunet_testbed_service.h"
30
31 /**
32  * Number of peers we want to start
33  */
34 #define NUM_PEERS 5
35
36 /**
37  * The array of peers; we fill this as the peers are given to us by the testbed
38  */
39 static struct GNUNET_TESTBED_Peer *peers[NUM_PEERS];
40
41 /**
42  * Operation handle
43  */
44 static struct GNUNET_TESTBED_Operation *op;
45
46 /**
47  * Abort task identifier
48  */
49 static GNUNET_SCHEDULER_TaskIdentifier abort_task;
50
51 /**
52  * Current peer id
53  */
54 static unsigned int peer_id;
55
56 /**
57  * Testing result
58  */
59 static int result;
60
61 /**
62  * Should we wait forever after testbed is initialized?
63  */
64 static int wait_forever;
65
66
67 /**
68  * Shutdown nicely
69  *
70  * @param cls NULL
71  * @param tc the task context
72  */
73 static void
74 do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
75 {
76   if (GNUNET_SCHEDULER_NO_TASK != abort_task)
77     GNUNET_SCHEDULER_cancel (abort_task);
78   GNUNET_SCHEDULER_shutdown (); /* Stop scheduler to shutdown testbed run */
79 }
80
81
82 /**
83  * abort task to run on test timed out
84  *
85  * @param cls NULL
86  * @param tc the task context
87  */
88 static void
89 do_abort (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
90 {
91   GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Test timedout -- Aborting\n");
92   abort_task = GNUNET_SCHEDULER_NO_TASK;
93   (void) GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
94 }
95
96
97 /**
98  * Signature of a main function for a testcase.
99  *
100  * @param cls closure
101  * @param h the run handle
102  * @param num_peers number of peers in 'peers'
103  * @param peers_ handle to peers run in the testbed
104  * @param links_succeeded the number of overlay link connection attempts that
105  *          succeeded
106  * @param links_failed the number of overlay link connection attempts that
107  *          failed
108  */
109 static void
110 test_master (void *cls, 
111              struct GNUNET_TESTBED_RunHandle *h,
112              unsigned int num_peers,
113              struct GNUNET_TESTBED_Peer **peers_,
114              unsigned int links_succeeded,
115              unsigned int links_failed)
116 {
117   result = GNUNET_OK;
118   if (GNUNET_YES == wait_forever)
119   {
120     if (GNUNET_SCHEDULER_NO_TASK == abort_task)
121       return;                   /* abort already scheduled */
122     GNUNET_SCHEDULER_cancel (abort_task);
123     abort_task = GNUNET_SCHEDULER_NO_TASK;
124     (void) GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
125                                          &do_shutdown, NULL);
126     return;
127   }
128   GNUNET_assert (NULL != peers[0]);
129   op = GNUNET_TESTBED_peer_stop (NULL, peers[0], NULL, NULL);
130   GNUNET_assert (NULL != op);
131 }
132
133
134 /**
135  * Controller event callback
136  *
137  * @param cls NULL
138  * @param event the controller event
139  */
140 static void
141 controller_event_cb (void *cls,
142                      const struct GNUNET_TESTBED_EventInformation *event)
143 {
144
145   switch (event->type)
146   {
147   case GNUNET_TESTBED_ET_PEER_START:
148     GNUNET_assert (NULL == peers[peer_id]);
149     GNUNET_assert (NULL != event->details.peer_start.peer);
150     peers[peer_id++] = event->details.peer_start.peer;
151     break;
152   case GNUNET_TESTBED_ET_PEER_STOP:
153     GNUNET_assert (NULL != op);
154     GNUNET_TESTBED_operation_done (op);
155     GNUNET_assert (peers[0] == event->details.peer_stop.peer);
156     GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
157     break;
158   default:
159     GNUNET_assert (0);
160   }
161 }
162
163
164 /**
165  * Main run function.
166  *
167  * @param cls NULL
168  * @param args arguments passed to GNUNET_PROGRAM_run
169  * @param cfgfile the path to configuration file
170  * @param cfg the configuration file handle
171  */
172 static void
173 run (void *cls, char *const *args, const char *cfgfile,
174      const struct GNUNET_CONFIGURATION_Handle *config)
175 {
176   uint64_t event_mask;
177
178   event_mask = 0;
179   event_mask |= (1LL << GNUNET_TESTBED_ET_PEER_START);
180   event_mask |= (1LL << GNUNET_TESTBED_ET_PEER_STOP);
181   GNUNET_TESTBED_run (NULL, config, NUM_PEERS, event_mask, &controller_event_cb,
182                       NULL, &test_master, NULL);
183   abort_task =
184       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
185                                     (GNUNET_TIME_UNIT_SECONDS, 300), &do_abort,
186                                     NULL);
187 }
188
189
190 /**
191  * Main function
192  */
193 int
194 main (int argc, char **argv)
195 {
196   char *argv2[] = {
197     "test_testbed_api_testbed_run",
198     "-c", NULL,
199     NULL
200   };
201   struct GNUNET_GETOPT_CommandLineOption options[] = {
202     GNUNET_GETOPT_OPTION_END
203   };
204   char *testname;
205   char *config_filename;
206   int ret;
207
208   if (NULL == (testname = strrchr (argv[0], (int) '_')))
209   {
210     GNUNET_break (0);
211     return 1;
212   }
213   testname++;
214   testname = GNUNET_strdup (testname);
215 #ifdef MINGW
216   {
217     char *period;
218
219     /* check and remove .exe extension */
220     period = strrchr (testname, (int) '.');
221     if (NULL != period)
222       *period = '\0';
223     else
224       GNUNET_break (0);         /* Windows with no .exe? */
225   }
226 #endif
227   if (0 == strcmp ("waitforever", testname))
228     wait_forever = GNUNET_YES;
229   if ( (GNUNET_YES != wait_forever) && (0 != strcmp ("run", testname)) )
230   {
231     GNUNET_asprintf (&config_filename, "test_testbed_api_testbed_run_%s.conf",
232                      testname);
233   }
234   else
235     config_filename = GNUNET_strdup ("test_testbed_api.conf");
236   GNUNET_free (testname);
237   argv2[2] = config_filename;
238   result = GNUNET_SYSERR;
239   ret =
240       GNUNET_PROGRAM_run ((sizeof (argv2) / sizeof (char *)) - 1, argv2,
241                           "test_testbed_api_testbed_run", "nohelp", options,
242                           &run, NULL);
243   GNUNET_free (config_filename);
244   if ((GNUNET_OK != ret) || (GNUNET_OK != result))
245     return 1;
246   return 0;
247 }
248
249 /* end of test_testbed_api_testbed_run.c */