stop peers before destroying them
[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 50
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  * Abort task identifier
43  */
44 static GNUNET_SCHEDULER_TaskIdentifier abort_task;
45
46 /**
47  * Current peer id
48  */
49 unsigned int peer_id;
50
51 /**
52  * Testing result
53  */
54 static int result;
55
56
57 /**
58  * Shutdown nicely
59  *
60  * @param cls NULL
61  * @param tc the task context
62  */
63 static void
64 do_shutdown (void *cls, const const struct GNUNET_SCHEDULER_TaskContext *tc)
65 {
66   if (GNUNET_SCHEDULER_NO_TASK != abort_task)
67     GNUNET_SCHEDULER_cancel (abort_task);
68   GNUNET_SCHEDULER_shutdown ();
69 }
70
71
72 /**
73  * abort task to run on test timed out
74  *
75  * @param cls NULL
76  * @param tc the task context
77  */
78 static void
79 do_abort (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
80 {
81   GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Test timedout -- Aborting\n");
82   abort_task = GNUNET_SCHEDULER_NO_TASK;
83   GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
84   GNUNET_SCHEDULER_shutdown (); /* Stop the scheduler */
85 }
86
87
88 /**
89  * Task to be executed when peers are ready
90  *
91  * @param cls NULL
92  * @param tc the task context
93  */
94 static void
95 master_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
96 {
97   result = GNUNET_OK;
98   /* Artificial delay */
99   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &do_shutdown, NULL);
100 }
101
102
103 /**
104  * Controller event callback
105  *
106  * @param cls NULL
107  * @param event the controller event
108  */
109 static void
110 controller_event_cb (void *cls,
111                      const struct GNUNET_TESTBED_EventInformation *event)
112 {
113   
114   switch (event->type)
115   {
116   case GNUNET_TESTBED_ET_PEER_START:
117     GNUNET_assert (NULL == peers[peer_id]);
118     GNUNET_assert (NULL != event->details.peer_start.peer);
119     peers[peer_id++] = event->details.peer_start.peer;
120     break;
121   default:
122     GNUNET_assert (0);
123   }
124 }
125
126
127 /**
128  * Main run function. 
129  *
130  * @param cls NULL
131  * @param args arguments passed to GNUNET_PROGRAM_run
132  * @param cfgfile the path to configuration file
133  * @param cfg the configuration file handle
134  */
135 static void
136 run (void *cls, char *const *args, const char *cfgfile,
137      const struct GNUNET_CONFIGURATION_Handle *config)
138 {
139   uint64_t event_mask;
140   
141   event_mask = 0;
142   event_mask |= (1LL << GNUNET_TESTBED_ET_PEER_START);
143   event_mask |= (1LL << GNUNET_TESTBED_ET_PEER_STOP);
144   event_mask |= (1LL << GNUNET_TESTBED_ET_CONNECT);
145   event_mask |= (1LL << GNUNET_TESTBED_ET_DISCONNECT);
146   event_mask |= (1LL << GNUNET_TESTBED_ET_OPERATION_FINISHED);
147   GNUNET_TESTBED_run (NULL, config, NUM_PEERS, event_mask, &controller_event_cb,
148                       NULL, &master_task, NULL);
149   abort_task = GNUNET_SCHEDULER_add_delayed 
150     (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5), &do_abort, NULL);
151 }
152
153
154 /**
155  * Main function
156  */
157 int main (int argc, char **argv)
158 {
159   int ret;
160   char *const argv2[] = { 
161     "test_testbed_api_testbed_run",
162     "-c", "test_testbed_api.conf",
163     NULL
164   };
165   struct GNUNET_GETOPT_CommandLineOption options[] = {
166     GNUNET_GETOPT_OPTION_END
167   };
168
169   result = GNUNET_SYSERR;
170   ret = GNUNET_PROGRAM_run ((sizeof (argv2) / sizeof (char *)) - 1, argv2,
171                             "test_testbed_api_testbed_run", "nohelp", options,
172                             &run, NULL);
173   if ((GNUNET_OK != ret) || (GNUNET_OK != result))
174     return 1;
175   return 0;
176 }