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, 1)
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, GNUNET_NO);
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).
146 #include "do_start_process.c"
150 * A client specifically requested starting of ARM itself.
151 * This function is called with information about whether
152 * or not ARM is running; if it is, report success. If
153 * it is not, start the ARM process.
155 * @param cls the context for the request that we will report on (struct RequestContext*)
156 * @param tc why were we called (reason says if ARM is running)
159 arm_service_report (void *cls,
160 const struct GNUNET_SCHEDULER_TaskContext *tc)
162 struct RequestContext *pos = cls;
169 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_PREREQ_DONE))
172 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
173 "Looks like `%s' is already running.\n",
174 "gnunet-service-arm");
176 /* arm is running! */
177 if (pos->callback != NULL)
178 pos->callback (pos->cls, GNUNET_YES);
183 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
184 "Looks like `%s' is not running, will start it.\n",
185 "gnunet-service-arm");
187 /* FIXME: should we check that HOSTNAME for 'arm' is localhost? */
190 GNUNET_CONFIGURATION_get_value_string (pos->h->cfg,
191 "arm", "PREFIX", &loprefix))
192 loprefix = GNUNET_strdup ("");
194 GNUNET_CONFIGURATION_get_value_string (pos->h->cfg,
195 "arm", "OPTIONS", &lopostfix))
196 lopostfix = GNUNET_strdup ("");
198 GNUNET_CONFIGURATION_get_value_string (pos->h->cfg,
203 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
204 _("Configuration failes to specify option `%s' in section `%s'!\n"),
207 if (pos->callback != NULL)
208 pos->callback (pos->cls, GNUNET_SYSERR);
210 GNUNET_free (loprefix);
211 GNUNET_free (lopostfix);
215 GNUNET_CONFIGURATION_get_value_filename (pos->h->cfg,
216 "arm", "CONFIG", &config))
218 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
219 _("Configuration fails to specify option `%s' in section `%s'!\n"),
222 if (pos->callback != NULL)
223 pos->callback (pos->cls, GNUNET_SYSERR);
224 GNUNET_free (binary);
226 GNUNET_free (loprefix);
227 GNUNET_free (lopostfix);
230 pid = do_start_process (loprefix,
239 GNUNET_free (binary);
240 GNUNET_free (config);
241 GNUNET_free (loprefix);
242 GNUNET_free (lopostfix);
245 if (pos->callback != NULL)
246 pos->callback (pos->cls, GNUNET_SYSERR);
250 if (pos->callback != NULL)
251 pos->callback (pos->cls, GNUNET_YES);
257 * Process a response from ARM to a request for a change in service
260 * @param cls the request context
261 * @param msg the response
264 handle_response (void *cls, const struct GNUNET_MessageHeader *msg)
266 struct RequestContext *sc = cls;
271 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
272 _("Error receiving response to `%s' request from ARM for service `%s'\n"),
273 (sc->type == GNUNET_MESSAGE_TYPE_ARM_START)
276 (const char*) &sc[1]);
277 GNUNET_CLIENT_disconnect (sc->h->client, GNUNET_NO);
278 sc->h->client = GNUNET_CLIENT_connect (sc->h->sched,
281 GNUNET_assert (NULL != sc->h->client);
282 GNUNET_CLIENT_ignore_shutdown (sc->h->client, GNUNET_YES);
283 if (sc->callback != NULL)
284 sc->callback (sc->cls, GNUNET_SYSERR);
289 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
290 "Received response from ARM for service `%s': %u\n",
291 (const char*) &sc[1],
294 switch (ntohs (msg->type))
296 case GNUNET_MESSAGE_TYPE_ARM_IS_UP:
299 case GNUNET_MESSAGE_TYPE_ARM_IS_DOWN:
302 case GNUNET_MESSAGE_TYPE_ARM_IS_UNKNOWN:
309 if (sc->callback != NULL)
310 sc->callback (sc->cls, ret);
316 * Start or stop a service.
318 * @param h handle to ARM
319 * @param service_name name of the service
320 * @param timeout how long to wait before failing for good
321 * @param cb callback to invoke when service is ready
322 * @param cb_cls closure for callback
323 * @param type type of the request
326 change_service (struct GNUNET_ARM_Handle *h,
327 const char *service_name,
328 struct GNUNET_TIME_Relative timeout,
329 GNUNET_ARM_Callback cb, void *cb_cls, uint16_t type)
331 struct RequestContext *sctx;
333 struct GNUNET_MessageHeader *msg;
335 slen = strlen (service_name) + 1;
336 if (slen + sizeof (struct GNUNET_MessageHeader) >
337 GNUNET_SERVER_MAX_MESSAGE_SIZE)
341 cb (cb_cls, GNUNET_NO);
345 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
346 (type == GNUNET_MESSAGE_TYPE_ARM_START)
347 ? _("Requesting start of service `%s'.\n")
348 : _("Requesting termination of service `%s'.\n"),
351 sctx = GNUNET_malloc (sizeof (struct RequestContext) + slen);
355 sctx->timeout = GNUNET_TIME_relative_to_absolute (timeout);
357 memcpy (&sctx[1], service_name, slen);
358 msg = GNUNET_malloc (sizeof (struct GNUNET_MessageHeader) + slen);
359 msg->size = htons (sizeof (struct GNUNET_MessageHeader) + slen);
360 msg->type = htons (sctx->type);
361 memcpy (&msg[1], service_name, slen);
363 GNUNET_CLIENT_transmit_and_get_response (sctx->h->client,
365 GNUNET_TIME_absolute_get_remaining (sctx->timeout),
370 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
371 (type == GNUNET_MESSAGE_TYPE_ARM_START)
372 ? _("Error while trying to transmit request to start `%s' to ARM\n")
373 : _("Error while trying to transmit request to stop `%s' to ARM\n"),
374 (const char*) &service_name);
376 cb (cb_cls, GNUNET_SYSERR);
388 * @param h handle to ARM
389 * @param service_name name of the service
390 * @param timeout how long to wait before failing for good
391 * @param cb callback to invoke when service is ready
392 * @param cb_cls closure for callback
395 GNUNET_ARM_start_service (struct GNUNET_ARM_Handle *h,
396 const char *service_name,
397 struct GNUNET_TIME_Relative timeout,
398 GNUNET_ARM_Callback cb, void *cb_cls)
400 struct RequestContext *sctx;
403 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
404 _("Asked to start service `%s' within %llu ms\n"), service_name,
405 (unsigned long long) timeout.value);
406 if (0 == strcasecmp ("arm", service_name))
408 slen = strlen ("arm") + 1;
409 sctx = GNUNET_malloc (sizeof (struct RequestContext) + slen);
413 sctx->timeout = GNUNET_TIME_relative_to_absolute (timeout);
414 memcpy (&sctx[1], service_name, slen);
415 GNUNET_CLIENT_service_test (h->sched,
417 h->cfg, timeout, &arm_service_report, sctx);
420 change_service (h, service_name, timeout, cb, cb_cls, GNUNET_MESSAGE_TYPE_ARM_START);
427 * @param h handle to ARM
428 * @param service_name name of the service
429 * @param timeout how long to wait before failing for good
430 * @param cb callback to invoke when service is ready
431 * @param cb_cls closure for callback
434 GNUNET_ARM_stop_service (struct GNUNET_ARM_Handle *h,
435 const char *service_name,
436 struct GNUNET_TIME_Relative timeout,
437 GNUNET_ARM_Callback cb, void *cb_cls)
439 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
440 _("Stopping service `%s' within %llu ms\n"), service_name,
441 (unsigned long long) timeout.value);
442 if (0 == strcasecmp ("arm", service_name))
444 GNUNET_CLIENT_service_shutdown (h->client);
447 cb (cb_cls, GNUNET_NO);
450 change_service (h, service_name, timeout, cb, cb_cls, GNUNET_MESSAGE_TYPE_ARM_STOP);
455 * Function to call for each service.
457 * @param h handle to ARM
458 * @param service_name name of the service
459 * @param timeout how long to wait before failing for good
460 * @param cb callback to invoke when service is ready
461 * @param cb_cls closure for callback
463 typedef void (*ServiceOperation) (struct GNUNET_ARM_Handle *h,
464 const char *service_name,
465 struct GNUNET_TIME_Relative timeout,
466 GNUNET_ARM_Callback cb, void *cb_cls);
470 * Context for starting or stopping multiple services.
475 * NULL-terminated array of services to start or stop.
482 struct GNUNET_ARM_Handle *h;
485 * Identifies the operation (start or stop).
490 * Current position in "services".
497 * Run the operation for the next service in the multi-service
500 * @param cls the "struct MultiContext" that is being processed
501 * @param success status of the previous operation (ignored)
504 next_operation (void *cls,
507 struct MultiContext *mc = cls;
510 if (NULL == (pos = mc->services[mc->pos]))
512 GNUNET_free (mc->services);
513 GNUNET_ARM_disconnect (mc->h);
518 mc->op (mc->h, pos, MULTI_TIMEOUT, &next_operation, mc);
524 * Run a multi-service request.
526 * @param cfg configuration to use (needed to contact ARM;
527 * the ARM service may internally use a different
528 * configuration to determine how to start the service).
529 * @param sched scheduler to use
530 * @param op the operation to perform for each service
531 * @param va NULL-terminated list of services
534 run_multi_request (const struct GNUNET_CONFIGURATION_Handle *cfg,
535 struct GNUNET_SCHEDULER_Handle *sched,
541 struct MultiContext *mc;
542 struct GNUNET_ARM_Handle *h;
545 h = GNUNET_ARM_connect (cfg, sched, NULL);
548 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
549 _("Error while trying to transmit to ARM service\n"));
554 while (NULL != (va_arg (cp, const char*))) total++;
556 mc = GNUNET_malloc (sizeof(struct MultiContext));
557 mc->services = GNUNET_malloc (total * sizeof (char*));
562 while (NULL != (c = va_arg (cp, const char*)))
563 mc->services[total++] = GNUNET_strdup (c);
565 next_operation (mc, GNUNET_YES);
570 * Start multiple services in the specified order. Convenience
571 * function. Works asynchronously, failures are not reported.
573 * @param cfg configuration to use (needed to contact ARM;
574 * the ARM service may internally use a different
575 * configuration to determine how to start the service).
576 * @param sched scheduler to use
577 * @param ... NULL-terminated list of service names (const char*)
580 GNUNET_ARM_start_services (const struct GNUNET_CONFIGURATION_Handle *cfg,
581 struct GNUNET_SCHEDULER_Handle *sched,
586 va_start (ap, sched);
587 run_multi_request (cfg, sched, &GNUNET_ARM_start_service, ap);
593 * Stop multiple services in the specified order. Convenience
594 * function. Works asynchronously, failures are not reported.
596 * @param cfg configuration to use (needed to contact ARM;
597 * the ARM service may internally use a different
598 * configuration to determine how to start the service).
599 * @param sched scheduler to use
600 * @param ... NULL-terminated list of service names (const char*)
603 GNUNET_ARM_stop_services (const struct GNUNET_CONFIGURATION_Handle *cfg,
604 struct GNUNET_SCHEDULER_Handle *sched,
609 va_start (ap, sched);
610 run_multi_request (cfg, sched, &GNUNET_ARM_stop_service, ap);
615 /* end of arm_api.c */