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? */
184 /* FIXME: interpret 'PREFIX' and 'OPTIONS' configuration options
185 (as done by 'gnunet-service-arm.c::start_process') */
188 GNUNET_CONFIGURATION_get_value_string (pos->h->cfg,
193 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
194 _("Configuration failes to specify option `%s' in section `%s'!\n"),
197 if (pos->callback != NULL)
198 pos->callback (pos->cls, GNUNET_SYSERR);
203 GNUNET_CONFIGURATION_get_value_filename (pos->h->cfg,
204 "arm", "CONFIG", &config))
206 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
207 _("Configuration fails to specify option `%s' in section `%s'!\n"),
210 if (pos->callback != NULL)
211 pos->callback (pos->cls, GNUNET_SYSERR);
212 GNUNET_free (binary);
216 pid = GNUNET_OS_start_process (NULL, NULL, binary, binary, "-d", "-c", config,
221 GNUNET_free (binary);
222 GNUNET_free (config);
225 if (pos->callback != NULL)
226 pos->callback (pos->cls, GNUNET_SYSERR);
230 if (pos->callback != NULL)
231 pos->callback (pos->cls, GNUNET_YES);
237 * Process a response from ARM to a request for a change in service
240 * @param cls the request context
241 * @param msg the response
244 handle_response (void *cls, const struct GNUNET_MessageHeader *msg)
246 struct RequestContext *sc = cls;
251 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
252 _("Error receiving response to `%s' request from ARM for service `%s'\n"),
253 (sc->type == GNUNET_MESSAGE_TYPE_ARM_START)
256 (const char*) &sc[1]);
257 GNUNET_CLIENT_disconnect (sc->h->client);
258 sc->h->client = GNUNET_CLIENT_connect (sc->h->sched,
261 GNUNET_assert (NULL != sc->h->client);
262 GNUNET_CLIENT_ignore_shutdown (sc->h->client, GNUNET_YES);
263 if (sc->callback != NULL)
264 sc->callback (sc->cls, GNUNET_SYSERR);
269 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
270 "Received response from ARM for service `%s': %u\n",
271 (const char*) &sc[1],
274 switch (ntohs (msg->type))
276 case GNUNET_MESSAGE_TYPE_ARM_IS_UP:
279 case GNUNET_MESSAGE_TYPE_ARM_IS_DOWN:
282 case GNUNET_MESSAGE_TYPE_ARM_IS_UNKNOWN:
289 if (sc->callback != NULL)
290 sc->callback (sc->cls, ret);
296 * Start or stop a service.
298 * @param h handle to ARM
299 * @param service_name name of the service
300 * @param timeout how long to wait before failing for good
301 * @param cb callback to invoke when service is ready
302 * @param cb_cls closure for callback
303 * @param type type of the request
306 change_service (struct GNUNET_ARM_Handle *h,
307 const char *service_name,
308 struct GNUNET_TIME_Relative timeout,
309 GNUNET_ARM_Callback cb, void *cb_cls, uint16_t type)
311 struct RequestContext *sctx;
313 struct GNUNET_MessageHeader *msg;
315 slen = strlen (service_name) + 1;
316 if (slen + sizeof (struct GNUNET_MessageHeader) >
317 GNUNET_SERVER_MAX_MESSAGE_SIZE)
321 cb (cb_cls, GNUNET_NO);
325 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
326 (type == GNUNET_MESSAGE_TYPE_ARM_START)
327 ? _("Requesting start of service `%s'.\n")
328 : _("Requesting termination of service `%s'.\n"),
331 sctx = GNUNET_malloc (sizeof (struct RequestContext) + slen);
335 sctx->timeout = GNUNET_TIME_relative_to_absolute (timeout);
337 memcpy (&sctx[1], service_name, slen);
338 msg = GNUNET_malloc (sizeof (struct GNUNET_MessageHeader) + slen);
339 msg->size = htons (sizeof (struct GNUNET_MessageHeader) + slen);
340 msg->type = htons (sctx->type);
341 memcpy (&msg[1], service_name, slen);
343 GNUNET_CLIENT_transmit_and_get_response (sctx->h->client,
345 GNUNET_TIME_absolute_get_remaining (sctx->timeout),
350 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
351 (type == GNUNET_MESSAGE_TYPE_ARM_START)
352 ? _("Error while trying to transmit request to start `%s' to ARM\n")
353 : _("Error while trying to transmit request to stop `%s' to ARM\n"),
354 (const char*) &service_name);
356 cb (cb_cls, GNUNET_SYSERR);
368 * @param h handle to ARM
369 * @param service_name name of the service
370 * @param timeout how long to wait before failing for good
371 * @param cb callback to invoke when service is ready
372 * @param cb_cls closure for callback
375 GNUNET_ARM_start_service (struct GNUNET_ARM_Handle *h,
376 const char *service_name,
377 struct GNUNET_TIME_Relative timeout,
378 GNUNET_ARM_Callback cb, void *cb_cls)
380 struct RequestContext *sctx;
383 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
384 _("Asked to start service `%s' within %llu ms\n"), service_name,
385 (unsigned long long) timeout.value);
386 if (0 == strcasecmp ("arm", service_name))
388 slen = strlen ("arm") + 1;
389 sctx = GNUNET_malloc (sizeof (struct RequestContext) + slen);
393 sctx->timeout = GNUNET_TIME_relative_to_absolute (timeout);
394 memcpy (&sctx[1], service_name, slen);
395 GNUNET_CLIENT_service_test (h->sched,
397 h->cfg, timeout, &arm_service_report, sctx);
400 change_service (h, service_name, timeout, cb, cb_cls, GNUNET_MESSAGE_TYPE_ARM_START);
407 * @param h handle to ARM
408 * @param service_name name of the service
409 * @param timeout how long to wait before failing for good
410 * @param cb callback to invoke when service is ready
411 * @param cb_cls closure for callback
414 GNUNET_ARM_stop_service (struct GNUNET_ARM_Handle *h,
415 const char *service_name,
416 struct GNUNET_TIME_Relative timeout,
417 GNUNET_ARM_Callback cb, void *cb_cls)
419 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
420 _("Stopping service `%s' within %llu ms\n"), service_name,
421 (unsigned long long) timeout.value);
422 if (0 == strcasecmp ("arm", service_name))
424 GNUNET_CLIENT_service_shutdown (h->client);
427 cb (cb_cls, GNUNET_NO);
430 change_service (h, service_name, timeout, cb, cb_cls, GNUNET_MESSAGE_TYPE_ARM_STOP);
435 * Function to call for each service.
437 * @param h handle to ARM
438 * @param service_name name of the service
439 * @param timeout how long to wait before failing for good
440 * @param cb callback to invoke when service is ready
441 * @param cb_cls closure for callback
443 typedef void (*ServiceOperation) (struct GNUNET_ARM_Handle *h,
444 const char *service_name,
445 struct GNUNET_TIME_Relative timeout,
446 GNUNET_ARM_Callback cb, void *cb_cls);
450 * Context for starting or stopping multiple services.
455 * NULL-terminated array of services to start or stop.
462 struct GNUNET_ARM_Handle *h;
465 * Identifies the operation (start or stop).
470 * Current position in "services".
477 * Run the operation for the next service in the multi-service
480 * @param cls the "struct MultiContext" that is being processed
481 * @param success status of the previous operation (ignored)
484 next_operation (void *cls,
487 struct MultiContext *mc = cls;
490 if (NULL == (pos = mc->services[mc->pos]))
492 GNUNET_free (mc->services);
493 GNUNET_ARM_disconnect (mc->h);
498 mc->op (mc->h, pos, MULTI_TIMEOUT, &next_operation, mc);
504 * Run a multi-service request.
506 * @param cfg configuration to use (needed to contact ARM;
507 * the ARM service may internally use a different
508 * configuration to determine how to start the service).
509 * @param sched scheduler to use
510 * @param op the operation to perform for each service
511 * @param va NULL-terminated list of services
514 run_multi_request (const struct GNUNET_CONFIGURATION_Handle *cfg,
515 struct GNUNET_SCHEDULER_Handle *sched,
521 struct MultiContext *mc;
522 struct GNUNET_ARM_Handle *h;
525 h = GNUNET_ARM_connect (cfg, sched, NULL);
528 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
529 _("Error while trying to transmit to ARM service\n"));
534 while (NULL != (va_arg (cp, const char*))) total++;
536 mc = GNUNET_malloc (sizeof(struct MultiContext));
537 mc->services = GNUNET_malloc (total * sizeof (char*));
542 while (NULL != (c = va_arg (cp, const char*)))
543 mc->services[total++] = GNUNET_strdup (c);
545 next_operation (mc, GNUNET_YES);
550 * Start multiple services in the specified order. Convenience
551 * function. Works asynchronously, failures are not reported.
553 * @param cfg configuration to use (needed to contact ARM;
554 * the ARM service may internally use a different
555 * configuration to determine how to start the service).
556 * @param sched scheduler to use
557 * @param ... NULL-terminated list of service names (const char*)
560 GNUNET_ARM_start_services (const struct GNUNET_CONFIGURATION_Handle *cfg,
561 struct GNUNET_SCHEDULER_Handle *sched,
566 va_start (ap, sched);
567 run_multi_request (cfg, sched, &GNUNET_ARM_start_service, ap);
573 * Stop multiple services in the specified order. Convenience
574 * function. Works asynchronously, failures are not reported.
576 * @param cfg configuration to use (needed to contact ARM;
577 * the ARM service may internally use a different
578 * configuration to determine how to start the service).
579 * @param sched scheduler to use
580 * @param ... NULL-terminated list of service names (const char*)
583 GNUNET_ARM_stop_services (const struct GNUNET_CONFIGURATION_Handle *cfg,
584 struct GNUNET_SCHEDULER_Handle *sched,
589 va_start (ap, sched);
590 run_multi_request (cfg, sched, &GNUNET_ARM_stop_service, ap);
595 /* end of arm_api.c */