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);
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);
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_SYSERR);
399 return GNUNET_SYSERR;
401 LOG (GNUNET_ERROR_TYPE_DEBUG,
402 "arm_api, GNUNET_CLIENT_connect returned non-NULL\n");
403 trigger_next_request (h, GNUNET_YES);
409 * Set up a context for communicating with ARM, then
410 * start connecting to the ARM service using that context.
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 * @param conn_status will be called when connecting/disconnecting
416 * @param cls closure for conn_status
417 * @return context to use for further ARM operations, NULL on error.
419 struct GNUNET_ARM_Handle *
420 GNUNET_ARM_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
421 GNUNET_ARM_ConnectionStatusCallback conn_status, void *cls)
423 struct GNUNET_ARM_Handle *h;
425 h = GNUNET_malloc (sizeof (struct GNUNET_ARM_Handle));
426 h->cfg = GNUNET_CONFIGURATION_dup (cfg);
427 h->currently_down = GNUNET_YES;
428 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
429 h->conn_status = conn_status;
430 h->conn_status_cls = cls;
431 if (GNUNET_OK != reconnect_arm (h))
441 * Disconnect from the ARM service (if connected) and destroy the context.
442 * Don't call inside an ARM callback!
444 * @param h the handle that was being used
447 GNUNET_ARM_disconnect_and_free (struct GNUNET_ARM_Handle *handle)
449 LOG (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting from ARM service\n");
450 if (NULL != handle->cth)
452 GNUNET_CLIENT_notify_transmit_ready_cancel (handle->cth);
455 clear_pending_messages (handle, GNUNET_ARM_REQUEST_DISCONNECTED);
456 if (NULL != handle->client)
458 GNUNET_CLIENT_disconnect (handle->client);
459 handle->client = NULL;
461 if (GNUNET_SCHEDULER_NO_TASK != handle->reconnect_task)
463 GNUNET_SCHEDULER_cancel (handle->reconnect_task);
464 handle->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
466 if (GNUNET_NO == handle->service_test_is_active)
468 GNUNET_CONFIGURATION_destroy (handle->cfg);
469 GNUNET_free (handle);
475 * Message timed out. Remove it from the queue.
477 * @param cls the message (struct ARMControlMessage *)
478 * @param tc task context
481 control_message_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
483 struct ARMControlMessage *cm = cls;
484 struct GNUNET_ARM_Message *arm_msg;
485 LOG (GNUNET_ERROR_TYPE_DEBUG,
486 "Control message timed out\n");
488 if ((NULL == arm_msg) || (0 == arm_msg->request_id))
490 GNUNET_CONTAINER_DLL_remove (cm->h->control_pending_head,
491 cm->h->control_pending_tail, cm);
495 GNUNET_CONTAINER_DLL_remove (cm->h->control_sent_head,
496 cm->h->control_sent_tail, cm);
498 if (NULL != cm->result_cont)
499 cm->result_cont (cm->cont_cls, cm->h, GNUNET_ARM_REQUEST_TIMEOUT, NULL, 0);
500 else if (NULL != cm->list_cont)
501 cm->list_cont (cm->cont_cls, cm->h, GNUNET_ARM_REQUEST_TIMEOUT, 0, NULL);
502 GNUNET_free_non_null (cm->msg);
507 #include "do_start_process.c"
511 * A client specifically requested starting of ARM itself.
512 * This function is called with information about whether
513 * or not ARM is running; if it is, report success. If
514 * it is not, start the ARM process.
516 * @param cls the context for the request that we will report on (struct ARMControlMessage *)
517 * @param tc why were we called (reason says if ARM is running)
520 arm_service_report (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
522 struct ARMControlMessage *cm = cls;
523 struct GNUNET_OS_Process *proc;
524 unsigned char test_is_active;
531 test_is_active = cm->h->service_test_is_active;
533 /* FIXME: shouldn't we check for GNUNET_SCHEDULER_REASON_SHUTDOWN ? */
534 if ((GNUNET_YES == test_is_active) &&
535 (0 != (tc->reason & GNUNET_SCHEDULER_REASON_PREREQ_DONE)))
537 LOG (GNUNET_ERROR_TYPE_DEBUG, "Looks like `%s' is already running.\n",
538 "gnunet-service-arm");
539 /* arm is running! */
541 cm->result_cont (cm->cont_cls, cm->h, GNUNET_ARM_REQUEST_SENT_OK, "arm", GNUNET_ARM_RESULT_IS_STARTED_ALREADY);
543 if (GNUNET_NO == test_is_active)
545 /* User disconnected & destroyed ARM handle in the middle of
546 * the service test, so we kept the handle around until now.
548 GNUNET_CONFIGURATION_destroy (cm->h->cfg);
551 if ((0 != (tc->reason & GNUNET_SCHEDULER_REASON_PREREQ_DONE)) ||
552 (GNUNET_NO == test_is_active))
557 cm->h->service_test_is_active = GNUNET_NO;
558 LOG (GNUNET_ERROR_TYPE_DEBUG,
559 "Looks like `%s' is not running, will start it.\n",
560 "gnunet-service-arm");
561 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (
562 cm->h->cfg, "arm", "PREFIX", &loprefix))
563 loprefix = GNUNET_strdup ("");
564 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (
565 cm->h->cfg, "arm", "OPTIONS", &lopostfix))
566 lopostfix = GNUNET_strdup ("");
567 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (
568 cm->h->cfg, "arm", "BINARY", &cbinary))
570 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_WARNING, "arm", "BINARY");
572 cm->result_cont (cm->cont_cls, cm->h, GNUNET_ARM_REQUEST_SENT_OK, "arm", GNUNET_ARM_RESULT_IS_NOT_KNOWN);
574 GNUNET_free (loprefix);
575 GNUNET_free (lopostfix);
578 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (
579 cm->h->cfg, "arm", "CONFIG", &config))
581 binary = GNUNET_OS_get_libexec_binary_path (cbinary);
582 GNUNET_free (cbinary);
583 if ((GNUNET_YES == GNUNET_CONFIGURATION_have_value (
584 cm->h->cfg, "TESTING", "WEAKRANDOM")) &&
585 (GNUNET_YES == GNUNET_CONFIGURATION_get_value_yesno (
586 cm->h->cfg, "TESTING", "WEAKRANDOM")) &&
587 (GNUNET_NO == GNUNET_CONFIGURATION_have_value (
588 cm->h->cfg, "TESTING", "HOSTFILE")))
590 /* Means we are ONLY running locally */
591 /* we're clearly running a test, don't daemonize */
593 proc = do_start_process (GNUNET_NO, cm->std_inheritance,
594 NULL, loprefix, binary,
595 /* no daemonization! */
598 proc = do_start_process (GNUNET_NO, cm->std_inheritance,
599 NULL, loprefix, binary, "-c", config,
600 /* no daemonization! */
606 proc = do_start_process (GNUNET_NO, cm->std_inheritance,
607 NULL, loprefix, binary,
608 "-d", lopostfix, NULL);
610 proc = do_start_process (GNUNET_NO, cm->std_inheritance,
611 NULL, loprefix, binary, "-c", config,
612 "-d", lopostfix, NULL);
614 GNUNET_free (binary);
615 GNUNET_free_non_null (config);
616 GNUNET_free (loprefix);
617 GNUNET_free (lopostfix);
621 cm->result_cont (cm->cont_cls, cm->h, GNUNET_ARM_REQUEST_SENT_OK, "arm",
622 GNUNET_ARM_RESULT_START_FAILED);
627 cm->result_cont (cm->cont_cls, cm->h, GNUNET_ARM_REQUEST_SENT_OK, "arm",
628 GNUNET_ARM_RESULT_STARTING);
629 GNUNET_OS_process_destroy (proc);
630 reconnect_arm (cm->h);
636 * Start or stop a service.
638 * @param h handle to ARM
639 * @param service_name name of the service
640 * @param timeout how long to wait before failing for good
641 * @param cb callback to invoke when service is ready
642 * @param cb_cls closure for callback
643 * @param type type of the request
646 change_service (struct GNUNET_ARM_Handle *h, const char *service_name,
647 struct GNUNET_TIME_Relative timeout, GNUNET_ARM_ResultCallback cb,
648 void *cb_cls, uint16_t type)
650 struct ARMControlMessage *cm;
652 struct GNUNET_ARM_Message *msg;
654 slen = strlen (service_name) + 1;
655 if (slen + sizeof (struct GNUNET_ARM_Message) >=
656 GNUNET_SERVER_MAX_MESSAGE_SIZE)
660 cb (cb_cls, h, GNUNET_ARM_REQUEST_TOO_LONG, NULL, 0);
663 LOG (GNUNET_ERROR_TYPE_DEBUG, "Requesting %s of service `%s'.\n",
664 (GNUNET_MESSAGE_TYPE_ARM_START == type) ? "start" : "termination",
666 cm = GNUNET_malloc (sizeof (struct ARMControlMessage) + slen);
668 cm->result_cont = cb;
669 cm->cont_cls = cb_cls;
670 cm->timeout = GNUNET_TIME_relative_to_absolute (timeout);
671 memcpy (&cm[1], service_name, slen);
672 msg = GNUNET_malloc (sizeof (struct GNUNET_ARM_Message) + slen);
673 msg->header.size = htons (sizeof (struct GNUNET_ARM_Message) + slen);
674 msg->header.type = htons (type);
675 memcpy (&msg[1], service_name, slen);
677 LOG (GNUNET_ERROR_TYPE_DEBUG,
678 "Inserting a control message into the queue. Timeout is %llu\n",
679 GNUNET_TIME_absolute_get_remaining (cm->timeout).rel_value);
680 GNUNET_CONTAINER_DLL_insert_tail (h->control_pending_head,
681 h->control_pending_tail, cm);
682 cm->timeout_task_id =
683 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_absolute_get_remaining
684 (cm->timeout), &control_message_timeout, cm);
685 trigger_next_request (h, GNUNET_NO);
690 * Request for a service to be started.
692 * @param h handle to ARM
693 * @param service_name name of the service
694 * @param std_inheritance inheritance of std streams
695 * @param timeout how long to wait before failing for good
696 * @param cont callback to invoke after request is sent or not sent
697 * @param cont_cls closure for callback
700 GNUNET_ARM_request_service_start (struct GNUNET_ARM_Handle *h,
701 const char *service_name, enum GNUNET_OS_InheritStdioFlags std_inheritance,
702 struct GNUNET_TIME_Relative timeout, GNUNET_ARM_ResultCallback cont,
705 struct ARMControlMessage *cm;
708 LOG (GNUNET_ERROR_TYPE_DEBUG,
709 "Asked to start service `%s' within %s\n", service_name,
710 GNUNET_STRINGS_relative_time_to_string (timeout, GNUNET_NO));
711 if (0 == strcasecmp ("arm", service_name))
714 * 1) We're connected to ARM already. Invoke the callback immediately.
715 * 2) We're not connected to ARM.
716 * Cancel any reconnection attempts temporarily, then perform
719 if (GNUNET_NO == h->currently_down)
721 LOG (GNUNET_ERROR_TYPE_DEBUG, "ARM is already running\n");
723 cont (cont_cls, h, GNUNET_ARM_REQUEST_SENT_OK, "arm", GNUNET_ARM_RESULT_IS_STARTED_ALREADY);
725 else if (GNUNET_NO == h->service_test_is_active)
729 GNUNET_CLIENT_notify_transmit_ready_cancel (h->cth);
732 if (NULL != h->client)
734 GNUNET_CLIENT_disconnect (h->client);
737 if (GNUNET_SCHEDULER_NO_TASK != h->reconnect_task)
739 GNUNET_SCHEDULER_cancel (h->reconnect_task);
740 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
743 LOG (GNUNET_ERROR_TYPE_DEBUG,
744 "Not connected to ARM, will do a service test\n");
746 slen = strlen ("arm") + 1;
747 cm = GNUNET_malloc (sizeof (struct ARMControlMessage) + slen);
749 cm->result_cont = cont;
750 cm->cont_cls = cont_cls;
751 cm->timeout = GNUNET_TIME_relative_to_absolute (timeout);
752 cm->std_inheritance = std_inheritance;
753 memcpy (&cm[1], service_name, slen);
754 h->service_test_is_active = GNUNET_YES;
755 GNUNET_CLIENT_service_test ("arm", h->cfg, timeout, &arm_service_report,
760 /* Service test is already running - tell user to chill out and try
763 LOG (GNUNET_ERROR_TYPE_DEBUG, "Service test is already in progress, we're busy\n");
765 cont (cont_cls, h, GNUNET_ARM_REQUEST_BUSY, NULL, 0);
769 change_service (h, service_name, timeout, cont, cont_cls,
770 GNUNET_MESSAGE_TYPE_ARM_START);
775 * Request a service to be stopped.
776 * Stopping arm itself will not invalidate its handle, and
777 * ARM API will try to restore connection to the ARM service,
778 * even if ARM connection was lost because you asked for ARM to be stopped.
779 * Call GNUNET_ARM_disconnect_and_free () to free the handle and prevent
780 * further connection attempts.
782 * @param h handle to ARM
783 * @param service_name name of the service
784 * @param timeout how long to wait before failing for good
785 * @param cont callback to invoke after request is sent or is not sent
786 * @param cont_cls closure for callback
789 GNUNET_ARM_request_service_stop (struct GNUNET_ARM_Handle *h,
790 const char *service_name, struct GNUNET_TIME_Relative timeout,
791 GNUNET_ARM_ResultCallback cont, void *cont_cls)
793 LOG (GNUNET_ERROR_TYPE_DEBUG,
794 "Stopping service `%s' within %s\n",
796 GNUNET_STRINGS_relative_time_to_string (timeout, GNUNET_NO));
797 change_service (h, service_name, timeout, cont, cont_cls,
798 GNUNET_MESSAGE_TYPE_ARM_STOP);
803 * Request a list of running services.
805 * @param h handle to ARM
806 * @param timeout how long to wait before failing for good
807 * @param cont callback to invoke after request is sent or is not sent
808 * @param cont_cls closure for callback
811 GNUNET_ARM_request_service_list (struct GNUNET_ARM_Handle *h,
812 struct GNUNET_TIME_Relative timeout,
813 GNUNET_ARM_ServiceListCallback cont, void *cont_cls)
815 struct ARMControlMessage *cm;
816 struct GNUNET_ARM_Message *msg;
818 LOG (GNUNET_ERROR_TYPE_DEBUG,
819 "Requesting LIST from ARM service with timeout: %s\n",
820 GNUNET_STRINGS_relative_time_to_string (timeout, GNUNET_YES));
822 cm = GNUNET_malloc (sizeof (struct ARMControlMessage));
824 cm->list_cont = cont;
825 cm->cont_cls = cont_cls;
826 cm->timeout = GNUNET_TIME_relative_to_absolute (timeout);
827 msg = GNUNET_malloc (sizeof (struct GNUNET_ARM_Message));
828 msg->header.size = htons (sizeof (struct GNUNET_ARM_Message));
829 msg->header.type = htons (GNUNET_MESSAGE_TYPE_ARM_LIST);
831 GNUNET_CONTAINER_DLL_insert_tail (h->control_pending_head,
832 h->control_pending_tail, cm);
833 cm->timeout_task_id =
834 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_absolute_get_remaining
835 (cm->timeout), &control_message_timeout, cm);
836 trigger_next_request (h, GNUNET_NO);
839 static struct ARMControlMessage *
840 find_cm_by_id (struct GNUNET_ARM_Handle *h, uint64_t id)
842 struct ARMControlMessage *result;
843 for (result = h->control_sent_head; result; result = result->next)
844 if (id == result->msg->request_id)
851 * Handler for ARM replies.
853 * @param cls our "struct GNUNET_ARM_Handle"
854 * @param msg the message received from the arm service
857 client_notify_handler (void *cls, const struct GNUNET_MessageHeader *msg)
859 struct GNUNET_ARM_Handle *h = cls;
865 const struct GNUNET_ARM_Message *arm_msg;
866 const struct GNUNET_ARM_ResultMessage *res;
867 const struct GNUNET_ARM_ListResultMessage *lres;
868 enum GNUNET_ARM_Result result;
869 struct ARMControlMessage *cm;
877 LOG (GNUNET_ERROR_TYPE_INFO,
878 _("Client was disconnected from arm service, trying to reconnect.\n"));
879 reconnect_arm_later (h);
882 msize = ntohs (msg->size);
883 LOG (GNUNET_ERROR_TYPE_DEBUG,
884 "Processing message of type %u and size %u from arm service\n",
885 ntohs (msg->type), msize);
886 if (msize < sizeof (struct GNUNET_ARM_Message))
889 reconnect_arm_later (h);
892 arm_msg = (const struct GNUNET_ARM_Message *) msg;
893 id = GNUNET_ntohll (arm_msg->request_id);
894 cm = find_cm_by_id (h, id);
897 LOG (GNUNET_ERROR_TYPE_DEBUG, "Message with unknown id %llu\n", id);
902 switch (ntohs (msg->type))
904 case GNUNET_MESSAGE_TYPE_ARM_RESULT:
905 if (msize < sizeof (struct GNUNET_ARM_ResultMessage))
911 res = (const struct GNUNET_ARM_ResultMessage *) msg;
912 LOG (GNUNET_ERROR_TYPE_DEBUG,
913 "Received response from ARM for service `%s': %u\n",
914 (const char *) &cm->msg[1], ntohs (msg->type));
915 result = (enum GNUNET_ARM_Result) ntohl (res->result);
916 if (NULL != cm->result_cont)
917 cm->result_cont (cm->cont_cls, h, GNUNET_ARM_REQUEST_SENT_OK, (const char *) &cm->msg[1], result);
919 case GNUNET_MESSAGE_TYPE_ARM_LIST_RESULT:
920 if (msize < sizeof (struct GNUNET_ARM_ListResultMessage))
929 lres = (const struct GNUNET_ARM_ListResultMessage *) msg;
930 rcount = ntohs (lres->count);
932 const char *list[rcount];
935 pos = (const char *)&lres[1];
936 for (i = 0; i < rcount; i++)
938 const char *end = memchr (pos, 0, msize - size_check);
946 size_check += (end - pos) + 1;
949 if (GNUNET_YES == fail)
951 if (NULL != cm->list_cont)
952 cm->list_cont (cm->cont_cls, h, GNUNET_ARM_REQUEST_SENT_OK, rcount, list);
961 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != cm->timeout_task_id);
962 GNUNET_SCHEDULER_cancel (cm->timeout_task_id);
963 GNUNET_CONTAINER_DLL_remove (h->control_sent_head,
964 h->control_sent_tail, cm);
965 GNUNET_free (cm->msg);
968 if (GNUNET_YES == fail)
969 reconnect_arm_later (h);
971 GNUNET_CLIENT_receive (h->client, &client_notify_handler, h,
972 GNUNET_TIME_UNIT_FOREVER_REL);
975 /* end of arm_api.c */