2 This file is part of GNUnet.
3 (C) 2009, 2010, 2012, 2013 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 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., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
23 * @brief API for accessing the ARM service
24 * @author Christian Grothoff, LRN
27 #include "gnunet_arm_service.h"
28 #include "gnunet_util_lib.h"
29 #include "gnunet_protocols.h"
32 #define LOG(kind,...) GNUNET_log_from (kind, "arm-api",__VA_ARGS__)
35 * Handle for interacting with ARM.
37 struct GNUNET_ARM_Handle
41 * Our control connection to the ARM service.
43 struct GNUNET_CLIENT_Connection *client;
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 * ID of the reconnect task (if any).
78 GNUNET_SCHEDULER_TaskIdentifier reconnect_task;
81 * Current delay we use for re-trying to connect to core.
83 struct GNUNET_TIME_Relative retry_backoff;
86 * Are we currently disconnected and hence unable to send?
88 unsigned char currently_down;
91 * Callback to invoke on connection/disconnection.
93 GNUNET_ARM_ConnectionStatusCallback conn_status;
96 * Closure for conn_status.
98 void *conn_status_cls;
101 * GNUNET_YES if we're running a service test.
103 unsigned char service_test_is_active;
106 * Counter for request identifiers
108 uint64_t request_id_counter;
113 * Entry in a doubly-linked list of control messages to be transmitted
114 * to the arm service.
116 * The actual message is allocated at the end of this struct.
118 struct ARMControlMessage
121 * This is a doubly-linked list.
123 struct ARMControlMessage *next;
126 * This is a doubly-linked list.
128 struct ARMControlMessage *prev;
131 * Callback for service state change requests.
133 GNUNET_ARM_ResultCallback result_cont;
136 * Callback for service list requests.
138 GNUNET_ARM_ServiceListCallback list_cont;
141 * Closure for 'result_cont' or 'list_cont'.
146 * Timeout for the operation.
148 struct GNUNET_TIME_Absolute timeout;
151 * Type of the request expressed as a message type (start, stop or list).
156 * Flags for passing std descriptors to ARM (when starting ARM).
158 enum GNUNET_OS_InheritStdioFlags std_inheritance;
163 struct GNUNET_ARM_Handle *h;
168 struct GNUNET_ARM_Message *msg;
171 * Task to run when request times out.
173 GNUNET_SCHEDULER_TaskIdentifier timeout_task_id;
177 client_notify_handler (void *cls, const struct GNUNET_MessageHeader *msg);
180 reconnect_arm (struct GNUNET_ARM_Handle *h);
183 trigger_next_request (struct GNUNET_ARM_Handle *h, int ignore_currently_down);
187 * Task scheduled to try to re-connect to arm.
189 * @param cls the 'struct GNUNET_ARM_Handle'
190 * @param tc task context
193 reconnect_arm_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
195 struct GNUNET_ARM_Handle *h = cls;
197 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
198 LOG (GNUNET_ERROR_TYPE_DEBUG, "Connecting to ARM service after delay\n");
204 clear_pending_messages (struct GNUNET_ARM_Handle *h, enum GNUNET_ARM_RequestStatus result)
206 struct ARMControlMessage *cm;
208 LOG (GNUNET_ERROR_TYPE_DEBUG,
209 "Clearing pending messages\n");
211 while (NULL != (cm = h->control_pending_head))
213 GNUNET_CONTAINER_DLL_remove (h->control_pending_head,
214 h->control_pending_tail, cm);
215 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != cm->timeout_task_id);
216 GNUNET_SCHEDULER_cancel (cm->timeout_task_id);
217 if (NULL != cm->result_cont)
218 cm->result_cont (cm->cont_cls, cm->h, result, NULL, 0);
219 GNUNET_free_non_null (cm->msg);
225 * Close down any existing connection to the ARM service and
226 * try re-establishing it later.
228 * @param h our handle
231 reconnect_arm_later (struct GNUNET_ARM_Handle *h)
233 if (GNUNET_NO != h->currently_down)
238 GNUNET_CLIENT_notify_transmit_ready_cancel (h->cth);
242 if (NULL != h->client)
244 GNUNET_CLIENT_disconnect (h->client);
248 if (NULL != h->conn_status)
249 h->conn_status (h->conn_status_cls, h, GNUNET_NO, GNUNET_NO);
251 h->currently_down = GNUNET_YES;
253 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == h->reconnect_task);
255 GNUNET_SCHEDULER_add_delayed (h->retry_backoff, &reconnect_arm_task, h);
256 /* Don't clear pending messages on disconnection, deliver them later
257 clear_pending_messages (h, GNUNET_ARM_REQUEST_DISCONNECTED);
258 GNUNET_assert (NULL == h->control_pending_head);
260 h->retry_backoff = GNUNET_TIME_STD_BACKOFF (h->retry_backoff);
264 * Transmit the next message to the arm service.
266 * @param cls closure with the 'struct GNUNET_ARM_Handle'
267 * @param size number of bytes available in buf
268 * @param buf where the callee should write the message
269 * @return number of bytes written to buf
272 transmit_arm_message (void *cls, size_t size, void *buf)
274 struct GNUNET_ARM_Handle *h = cls;
275 struct ARMControlMessage *cm;
276 struct GNUNET_ARM_Message *arm_msg;
280 LOG (GNUNET_ERROR_TYPE_DEBUG,
281 "transmit_arm_message is running with %p buffer of size %lu. ARM is known to be %s\n",
282 buf, size, h->currently_down ? "unconnected" : "connected");
283 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == h->reconnect_task);
285 if ((GNUNET_YES == h->currently_down) && (NULL != buf))
287 h->currently_down = GNUNET_NO;
288 if (NULL != h->conn_status)
289 h->conn_status (h->conn_status_cls, h, GNUNET_YES, GNUNET_NO);
290 h->retry_backoff = GNUNET_TIME_UNIT_MILLISECONDS;
291 GNUNET_CLIENT_receive (h->client, &client_notify_handler, h,
292 GNUNET_TIME_UNIT_FOREVER_REL);
296 LOG (GNUNET_ERROR_TYPE_DEBUG,
297 "Transmission failed, initiating reconnect\n");
298 reconnect_arm_later (h);
301 if (NULL == (cm = h->control_pending_head))
303 LOG (GNUNET_ERROR_TYPE_DEBUG, "Queue is empty, not sending anything\n");
307 GNUNET_assert (NULL != cm->msg);
308 msize = ntohs (cm->msg->header.size);
311 LOG (GNUNET_ERROR_TYPE_DEBUG,
312 "Request is too big (%u < %u), not sending it\n", size, msize);
313 trigger_next_request (h, GNUNET_NO);
317 if (0 == h->request_id_counter)
318 h->request_id_counter++;
319 request_id = h->request_id_counter++;
320 LOG (GNUNET_ERROR_TYPE_DEBUG,
321 "Transmitting control message with %u bytes of type %u to arm with id %llu\n",
322 (unsigned int) msize, (unsigned int) ntohs (cm->msg->header.type), request_id);
323 arm_msg->request_id = GNUNET_htonll (request_id);
324 memcpy (buf, cm->msg, msize);
325 /* Otherwise we won't be able to find it later! */
326 arm_msg->request_id = request_id;
328 GNUNET_CONTAINER_DLL_remove (h->control_pending_head,
329 h->control_pending_tail, cm);
330 GNUNET_CONTAINER_DLL_insert_tail (h->control_sent_head,
331 h->control_sent_tail, cm);
333 /* Don't free msg, keep it around (kind of wasteful, but then we don't
334 * really have many messages to handle, and it'll be freed when it times
337 trigger_next_request (h, GNUNET_NO);
343 * Check the list of pending requests, send the next
346 * @param h arm handle
347 * @param ignore_currently_down transmit message even if not initialized?
350 trigger_next_request (struct GNUNET_ARM_Handle *h, int ignore_currently_down)
354 if ((GNUNET_YES == h->currently_down) && (ignore_currently_down == GNUNET_NO))
356 LOG (GNUNET_ERROR_TYPE_DEBUG,
357 "ARM connection down, not processing queue\n");
362 LOG (GNUNET_ERROR_TYPE_DEBUG, "Request pending, not processing queue\n");
365 if (NULL != h->control_pending_head)
367 ntohs (((struct GNUNET_MessageHeader *) &h->
368 control_pending_head[1])->size);
369 else if (GNUNET_NO == ignore_currently_down)
371 LOG (GNUNET_ERROR_TYPE_DEBUG,
372 "Request queue empty, not processing queue\n");
373 return; /* no pending message */
376 GNUNET_CLIENT_notify_transmit_ready (h->client, msize,
377 GNUNET_TIME_UNIT_FOREVER_REL,
378 GNUNET_NO, &transmit_arm_message, h);
385 * @param h arm handle
388 reconnect_arm (struct GNUNET_ARM_Handle *h)
390 GNUNET_assert (NULL == h->client);
391 GNUNET_assert (GNUNET_YES == h->currently_down);
392 h->client = GNUNET_CLIENT_connect ("arm", h->cfg);
393 if (NULL == h->client)
395 LOG (GNUNET_ERROR_TYPE_DEBUG,
396 "arm_api, GNUNET_CLIENT_connect returned NULL\n");
397 if (NULL != h->conn_status)
398 h->conn_status (h->conn_status_cls, h, GNUNET_NO, GNUNET_YES);
401 LOG (GNUNET_ERROR_TYPE_DEBUG,
402 "arm_api, GNUNET_CLIENT_connect returned non-NULL\n");
403 trigger_next_request (h, GNUNET_YES);
408 * Set up a context for communicating with ARM. Note that this
409 * can be done even if the ARM service is not yet running.
412 * @param cfg configuration to use (needed to contact ARM;
413 * the ARM service may internally use a different
414 * configuration to determine how to start the service).
415 * @return context to use for further ARM operations
417 struct GNUNET_ARM_Handle *
418 GNUNET_ARM_alloc (const struct GNUNET_CONFIGURATION_Handle *cfg)
420 struct GNUNET_ARM_Handle *ret;
422 ret = GNUNET_malloc (sizeof (struct GNUNET_ARM_Handle));
423 ret->cfg = GNUNET_CONFIGURATION_dup (cfg);
424 ret->currently_down = GNUNET_YES;
425 ret->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
431 * Start connecting to the ARM service using the context.
433 * @param h ARM handle
434 * @param conn_status will be called when connecting/disconnecting
435 * @param cls closure for conn_status
438 GNUNET_ARM_connect (struct GNUNET_ARM_Handle *h,
439 GNUNET_ARM_ConnectionStatusCallback conn_status, void *cls)
441 h->conn_status = conn_status;
442 h->conn_status_cls = cls;
448 * Disconnect from the ARM service (if connected) and destroy the context.
449 * Don't call inside an ARM callback!
451 * @param h the handle that was being used
454 GNUNET_ARM_disconnect (struct GNUNET_ARM_Handle *handle)
456 LOG (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting from ARM service\n");
457 if (NULL != handle->cth)
459 GNUNET_CLIENT_notify_transmit_ready_cancel (handle->cth);
462 clear_pending_messages (handle, GNUNET_ARM_REQUEST_DISCONNECTED);
463 if (NULL != handle->client)
465 GNUNET_CLIENT_disconnect (handle->client);
466 handle->client = NULL;
468 if (GNUNET_SCHEDULER_NO_TASK != handle->reconnect_task)
470 GNUNET_SCHEDULER_cancel (handle->reconnect_task);
471 handle->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
473 if (GNUNET_NO == handle->service_test_is_active)
475 GNUNET_CONFIGURATION_destroy (handle->cfg);
476 GNUNET_free (handle);
482 * Message timed out. Remove it from the queue.
484 * @param cls the message (struct ARMControlMessage *)
485 * @param tc task context
488 control_message_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
490 struct ARMControlMessage *cm = cls;
491 struct GNUNET_ARM_Message *arm_msg;
492 LOG (GNUNET_ERROR_TYPE_DEBUG,
493 "Control message timed out\n");
495 if ((NULL == arm_msg) || (0 == arm_msg->request_id))
497 GNUNET_CONTAINER_DLL_remove (cm->h->control_pending_head,
498 cm->h->control_pending_tail, cm);
502 GNUNET_CONTAINER_DLL_remove (cm->h->control_sent_head,
503 cm->h->control_sent_tail, cm);
505 if (NULL != cm->result_cont)
506 cm->result_cont (cm->cont_cls, cm->h, GNUNET_ARM_REQUEST_TIMEOUT, NULL, 0);
507 else if (NULL != cm->list_cont)
508 cm->list_cont (cm->cont_cls, cm->h, GNUNET_ARM_REQUEST_TIMEOUT, 0, NULL);
509 GNUNET_free_non_null (cm->msg);
514 #include "do_start_process.c"
518 * A client specifically requested starting of ARM itself.
519 * This function is called with information about whether
520 * or not ARM is running; if it is, report success. If
521 * it is not, start the ARM process.
523 * @param cls the context for the request that we will report on (struct ARMControlMessage *)
524 * @param tc why were we called (reason says if ARM is running)
527 arm_service_report (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
529 struct ARMControlMessage *cm = cls;
530 struct GNUNET_OS_Process *proc;
531 unsigned char test_is_active;
538 test_is_active = cm->h->service_test_is_active;
540 /* FIXME: shouldn't we check for GNUNET_SCHEDULER_REASON_SHUTDOWN ? */
541 if ((GNUNET_YES == test_is_active) &&
542 (0 != (tc->reason & GNUNET_SCHEDULER_REASON_PREREQ_DONE)))
544 LOG (GNUNET_ERROR_TYPE_DEBUG, "Looks like `%s' is already running.\n",
545 "gnunet-service-arm");
546 /* arm is running! */
548 cm->result_cont (cm->cont_cls, cm->h, GNUNET_ARM_REQUEST_SENT_OK, "arm", GNUNET_ARM_RESULT_IS_STARTED_ALREADY);
550 if (GNUNET_NO == test_is_active)
552 /* User disconnected & destroyed ARM handle in the middle of
553 * the service test, so we kept the handle around until now.
555 GNUNET_CONFIGURATION_destroy (cm->h->cfg);
558 if ((0 != (tc->reason & GNUNET_SCHEDULER_REASON_PREREQ_DONE)) ||
559 (GNUNET_NO == test_is_active))
564 cm->h->service_test_is_active = GNUNET_NO;
565 LOG (GNUNET_ERROR_TYPE_DEBUG,
566 "Looks like `%s' is not running, will start it.\n",
567 "gnunet-service-arm");
568 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (
569 cm->h->cfg, "arm", "PREFIX", &loprefix))
570 loprefix = GNUNET_strdup ("");
571 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (
572 cm->h->cfg, "arm", "OPTIONS", &lopostfix))
573 lopostfix = GNUNET_strdup ("");
574 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (
575 cm->h->cfg, "arm", "BINARY", &cbinary))
577 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_WARNING, "arm", "BINARY");
579 cm->result_cont (cm->cont_cls, cm->h, GNUNET_ARM_REQUEST_SENT_OK, "arm", GNUNET_ARM_RESULT_IS_NOT_KNOWN);
581 GNUNET_free (loprefix);
582 GNUNET_free (lopostfix);
585 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (
586 cm->h->cfg, "arm", "CONFIG", &config))
588 binary = GNUNET_OS_get_libexec_binary_path (cbinary);
589 GNUNET_free (cbinary);
590 if ((GNUNET_YES == GNUNET_CONFIGURATION_have_value (
591 cm->h->cfg, "TESTING", "WEAKRANDOM")) &&
592 (GNUNET_YES == GNUNET_CONFIGURATION_get_value_yesno (
593 cm->h->cfg, "TESTING", "WEAKRANDOM")) &&
594 (GNUNET_NO == GNUNET_CONFIGURATION_have_value (
595 cm->h->cfg, "TESTING", "HOSTFILE")))
597 /* Means we are ONLY running locally */
598 /* we're clearly running a test, don't daemonize */
600 proc = do_start_process (GNUNET_NO, cm->std_inheritance,
601 NULL, loprefix, binary,
602 /* no daemonization! */
605 proc = do_start_process (GNUNET_NO, cm->std_inheritance,
606 NULL, loprefix, binary, "-c", config,
607 /* no daemonization! */
613 proc = do_start_process (GNUNET_NO, cm->std_inheritance,
614 NULL, loprefix, binary,
615 "-d", lopostfix, NULL);
617 proc = do_start_process (GNUNET_NO, cm->std_inheritance,
618 NULL, loprefix, binary, "-c", config,
619 "-d", lopostfix, NULL);
621 GNUNET_free (binary);
622 GNUNET_free_non_null (config);
623 GNUNET_free (loprefix);
624 GNUNET_free (lopostfix);
628 cm->result_cont (cm->cont_cls, cm->h, GNUNET_ARM_REQUEST_SENT_OK, "arm",
629 GNUNET_ARM_RESULT_START_FAILED);
634 cm->result_cont (cm->cont_cls, cm->h, GNUNET_ARM_REQUEST_SENT_OK, "arm",
635 GNUNET_ARM_RESULT_STARTING);
636 GNUNET_OS_process_destroy (proc);
637 reconnect_arm (cm->h);
643 * Start or stop a service.
645 * @param h handle to ARM
646 * @param service_name name of the service
647 * @param timeout how long to wait before failing for good
648 * @param cb callback to invoke when service is ready
649 * @param cb_cls closure for callback
650 * @param type type of the request
653 change_service (struct GNUNET_ARM_Handle *h, const char *service_name,
654 struct GNUNET_TIME_Relative timeout, GNUNET_ARM_ResultCallback cb,
655 void *cb_cls, uint16_t type)
657 struct ARMControlMessage *cm;
659 struct GNUNET_ARM_Message *msg;
661 slen = strlen (service_name) + 1;
662 if (slen + sizeof (struct GNUNET_ARM_Message) >=
663 GNUNET_SERVER_MAX_MESSAGE_SIZE)
667 cb (cb_cls, h, GNUNET_ARM_REQUEST_TOO_LONG, NULL, 0);
670 LOG (GNUNET_ERROR_TYPE_DEBUG, "Requesting %s of service `%s'.\n",
671 (GNUNET_MESSAGE_TYPE_ARM_START == type) ? "start" : "termination",
673 cm = GNUNET_malloc (sizeof (struct ARMControlMessage) + slen);
675 cm->result_cont = cb;
676 cm->cont_cls = cb_cls;
677 cm->timeout = GNUNET_TIME_relative_to_absolute (timeout);
678 memcpy (&cm[1], service_name, slen);
679 msg = GNUNET_malloc (sizeof (struct GNUNET_ARM_Message) + slen);
680 msg->header.size = htons (sizeof (struct GNUNET_ARM_Message) + slen);
681 msg->header.type = htons (type);
682 memcpy (&msg[1], service_name, slen);
684 LOG (GNUNET_ERROR_TYPE_DEBUG,
685 "Inserting a control message into the queue. Timeout is %llu\n",
686 GNUNET_TIME_absolute_get_remaining (cm->timeout).rel_value);
687 GNUNET_CONTAINER_DLL_insert_tail (h->control_pending_head,
688 h->control_pending_tail, cm);
689 cm->timeout_task_id =
690 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_absolute_get_remaining
691 (cm->timeout), &control_message_timeout, cm);
692 trigger_next_request (h, GNUNET_NO);
697 * Request for a service to be started.
699 * @param h handle to ARM
700 * @param service_name name of the service
701 * @param std_inheritance inheritance of std streams
702 * @param timeout how long to wait before failing for good
703 * @param cont callback to invoke after request is sent or not sent
704 * @param cont_cls closure for callback
707 GNUNET_ARM_request_service_start (struct GNUNET_ARM_Handle *h,
708 const char *service_name, enum GNUNET_OS_InheritStdioFlags std_inheritance,
709 struct GNUNET_TIME_Relative timeout, GNUNET_ARM_ResultCallback cont,
712 struct ARMControlMessage *cm;
715 LOG (GNUNET_ERROR_TYPE_DEBUG,
716 "Asked to start service `%s' within %s\n", service_name,
717 GNUNET_STRINGS_relative_time_to_string (timeout, GNUNET_NO));
718 if (0 == strcasecmp ("arm", service_name))
721 * 1) We're connected to ARM already. Invoke the callback immediately.
722 * 2) We're not connected to ARM.
723 * Cancel any reconnection attempts temporarily, then perform
726 if (GNUNET_NO == h->currently_down)
728 LOG (GNUNET_ERROR_TYPE_DEBUG, "ARM is already running\n");
730 cont (cont_cls, h, GNUNET_ARM_REQUEST_SENT_OK, "arm", GNUNET_ARM_RESULT_IS_STARTED_ALREADY);
732 else if (GNUNET_NO == h->service_test_is_active)
736 GNUNET_CLIENT_notify_transmit_ready_cancel (h->cth);
739 if (NULL != h->client)
741 GNUNET_CLIENT_disconnect (h->client);
744 if (GNUNET_SCHEDULER_NO_TASK != h->reconnect_task)
746 GNUNET_SCHEDULER_cancel (h->reconnect_task);
747 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
750 LOG (GNUNET_ERROR_TYPE_DEBUG,
751 "Not connected to ARM, will do a service test\n");
753 slen = strlen ("arm") + 1;
754 cm = GNUNET_malloc (sizeof (struct ARMControlMessage) + slen);
756 cm->result_cont = cont;
757 cm->cont_cls = cont_cls;
758 cm->timeout = GNUNET_TIME_relative_to_absolute (timeout);
759 cm->std_inheritance = std_inheritance;
760 memcpy (&cm[1], service_name, slen);
761 h->service_test_is_active = GNUNET_YES;
762 GNUNET_CLIENT_service_test ("arm", h->cfg, timeout, &arm_service_report,
767 /* Service test is already running - tell user to chill out and try
770 LOG (GNUNET_ERROR_TYPE_DEBUG, "Service test is already in progress, we're busy\n");
772 cont (cont_cls, h, GNUNET_ARM_REQUEST_BUSY, NULL, 0);
776 change_service (h, service_name, timeout, cont, cont_cls,
777 GNUNET_MESSAGE_TYPE_ARM_START);
782 * Request a service to be stopped.
783 * Stopping arm itself will not invalidate its handle, and
784 * ARM API will try to restore connection to the ARM service,
785 * even if ARM connection was lost because you asked for ARM to be stopped.
786 * Call GNUNET_ARM_disconnect () to free the handle and prevent
787 * further connection attempts.
789 * @param h handle to ARM
790 * @param service_name name of the service
791 * @param timeout how long to wait before failing for good
792 * @param cont callback to invoke after request is sent or is not sent
793 * @param cont_cls closure for callback
796 GNUNET_ARM_request_service_stop (struct GNUNET_ARM_Handle *h,
797 const char *service_name, struct GNUNET_TIME_Relative timeout,
798 GNUNET_ARM_ResultCallback cont, void *cont_cls)
800 LOG (GNUNET_ERROR_TYPE_DEBUG,
801 "Stopping service `%s' within %s\n",
803 GNUNET_STRINGS_relative_time_to_string (timeout, GNUNET_NO));
804 change_service (h, service_name, timeout, cont, cont_cls,
805 GNUNET_MESSAGE_TYPE_ARM_STOP);
810 * Request a list of running services.
812 * @param h handle to ARM
813 * @param timeout how long to wait before failing for good
814 * @param cont callback to invoke after request is sent or is not sent
815 * @param cont_cls closure for callback
818 GNUNET_ARM_request_service_list (struct GNUNET_ARM_Handle *h,
819 struct GNUNET_TIME_Relative timeout,
820 GNUNET_ARM_ServiceListCallback cont, void *cont_cls)
822 struct ARMControlMessage *cm;
823 struct GNUNET_ARM_Message *msg;
825 LOG (GNUNET_ERROR_TYPE_DEBUG,
826 "Requesting LIST from ARM service with timeout: %s\n",
827 GNUNET_STRINGS_relative_time_to_string (timeout, GNUNET_YES));
829 cm = GNUNET_malloc (sizeof (struct ARMControlMessage));
831 cm->list_cont = cont;
832 cm->cont_cls = cont_cls;
833 cm->timeout = GNUNET_TIME_relative_to_absolute (timeout);
834 msg = GNUNET_malloc (sizeof (struct GNUNET_ARM_Message));
835 msg->header.size = htons (sizeof (struct GNUNET_ARM_Message));
836 msg->header.type = htons (GNUNET_MESSAGE_TYPE_ARM_LIST);
838 GNUNET_CONTAINER_DLL_insert_tail (h->control_pending_head,
839 h->control_pending_tail, cm);
840 cm->timeout_task_id =
841 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_absolute_get_remaining
842 (cm->timeout), &control_message_timeout, cm);
843 trigger_next_request (h, GNUNET_NO);
846 static struct ARMControlMessage *
847 find_cm_by_id (struct GNUNET_ARM_Handle *h, uint64_t id)
849 struct ARMControlMessage *result;
850 for (result = h->control_sent_head; result; result = result->next)
851 if (id == result->msg->request_id)
858 * Handler for ARM replies.
860 * @param cls our "struct GNUNET_ARM_Handle"
861 * @param msg the message received from the arm service
864 client_notify_handler (void *cls, const struct GNUNET_MessageHeader *msg)
866 struct GNUNET_ARM_Handle *h = cls;
872 const struct GNUNET_ARM_Message *arm_msg;
873 const struct GNUNET_ARM_ResultMessage *res;
874 const struct GNUNET_ARM_ListResultMessage *lres;
875 enum GNUNET_ARM_Result result;
876 struct ARMControlMessage *cm;
884 LOG (GNUNET_ERROR_TYPE_INFO,
885 _("Client was disconnected from arm service, trying to reconnect.\n"));
886 reconnect_arm_later (h);
889 msize = ntohs (msg->size);
890 LOG (GNUNET_ERROR_TYPE_DEBUG,
891 "Processing message of type %u and size %u from arm service\n",
892 ntohs (msg->type), msize);
893 if (msize < sizeof (struct GNUNET_ARM_Message))
896 reconnect_arm_later (h);
899 arm_msg = (const struct GNUNET_ARM_Message *) msg;
900 id = GNUNET_ntohll (arm_msg->request_id);
901 cm = find_cm_by_id (h, id);
904 LOG (GNUNET_ERROR_TYPE_DEBUG, "Message with unknown id %llu\n", id);
909 switch (ntohs (msg->type))
911 case GNUNET_MESSAGE_TYPE_ARM_RESULT:
912 if (msize < sizeof (struct GNUNET_ARM_ResultMessage))
918 res = (const struct GNUNET_ARM_ResultMessage *) msg;
919 LOG (GNUNET_ERROR_TYPE_DEBUG,
920 "Received response from ARM for service `%s': %u\n",
921 (const char *) &cm->msg[1], ntohs (msg->type));
922 result = (enum GNUNET_ARM_Result) ntohl (res->result);
923 if (NULL != cm->result_cont)
924 cm->result_cont (cm->cont_cls, h, GNUNET_ARM_REQUEST_SENT_OK, (const char *) &cm->msg[1], result);
926 case GNUNET_MESSAGE_TYPE_ARM_LIST_RESULT:
927 if (msize < sizeof (struct GNUNET_ARM_ListResultMessage))
936 lres = (const struct GNUNET_ARM_ListResultMessage *) msg;
937 rcount = ntohs (lres->count);
939 const char *list[rcount];
942 pos = (const char *)&lres[1];
943 for (i = 0; i < rcount; i++)
945 const char *end = memchr (pos, 0, msize - size_check);
953 size_check += (end - pos) + 1;
956 if (GNUNET_YES == fail)
958 if (NULL != cm->list_cont)
959 cm->list_cont (cm->cont_cls, h, GNUNET_ARM_REQUEST_SENT_OK, rcount, list);
968 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != cm->timeout_task_id);
969 GNUNET_SCHEDULER_cancel (cm->timeout_task_id);
970 GNUNET_CONTAINER_DLL_remove (h->control_sent_head,
971 h->control_sent_tail, cm);
972 GNUNET_free (cm->msg);
975 if (GNUNET_YES == fail)
976 reconnect_arm_later (h);
978 GNUNET_CLIENT_receive (h->client, &client_notify_handler, h,
979 GNUNET_TIME_UNIT_FOREVER_REL);
982 /* end of arm_api.c */