improving ARM API
[oweals/gnunet.git] / src / arm / gnunet-arm.c
1 /*
2      This file is part of GNUnet.
3      (C) 2009 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 2, 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 arm/gnunet-arm.c
23  * @brief arm for writing a tool
24  * @author Christian Grothoff
25  */
26 #include "platform.h"
27 #include "gnunet_arm_service.h"
28 #include "gnunet_client_lib.h"
29 #include "gnunet_getopt_lib.h"
30 #include "gnunet_program_lib.h"
31 #include "gnunet_time_lib.h"
32
33 /**
34  * Timeout for all operations.
35  */
36 #define TIMEOUT  GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
37
38 /**
39  * Set if we are to shutdown all services (including ARM).
40  */
41 static int end;
42
43 /**
44  * Set if we are to start default services (including ARM).
45  */
46 static int start;
47
48 /**
49  * Set to the name of a service to start.
50  */
51 static char *init;
52
53 /**
54  * Set to the name of a service to kill.
55  */
56 static char *term;
57
58 /**
59  * Set to the name of a service to test.
60  */
61 static char *test;
62
63 /**
64  * Final status code.
65  */
66 static int ret;
67
68 /**
69  * Connection with ARM.
70  */
71 static struct GNUNET_ARM_Handle *h;
72
73 /**
74  * Our scheduler.
75  */
76 static struct GNUNET_SCHEDULER_Handle *sched;
77
78 /**
79  * Our configuration.
80  */
81 const struct GNUNET_CONFIGURATION_Handle *cfg;
82
83 /**
84  * Processing stage that we are in.  Simple counter.
85  */
86 static unsigned int phase;
87
88
89 /**
90  * Main continuation-passing-style loop.  Runs the various
91  * jobs that we've been asked to do in order.
92  * 
93  * @param cls closure, unused
94  * @param tc context, unused
95  */
96 static void
97 cps_loop (void *cls,
98           const struct GNUNET_SCHEDULER_TaskContext *tc);
99
100
101 /**
102  * Callback invoked with the status of the last operation.  Reports to the
103  * user and then runs the next phase in the FSM.
104  *
105  * @param cls pointer to "const char*" identifying service that was manipulated
106  * @param success GNUNET_OK if service is now running, GNUNET_NO if not, GNUNET_SYSERR on error
107  */
108 static void
109 confirm_cb (void *cls, int success)
110 {
111   const char *service = cls;
112   switch (success)
113     {
114     case GNUNET_OK:
115       fprintf (stdout, _("Service `%s' is now running.\n"), service);
116       break;
117     case GNUNET_NO:
118       fprintf (stdout, _("Service `%s' is not running.\n"), service);
119       break;
120     case GNUNET_SYSERR:
121       fprintf (stdout,
122                _("Error updating service `%s': ARM not running\n"), service);
123       break;
124     }
125   GNUNET_SCHEDULER_add_continuation (sched,
126                                      GNUNET_NO,
127                                      &cps_loop,
128                                      NULL,
129                                      GNUNET_SCHEDULER_REASON_PREREQ_DONE);
130 }
131
132
133 /**
134  * Function called to confirm that a service is running (or that
135  * it is not running).
136  *
137  * @param cls pointer to "const char*" identifying service that was manipulated
138  * @param tc reason determines if service is now running
139  */
140 static void
141 confirm_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
142 {
143   const char *service = cls;
144
145   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_PREREQ_DONE))
146     fprintf (stdout, _("Service `%s' is running.\n"), service);
147   else
148     fprintf (stdout, _("Service `%s' is not running.\n"), service);
149   GNUNET_SCHEDULER_add_continuation (sched,
150                                      GNUNET_NO,
151                                      &cps_loop,
152                                      NULL,
153                                      GNUNET_SCHEDULER_REASON_PREREQ_DONE);
154 }
155
156
157 /**
158  * Main function that will be run by the scheduler.
159  *
160  * @param cls closure
161  * @param s the scheduler to use
162  * @param args remaining command-line arguments
163  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
164  * @param c configuration
165  */
166 static void
167 run (void *cls,
168      struct GNUNET_SCHEDULER_Handle *s,
169      char *const *args,
170      const char *cfgfile, 
171      const struct GNUNET_CONFIGURATION_Handle *c)
172 {
173   sched = s;
174   cfg = c;
175   h = GNUNET_ARM_connect (cfg, sched, NULL);
176   if (h == NULL)
177     {
178       fprintf (stderr,
179                _("Fatal error initializing ARM API.\n"));
180       ret = 1;
181       return;
182     }
183   GNUNET_SCHEDULER_add_continuation (sched,
184                                      GNUNET_NO,
185                                      &cps_loop,
186                                      NULL,
187                                      GNUNET_SCHEDULER_REASON_PREREQ_DONE);
188 }
189
190
191 /**
192  * Main continuation-passing-style loop.  Runs the various
193  * jobs that we've been asked to do in order.
194  * 
195  * @param cls closure, unused
196  * @param tc context, unused
197  */
198 static void
199 cps_loop (void *cls,
200           const struct GNUNET_SCHEDULER_TaskContext *tc)
201 {
202   while (1)
203     {
204       switch (phase++)
205         {
206         case 0:
207           if (term != NULL)
208             {
209               GNUNET_ARM_stop_service (h, term, TIMEOUT, &confirm_cb, term);
210               return;
211             }
212           break;
213         case 1:
214           if (end)
215             {
216               GNUNET_ARM_stop_service (h, "arm", TIMEOUT, &confirm_cb, "arm");
217               return;
218             }
219           break;
220         case 2:
221           if (start)
222             {
223               GNUNET_ARM_start_service (h, "arm", TIMEOUT, &confirm_cb, "arm");
224               return;
225             }
226           break;
227         case 3:
228           if (init != NULL)
229             {
230               GNUNET_ARM_start_service (h, init, TIMEOUT, &confirm_cb, init);
231               return;
232             }
233           break;
234         case 4:
235           if (test != NULL)
236             {
237               GNUNET_CLIENT_service_test (sched, test, cfg, TIMEOUT, &confirm_task, test);
238               return;
239             }
240           break;
241         default: /* last phase */
242           GNUNET_ARM_disconnect (h);
243           return;
244         }
245     }
246 }
247
248
249 /**
250  * gnunet-arm command line options
251  */
252 static struct GNUNET_GETOPT_CommandLineOption options[] = {
253   {'e', "end", NULL, gettext_noop ("stop all GNUnet services"),
254    GNUNET_NO, &GNUNET_GETOPT_set_one, &end},
255   {'i', "init", "SERVICE", gettext_noop ("start a particular service"),
256    GNUNET_YES, &GNUNET_GETOPT_set_string, &init},
257   {'k', "kill", "SERVICE", gettext_noop ("stop a particular service"),
258    GNUNET_YES, &GNUNET_GETOPT_set_string, &term},
259   {'s', "start", NULL, gettext_noop ("start all GNUnet default services"),
260    GNUNET_NO, &GNUNET_GETOPT_set_one, &start},
261   {'t', "test", "SERVICE",
262    gettext_noop ("test if a particular service is running"),
263    GNUNET_YES, &GNUNET_GETOPT_set_string, &test},
264   GNUNET_GETOPT_OPTION_END
265 };
266
267
268 /**
269  * The main function to obtain arm from gnunetd.
270  *
271  * @param argc number of arguments from the command line
272  * @param argv command line arguments
273  * @return 0 ok, 1 on error
274  */
275 int
276 main (int argc, char *const *argv)
277 {
278   return (GNUNET_OK ==
279           GNUNET_PROGRAM_run (argc,
280                               argv,
281                               "gnunet-arm",
282                               gettext_noop
283                               ("Control services and the Automated Restart Manager (ARM)"),
284                               options, &run, NULL)) ? ret : 1;
285 }
286
287 /* end of gnunet-arm.c */