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
40 * Our control connection to the ARM service.
42 struct GNUNET_CLIENT_Connection *client;
45 * The configuration that we are using.
47 struct GNUNET_CONFIGURATION_Handle *cfg;
50 * Handle for our current transmission request.
52 struct GNUNET_CLIENT_TransmitHandle *cth;
55 * Head of doubly-linked list of pending requests.
57 struct ARMControlMessage *control_pending_head;
60 * Tail of doubly-linked list of pending requests.
62 struct ARMControlMessage *control_pending_tail;
65 * Head of doubly-linked list of sent requests.
67 struct ARMControlMessage *control_sent_head;
70 * Tail of doubly-linked list of sent requests.
72 struct ARMControlMessage *control_sent_tail;
75 * ID of the reconnect task (if any).
77 GNUNET_SCHEDULER_TaskIdentifier reconnect_task;
80 * Current delay we use for re-trying to connect to core.
82 struct GNUNET_TIME_Relative retry_backoff;
85 * Callback to invoke on connection/disconnection.
87 GNUNET_ARM_ConnectionStatusCallback conn_status;
90 * Closure for conn_status.
92 void *conn_status_cls;
95 * Counter for request identifiers
97 uint64_t request_id_counter;
100 * Are we currently disconnected and hence unable to send?
102 unsigned char currently_down;
105 * GNUNET_YES if we're running a service test.
107 unsigned char service_test_is_active;
112 * Entry in a doubly-linked list of control messages to be transmitted
113 * to the arm service.
115 * The actual message is allocated at the end of this struct.
117 struct ARMControlMessage
120 * This is a doubly-linked list.
122 struct ARMControlMessage *next;
125 * This is a doubly-linked list.
127 struct ARMControlMessage *prev;
132 struct GNUNET_ARM_Handle *h;
137 struct GNUNET_ARM_Message *msg;
140 * Callback for service state change requests.
142 GNUNET_ARM_ResultCallback result_cont;
145 * Callback for service list requests.
147 GNUNET_ARM_ServiceListCallback list_cont;
150 * Closure for 'result_cont' or 'list_cont'.
155 * Timeout for the operation.
157 struct GNUNET_TIME_Absolute timeout;
160 * Flags for passing std descriptors to ARM (when starting ARM).
162 enum GNUNET_OS_InheritStdioFlags std_inheritance;
165 * Task to run when request times out.
167 GNUNET_SCHEDULER_TaskIdentifier timeout_task_id;
170 * Type of the request expressed as a message type (start, stop or list).
176 client_notify_handler (void *cls, const struct GNUNET_MessageHeader *msg);
179 reconnect_arm (struct GNUNET_ARM_Handle *h);
182 trigger_next_request (struct GNUNET_ARM_Handle *h, int ignore_currently_down);
186 * Task scheduled to try to re-connect to arm.
188 * @param cls the 'struct GNUNET_ARM_Handle'
189 * @param tc task context
192 reconnect_arm_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
194 struct GNUNET_ARM_Handle *h = cls;
196 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
197 LOG (GNUNET_ERROR_TYPE_DEBUG, "Connecting to ARM service after delay\n");
203 * Close down any existing connection to the ARM service and
204 * try re-establishing it later.
206 * @param h our handle
209 reconnect_arm_later (struct GNUNET_ARM_Handle *h)
211 if (GNUNET_NO != h->currently_down)
215 GNUNET_CLIENT_notify_transmit_ready_cancel (h->cth);
218 if (NULL != h->client)
220 GNUNET_CLIENT_disconnect (h->client);
223 h->currently_down = GNUNET_YES;
224 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == h->reconnect_task);
226 GNUNET_SCHEDULER_add_delayed (h->retry_backoff, &reconnect_arm_task, h);
227 /* Don't clear pending messages on disconnection, deliver them later
228 clear_pending_messages (h, GNUNET_ARM_REQUEST_DISCONNECTED);
229 GNUNET_assert (NULL == h->control_pending_head);
231 h->retry_backoff = GNUNET_TIME_STD_BACKOFF (h->retry_backoff);
232 if (NULL != h->conn_status)
233 h->conn_status (h->conn_status_cls, GNUNET_NO);
237 * Transmit the next message to the arm service.
239 * @param cls closure with the 'struct GNUNET_ARM_Handle'
240 * @param size number of bytes available in buf
241 * @param buf where the callee should write the message
242 * @return number of bytes written to buf
245 transmit_arm_message (void *cls, size_t size, void *buf)
247 struct GNUNET_ARM_Handle *h = cls;
248 struct ARMControlMessage *cm;
249 struct GNUNET_ARM_Message *arm_msg;
251 int notify_connection;
254 notify_connection = GNUNET_NO;
255 LOG (GNUNET_ERROR_TYPE_DEBUG,
256 "transmit_arm_message is running with %p buffer of size %lu. ARM is known to be %s\n",
257 buf, size, h->currently_down ? "unconnected" : "connected");
258 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == h->reconnect_task);
260 if ((GNUNET_YES == h->currently_down) && (NULL != buf))
262 h->currently_down = GNUNET_NO;
263 notify_connection = GNUNET_YES;
264 h->retry_backoff = GNUNET_TIME_UNIT_MILLISECONDS;
265 GNUNET_CLIENT_receive (h->client, &client_notify_handler, h,
266 GNUNET_TIME_UNIT_FOREVER_REL);
270 LOG (GNUNET_ERROR_TYPE_DEBUG,
271 "Transmission failed, initiating reconnect\n");
272 reconnect_arm_later (h);
275 if (NULL == (cm = h->control_pending_head))
277 LOG (GNUNET_ERROR_TYPE_DEBUG, "Queue is empty, not sending anything\n");
281 GNUNET_assert (NULL != cm->msg);
282 msize = ntohs (cm->msg->header.size);
285 LOG (GNUNET_ERROR_TYPE_DEBUG,
286 "Request is too big (%u < %u), not sending it\n", size, msize);
287 trigger_next_request (h, GNUNET_NO);
292 if (0 == h->request_id_counter)
293 h->request_id_counter++;
294 request_id = h->request_id_counter++;
295 LOG (GNUNET_ERROR_TYPE_DEBUG,
296 "Transmitting control message with %u bytes of type %u to arm with id %llu\n",
297 (unsigned int) msize, (unsigned int) ntohs (cm->msg->header.type), request_id);
298 arm_msg->request_id = GNUNET_htonll (request_id);
299 memcpy (buf, cm->msg, msize);
300 /* Otherwise we won't be able to find it later! */
301 arm_msg->request_id = request_id;
302 GNUNET_CONTAINER_DLL_remove (h->control_pending_head,
303 h->control_pending_tail, cm);
304 GNUNET_CONTAINER_DLL_insert_tail (h->control_sent_head,
305 h->control_sent_tail, cm);
306 /* Don't free msg, keep it around (kind of wasteful, but then we don't
307 * really have many messages to handle, and it'll be freed when it times
310 trigger_next_request (h, GNUNET_NO);
313 if ((GNUNET_YES == notify_connection) && (NULL != h->conn_status))
314 h->conn_status (h->conn_status_cls, GNUNET_YES);
320 * Check the list of pending requests, send the next
323 * @param h arm handle
324 * @param ignore_currently_down transmit message even if not initialized?
327 trigger_next_request (struct GNUNET_ARM_Handle *h, int ignore_currently_down)
331 msize = sizeof (struct GNUNET_MessageHeader);
332 if ((GNUNET_YES == h->currently_down) && (ignore_currently_down == GNUNET_NO))
334 LOG (GNUNET_ERROR_TYPE_DEBUG,
335 "ARM connection down, not processing queue\n");
340 LOG (GNUNET_ERROR_TYPE_DEBUG, "Request pending, not processing queue\n");
343 if (NULL != h->control_pending_head)
345 ntohs (((struct GNUNET_MessageHeader *) &h->
346 control_pending_head[1])->size);
347 else if (GNUNET_NO == ignore_currently_down)
349 LOG (GNUNET_ERROR_TYPE_DEBUG,
350 "Request queue empty, not processing queue\n");
351 return; /* no pending message */
354 GNUNET_CLIENT_notify_transmit_ready (h->client, msize,
355 GNUNET_TIME_UNIT_FOREVER_REL,
356 GNUNET_NO, &transmit_arm_message, h);
363 * @param h arm handle
366 reconnect_arm (struct GNUNET_ARM_Handle *h)
368 GNUNET_assert (NULL == h->client);
369 GNUNET_assert (GNUNET_YES == h->currently_down);
370 h->client = GNUNET_CLIENT_connect ("arm", h->cfg);
371 if (NULL == h->client)
373 LOG (GNUNET_ERROR_TYPE_DEBUG,
374 "arm_api, GNUNET_CLIENT_connect returned NULL\n");
375 if (NULL != h->conn_status)
376 h->conn_status (h->conn_status_cls, GNUNET_SYSERR);
377 return GNUNET_SYSERR;
379 LOG (GNUNET_ERROR_TYPE_DEBUG,
380 "arm_api, GNUNET_CLIENT_connect returned non-NULL\n");
381 trigger_next_request (h, GNUNET_YES);
387 * Set up a context for communicating with ARM, then
388 * start connecting to the ARM service using that context.
390 * @param cfg configuration to use (needed to contact ARM;
391 * the ARM service may internally use a different
392 * configuration to determine how to start the service).
393 * @param conn_status will be called when connecting/disconnecting
394 * @param cls closure for conn_status
395 * @return context to use for further ARM operations, NULL on error.
397 struct GNUNET_ARM_Handle *
398 GNUNET_ARM_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
399 GNUNET_ARM_ConnectionStatusCallback conn_status, void *cls)
401 struct GNUNET_ARM_Handle *h;
403 h = GNUNET_malloc (sizeof (struct GNUNET_ARM_Handle));
404 h->cfg = GNUNET_CONFIGURATION_dup (cfg);
405 h->currently_down = GNUNET_YES;
406 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
407 h->conn_status = conn_status;
408 h->conn_status_cls = cls;
409 if (GNUNET_OK != reconnect_arm (h))
419 * Disconnect from the ARM service (if connected) and destroy the context.
421 * @param h the handle that was being used
424 GNUNET_ARM_disconnect_and_free (struct GNUNET_ARM_Handle *h)
426 struct ARMControlMessage *cm;
428 LOG (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting from ARM service\n");
431 GNUNET_CLIENT_notify_transmit_ready_cancel (h->cth);
434 while ((NULL != (cm = h->control_pending_head))
435 || (NULL != (cm = h->control_sent_head)) )
437 if (NULL != h->control_pending_head)
438 GNUNET_CONTAINER_DLL_remove (h->control_pending_head,
439 h->control_pending_tail, cm);
441 GNUNET_CONTAINER_DLL_remove (h->control_sent_head,
442 h->control_sent_tail, cm);
443 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != cm->timeout_task_id);
444 GNUNET_SCHEDULER_cancel (cm->timeout_task_id);
445 if (NULL != cm->result_cont)
446 cm->result_cont (cm->cont_cls, GNUNET_ARM_REQUEST_DISCONNECTED,
448 /* FIXME: What about list callback? */
449 GNUNET_free_non_null (cm->msg);
452 if (NULL != h->client)
454 GNUNET_CLIENT_disconnect (h->client);
457 if (GNUNET_SCHEDULER_NO_TASK != h->reconnect_task)
459 GNUNET_SCHEDULER_cancel (h->reconnect_task);
460 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
462 if (GNUNET_NO == h->service_test_is_active)
464 GNUNET_CONFIGURATION_destroy (h->cfg);
471 * Message timed out. Remove it from the queue.
473 * @param cls the message (struct ARMControlMessage *)
474 * @param tc task context
477 control_message_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
479 struct ARMControlMessage *cm = cls;
480 struct GNUNET_ARM_Message *arm_msg;
481 LOG (GNUNET_ERROR_TYPE_DEBUG,
482 "Control message timed out\n");
484 if ((NULL == arm_msg) || (0 == arm_msg->request_id))
486 GNUNET_CONTAINER_DLL_remove (cm->h->control_pending_head,
487 cm->h->control_pending_tail, cm);
491 GNUNET_CONTAINER_DLL_remove (cm->h->control_sent_head,
492 cm->h->control_sent_tail, cm);
494 if (NULL != cm->result_cont)
495 cm->result_cont (cm->cont_cls, GNUNET_ARM_REQUEST_TIMEOUT, NULL, 0);
496 else if (NULL != cm->list_cont)
497 cm->list_cont (cm->cont_cls, GNUNET_ARM_REQUEST_TIMEOUT, 0, NULL);
498 GNUNET_free_non_null (cm->msg);
503 #include "do_start_process.c"
507 * A client specifically requested starting of ARM itself.
508 * This function is called with information about whether
509 * or not ARM is running; if it is, report success. If
510 * it is not, start the ARM process.
512 * @param cls the context for the request that we will report on (struct ARMControlMessage *)
513 * @param tc why were we called (reason says if ARM is running)
516 arm_service_report (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
518 struct ARMControlMessage *cm = cls;
519 struct GNUNET_ARM_Handle *h;
520 struct GNUNET_OS_Process *proc;
521 unsigned char test_is_active;
528 test_is_active = cm->h->service_test_is_active;
530 /* FIXME: shouldn't we check for GNUNET_SCHEDULER_REASON_SHUTDOWN ? */
531 if ((GNUNET_YES == test_is_active) &&
532 (0 != (tc->reason & GNUNET_SCHEDULER_REASON_PREREQ_DONE)))
534 LOG (GNUNET_ERROR_TYPE_DEBUG, "Looks like `%s' is already running.\n",
535 "gnunet-service-arm");
536 /* arm is running! */
538 cm->result_cont (cm->cont_cls,
539 GNUNET_ARM_REQUEST_SENT_OK, "arm",
540 GNUNET_ARM_RESULT_IS_STARTED_ALREADY);
542 if (GNUNET_NO == test_is_active)
544 /* User disconnected & destroyed ARM handle in the middle of
545 * the service test, so we kept the handle around until now.
547 GNUNET_CONFIGURATION_destroy (cm->h->cfg);
550 if ((0 != (tc->reason & GNUNET_SCHEDULER_REASON_PREREQ_DONE)) ||
551 (GNUNET_NO == test_is_active))
556 cm->h->service_test_is_active = GNUNET_NO;
557 LOG (GNUNET_ERROR_TYPE_DEBUG,
558 "Looks like `%s' is not running, will start it.\n",
559 "gnunet-service-arm");
560 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (
561 cm->h->cfg, "arm", "PREFIX", &loprefix))
562 loprefix = GNUNET_strdup ("");
563 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (
564 cm->h->cfg, "arm", "OPTIONS", &lopostfix))
565 lopostfix = GNUNET_strdup ("");
566 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (
567 cm->h->cfg, "arm", "BINARY", &cbinary))
569 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_WARNING, "arm", "BINARY");
571 cm->result_cont (cm->cont_cls,
572 GNUNET_ARM_REQUEST_SENT_OK, "arm",
573 GNUNET_ARM_RESULT_IS_NOT_KNOWN);
575 GNUNET_free (loprefix);
576 GNUNET_free (lopostfix);
579 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (
580 cm->h->cfg, "arm", "CONFIG", &config))
582 binary = GNUNET_OS_get_libexec_binary_path (cbinary);
583 GNUNET_free (cbinary);
584 if ((GNUNET_YES == GNUNET_CONFIGURATION_have_value (
585 cm->h->cfg, "TESTING", "WEAKRANDOM")) &&
586 (GNUNET_YES == GNUNET_CONFIGURATION_get_value_yesno (
587 cm->h->cfg, "TESTING", "WEAKRANDOM")) &&
588 (GNUNET_NO == GNUNET_CONFIGURATION_have_value (
589 cm->h->cfg, "TESTING", "HOSTFILE")))
591 /* Means we are ONLY running locally */
592 /* we're clearly running a test, don't daemonize */
594 proc = do_start_process (GNUNET_NO, cm->std_inheritance,
595 NULL, loprefix, binary,
596 /* no daemonization! */
599 proc = do_start_process (GNUNET_NO, cm->std_inheritance,
600 NULL, loprefix, binary, "-c", config,
601 /* no daemonization! */
607 proc = do_start_process (GNUNET_NO, cm->std_inheritance,
608 NULL, loprefix, binary,
609 "-d", lopostfix, NULL);
611 proc = do_start_process (GNUNET_NO, cm->std_inheritance,
612 NULL, loprefix, binary, "-c", config,
613 "-d", lopostfix, NULL);
615 GNUNET_free (binary);
616 GNUNET_free_non_null (config);
617 GNUNET_free (loprefix);
618 GNUNET_free (lopostfix);
622 cm->result_cont (cm->cont_cls, GNUNET_ARM_REQUEST_SENT_OK, "arm",
623 GNUNET_ARM_RESULT_START_FAILED);
628 cm->result_cont (cm->cont_cls, GNUNET_ARM_REQUEST_SENT_OK, "arm",
629 GNUNET_ARM_RESULT_STARTING);
630 GNUNET_OS_process_destroy (proc);
638 * Start or stop a service.
640 * @param h handle to ARM
641 * @param service_name name of the service
642 * @param timeout how long to wait before failing for good
643 * @param cb callback to invoke when service is ready
644 * @param cb_cls closure for callback
645 * @param type type of the request
648 change_service (struct GNUNET_ARM_Handle *h, const char *service_name,
649 struct GNUNET_TIME_Relative timeout, GNUNET_ARM_ResultCallback cb,
650 void *cb_cls, uint16_t type)
652 struct ARMControlMessage *cm;
654 struct GNUNET_ARM_Message *msg;
656 slen = strlen (service_name) + 1;
657 if (slen + sizeof (struct GNUNET_ARM_Message) >=
658 GNUNET_SERVER_MAX_MESSAGE_SIZE)
662 cb (cb_cls, GNUNET_ARM_REQUEST_TOO_LONG, NULL, 0);
665 LOG (GNUNET_ERROR_TYPE_DEBUG, "Requesting %s of service `%s'.\n",
666 (GNUNET_MESSAGE_TYPE_ARM_START == type) ? "start" : "termination",
668 cm = GNUNET_malloc (sizeof (struct ARMControlMessage) + slen);
670 cm->result_cont = cb;
671 cm->cont_cls = cb_cls;
672 cm->timeout = GNUNET_TIME_relative_to_absolute (timeout);
673 memcpy (&cm[1], service_name, slen);
674 msg = GNUNET_malloc (sizeof (struct GNUNET_ARM_Message) + slen);
675 msg->header.size = htons (sizeof (struct GNUNET_ARM_Message) + slen);
676 msg->header.type = htons (type);
677 memcpy (&msg[1], service_name, slen);
679 LOG (GNUNET_ERROR_TYPE_DEBUG,
680 "Inserting a control message into the queue. Timeout is %llu\n",
681 GNUNET_TIME_absolute_get_remaining (cm->timeout).rel_value);
682 GNUNET_CONTAINER_DLL_insert_tail (h->control_pending_head,
683 h->control_pending_tail, cm);
684 cm->timeout_task_id =
685 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_absolute_get_remaining
686 (cm->timeout), &control_message_timeout, cm);
687 trigger_next_request (h, GNUNET_NO);
692 * Request for a service to be started.
694 * @param h handle to ARM
695 * @param service_name name of the service
696 * @param std_inheritance inheritance of std streams
697 * @param timeout how long to wait before failing for good
698 * @param cont callback to invoke after request is sent or not sent
699 * @param cont_cls closure for callback
702 GNUNET_ARM_request_service_start (struct GNUNET_ARM_Handle *h,
703 const char *service_name, enum GNUNET_OS_InheritStdioFlags std_inheritance,
704 struct GNUNET_TIME_Relative timeout, GNUNET_ARM_ResultCallback cont,
707 struct ARMControlMessage *cm;
710 LOG (GNUNET_ERROR_TYPE_DEBUG,
711 "Asked to start service `%s' within %s\n", service_name,
712 GNUNET_STRINGS_relative_time_to_string (timeout, GNUNET_NO));
713 if (0 == strcasecmp ("arm", service_name))
716 * 1) We're connected to ARM already. Invoke the callback immediately.
717 * 2) We're not connected to ARM.
718 * Cancel any reconnection attempts temporarily, then perform
721 if (GNUNET_NO == h->currently_down)
723 LOG (GNUNET_ERROR_TYPE_DEBUG, "ARM is already running\n");
725 cont (cont_cls, GNUNET_ARM_REQUEST_SENT_OK, "arm", GNUNET_ARM_RESULT_IS_STARTED_ALREADY);
727 else if (GNUNET_NO == h->service_test_is_active)
731 GNUNET_CLIENT_notify_transmit_ready_cancel (h->cth);
734 if (NULL != h->client)
736 GNUNET_CLIENT_disconnect (h->client);
739 if (GNUNET_SCHEDULER_NO_TASK != h->reconnect_task)
741 GNUNET_SCHEDULER_cancel (h->reconnect_task);
742 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
745 LOG (GNUNET_ERROR_TYPE_DEBUG,
746 "Not connected to ARM, will do a service test\n");
748 slen = strlen ("arm") + 1;
749 cm = GNUNET_malloc (sizeof (struct ARMControlMessage) + slen);
751 cm->result_cont = cont;
752 cm->cont_cls = cont_cls;
753 cm->timeout = GNUNET_TIME_relative_to_absolute (timeout);
754 cm->std_inheritance = std_inheritance;
755 memcpy (&cm[1], service_name, slen);
756 h->service_test_is_active = GNUNET_YES;
757 GNUNET_CLIENT_service_test ("arm", h->cfg, timeout, &arm_service_report,
762 /* Service test is already running - tell user to chill out and try
765 LOG (GNUNET_ERROR_TYPE_DEBUG, "Service test is already in progress, we're busy\n");
767 cont (cont_cls, GNUNET_ARM_REQUEST_BUSY, NULL, 0);
771 change_service (h, service_name, timeout, cont, cont_cls,
772 GNUNET_MESSAGE_TYPE_ARM_START);
777 * Request a service to be stopped.
778 * Stopping arm itself will not invalidate its handle, and
779 * ARM API will try to restore connection to the ARM service,
780 * even if ARM connection was lost because you asked for ARM to be stopped.
781 * Call GNUNET_ARM_disconnect_and_free () to free the handle and prevent
782 * further connection attempts.
784 * @param h handle to ARM
785 * @param service_name name of the service
786 * @param timeout how long to wait before failing for good
787 * @param cont callback to invoke after request is sent or is not sent
788 * @param cont_cls closure for callback
791 GNUNET_ARM_request_service_stop (struct GNUNET_ARM_Handle *h,
792 const char *service_name, struct GNUNET_TIME_Relative timeout,
793 GNUNET_ARM_ResultCallback cont, void *cont_cls)
795 LOG (GNUNET_ERROR_TYPE_DEBUG,
796 "Stopping service `%s' within %s\n",
798 GNUNET_STRINGS_relative_time_to_string (timeout, GNUNET_NO));
799 change_service (h, service_name, timeout, cont, cont_cls,
800 GNUNET_MESSAGE_TYPE_ARM_STOP);
805 * Request a list of running services.
807 * @param h handle to ARM
808 * @param timeout how long to wait before failing for good
809 * @param cont callback to invoke after request is sent or is not sent
810 * @param cont_cls closure for callback
813 GNUNET_ARM_request_service_list (struct GNUNET_ARM_Handle *h,
814 struct GNUNET_TIME_Relative timeout,
815 GNUNET_ARM_ServiceListCallback cont,
818 struct ARMControlMessage *cm;
819 struct GNUNET_ARM_Message *msg;
821 LOG (GNUNET_ERROR_TYPE_DEBUG,
822 "Requesting LIST from ARM service with timeout: %s\n",
823 GNUNET_STRINGS_relative_time_to_string (timeout, GNUNET_YES));
824 cm = GNUNET_malloc (sizeof (struct ARMControlMessage));
826 cm->list_cont = cont;
827 cm->cont_cls = cont_cls;
828 cm->timeout = GNUNET_TIME_relative_to_absolute (timeout);
829 msg = GNUNET_malloc (sizeof (struct GNUNET_ARM_Message));
830 msg->header.size = htons (sizeof (struct GNUNET_ARM_Message));
831 msg->header.type = htons (GNUNET_MESSAGE_TYPE_ARM_LIST);
833 GNUNET_CONTAINER_DLL_insert_tail (h->control_pending_head,
834 h->control_pending_tail, cm);
835 cm->timeout_task_id =
836 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_absolute_get_remaining
837 (cm->timeout), &control_message_timeout, cm);
838 trigger_next_request (h, GNUNET_NO);
842 static struct ARMControlMessage *
843 find_cm_by_id (struct GNUNET_ARM_Handle *h, uint64_t id)
845 struct ARMControlMessage *result;
846 for (result = h->control_sent_head; result; result = result->next)
847 if (id == result->msg->request_id)
854 * Handler for ARM replies.
856 * @param cls our "struct GNUNET_ARM_Handle"
857 * @param msg the message received from the arm service
860 client_notify_handler (void *cls, const struct GNUNET_MessageHeader *msg)
862 struct GNUNET_ARM_Handle *h = cls;
863 const struct GNUNET_ARM_Message *arm_msg;
864 const struct GNUNET_ARM_ResultMessage *res;
865 const struct GNUNET_ARM_ListResultMessage *lres;
866 struct ARMControlMessage *cm;
870 enum GNUNET_ARM_Result result;
879 LOG (GNUNET_ERROR_TYPE_INFO,
880 _("Client was disconnected from arm service, trying to reconnect.\n"));
881 reconnect_arm_later (h);
884 msize = ntohs (msg->size);
885 LOG (GNUNET_ERROR_TYPE_DEBUG,
886 "Processing message of type %u and size %u from arm service\n",
887 ntohs (msg->type), msize);
888 if (msize < sizeof (struct GNUNET_ARM_Message))
891 reconnect_arm_later (h);
894 arm_msg = (const struct GNUNET_ARM_Message *) msg;
895 id = GNUNET_ntohll (arm_msg->request_id);
896 cm = find_cm_by_id (h, id);
899 LOG (GNUNET_ERROR_TYPE_DEBUG, "Message with unknown id %llu\n", id);
903 switch (ntohs (msg->type))
905 case GNUNET_MESSAGE_TYPE_ARM_RESULT:
906 if (msize < sizeof (struct GNUNET_ARM_ResultMessage))
912 case GNUNET_MESSAGE_TYPE_ARM_LIST_RESULT:
913 if (msize < sizeof (struct GNUNET_ARM_ListResultMessage))
920 lres = (const struct GNUNET_ARM_ListResultMessage *) msg;
921 rcount = ntohs (lres->count);
925 list = GNUNET_malloc (sizeof (const char *) * rcount);
926 pos = (const char *)&lres[1];
927 for (i = 0; i < rcount; i++)
929 const char *end = memchr (pos, 0, msize - size_check);
937 size_check += (end - pos) + 1;
940 if (GNUNET_YES == fail)
951 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != cm->timeout_task_id);
952 GNUNET_SCHEDULER_cancel (cm->timeout_task_id);
953 GNUNET_CONTAINER_DLL_remove (h->control_sent_head,
954 h->control_sent_tail, cm);
955 if (GNUNET_YES == fail)
957 reconnect_arm_later (h);
958 GNUNET_free (cm->msg);
962 GNUNET_CLIENT_receive (h->client, &client_notify_handler, h,
963 GNUNET_TIME_UNIT_FOREVER_REL);
964 switch (ntohs (msg->type))
966 case GNUNET_MESSAGE_TYPE_ARM_RESULT:
967 res = (const struct GNUNET_ARM_ResultMessage *) msg;
968 LOG (GNUNET_ERROR_TYPE_DEBUG,
969 "Received response from ARM for service `%s': %u\n",
970 (const char *) &cm->msg[1], ntohs (msg->type));
971 result = (enum GNUNET_ARM_Result) ntohl (res->result);
972 if (NULL != cm->result_cont)
973 cm->result_cont (cm->cont_cls, GNUNET_ARM_REQUEST_SENT_OK,
974 (const char *) &cm->msg[1], result);
976 case GNUNET_MESSAGE_TYPE_ARM_LIST_RESULT:
977 if (NULL != cm->list_cont)
978 cm->list_cont (cm->cont_cls, GNUNET_ARM_REQUEST_SENT_OK, rcount,
983 GNUNET_free (cm->msg);
987 /* end of arm_api.c */