2 This file is part of GNUnet.
3 Copyright (C) 2009, 2010, 2012, 2013, 2016 GNUnet e.V.
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 3, 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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
23 * @brief API for accessing the ARM service
24 * @author Christian Grothoff
28 #include "gnunet_util_lib.h"
29 #include "gnunet_arm_service.h"
30 #include "gnunet_protocols.h"
33 #define LOG(kind,...) GNUNET_log_from (kind, "arm-api",__VA_ARGS__)
36 * Handle for interacting with ARM.
38 struct GNUNET_ARM_Handle
41 * Our control connection to the ARM service.
43 struct GNUNET_MQ_Handle *mq;
46 * The configuration that we are using.
48 struct GNUNET_CONFIGURATION_Handle *cfg;
51 * Handle for our current transmission request.
53 struct GNUNET_CLIENT_TransmitHandle *cth;
56 * Head of doubly-linked list of pending requests.
58 struct ARMControlMessage *control_pending_head;
61 * Tail of doubly-linked list of pending requests.
63 struct ARMControlMessage *control_pending_tail;
66 * Head of doubly-linked list of sent requests.
68 struct ARMControlMessage *control_sent_head;
71 * Tail of doubly-linked list of sent requests.
73 struct ARMControlMessage *control_sent_tail;
76 * Callback to invoke on connection/disconnection.
78 GNUNET_ARM_ConnectionStatusCallback conn_status;
81 * Closure for conn_status.
83 void *conn_status_cls;
86 * ARM control message for the 'arm_termination_handler'
87 * with the continuation to call once the ARM shutdown is done.
89 struct ARMControlMessage *thm;
92 * ID of the reconnect task (if any).
94 struct GNUNET_SCHEDULER_Task *reconnect_task;
97 * Current delay we use for re-trying to connect to core.
99 struct GNUNET_TIME_Relative retry_backoff;
102 * Counter for request identifiers
104 uint64_t request_id_counter;
107 * Are we currently disconnected and hence unable to send?
109 unsigned char currently_down;
112 * #GNUNET_YES if we're running a service test.
114 unsigned char service_test_is_active;
119 * Entry in a doubly-linked list of control messages to be transmitted
120 * to the arm service.
122 * The actual message is allocated at the end of this struct.
124 struct ARMControlMessage
127 * This is a doubly-linked list.
129 struct ARMControlMessage *next;
132 * This is a doubly-linked list.
134 struct ARMControlMessage *prev;
139 struct GNUNET_ARM_Handle *h;
144 struct GNUNET_ARM_Message *msg;
147 * Callback for service state change requests.
149 GNUNET_ARM_ResultCallback result_cont;
152 * Callback for service list requests.
154 GNUNET_ARM_ServiceListCallback list_cont;
157 * Closure for @e result_cont or @e list_cont.
162 * Timeout for the operation.
164 struct GNUNET_TIME_Absolute timeout;
167 * Task to run when request times out.
169 struct GNUNET_SCHEDULER_Task *timeout_task_id;
172 * Flags for passing std descriptors to ARM (when starting ARM).
174 enum GNUNET_OS_InheritStdioFlags std_inheritance;
177 * Type of the request expressed as a message type (start, stop or list).
186 * @param h arm handle
187 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
190 reconnect_arm (struct GNUNET_ARM_Handle *h);
194 * Check the list of pending requests, send the next
197 * @param h arm handle
198 * @param ignore_currently_down transmit message even if not initialized?
201 trigger_next_request (struct GNUNET_ARM_Handle *h,
202 int ignore_currently_down);
206 * Task scheduled to try to re-connect to arm.
208 * @param cls the `struct GNUNET_ARM_Handle`
211 reconnect_arm_task (void *cls)
213 struct GNUNET_ARM_Handle *h = cls;
215 h->reconnect_task = NULL;
216 LOG (GNUNET_ERROR_TYPE_DEBUG,
217 "Connecting to ARM service after delay\n");
223 * Close down any existing connection to the ARM service and
224 * try re-establishing it later.
226 * @param h our handle
229 reconnect_arm_later (struct GNUNET_ARM_Handle *h)
231 if (GNUNET_NO != h->currently_down)
235 GNUNET_CLIENT_notify_transmit_ready_cancel (h->cth);
238 if (NULL != h->client)
240 GNUNET_CLIENT_disconnect (h->client);
243 h->currently_down = GNUNET_YES;
244 GNUNET_assert (NULL == h->reconnect_task);
246 GNUNET_SCHEDULER_add_delayed (h->retry_backoff,
249 /* Don't clear pending messages on disconnection, deliver them later
250 clear_pending_messages (h, GNUNET_ARM_REQUEST_DISCONNECTED);
251 GNUNET_assert (NULL == h->control_pending_head);
253 h->retry_backoff = GNUNET_TIME_STD_BACKOFF (h->retry_backoff);
254 if (NULL != h->conn_status)
255 h->conn_status (h->conn_status_cls,
261 * Find a control message by its unique ID.
263 * @param h ARM handle
264 * @param id unique message ID to use for the lookup
265 * @return NULL if not found
267 static struct ARMControlMessage *
268 find_cm_by_id (struct GNUNET_ARM_Handle *h,
271 struct ARMControlMessage *result;
273 for (result = h->control_sent_head; NULL != result; result = result->next)
274 if (id == result->msg->request_id)
281 * Handler for ARM 'termination' reply (failure to receive).
283 * @param cls our `struct GNUNET_ARM_Handle`
284 * @param msg expected to be NULL
287 arm_termination_handler (void *cls,
288 const struct GNUNET_MessageHeader *msg)
290 struct GNUNET_ARM_Handle *h = cls;
291 struct ARMControlMessage *cm;
296 GNUNET_CLIENT_receive (h->client,
297 &arm_termination_handler,
299 GNUNET_TIME_UNIT_FOREVER_REL);
304 h->currently_down = GNUNET_YES;
305 GNUNET_CLIENT_disconnect (h->client);
307 if (NULL != cm->result_cont)
308 cm->result_cont (cm->cont_cls,
309 GNUNET_ARM_REQUEST_SENT_OK,
310 (const char *) &cm->msg[1],
311 GNUNET_ARM_RESULT_STOPPED);
312 GNUNET_free (cm->msg);
318 * Handler for ARM replies.
320 * @param cls our `struct GNUNET_ARM_Handle`
321 * @param msg the message received from the arm service
324 client_notify_handler (void *cls,
325 const struct GNUNET_MessageHeader *msg)
327 struct GNUNET_ARM_Handle *h = cls;
328 const struct GNUNET_ARM_Message *arm_msg;
329 const struct GNUNET_ARM_ResultMessage *res;
330 const struct GNUNET_ARM_ListResultMessage *lres;
331 struct ARMControlMessage *cm;
335 enum GNUNET_ARM_Result result;
345 LOG (GNUNET_ERROR_TYPE_INFO,
346 _("Client was disconnected from arm service, trying to reconnect.\n"));
347 reconnect_arm_later (h);
350 msize = ntohs (msg->size);
351 LOG (GNUNET_ERROR_TYPE_DEBUG,
352 "Processing message of type %u and size %u from arm service\n",
353 ntohs (msg->type), msize);
354 if (msize < sizeof (struct GNUNET_ARM_Message))
357 reconnect_arm_later (h);
360 arm_msg = (const struct GNUNET_ARM_Message *) msg;
361 GNUNET_break (0 == ntohl (arm_msg->reserved));
362 id = GNUNET_ntohll (arm_msg->request_id);
363 cm = find_cm_by_id (h, id);
366 LOG (GNUNET_ERROR_TYPE_DEBUG,
367 "Message with unknown id %llu\n",
372 switch (ntohs (msg->type))
374 case GNUNET_MESSAGE_TYPE_ARM_RESULT:
375 if (msize < sizeof (struct GNUNET_ARM_ResultMessage))
381 case GNUNET_MESSAGE_TYPE_ARM_LIST_RESULT:
382 if (msize < sizeof (struct GNUNET_ARM_ListResultMessage))
389 lres = (const struct GNUNET_ARM_ListResultMessage *) msg;
390 rcount = ntohs (lres->count);
394 list = GNUNET_malloc (sizeof (const char *) * rcount);
395 pos = (const char *)&lres[1];
396 for (i = 0; i < rcount; i++)
398 const char *end = memchr (pos, 0, msize - size_check);
406 size_check += (end - pos) + 1;
409 if (GNUNET_YES == fail)
420 GNUNET_assert (NULL != cm->timeout_task_id);
421 GNUNET_SCHEDULER_cancel (cm->timeout_task_id);
422 GNUNET_CONTAINER_DLL_remove (h->control_sent_head,
423 h->control_sent_tail,
425 if (GNUNET_YES == fail)
427 reconnect_arm_later (h);
428 GNUNET_free (cm->msg);
432 if ( (GNUNET_MESSAGE_TYPE_ARM_RESULT == ntohs (msg->type)) &&
433 (0 == strcasecmp ((const char *) &cm->msg[1],
435 (NULL != (res = (const struct GNUNET_ARM_ResultMessage *) msg)) &&
436 (GNUNET_ARM_RESULT_STOPPING == ntohl (res->result)) )
438 /* special case: if we are stopping 'gnunet-service-arm', we do not just
439 wait for the result message, but also wait for the service to close
440 the connection (and then we have to close our client handle as well);
441 this is done by installing a different receive handler, waiting for
442 the connection to go down */
446 cm->result_cont (h->thm->cont_cls,
447 GNUNET_ARM_REQUEST_SENT_OK,
448 (const char *) &h->thm->msg[1],
449 GNUNET_ARM_RESULT_IS_NOT_KNOWN);
450 GNUNET_free (h->thm->msg);
451 GNUNET_free (h->thm);
454 GNUNET_CLIENT_receive (h->client,
455 &arm_termination_handler,
457 GNUNET_TIME_UNIT_FOREVER_REL);
460 GNUNET_CLIENT_receive (h->client,
461 &client_notify_handler,
463 GNUNET_TIME_UNIT_FOREVER_REL);
464 switch (ntohs (msg->type))
466 case GNUNET_MESSAGE_TYPE_ARM_RESULT:
467 res = (const struct GNUNET_ARM_ResultMessage *) msg;
468 LOG (GNUNET_ERROR_TYPE_DEBUG,
469 "Received response from ARM for service `%s': %u\n",
470 (const char *) &cm->msg[1], ntohs (msg->type));
471 result = (enum GNUNET_ARM_Result) ntohl (res->result);
472 if (NULL != cm->result_cont)
473 cm->result_cont (cm->cont_cls,
474 GNUNET_ARM_REQUEST_SENT_OK,
475 (const char *) &cm->msg[1],
478 case GNUNET_MESSAGE_TYPE_ARM_LIST_RESULT:
479 if (NULL != cm->list_cont)
480 cm->list_cont (cm->cont_cls,
481 GNUNET_ARM_REQUEST_SENT_OK,
484 GNUNET_free_non_null (list);
487 GNUNET_free (cm->msg);
493 * Transmit the next message to the arm service.
495 * @param cls closure with the `struct GNUNET_ARM_Handle`
496 * @param size number of bytes available in @a buf
497 * @param buf where the callee should write the message
498 * @return number of bytes written to @a buf
501 transmit_arm_message (void *cls,
505 struct GNUNET_ARM_Handle *h = cls;
506 struct ARMControlMessage *cm;
507 struct GNUNET_ARM_Message *arm_msg;
509 int notify_connection;
512 notify_connection = GNUNET_NO;
513 LOG (GNUNET_ERROR_TYPE_DEBUG,
514 "transmit_arm_message is running with %p buffer of size %lu. ARM is known to be %s\n",
515 buf, size, h->currently_down ? "unconnected" : "connected");
516 GNUNET_assert (NULL == h->reconnect_task);
518 if ((GNUNET_YES == h->currently_down) && (NULL != buf))
520 h->currently_down = GNUNET_NO;
521 notify_connection = GNUNET_YES;
522 h->retry_backoff = GNUNET_TIME_UNIT_MILLISECONDS;
523 GNUNET_CLIENT_receive (h->client, &client_notify_handler, h,
524 GNUNET_TIME_UNIT_FOREVER_REL);
528 LOG (GNUNET_ERROR_TYPE_DEBUG,
529 "Transmission failed, initiating reconnect\n");
530 reconnect_arm_later (h);
533 if (NULL == (cm = h->control_pending_head))
535 LOG (GNUNET_ERROR_TYPE_DEBUG,
536 "Queue is empty, not sending anything\n");
540 GNUNET_assert (NULL != cm->msg);
541 msize = ntohs (cm->msg->header.size);
544 LOG (GNUNET_ERROR_TYPE_DEBUG,
545 "Request is too big (%u < %u), not sending it\n", size, msize);
546 trigger_next_request (h, GNUNET_NO);
551 if (0 == h->request_id_counter)
552 h->request_id_counter++;
553 request_id = h->request_id_counter++;
554 LOG (GNUNET_ERROR_TYPE_DEBUG,
555 "Transmitting control message with %u bytes of type %u to arm with id %llu\n",
556 (unsigned int) msize,
557 (unsigned int) ntohs (cm->msg->header.type),
559 arm_msg->reserved = htonl (0);
560 arm_msg->request_id = GNUNET_htonll (request_id);
561 memcpy (buf, cm->msg, msize);
562 /* Otherwise we won't be able to find it later! */
563 arm_msg->request_id = request_id;
564 GNUNET_CONTAINER_DLL_remove (h->control_pending_head,
565 h->control_pending_tail,
567 GNUNET_CONTAINER_DLL_insert_tail (h->control_sent_head,
568 h->control_sent_tail,
570 /* Don't free msg, keep it around (kind of wasteful, but then we don't
571 * really have many messages to handle, and it'll be freed when it times
574 trigger_next_request (h, GNUNET_NO);
577 if ((GNUNET_YES == notify_connection) && (NULL != h->conn_status))
578 h->conn_status (h->conn_status_cls, GNUNET_YES);
584 * Check the list of pending requests, send the next
587 * @param h arm handle
588 * @param ignore_currently_down transmit message even if not initialized?
591 trigger_next_request (struct GNUNET_ARM_Handle *h,
592 int ignore_currently_down)
596 msize = sizeof (struct GNUNET_MessageHeader);
597 if ((GNUNET_YES == h->currently_down) && (ignore_currently_down == GNUNET_NO))
599 LOG (GNUNET_ERROR_TYPE_DEBUG,
600 "ARM connection down, not processing queue\n");
605 LOG (GNUNET_ERROR_TYPE_DEBUG,
606 "Request pending, not processing queue\n");
609 if (NULL != h->control_pending_head)
611 ntohs (h->control_pending_head->msg->header.size);
612 else if (GNUNET_NO == ignore_currently_down)
614 LOG (GNUNET_ERROR_TYPE_DEBUG,
615 "Request queue empty, not processing queue\n");
616 return; /* no pending message */
619 GNUNET_CLIENT_notify_transmit_ready (h->client,
621 GNUNET_TIME_UNIT_FOREVER_REL,
623 &transmit_arm_message, h);
630 * @param h arm handle
631 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
634 reconnect_arm (struct GNUNET_ARM_Handle *h)
636 GNUNET_assert (NULL == h->client);
637 GNUNET_assert (GNUNET_YES == h->currently_down);
638 h->client = GNUNET_CLIENT_connect ("arm", h->cfg);
639 if (NULL == h->client)
641 LOG (GNUNET_ERROR_TYPE_DEBUG,
642 "arm_api, GNUNET_CLIENT_connect returned NULL\n");
643 if (NULL != h->conn_status)
644 h->conn_status (h->conn_status_cls,
646 return GNUNET_SYSERR;
648 LOG (GNUNET_ERROR_TYPE_DEBUG,
649 "arm_api, GNUNET_CLIENT_connect returned non-NULL\n");
650 trigger_next_request (h,
657 * Set up a context for communicating with ARM, then
658 * start connecting to the ARM service using that context.
660 * @param cfg configuration to use (needed to contact ARM;
661 * the ARM service may internally use a different
662 * configuration to determine how to start the service).
663 * @param conn_status will be called when connecting/disconnecting
664 * @param cls closure for conn_status
665 * @return context to use for further ARM operations, NULL on error.
667 struct GNUNET_ARM_Handle *
668 GNUNET_ARM_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
669 GNUNET_ARM_ConnectionStatusCallback conn_status,
672 struct GNUNET_ARM_Handle *h;
674 h = GNUNET_new (struct GNUNET_ARM_Handle);
675 h->cfg = GNUNET_CONFIGURATION_dup (cfg);
676 h->currently_down = GNUNET_YES;
677 h->reconnect_task = NULL;
678 h->conn_status = conn_status;
679 h->conn_status_cls = cls;
680 if (GNUNET_OK != reconnect_arm (h))
690 * Disconnect from the ARM service (if connected) and destroy the context.
692 * @param h the handle that was being used
695 GNUNET_ARM_disconnect_and_free (struct GNUNET_ARM_Handle *h)
697 struct ARMControlMessage *cm;
699 LOG (GNUNET_ERROR_TYPE_DEBUG,
700 "Disconnecting from ARM service\n");
703 GNUNET_CLIENT_notify_transmit_ready_cancel (h->cth);
706 while ((NULL != (cm = h->control_pending_head))
707 || (NULL != (cm = h->control_sent_head)) )
709 if (NULL != h->control_pending_head)
710 GNUNET_CONTAINER_DLL_remove (h->control_pending_head,
711 h->control_pending_tail,
714 GNUNET_CONTAINER_DLL_remove (h->control_sent_head,
715 h->control_sent_tail,
717 GNUNET_assert (NULL != cm->timeout_task_id);
718 GNUNET_SCHEDULER_cancel (cm->timeout_task_id);
719 if (NULL != cm->result_cont)
720 cm->result_cont (cm->cont_cls,
721 GNUNET_ARM_REQUEST_DISCONNECTED,
724 /* FIXME: What about list callback? */
725 GNUNET_free_non_null (cm->msg);
728 if (NULL != h->client)
730 GNUNET_CLIENT_disconnect (h->client);
733 if (NULL != h->reconnect_task)
735 GNUNET_SCHEDULER_cancel (h->reconnect_task);
736 h->reconnect_task = NULL;
738 if (GNUNET_NO == h->service_test_is_active)
740 GNUNET_CONFIGURATION_destroy (h->cfg);
747 * Message timed out. Remove it from the queue.
749 * @param cls the message (struct ARMControlMessage *)
752 control_message_timeout (void *cls)
754 struct ARMControlMessage *cm = cls;
755 struct GNUNET_ARM_Message *arm_msg;
757 LOG (GNUNET_ERROR_TYPE_DEBUG,
758 "Control message timed out\n");
760 if ((NULL == arm_msg) || (0 == arm_msg->request_id))
762 GNUNET_CONTAINER_DLL_remove (cm->h->control_pending_head,
763 cm->h->control_pending_tail,
768 GNUNET_CONTAINER_DLL_remove (cm->h->control_sent_head,
769 cm->h->control_sent_tail,
772 if (NULL != cm->result_cont)
773 cm->result_cont (cm->cont_cls,
774 GNUNET_ARM_REQUEST_TIMEOUT,
776 else if (NULL != cm->list_cont)
777 cm->list_cont (cm->cont_cls,
778 GNUNET_ARM_REQUEST_TIMEOUT,
780 GNUNET_free_non_null (cm->msg);
786 * A client specifically requested starting of ARM itself.
787 * This function is called with information about whether
788 * or not ARM is running; if it is, report success. If
789 * it is not, start the ARM process.
791 * @param cls the context for the request that we will report on (struct ARMControlMessage *)
792 * @param result #GNUNET_YES if ARM is running
795 arm_service_report (void *cls,
798 struct ARMControlMessage *cm = cls;
799 struct GNUNET_ARM_Handle *h;
800 struct GNUNET_OS_Process *proc;
801 unsigned char test_is_active;
809 test_is_active = cm->h->service_test_is_active;
810 if ((GNUNET_YES == test_is_active) &&
811 (GNUNET_YES == result))
813 LOG (GNUNET_ERROR_TYPE_DEBUG,
814 "Looks like `%s' is already running.\n",
815 "gnunet-service-arm");
816 /* arm is running! */
818 cm->result_cont (cm->cont_cls,
819 GNUNET_ARM_REQUEST_SENT_OK, "arm",
820 GNUNET_ARM_RESULT_IS_STARTED_ALREADY);
822 if (GNUNET_NO == test_is_active)
824 /* User disconnected & destroyed ARM handle in the middle of
825 * the service test, so we kept the handle around until now.
827 GNUNET_CONFIGURATION_destroy (cm->h->cfg);
830 if ((GNUNET_YES == result) ||
831 (GNUNET_NO == test_is_active))
836 cm->h->service_test_is_active = GNUNET_NO;
837 LOG (GNUNET_ERROR_TYPE_DEBUG,
838 "Looks like `%s' is not running, will start it.\n",
839 "gnunet-service-arm");
841 GNUNET_CONFIGURATION_get_value_string (cm->h->cfg,
845 loprefix = GNUNET_strdup ("");
847 GNUNET_CONFIGURATION_get_value_string (cm->h->cfg,
851 lopostfix = GNUNET_strdup ("");
853 GNUNET_CONFIGURATION_get_value_string (cm->h->cfg,
858 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_WARNING,
862 cm->result_cont (cm->cont_cls,
863 GNUNET_ARM_REQUEST_SENT_OK, "arm",
864 GNUNET_ARM_RESULT_IS_NOT_KNOWN);
866 GNUNET_free (loprefix);
867 GNUNET_free (lopostfix);
871 GNUNET_CONFIGURATION_get_value_filename (cm->h->cfg,
875 binary = GNUNET_OS_get_libexec_binary_path (cbinary);
876 GNUNET_asprintf ("edbinary,
879 GNUNET_free (cbinary);
881 GNUNET_CONFIGURATION_have_value (cm->h->cfg,
885 GNUNET_CONFIGURATION_get_value_yesno (cm->h->cfg,
889 GNUNET_CONFIGURATION_have_value (cm->h->cfg,
893 /* Means we are ONLY running locally */
894 /* we're clearly running a test, don't daemonize */
896 proc = GNUNET_OS_start_process_s (GNUNET_NO,
901 /* no daemonization! */
904 proc = GNUNET_OS_start_process_s (GNUNET_NO,
910 /* no daemonization! */
916 proc = GNUNET_OS_start_process_s (GNUNET_NO,
921 "-d", lopostfix, NULL);
923 proc = GNUNET_OS_start_process_s (GNUNET_NO,
932 GNUNET_free (binary);
933 GNUNET_free (quotedbinary);
934 GNUNET_free_non_null (config);
935 GNUNET_free (loprefix);
936 GNUNET_free (lopostfix);
940 cm->result_cont (cm->cont_cls,
941 GNUNET_ARM_REQUEST_SENT_OK, "arm",
942 GNUNET_ARM_RESULT_START_FAILED);
947 cm->result_cont (cm->cont_cls,
948 GNUNET_ARM_REQUEST_SENT_OK, "arm",
949 GNUNET_ARM_RESULT_STARTING);
950 GNUNET_OS_process_destroy (proc);
958 * Start or stop a service.
960 * @param h handle to ARM
961 * @param service_name name of the service
962 * @param timeout how long to wait before failing for good
963 * @param cb callback to invoke when service is ready
964 * @param cb_cls closure for @a cb
965 * @param type type of the request
968 change_service (struct GNUNET_ARM_Handle *h,
969 const char *service_name,
970 struct GNUNET_TIME_Relative timeout,
971 GNUNET_ARM_ResultCallback cb,
975 struct ARMControlMessage *cm;
977 struct GNUNET_ARM_Message *msg;
979 slen = strlen (service_name) + 1;
980 if (slen + sizeof (struct GNUNET_ARM_Message) >=
981 GNUNET_SERVER_MAX_MESSAGE_SIZE)
985 cb (cb_cls, GNUNET_ARM_REQUEST_TOO_LONG, NULL, 0);
988 LOG (GNUNET_ERROR_TYPE_DEBUG, "Requesting %s of service `%s'.\n",
989 (GNUNET_MESSAGE_TYPE_ARM_START == type) ? "start" : "termination",
991 cm = GNUNET_malloc (sizeof (struct ARMControlMessage) + slen);
993 cm->result_cont = cb;
994 cm->cont_cls = cb_cls;
995 cm->timeout = GNUNET_TIME_relative_to_absolute (timeout);
996 memcpy (&cm[1], service_name, slen);
997 msg = GNUNET_malloc (sizeof (struct GNUNET_ARM_Message) + slen);
998 msg->header.size = htons (sizeof (struct GNUNET_ARM_Message) + slen);
999 msg->header.type = htons (type);
1000 msg->reserved = htonl (0);
1001 memcpy (&msg[1], service_name, slen);
1003 LOG (GNUNET_ERROR_TYPE_DEBUG,
1004 "Inserting a control message into the queue. Timeout is %s\n",
1005 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_remaining (cm->timeout),
1007 GNUNET_CONTAINER_DLL_insert_tail (h->control_pending_head,
1008 h->control_pending_tail,
1010 cm->timeout_task_id =
1011 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_absolute_get_remaining
1013 &control_message_timeout,
1015 trigger_next_request (h, GNUNET_NO);
1020 * Request for a service to be started.
1022 * @param h handle to ARM
1023 * @param service_name name of the service
1024 * @param std_inheritance inheritance of std streams
1025 * @param timeout how long to wait before failing for good
1026 * @param cont callback to invoke after request is sent or not sent
1027 * @param cont_cls closure for @a cont
1030 GNUNET_ARM_request_service_start (struct GNUNET_ARM_Handle *h,
1031 const char *service_name,
1032 enum GNUNET_OS_InheritStdioFlags std_inheritance,
1033 struct GNUNET_TIME_Relative timeout,
1034 GNUNET_ARM_ResultCallback cont,
1037 struct ARMControlMessage *cm;
1040 LOG (GNUNET_ERROR_TYPE_DEBUG,
1041 "Asked to start service `%s' within %s\n", service_name,
1042 GNUNET_STRINGS_relative_time_to_string (timeout, GNUNET_NO));
1043 if (0 == strcasecmp ("arm", service_name))
1046 * 1) We're connected to ARM already. Invoke the callback immediately.
1047 * 2) We're not connected to ARM.
1048 * Cancel any reconnection attempts temporarily, then perform
1051 if (GNUNET_NO == h->currently_down)
1053 LOG (GNUNET_ERROR_TYPE_DEBUG,
1054 "ARM is already running\n");
1057 GNUNET_ARM_REQUEST_SENT_OK,
1059 GNUNET_ARM_RESULT_IS_STARTED_ALREADY);
1061 else if (GNUNET_NO == h->service_test_is_active)
1065 GNUNET_CLIENT_notify_transmit_ready_cancel (h->cth);
1068 if (NULL != h->client)
1070 GNUNET_CLIENT_disconnect (h->client);
1073 if (NULL != h->reconnect_task)
1075 GNUNET_SCHEDULER_cancel (h->reconnect_task);
1076 h->reconnect_task = NULL;
1079 LOG (GNUNET_ERROR_TYPE_DEBUG,
1080 "Not connected to ARM, will do a service test\n");
1082 slen = strlen ("arm") + 1;
1083 cm = GNUNET_malloc (sizeof (struct ARMControlMessage) + slen);
1085 cm->result_cont = cont;
1086 cm->cont_cls = cont_cls;
1087 cm->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1088 cm->std_inheritance = std_inheritance;
1089 memcpy (&cm[1], service_name, slen);
1090 h->service_test_is_active = GNUNET_YES;
1091 GNUNET_CLIENT_service_test ("arm",
1094 &arm_service_report,
1099 /* Service test is already running - tell user to chill out and try
1102 LOG (GNUNET_ERROR_TYPE_DEBUG,
1103 "Service test is already in progress, we're busy\n");
1106 GNUNET_ARM_REQUEST_BUSY,
1115 GNUNET_MESSAGE_TYPE_ARM_START);
1120 * Request a service to be stopped.
1121 * Stopping arm itself will not invalidate its handle, and
1122 * ARM API will try to restore connection to the ARM service,
1123 * even if ARM connection was lost because you asked for ARM to be stopped.
1124 * Call #GNUNET_ARM_disconnect_and_free() to free the handle and prevent
1125 * further connection attempts.
1127 * @param h handle to ARM
1128 * @param service_name name of the service
1129 * @param timeout how long to wait before failing for good
1130 * @param cont callback to invoke after request is sent or is not sent
1131 * @param cont_cls closure for @a cont
1134 GNUNET_ARM_request_service_stop (struct GNUNET_ARM_Handle *h,
1135 const char *service_name,
1136 struct GNUNET_TIME_Relative timeout,
1137 GNUNET_ARM_ResultCallback cont,
1140 LOG (GNUNET_ERROR_TYPE_DEBUG,
1141 "Stopping service `%s' within %s\n",
1143 GNUNET_STRINGS_relative_time_to_string (timeout,
1150 GNUNET_MESSAGE_TYPE_ARM_STOP);
1155 * Request a list of running services.
1157 * @param h handle to ARM
1158 * @param timeout how long to wait before failing for good
1159 * @param cont callback to invoke after request is sent or is not sent
1160 * @param cont_cls closure for @a cont
1163 GNUNET_ARM_request_service_list (struct GNUNET_ARM_Handle *h,
1164 struct GNUNET_TIME_Relative timeout,
1165 GNUNET_ARM_ServiceListCallback cont,
1168 struct ARMControlMessage *cm;
1169 struct GNUNET_ARM_Message *msg;
1171 LOG (GNUNET_ERROR_TYPE_DEBUG,
1172 "Requesting LIST from ARM service with timeout: %s\n",
1173 GNUNET_STRINGS_relative_time_to_string (timeout,
1175 cm = GNUNET_new (struct ARMControlMessage);
1177 cm->list_cont = cont;
1178 cm->cont_cls = cont_cls;
1179 cm->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1180 msg = GNUNET_malloc (sizeof (struct GNUNET_ARM_Message));
1181 msg->header.size = htons (sizeof (struct GNUNET_ARM_Message));
1182 msg->header.type = htons (GNUNET_MESSAGE_TYPE_ARM_LIST);
1183 msg->reserved = htonl (0);
1185 GNUNET_CONTAINER_DLL_insert_tail (h->control_pending_head,
1186 h->control_pending_tail,
1188 cm->timeout_task_id =
1189 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_absolute_get_remaining
1191 &control_message_timeout,
1193 trigger_next_request (h,
1198 /* end of arm_api.c */