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"
36 * How often do we re-try tranmsitting requests to ARM before
37 * giving up? Note that if we succeeded transmitting a request
38 * but failed to read a response, we do NOT re-try (since that
39 * might result in ARM getting a request twice).
41 #define MAX_ATTEMPTS 4
44 * Minimum delay between attempts to talk to ARM.
46 #define MIN_RETRY_DELAY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 100)
50 * How long are we willing to wait for a service operation during the multi-operation
53 #define MULTI_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
57 * Handle for interacting with ARM.
59 struct GNUNET_ARM_Handle
63 * Our connection to the ARM service.
65 struct GNUNET_CLIENT_Connection *client;
68 * The configuration that we are using.
70 const struct GNUNET_CONFIGURATION_Handle *cfg;
75 struct GNUNET_SCHEDULER_Handle *sched;
81 * Setup a context for communicating with ARM. Note that this
82 * can be done even if the ARM service is not yet running.
84 * @param cfg configuration to use (needed to contact ARM;
85 * the ARM service may internally use a different
86 * configuration to determine how to start the service).
87 * @param sched scheduler to use
88 * @param service service that *this* process is implementing/providing, can be NULL
89 * @return context to use for further ARM operations, NULL on error
91 struct GNUNET_ARM_Handle *
92 GNUNET_ARM_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
93 struct GNUNET_SCHEDULER_Handle *sched,
96 struct GNUNET_ARM_Handle *ret;
97 struct GNUNET_CLIENT_Connection *client;
99 client = GNUNET_CLIENT_connect (sched, "arm", cfg);
102 ret = GNUNET_malloc (sizeof (struct GNUNET_ARM_Handle));
105 ret->client = client;
111 * Disconnect from the ARM service.
113 * @param h the handle that was being used
116 GNUNET_ARM_disconnect (struct GNUNET_ARM_Handle *h)
118 if (h->client != NULL)
119 GNUNET_CLIENT_disconnect (h->client);
125 * Internal state for a request with ARM.
127 struct RequestContext
131 * Pointer to our handle with ARM.
133 struct GNUNET_ARM_Handle *h;
136 * Function to call with a status code for the requested operation.
138 GNUNET_ARM_Callback callback;
141 * Closure for "callback".
146 * The service that is being manipulated. Do not free.
148 const char *service_name;
151 * Timeout for the operation.
153 struct GNUNET_TIME_Absolute timeout;
156 * Length of service_name plus one.
161 * Number of attempts left for transmitting the request to ARM.
162 * We may fail the first time (say because ARM is not yet up),
163 * in which case we wait a bit and re-try (timeout permitting).
165 unsigned int attempts_left;
168 * Type of the request expressed as a message type (start or stop).
176 * A client specifically requested starting of ARM itself.
177 * This function is called with information about whether
178 * or not ARM is running; if it is, report success. If
179 * it is not, start the ARM process.
181 * @param cls the context for the request that we will report on (struct RequestContext*)
182 * @param tc why were we called (reason says if ARM is running)
185 arm_service_report (void *cls,
186 const struct GNUNET_SCHEDULER_TaskContext *tc)
188 struct RequestContext *pos = cls;
193 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_PREREQ_DONE))
195 /* arm is running! */
196 if (pos->callback != NULL)
197 pos->callback (pos->cls, GNUNET_YES);
201 /* FIXME: should we check that HOSTNAME for 'arm' is localhost? */
204 GNUNET_CONFIGURATION_get_value_string (pos->h->cfg,
209 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
210 _("Configuration failes to specify option `%s' in section `%s'!\n"),
213 if (pos->callback != NULL)
214 pos->callback (pos->cls, GNUNET_SYSERR);
219 GNUNET_CONFIGURATION_get_value_filename (pos->h->cfg,
220 "arm", "CONFIG", &config))
222 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
223 _("Configuration fails to specify option `%s' in section `%s'!\n"),
226 if (pos->callback != NULL)
227 pos->callback (pos->cls, GNUNET_SYSERR);
228 GNUNET_free (binary);
232 pid = GNUNET_OS_start_process (binary, binary, "-d", "-c", config,
237 GNUNET_free (binary);
238 GNUNET_free (config);
241 if (pos->callback != NULL)
242 pos->callback (pos->cls, GNUNET_SYSERR);
246 if (pos->callback != NULL)
247 pos->callback (pos->cls, GNUNET_YES);
253 * Process a response from ARM to a request for a change in service
256 * @param cls the request context
257 * @param msg the response
260 handle_response (void *cls, const struct GNUNET_MessageHeader *msg)
262 struct RequestContext *sc = cls;
267 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
268 _("Error receiving response from ARM service\n"));
269 GNUNET_CLIENT_disconnect (sc->h->client);
270 sc->h->client = GNUNET_CLIENT_connect (sc->h->sched,
273 GNUNET_assert (NULL != sc->h->client);
274 if (sc->callback != NULL)
275 sc->callback (sc->cls, GNUNET_SYSERR);
280 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
281 _("Received response from ARM service\n"));
283 switch (ntohs (msg->type))
285 case GNUNET_MESSAGE_TYPE_ARM_IS_UP:
288 case GNUNET_MESSAGE_TYPE_ARM_IS_DOWN:
291 case GNUNET_MESSAGE_TYPE_ARM_IS_UNKNOWN:
298 if (sc->callback != NULL)
299 sc->callback (sc->cls, ret);
305 * We've failed to transmit the request to the ARM service.
306 * Report our failure and clean up the state.
308 * @param sctx the state of the (now failed) request
311 report_transmit_failure (struct RequestContext *sctx)
313 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
314 _("Error while trying to transmit to ARM service\n"));
315 if (sctx->callback != NULL)
316 sctx->callback (sctx->cls, GNUNET_SYSERR);
322 * Transmit a request for a service status change to the
325 * @param cls the "struct RequestContext" identifying the request
326 * @param size how many bytes are available in buf
327 * @param buf where to write the request, NULL on error
328 * @return number of bytes written to buf
331 send_service_msg (void *cls, size_t size, void *buf);
335 * We've failed to transmit the request to the ARM service but
336 * are now going to try again.
338 * @param cls state of the request
339 * @param tc task context (unused)
342 retry_request (void *cls,
343 const struct GNUNET_SCHEDULER_TaskContext *tc)
345 struct RequestContext *sctx = cls;
348 GNUNET_CLIENT_notify_transmit_ready (sctx->h->client,
351 GNUNET_MessageHeader),
352 GNUNET_TIME_absolute_get_remaining (sctx->timeout),
356 report_transmit_failure (sctx);
363 * Transmit a request for a service status change to the
366 * @param cls the "struct RequestContext" identifying the request
367 * @param size how many bytes are available in buf
368 * @param buf where to write the request, NULL on error
369 * @return number of bytes written to buf
372 send_service_msg (void *cls, size_t size, void *buf)
374 struct RequestContext *sctx = cls;
375 struct GNUNET_MessageHeader *msg;
376 struct GNUNET_TIME_Relative rem;
380 GNUNET_CLIENT_disconnect (sctx->h->client);
381 sctx->h->client = GNUNET_CLIENT_connect (sctx->h->sched,
384 GNUNET_assert (sctx->h->client != NULL);
385 rem = GNUNET_TIME_absolute_get_remaining (sctx->timeout);
386 if ( (sctx->attempts_left-- > 0) &&
389 GNUNET_SCHEDULER_add_delayed (sctx->h->sched,
391 GNUNET_SCHEDULER_PRIORITY_KEEP,
392 GNUNET_SCHEDULER_NO_TASK,
393 GNUNET_TIME_relative_min (MIN_RETRY_DELAY,
399 report_transmit_failure (sctx);
403 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
404 _("Transmitting service request to ARM.\n"));
406 GNUNET_assert (size >= sctx->slen);
408 msg->size = htons (sizeof (struct GNUNET_MessageHeader) + sctx->slen);
409 msg->type = htons (sctx->type);
410 memcpy (&msg[1], sctx->service_name, sctx->slen);
411 GNUNET_CLIENT_receive (sctx->h->client,
414 GNUNET_TIME_absolute_get_remaining (sctx->timeout));
415 return sctx->slen + sizeof (struct GNUNET_MessageHeader);
420 * Start or stop a 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
427 * @param type type of the request
430 change_service (struct GNUNET_ARM_Handle *h,
431 const char *service_name,
432 struct GNUNET_TIME_Relative timeout,
433 GNUNET_ARM_Callback cb, void *cb_cls, uint16_t type)
435 struct RequestContext *sctx;
438 slen = strlen (service_name) + 1;
439 if (slen + sizeof (struct GNUNET_MessageHeader) >
440 GNUNET_SERVER_MAX_MESSAGE_SIZE)
444 cb (cb_cls, GNUNET_NO);
448 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
449 _("ARM requests starting of service `%s'.\n"), service_name);
451 sctx = GNUNET_malloc (sizeof (struct RequestContext) + slen);
455 sctx->service_name = (const char*) &sctx[1];
459 sctx->timeout = GNUNET_TIME_relative_to_absolute (timeout);
461 sctx->attempts_left = MAX_ATTEMPTS;
463 retry_request (sctx, NULL);
470 * @param h handle to ARM
471 * @param service_name name of the service
472 * @param timeout how long to wait before failing for good
473 * @param cb callback to invoke when service is ready
474 * @param cb_cls closure for callback
477 GNUNET_ARM_start_service (struct GNUNET_ARM_Handle *h,
478 const char *service_name,
479 struct GNUNET_TIME_Relative timeout,
480 GNUNET_ARM_Callback cb, void *cb_cls)
482 struct RequestContext *sctx;
484 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
485 _("Starting service `%s'\n"), service_name);
486 if (0 == strcmp ("arm", service_name))
488 sctx = GNUNET_malloc (sizeof (struct RequestContext));
492 sctx->timeout = GNUNET_TIME_relative_to_absolute (timeout);
493 GNUNET_CLIENT_service_test (h->sched,
495 h->cfg, timeout, &arm_service_report, sctx);
498 change_service (h, service_name, timeout, cb, cb_cls, GNUNET_MESSAGE_TYPE_ARM_START);
505 * @param h handle to ARM
506 * @param service_name name of the service
507 * @param timeout how long to wait before failing for good
508 * @param cb callback to invoke when service is ready
509 * @param cb_cls closure for callback
512 GNUNET_ARM_stop_service (struct GNUNET_ARM_Handle *h,
513 const char *service_name,
514 struct GNUNET_TIME_Relative timeout,
515 GNUNET_ARM_Callback cb, void *cb_cls)
517 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
518 _("Stopping service `%s'\n"), service_name);
519 if (0 == strcmp ("arm", service_name))
521 GNUNET_CLIENT_service_shutdown (h->client);
523 cb (cb_cls, GNUNET_NO);
526 change_service (h, service_name, timeout, cb, cb_cls, GNUNET_MESSAGE_TYPE_ARM_STOP);
531 * Function to call for each service.
533 * @param h handle to ARM
534 * @param service_name name of the service
535 * @param timeout how long to wait before failing for good
536 * @param cb callback to invoke when service is ready
537 * @param cb_cls closure for callback
539 typedef void (*ServiceOperation) (struct GNUNET_ARM_Handle *h,
540 const char *service_name,
541 struct GNUNET_TIME_Relative timeout,
542 GNUNET_ARM_Callback cb, void *cb_cls);
546 * Context for starting or stopping multiple services.
551 * NULL-terminated array of services to start or stop.
558 struct GNUNET_ARM_Handle *h;
561 * Identifies the operation (start or stop).
566 * Current position in "services".
573 * Run the operation for the next service in the multi-service
576 * @param cls the "struct MultiContext" that is being processed
577 * @param success status of the previous operation (ignored)
580 next_operation (void *cls,
583 struct MultiContext *mc = cls;
586 if (NULL == (pos = mc->services[mc->pos]))
588 GNUNET_free (mc->services);
589 GNUNET_ARM_disconnect (mc->h);
594 mc->op (mc->h, pos, MULTI_TIMEOUT, &next_operation, mc);
600 * Run a multi-service request.
602 * @param cfg configuration to use (needed to contact ARM;
603 * the ARM service may internally use a different
604 * configuration to determine how to start the service).
605 * @param sched scheduler to use
606 * @param op the operation to perform for each service
607 * @param va NULL-terminated list of services
610 run_multi_request (const struct GNUNET_CONFIGURATION_Handle *cfg,
611 struct GNUNET_SCHEDULER_Handle *sched,
617 struct MultiContext *mc;
618 struct GNUNET_ARM_Handle *h;
621 h = GNUNET_ARM_connect (cfg, sched, NULL);
624 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
625 _("Error while trying to transmit to ARM service\n"));
630 while (NULL != (va_arg (cp, const char*))) total++;
632 mc = GNUNET_malloc (sizeof(struct MultiContext));
633 mc->services = GNUNET_malloc (total * sizeof (char*));
638 while (NULL != (c = va_arg (cp, const char*)))
639 mc->services[total++] = GNUNET_strdup (c);
641 next_operation (mc, GNUNET_YES);
646 * Start multiple services in the specified order. Convenience
647 * function. Works asynchronously, failures are not reported.
649 * @param cfg configuration to use (needed to contact ARM;
650 * the ARM service may internally use a different
651 * configuration to determine how to start the service).
652 * @param sched scheduler to use
653 * @param ... NULL-terminated list of service names (const char*)
656 GNUNET_ARM_start_services (const struct GNUNET_CONFIGURATION_Handle *cfg,
657 struct GNUNET_SCHEDULER_Handle *sched,
662 va_start (ap, sched);
663 run_multi_request (cfg, sched, &GNUNET_ARM_start_service, ap);
669 * Stop multiple services in the specified order. Convenience
670 * function. Works asynchronously, failures are not reported.
672 * @param cfg configuration to use (needed to contact ARM;
673 * the ARM service may internally use a different
674 * configuration to determine how to start the service).
675 * @param sched scheduler to use
676 * @param ... NULL-terminated list of service names (const char*)
679 GNUNET_ARM_stop_services (const struct GNUNET_CONFIGURATION_Handle *cfg,
680 struct GNUNET_SCHEDULER_Handle *sched,
685 va_start (ap, sched);
686 run_multi_request (cfg, sched, &GNUNET_ARM_stop_service, ap);
691 /* end of arm_api.c */