2 This file is part of GNUnet.
3 (C) 2009 Christian Grothoff (and other contributing authors)
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.
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.
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.
23 * @brief API for accessing the ARM service
24 * @author Christian Grothoff
27 #include "gnunet_arm_service.h"
28 #include "gnunet_client_lib.h"
29 #include "gnunet_getopt_lib.h"
30 #include "gnunet_os_lib.h"
31 #include "gnunet_protocols.h"
32 #include "gnunet_server_lib.h"
37 * How long are we willing to wait for a service operation during the multi-operation
40 #define MULTI_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
44 * Handle for interacting with ARM.
46 struct GNUNET_ARM_Handle
50 * Our connection to the ARM service.
52 struct GNUNET_CLIENT_Connection *client;
55 * The configuration that we are using.
57 struct GNUNET_CONFIGURATION_Handle *cfg;
62 struct GNUNET_SCHEDULER_Handle *sched;
68 * Setup a context for communicating with ARM. Note that this
69 * can be done even if the ARM service is not yet running.
71 * @param cfg configuration to use (needed to contact ARM;
72 * the ARM service may internally use a different
73 * configuration to determine how to start the service).
74 * @param sched scheduler to use
75 * @param service service that *this* process is implementing/providing, can be NULL
76 * @return context to use for further ARM operations, NULL on error
78 struct GNUNET_ARM_Handle *
79 GNUNET_ARM_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
80 struct GNUNET_SCHEDULER_Handle *sched,
83 struct GNUNET_ARM_Handle *ret;
84 struct GNUNET_CLIENT_Connection *client;
86 client = GNUNET_CLIENT_connect (sched, "arm", cfg);
89 ret = GNUNET_malloc (sizeof (struct GNUNET_ARM_Handle));
90 ret->cfg = GNUNET_CONFIGURATION_dup (cfg);
98 * Disconnect from the ARM service.
100 * @param h the handle that was being used
103 GNUNET_ARM_disconnect (struct GNUNET_ARM_Handle *h)
105 if (h->client != NULL)
106 GNUNET_CLIENT_disconnect (h->client);
107 GNUNET_CONFIGURATION_destroy (h->cfg);
113 * Internal state for a request with ARM.
115 struct RequestContext
119 * Pointer to our handle with ARM.
121 struct GNUNET_ARM_Handle *h;
124 * Function to call with a status code for the requested operation.
126 GNUNET_ARM_Callback callback;
129 * Closure for "callback".
134 * Timeout for the operation.
136 struct GNUNET_TIME_Absolute timeout;
139 * Type of the request expressed as a message type (start or stop).
147 * A client specifically requested starting of ARM itself.
148 * This function is called with information about whether
149 * or not ARM is running; if it is, report success. If
150 * it is not, start the ARM process.
152 * @param cls the context for the request that we will report on (struct RequestContext*)
153 * @param tc why were we called (reason says if ARM is running)
156 arm_service_report (void *cls,
157 const struct GNUNET_SCHEDULER_TaskContext *tc)
159 struct RequestContext *pos = cls;
164 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_PREREQ_DONE))
167 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
168 "Looks like `%s' is already running.\n",
169 "gnunet-service-arm");
171 /* arm is running! */
172 if (pos->callback != NULL)
173 pos->callback (pos->cls, GNUNET_YES);
178 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
179 "Looks like `%s' is not running, will start it.\n",
180 "gnunet-service-arm");
182 /* FIXME: should we check that HOSTNAME for 'arm' is localhost? */
185 GNUNET_CONFIGURATION_get_value_string (pos->h->cfg,
190 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
191 _("Configuration failes to specify option `%s' in section `%s'!\n"),
194 if (pos->callback != NULL)
195 pos->callback (pos->cls, GNUNET_SYSERR);
200 GNUNET_CONFIGURATION_get_value_filename (pos->h->cfg,
201 "arm", "CONFIG", &config))
203 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
204 _("Configuration fails to specify option `%s' in section `%s'!\n"),
207 if (pos->callback != NULL)
208 pos->callback (pos->cls, GNUNET_SYSERR);
209 GNUNET_free (binary);
213 pid = GNUNET_OS_start_process (binary, binary, "-d", "-c", config,
218 GNUNET_free (binary);
219 GNUNET_free (config);
222 if (pos->callback != NULL)
223 pos->callback (pos->cls, GNUNET_SYSERR);
227 if (pos->callback != NULL)
228 pos->callback (pos->cls, GNUNET_YES);
234 * Process a response from ARM to a request for a change in service
237 * @param cls the request context
238 * @param msg the response
241 handle_response (void *cls, const struct GNUNET_MessageHeader *msg)
243 struct RequestContext *sc = cls;
248 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
249 _("Error receiving response to `%s' request from ARM service\n"),
250 (sc->type == GNUNET_MESSAGE_TYPE_ARM_START)
253 GNUNET_CLIENT_disconnect (sc->h->client);
254 sc->h->client = GNUNET_CLIENT_connect (sc->h->sched,
257 GNUNET_assert (NULL != sc->h->client);
258 if (sc->callback != NULL)
259 sc->callback (sc->cls, GNUNET_SYSERR);
264 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
265 "Received response from ARM service: %u\n",
268 switch (ntohs (msg->type))
270 case GNUNET_MESSAGE_TYPE_ARM_IS_UP:
273 case GNUNET_MESSAGE_TYPE_ARM_IS_DOWN:
276 case GNUNET_MESSAGE_TYPE_ARM_IS_UNKNOWN:
283 if (sc->callback != NULL)
284 sc->callback (sc->cls, ret);
290 * Start or stop a service.
292 * @param h handle to ARM
293 * @param service_name name of the service
294 * @param timeout how long to wait before failing for good
295 * @param cb callback to invoke when service is ready
296 * @param cb_cls closure for callback
297 * @param type type of the request
300 change_service (struct GNUNET_ARM_Handle *h,
301 const char *service_name,
302 struct GNUNET_TIME_Relative timeout,
303 GNUNET_ARM_Callback cb, void *cb_cls, uint16_t type)
305 struct RequestContext *sctx;
307 struct GNUNET_MessageHeader *msg;
309 slen = strlen (service_name) + 1;
310 if (slen + sizeof (struct GNUNET_MessageHeader) >
311 GNUNET_SERVER_MAX_MESSAGE_SIZE)
315 cb (cb_cls, GNUNET_NO);
319 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
320 (type == GNUNET_MESSAGE_TYPE_ARM_START)
321 ? _("Requesting start of service `%s'.\n")
322 : _("Requesting termination of service `%s'.\n"),
325 sctx = GNUNET_malloc (sizeof (struct RequestContext) + slen);
329 sctx->timeout = GNUNET_TIME_relative_to_absolute (timeout);
331 msg = GNUNET_malloc (sizeof (struct GNUNET_MessageHeader) + slen);
332 msg->size = htons (sizeof (struct GNUNET_MessageHeader) + slen);
333 msg->type = htons (sctx->type);
334 memcpy (&msg[1], service_name, slen);
336 GNUNET_CLIENT_transmit_and_get_response (sctx->h->client,
338 GNUNET_TIME_absolute_get_remaining (sctx->timeout),
343 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
344 _("Error while trying to transmit to ARM service\n"));
346 cb (cb_cls, GNUNET_SYSERR);
358 * @param h handle to ARM
359 * @param service_name name of the service
360 * @param timeout how long to wait before failing for good
361 * @param cb callback to invoke when service is ready
362 * @param cb_cls closure for callback
365 GNUNET_ARM_start_service (struct GNUNET_ARM_Handle *h,
366 const char *service_name,
367 struct GNUNET_TIME_Relative timeout,
368 GNUNET_ARM_Callback cb, void *cb_cls)
370 struct RequestContext *sctx;
372 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
373 _("Starting service `%s'\n"), service_name);
374 if (0 == strcasecmp ("arm", service_name))
376 sctx = GNUNET_malloc (sizeof (struct RequestContext));
380 sctx->timeout = GNUNET_TIME_relative_to_absolute (timeout);
381 GNUNET_CLIENT_service_test (h->sched,
383 h->cfg, timeout, &arm_service_report, sctx);
386 change_service (h, service_name, timeout, cb, cb_cls, GNUNET_MESSAGE_TYPE_ARM_START);
393 * @param h handle to ARM
394 * @param service_name name of the service
395 * @param timeout how long to wait before failing for good
396 * @param cb callback to invoke when service is ready
397 * @param cb_cls closure for callback
400 GNUNET_ARM_stop_service (struct GNUNET_ARM_Handle *h,
401 const char *service_name,
402 struct GNUNET_TIME_Relative timeout,
403 GNUNET_ARM_Callback cb, void *cb_cls)
405 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
406 _("Stopping service `%s'\n"), service_name);
407 if (0 == strcasecmp ("arm", service_name))
409 GNUNET_CLIENT_service_shutdown (h->client);
412 cb (cb_cls, GNUNET_NO);
415 change_service (h, service_name, timeout, cb, cb_cls, GNUNET_MESSAGE_TYPE_ARM_STOP);
420 * Function to call for each service.
422 * @param h handle to ARM
423 * @param service_name name of the service
424 * @param timeout how long to wait before failing for good
425 * @param cb callback to invoke when service is ready
426 * @param cb_cls closure for callback
428 typedef void (*ServiceOperation) (struct GNUNET_ARM_Handle *h,
429 const char *service_name,
430 struct GNUNET_TIME_Relative timeout,
431 GNUNET_ARM_Callback cb, void *cb_cls);
435 * Context for starting or stopping multiple services.
440 * NULL-terminated array of services to start or stop.
447 struct GNUNET_ARM_Handle *h;
450 * Identifies the operation (start or stop).
455 * Current position in "services".
462 * Run the operation for the next service in the multi-service
465 * @param cls the "struct MultiContext" that is being processed
466 * @param success status of the previous operation (ignored)
469 next_operation (void *cls,
472 struct MultiContext *mc = cls;
475 if (NULL == (pos = mc->services[mc->pos]))
477 GNUNET_free (mc->services);
478 GNUNET_ARM_disconnect (mc->h);
483 mc->op (mc->h, pos, MULTI_TIMEOUT, &next_operation, mc);
489 * Run a multi-service request.
491 * @param cfg configuration to use (needed to contact ARM;
492 * the ARM service may internally use a different
493 * configuration to determine how to start the service).
494 * @param sched scheduler to use
495 * @param op the operation to perform for each service
496 * @param va NULL-terminated list of services
499 run_multi_request (const struct GNUNET_CONFIGURATION_Handle *cfg,
500 struct GNUNET_SCHEDULER_Handle *sched,
506 struct MultiContext *mc;
507 struct GNUNET_ARM_Handle *h;
510 h = GNUNET_ARM_connect (cfg, sched, NULL);
513 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
514 _("Error while trying to transmit to ARM service\n"));
519 while (NULL != (va_arg (cp, const char*))) total++;
521 mc = GNUNET_malloc (sizeof(struct MultiContext));
522 mc->services = GNUNET_malloc (total * sizeof (char*));
527 while (NULL != (c = va_arg (cp, const char*)))
528 mc->services[total++] = GNUNET_strdup (c);
530 next_operation (mc, GNUNET_YES);
535 * Start multiple services in the specified order. Convenience
536 * function. Works asynchronously, failures are not reported.
538 * @param cfg configuration to use (needed to contact ARM;
539 * the ARM service may internally use a different
540 * configuration to determine how to start the service).
541 * @param sched scheduler to use
542 * @param ... NULL-terminated list of service names (const char*)
545 GNUNET_ARM_start_services (const struct GNUNET_CONFIGURATION_Handle *cfg,
546 struct GNUNET_SCHEDULER_Handle *sched,
551 va_start (ap, sched);
552 run_multi_request (cfg, sched, &GNUNET_ARM_start_service, ap);
558 * Stop multiple services in the specified order. Convenience
559 * function. Works asynchronously, failures are not reported.
561 * @param cfg configuration to use (needed to contact ARM;
562 * the ARM service may internally use a different
563 * configuration to determine how to start the service).
564 * @param sched scheduler to use
565 * @param ... NULL-terminated list of service names (const char*)
568 GNUNET_ARM_stop_services (const struct GNUNET_CONFIGURATION_Handle *cfg,
569 struct GNUNET_SCHEDULER_Handle *sched,
574 va_start (ap, sched);
575 run_multi_request (cfg, sched, &GNUNET_ARM_stop_service, ap);
580 /* end of arm_api.c */