- adapt mesh to strided regex implementation
[oweals/gnunet.git] / src / testbed / test_testbed_api_testbed_run.c
1 /*
2   This file is part of GNUnet
3   (C) 2008--2012 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 13
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 unsigned int peer_id;
55
56 /**
57  * Testing result
58  */
59 static int result;
60
61
62 /**
63  * Shutdown nicely
64  *
65  * @param cls NULL
66  * @param tc the task context
67  */
68 static void
69 do_shutdown (void *cls, const const struct GNUNET_SCHEDULER_TaskContext *tc)
70 {
71   if (GNUNET_SCHEDULER_NO_TASK != abort_task)
72     GNUNET_SCHEDULER_cancel (abort_task);
73   GNUNET_SCHEDULER_shutdown ();
74 }
75
76
77 /**
78  * abort task to run on test timed out
79  *
80  * @param cls NULL
81  * @param tc the task context
82  */
83 static void
84 do_abort (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
85 {
86   GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Test timedout -- Aborting\n");
87   abort_task = GNUNET_SCHEDULER_NO_TASK;
88   GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
89   GNUNET_SCHEDULER_shutdown (); /* Stop the scheduler */
90 }
91
92
93 /**
94  * Task to be executed when peers are ready
95  *
96  * @param cls NULL
97  * @param tc the task context
98  */
99 static void
100 master_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
101 {
102   result = GNUNET_OK;
103   GNUNET_assert (NULL != peers[0]);
104   op = GNUNET_TESTBED_peer_stop (peers[0], NULL, NULL);
105   GNUNET_assert (NULL != op);
106 }
107
108
109 /**
110  * Controller event callback
111  *
112  * @param cls NULL
113  * @param event the controller event
114  */
115 static void
116 controller_event_cb (void *cls,
117                      const struct GNUNET_TESTBED_EventInformation *event)
118 {
119
120   switch (event->type)
121   {
122   case GNUNET_TESTBED_ET_PEER_START:
123     GNUNET_assert (NULL == peers[peer_id]);
124     GNUNET_assert (NULL != event->details.peer_start.peer);
125     peers[peer_id++] = event->details.peer_start.peer;
126     break;
127   case GNUNET_TESTBED_ET_PEER_STOP:
128     GNUNET_assert (NULL != op);
129     GNUNET_TESTBED_operation_done (op);
130     GNUNET_assert (peers[0] == event->details.peer_stop.peer);
131     GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
132     break;
133   default:
134     GNUNET_assert (0);
135   }
136 }
137
138
139 /**
140  * Main run function.
141  *
142  * @param cls NULL
143  * @param args arguments passed to GNUNET_PROGRAM_run
144  * @param cfgfile the path to configuration file
145  * @param cfg the configuration file handle
146  */
147 static void
148 run (void *cls, char *const *args, const char *cfgfile,
149      const struct GNUNET_CONFIGURATION_Handle *config)
150 {
151   uint64_t event_mask;
152
153   event_mask = 0;
154   event_mask |= (1LL << GNUNET_TESTBED_ET_PEER_START);
155   event_mask |= (1LL << GNUNET_TESTBED_ET_PEER_STOP);
156   event_mask |= (1LL << GNUNET_TESTBED_ET_CONNECT);
157   event_mask |= (1LL << GNUNET_TESTBED_ET_DISCONNECT);
158   GNUNET_TESTBED_run (NULL, config, NUM_PEERS, event_mask, &controller_event_cb,
159                       NULL, &master_task, NULL);
160   abort_task =
161       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
162                                     (GNUNET_TIME_UNIT_SECONDS, 5), &do_abort,
163                                     NULL);
164 }
165
166
167 /**
168  * Main function
169  */
170 int
171 main (int argc, char **argv)
172 {
173   int ret;
174
175   char *const argv2[] = {
176     "test_testbed_api_testbed_run",
177     "-c", "test_testbed_api.conf",
178     NULL
179   };
180   struct GNUNET_GETOPT_CommandLineOption options[] = {
181     GNUNET_GETOPT_OPTION_END
182   };
183
184   result = GNUNET_SYSERR;
185   ret =
186       GNUNET_PROGRAM_run ((sizeof (argv2) / sizeof (char *)) - 1, argv2,
187                           "test_testbed_api_testbed_run", "nohelp", options,
188                           &run, NULL);
189   if ((GNUNET_OK != ret) || (GNUNET_OK != result))
190     return 1;
191   return 0;
192 }
193
194 /* end of test_testbed_api_testbed_run.c */