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 GNUNET_CLIENT_ignore_shutdown (client, GNUNET_YES);
90 ret = GNUNET_malloc (sizeof (struct GNUNET_ARM_Handle));
91 ret->cfg = GNUNET_CONFIGURATION_dup (cfg);
99 * Disconnect from the ARM service.
101 * @param h the handle that was being used
104 GNUNET_ARM_disconnect (struct GNUNET_ARM_Handle *h)
106 if (h->client != NULL)
107 GNUNET_CLIENT_disconnect (h->client);
108 GNUNET_CONFIGURATION_destroy (h->cfg);
114 * Internal state for a request with ARM.
116 struct RequestContext
120 * Pointer to our handle with ARM.
122 struct GNUNET_ARM_Handle *h;
125 * Function to call with a status code for the requested operation.
127 GNUNET_ARM_Callback callback;
130 * Closure for "callback".
135 * Timeout for the operation.
137 struct GNUNET_TIME_Absolute timeout;
140 * Type of the request expressed as a message type (start or stop).
148 * A client specifically requested starting of ARM itself.
149 * This function is called with information about whether
150 * or not ARM is running; if it is, report success. If
151 * it is not, start the ARM process.
153 * @param cls the context for the request that we will report on (struct RequestContext*)
154 * @param tc why were we called (reason says if ARM is running)
157 arm_service_report (void *cls,
158 const struct GNUNET_SCHEDULER_TaskContext *tc)
160 struct RequestContext *pos = cls;
165 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_PREREQ_DONE))
168 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
169 "Looks like `%s' is already running.\n",
170 "gnunet-service-arm");
172 /* arm is running! */
173 if (pos->callback != NULL)
174 pos->callback (pos->cls, GNUNET_YES);
179 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
180 "Looks like `%s' is not running, will start it.\n",
181 "gnunet-service-arm");
183 /* FIXME: should we check that HOSTNAME for 'arm' is localhost? */
186 GNUNET_CONFIGURATION_get_value_string (pos->h->cfg,
191 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
192 _("Configuration failes to specify option `%s' in section `%s'!\n"),
195 if (pos->callback != NULL)
196 pos->callback (pos->cls, GNUNET_SYSERR);
201 GNUNET_CONFIGURATION_get_value_filename (pos->h->cfg,
202 "arm", "CONFIG", &config))
204 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
205 _("Configuration fails to specify option `%s' in section `%s'!\n"),
208 if (pos->callback != NULL)
209 pos->callback (pos->cls, GNUNET_SYSERR);
210 GNUNET_free (binary);
214 pid = GNUNET_OS_start_process (binary, binary, "-d", "-c", config,
219 GNUNET_free (binary);
220 GNUNET_free (config);
223 if (pos->callback != NULL)
224 pos->callback (pos->cls, GNUNET_SYSERR);
228 if (pos->callback != NULL)
229 pos->callback (pos->cls, GNUNET_YES);
235 * Process a response from ARM to a request for a change in service
238 * @param cls the request context
239 * @param msg the response
242 handle_response (void *cls, const struct GNUNET_MessageHeader *msg)
244 struct RequestContext *sc = cls;
249 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
250 _("Error receiving response to `%s' request from ARM for service `%s'\n"),
251 (sc->type == GNUNET_MESSAGE_TYPE_ARM_START)
254 (const char*) &sc[1]);
255 GNUNET_CLIENT_disconnect (sc->h->client);
256 sc->h->client = GNUNET_CLIENT_connect (sc->h->sched,
259 GNUNET_assert (NULL != sc->h->client);
260 GNUNET_CLIENT_ignore_shutdown (sc->h->client, GNUNET_YES);
261 if (sc->callback != NULL)
262 sc->callback (sc->cls, GNUNET_SYSERR);
267 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
268 "Received response from ARM for service `%s': %u\n",
269 (const char*) &sc[1],
272 switch (ntohs (msg->type))
274 case GNUNET_MESSAGE_TYPE_ARM_IS_UP:
277 case GNUNET_MESSAGE_TYPE_ARM_IS_DOWN:
280 case GNUNET_MESSAGE_TYPE_ARM_IS_UNKNOWN:
287 if (sc->callback != NULL)
288 sc->callback (sc->cls, ret);
294 * Start or stop a service.
296 * @param h handle to ARM
297 * @param service_name name of the service
298 * @param timeout how long to wait before failing for good
299 * @param cb callback to invoke when service is ready
300 * @param cb_cls closure for callback
301 * @param type type of the request
304 change_service (struct GNUNET_ARM_Handle *h,
305 const char *service_name,
306 struct GNUNET_TIME_Relative timeout,
307 GNUNET_ARM_Callback cb, void *cb_cls, uint16_t type)
309 struct RequestContext *sctx;
311 struct GNUNET_MessageHeader *msg;
313 slen = strlen (service_name) + 1;
314 if (slen + sizeof (struct GNUNET_MessageHeader) >
315 GNUNET_SERVER_MAX_MESSAGE_SIZE)
319 cb (cb_cls, GNUNET_NO);
323 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
324 (type == GNUNET_MESSAGE_TYPE_ARM_START)
325 ? _("Requesting start of service `%s'.\n")
326 : _("Requesting termination of service `%s'.\n"),
329 sctx = GNUNET_malloc (sizeof (struct RequestContext) + slen);
333 sctx->timeout = GNUNET_TIME_relative_to_absolute (timeout);
335 memcpy (&sctx[1], service_name, slen);
336 msg = GNUNET_malloc (sizeof (struct GNUNET_MessageHeader) + slen);
337 msg->size = htons (sizeof (struct GNUNET_MessageHeader) + slen);
338 msg->type = htons (sctx->type);
339 memcpy (&msg[1], service_name, slen);
341 GNUNET_CLIENT_transmit_and_get_response (sctx->h->client,
343 GNUNET_TIME_absolute_get_remaining (sctx->timeout),
348 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
349 (type == GNUNET_MESSAGE_TYPE_ARM_START)
350 ? _("Error while trying to transmit request to start `%s' to ARM\n")
351 : _("Error while trying to transmit request to stop `%s' to ARM\n"),
352 (const char*) &service_name);
354 cb (cb_cls, GNUNET_SYSERR);
366 * @param h handle to ARM
367 * @param service_name name of the service
368 * @param timeout how long to wait before failing for good
369 * @param cb callback to invoke when service is ready
370 * @param cb_cls closure for callback
373 GNUNET_ARM_start_service (struct GNUNET_ARM_Handle *h,
374 const char *service_name,
375 struct GNUNET_TIME_Relative timeout,
376 GNUNET_ARM_Callback cb, void *cb_cls)
378 struct RequestContext *sctx;
381 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
382 _("Asked to starting service `%s' within %llu ms\n"), service_name,
383 (unsigned long long) timeout.value);
384 if (0 == strcasecmp ("arm", service_name))
386 slen = strlen ("arm") + 1;
387 sctx = GNUNET_malloc (sizeof (struct RequestContext) + slen);
391 sctx->timeout = GNUNET_TIME_relative_to_absolute (timeout);
392 memcpy (&sctx[1], service_name, slen);
393 GNUNET_CLIENT_service_test (h->sched,
395 h->cfg, timeout, &arm_service_report, sctx);
398 change_service (h, service_name, timeout, cb, cb_cls, GNUNET_MESSAGE_TYPE_ARM_START);
405 * @param h handle to ARM
406 * @param service_name name of the service
407 * @param timeout how long to wait before failing for good
408 * @param cb callback to invoke when service is ready
409 * @param cb_cls closure for callback
412 GNUNET_ARM_stop_service (struct GNUNET_ARM_Handle *h,
413 const char *service_name,
414 struct GNUNET_TIME_Relative timeout,
415 GNUNET_ARM_Callback cb, void *cb_cls)
417 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
418 _("Stopping service `%s' within %llu ms\n"), service_name,
419 (unsigned long long) timeout.value);
420 if (0 == strcasecmp ("arm", service_name))
422 GNUNET_CLIENT_service_shutdown (h->client);
425 cb (cb_cls, GNUNET_NO);
428 change_service (h, service_name, timeout, cb, cb_cls, GNUNET_MESSAGE_TYPE_ARM_STOP);
433 * Function to call for each service.
435 * @param h handle to ARM
436 * @param service_name name of the service
437 * @param timeout how long to wait before failing for good
438 * @param cb callback to invoke when service is ready
439 * @param cb_cls closure for callback
441 typedef void (*ServiceOperation) (struct GNUNET_ARM_Handle *h,
442 const char *service_name,
443 struct GNUNET_TIME_Relative timeout,
444 GNUNET_ARM_Callback cb, void *cb_cls);
448 * Context for starting or stopping multiple services.
453 * NULL-terminated array of services to start or stop.
460 struct GNUNET_ARM_Handle *h;
463 * Identifies the operation (start or stop).
468 * Current position in "services".
475 * Run the operation for the next service in the multi-service
478 * @param cls the "struct MultiContext" that is being processed
479 * @param success status of the previous operation (ignored)
482 next_operation (void *cls,
485 struct MultiContext *mc = cls;
488 if (NULL == (pos = mc->services[mc->pos]))
490 GNUNET_free (mc->services);
491 GNUNET_ARM_disconnect (mc->h);
496 mc->op (mc->h, pos, MULTI_TIMEOUT, &next_operation, mc);
502 * Run a multi-service request.
504 * @param cfg configuration to use (needed to contact ARM;
505 * the ARM service may internally use a different
506 * configuration to determine how to start the service).
507 * @param sched scheduler to use
508 * @param op the operation to perform for each service
509 * @param va NULL-terminated list of services
512 run_multi_request (const struct GNUNET_CONFIGURATION_Handle *cfg,
513 struct GNUNET_SCHEDULER_Handle *sched,
519 struct MultiContext *mc;
520 struct GNUNET_ARM_Handle *h;
523 h = GNUNET_ARM_connect (cfg, sched, NULL);
526 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
527 _("Error while trying to transmit to ARM service\n"));
532 while (NULL != (va_arg (cp, const char*))) total++;
534 mc = GNUNET_malloc (sizeof(struct MultiContext));
535 mc->services = GNUNET_malloc (total * sizeof (char*));
540 while (NULL != (c = va_arg (cp, const char*)))
541 mc->services[total++] = GNUNET_strdup (c);
543 next_operation (mc, GNUNET_YES);
548 * Start multiple services in the specified order. Convenience
549 * function. Works asynchronously, failures are not reported.
551 * @param cfg configuration to use (needed to contact ARM;
552 * the ARM service may internally use a different
553 * configuration to determine how to start the service).
554 * @param sched scheduler to use
555 * @param ... NULL-terminated list of service names (const char*)
558 GNUNET_ARM_start_services (const struct GNUNET_CONFIGURATION_Handle *cfg,
559 struct GNUNET_SCHEDULER_Handle *sched,
564 va_start (ap, sched);
565 run_multi_request (cfg, sched, &GNUNET_ARM_start_service, ap);
571 * Stop multiple services in the specified order. Convenience
572 * function. Works asynchronously, failures are not reported.
574 * @param cfg configuration to use (needed to contact ARM;
575 * the ARM service may internally use a different
576 * configuration to determine how to start the service).
577 * @param sched scheduler to use
578 * @param ... NULL-terminated list of service names (const char*)
581 GNUNET_ARM_stop_services (const struct GNUNET_CONFIGURATION_Handle *cfg,
582 struct GNUNET_SCHEDULER_Handle *sched,
587 va_start (ap, sched);
588 run_multi_request (cfg, sched, &GNUNET_ARM_stop_service, ap);
593 /* end of arm_api.c */