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 const 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));
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);
112 * Internal state for a request with ARM.
114 struct RequestContext
118 * Pointer to our handle with ARM.
120 struct GNUNET_ARM_Handle *h;
123 * Function to call with a status code for the requested operation.
125 GNUNET_ARM_Callback callback;
128 * Closure for "callback".
133 * Timeout for the operation.
135 struct GNUNET_TIME_Absolute timeout;
138 * Type of the request expressed as a message type (start or stop).
146 * A client specifically requested starting of ARM itself.
147 * This function is called with information about whether
148 * or not ARM is running; if it is, report success. If
149 * it is not, start the ARM process.
151 * @param cls the context for the request that we will report on (struct RequestContext*)
152 * @param tc why were we called (reason says if ARM is running)
155 arm_service_report (void *cls,
156 const struct GNUNET_SCHEDULER_TaskContext *tc)
158 struct RequestContext *pos = cls;
163 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_PREREQ_DONE))
165 /* arm is running! */
166 if (pos->callback != NULL)
167 pos->callback (pos->cls, GNUNET_YES);
171 /* FIXME: should we check that HOSTNAME for 'arm' is localhost? */
174 GNUNET_CONFIGURATION_get_value_string (pos->h->cfg,
179 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
180 _("Configuration failes to specify option `%s' in section `%s'!\n"),
183 if (pos->callback != NULL)
184 pos->callback (pos->cls, GNUNET_SYSERR);
189 GNUNET_CONFIGURATION_get_value_filename (pos->h->cfg,
190 "arm", "CONFIG", &config))
192 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
193 _("Configuration fails to specify option `%s' in section `%s'!\n"),
196 if (pos->callback != NULL)
197 pos->callback (pos->cls, GNUNET_SYSERR);
198 GNUNET_free (binary);
202 pid = GNUNET_OS_start_process (binary, binary, "-d", "-c", config,
207 GNUNET_free (binary);
208 GNUNET_free (config);
211 if (pos->callback != NULL)
212 pos->callback (pos->cls, GNUNET_SYSERR);
216 if (pos->callback != NULL)
217 pos->callback (pos->cls, GNUNET_YES);
223 * Process a response from ARM to a request for a change in service
226 * @param cls the request context
227 * @param msg the response
230 handle_response (void *cls, const struct GNUNET_MessageHeader *msg)
232 struct RequestContext *sc = cls;
237 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
238 _("Error receiving response from ARM service\n"));
239 GNUNET_CLIENT_disconnect (sc->h->client);
240 sc->h->client = GNUNET_CLIENT_connect (sc->h->sched,
243 GNUNET_assert (NULL != sc->h->client);
244 if (sc->callback != NULL)
245 sc->callback (sc->cls, GNUNET_SYSERR);
250 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
251 "Received response from ARM service: %u\n",
254 switch (ntohs (msg->type))
256 case GNUNET_MESSAGE_TYPE_ARM_IS_UP:
259 case GNUNET_MESSAGE_TYPE_ARM_IS_DOWN:
262 case GNUNET_MESSAGE_TYPE_ARM_IS_UNKNOWN:
269 if (sc->callback != NULL)
270 sc->callback (sc->cls, ret);
276 * Start or stop a service.
278 * @param h handle to ARM
279 * @param service_name name of the service
280 * @param timeout how long to wait before failing for good
281 * @param cb callback to invoke when service is ready
282 * @param cb_cls closure for callback
283 * @param type type of the request
286 change_service (struct GNUNET_ARM_Handle *h,
287 const char *service_name,
288 struct GNUNET_TIME_Relative timeout,
289 GNUNET_ARM_Callback cb, void *cb_cls, uint16_t type)
291 struct RequestContext *sctx;
293 struct GNUNET_MessageHeader *msg;
295 slen = strlen (service_name) + 1;
296 if (slen + sizeof (struct GNUNET_MessageHeader) >
297 GNUNET_SERVER_MAX_MESSAGE_SIZE)
301 cb (cb_cls, GNUNET_NO);
305 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
306 _("ARM requests starting of service `%s'.\n"), service_name);
308 sctx = GNUNET_malloc (sizeof (struct RequestContext) + slen);
312 sctx->timeout = GNUNET_TIME_relative_to_absolute (timeout);
314 msg = GNUNET_malloc (sizeof (struct GNUNET_MessageHeader) + slen);
315 msg->size = htons (sizeof (struct GNUNET_MessageHeader) + slen);
316 msg->type = htons (sctx->type);
317 memcpy (&msg[1], service_name, slen);
319 GNUNET_CLIENT_transmit_and_get_response (sctx->h->client,
321 GNUNET_TIME_absolute_get_remaining (sctx->timeout),
326 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
327 _("Error while trying to transmit to ARM service\n"));
329 cb (cb_cls, GNUNET_SYSERR);
341 * @param h handle to ARM
342 * @param service_name name of the service
343 * @param timeout how long to wait before failing for good
344 * @param cb callback to invoke when service is ready
345 * @param cb_cls closure for callback
348 GNUNET_ARM_start_service (struct GNUNET_ARM_Handle *h,
349 const char *service_name,
350 struct GNUNET_TIME_Relative timeout,
351 GNUNET_ARM_Callback cb, void *cb_cls)
353 struct RequestContext *sctx;
355 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
356 _("Starting service `%s'\n"), service_name);
357 if (0 == strcmp ("arm", service_name))
359 sctx = GNUNET_malloc (sizeof (struct RequestContext));
363 sctx->timeout = GNUNET_TIME_relative_to_absolute (timeout);
364 GNUNET_CLIENT_service_test (h->sched,
366 h->cfg, timeout, &arm_service_report, sctx);
369 change_service (h, service_name, timeout, cb, cb_cls, GNUNET_MESSAGE_TYPE_ARM_START);
376 * @param h handle to ARM
377 * @param service_name name of the service
378 * @param timeout how long to wait before failing for good
379 * @param cb callback to invoke when service is ready
380 * @param cb_cls closure for callback
383 GNUNET_ARM_stop_service (struct GNUNET_ARM_Handle *h,
384 const char *service_name,
385 struct GNUNET_TIME_Relative timeout,
386 GNUNET_ARM_Callback cb, void *cb_cls)
388 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
389 _("Stopping service `%s'\n"), service_name);
390 if (0 == strcmp ("arm", service_name))
392 GNUNET_CLIENT_service_shutdown (h->client);
394 cb (cb_cls, GNUNET_NO);
397 change_service (h, service_name, timeout, cb, cb_cls, GNUNET_MESSAGE_TYPE_ARM_STOP);
402 * Function to call for each service.
404 * @param h handle to ARM
405 * @param service_name name of the service
406 * @param timeout how long to wait before failing for good
407 * @param cb callback to invoke when service is ready
408 * @param cb_cls closure for callback
410 typedef void (*ServiceOperation) (struct GNUNET_ARM_Handle *h,
411 const char *service_name,
412 struct GNUNET_TIME_Relative timeout,
413 GNUNET_ARM_Callback cb, void *cb_cls);
417 * Context for starting or stopping multiple services.
422 * NULL-terminated array of services to start or stop.
429 struct GNUNET_ARM_Handle *h;
432 * Identifies the operation (start or stop).
437 * Current position in "services".
444 * Run the operation for the next service in the multi-service
447 * @param cls the "struct MultiContext" that is being processed
448 * @param success status of the previous operation (ignored)
451 next_operation (void *cls,
454 struct MultiContext *mc = cls;
457 if (NULL == (pos = mc->services[mc->pos]))
459 GNUNET_free (mc->services);
460 GNUNET_ARM_disconnect (mc->h);
465 mc->op (mc->h, pos, MULTI_TIMEOUT, &next_operation, mc);
471 * Run a multi-service request.
473 * @param cfg configuration to use (needed to contact ARM;
474 * the ARM service may internally use a different
475 * configuration to determine how to start the service).
476 * @param sched scheduler to use
477 * @param op the operation to perform for each service
478 * @param va NULL-terminated list of services
481 run_multi_request (const struct GNUNET_CONFIGURATION_Handle *cfg,
482 struct GNUNET_SCHEDULER_Handle *sched,
488 struct MultiContext *mc;
489 struct GNUNET_ARM_Handle *h;
492 h = GNUNET_ARM_connect (cfg, sched, NULL);
495 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
496 _("Error while trying to transmit to ARM service\n"));
501 while (NULL != (va_arg (cp, const char*))) total++;
503 mc = GNUNET_malloc (sizeof(struct MultiContext));
504 mc->services = GNUNET_malloc (total * sizeof (char*));
509 while (NULL != (c = va_arg (cp, const char*)))
510 mc->services[total++] = GNUNET_strdup (c);
512 next_operation (mc, GNUNET_YES);
517 * Start multiple services in the specified order. Convenience
518 * function. Works asynchronously, failures are not reported.
520 * @param cfg configuration to use (needed to contact ARM;
521 * the ARM service may internally use a different
522 * configuration to determine how to start the service).
523 * @param sched scheduler to use
524 * @param ... NULL-terminated list of service names (const char*)
527 GNUNET_ARM_start_services (const struct GNUNET_CONFIGURATION_Handle *cfg,
528 struct GNUNET_SCHEDULER_Handle *sched,
533 va_start (ap, sched);
534 run_multi_request (cfg, sched, &GNUNET_ARM_start_service, ap);
540 * Stop multiple services in the specified order. Convenience
541 * function. Works asynchronously, failures are not reported.
543 * @param cfg configuration to use (needed to contact ARM;
544 * the ARM service may internally use a different
545 * configuration to determine how to start the service).
546 * @param sched scheduler to use
547 * @param ... NULL-terminated list of service names (const char*)
550 GNUNET_ARM_stop_services (const struct GNUNET_CONFIGURATION_Handle *cfg,
551 struct GNUNET_SCHEDULER_Handle *sched,
556 va_start (ap, sched);
557 run_multi_request (cfg, sched, &GNUNET_ARM_stop_service, ap);
562 /* end of arm_api.c */