/*
This file is part of GNUnet.
- (C) 2009, 2010, 2012, 2013 Christian Grothoff (and other contributing authors)
+ Copyright (C) 2009, 2010, 2012, 2013, 2016 GNUnet e.V.
GNUnet is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published
You should have received a copy of the GNU General Public License
along with GNUnet; see the file COPYING. If not, write to the
- Free Software Foundation, Inc., 59 Temple Place - Suite 330,
- Boston, MA 02111-1307, USA.
+ Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ Boston, MA 02110-1301, USA.
*/
/**
* @file arm/arm_api.c
* @brief API for accessing the ARM service
- * @author Christian Grothoff, LRN
+ * @author Christian Grothoff
+ * @author LRN
*/
#include "platform.h"
-#include "gnunet_arm_service.h"
#include "gnunet_util_lib.h"
+#include "gnunet_arm_service.h"
#include "gnunet_protocols.h"
#include "arm.h"
#define LOG(kind,...) GNUNET_log_from (kind, "arm-api",__VA_ARGS__)
+
/**
- * Handle for interacting with ARM.
+ * Entry in a doubly-linked list of operations awaiting for replies
+ * (in-order) from the ARM service.
*/
-struct GNUNET_ARM_Handle
+struct GNUNET_ARM_Operation
{
-
- /**
- * Our control connection to the ARM service.
- */
- struct GNUNET_CLIENT_Connection *client;
-
- /**
- * The configuration that we are using.
- */
- struct GNUNET_CONFIGURATION_Handle *cfg;
-
- /**
- * Handle for our current transmission request.
- */
- struct GNUNET_CLIENT_TransmitHandle *cth;
-
- /**
- * Head of doubly-linked list of pending requests.
- */
- struct ARMControlMessage *control_pending_head;
-
/**
- * Tail of doubly-linked list of pending requests.
+ * This is a doubly-linked list.
*/
- struct ARMControlMessage *control_pending_tail;
+ struct GNUNET_ARM_Operation *next;
/**
- * Head of doubly-linked list of sent requests.
+ * This is a doubly-linked list.
*/
- struct ARMControlMessage *control_sent_head;
+ struct GNUNET_ARM_Operation *prev;
/**
- * Tail of doubly-linked list of sent requests.
+ * ARM handle.
*/
- struct ARMControlMessage *control_sent_tail;
+ struct GNUNET_ARM_Handle *h;
/**
- * ID of the reconnect task (if any).
+ * Callback for service state change requests.
*/
- GNUNET_SCHEDULER_TaskIdentifier reconnect_task;
+ GNUNET_ARM_ResultCallback result_cont;
/**
- * Current delay we use for re-trying to connect to core.
+ * Callback for service list requests.
*/
- struct GNUNET_TIME_Relative retry_backoff;
+ GNUNET_ARM_ServiceListCallback list_cont;
/**
- * Are we currently disconnected and hence unable to send?
+ * Closure for @e result_cont or @e list_cont.
*/
- unsigned char currently_down;
+ void *cont_cls;
/**
- * Callback to invoke on connection/disconnection.
+ * Task for async completion.
*/
- GNUNET_ARM_ConnectionStatusCallback conn_status;
+ struct GNUNET_SCHEDULER_Task *async;
/**
- * Closure for conn_status.
+ * Unique ID for the request.
*/
- void *conn_status_cls;
+ uint64_t id;
/**
- * GNUNET_YES if we're running a service test.
+ * Result of this operation for #notify_starting().
*/
- unsigned char service_test_is_active;
+ enum GNUNET_ARM_Result starting_ret;
/**
- * Counter for request identifiers
+ * Is this an operation to stop the ARM service?
*/
- uint64_t request_id_counter;
+ int is_arm_stop;
};
/**
- * Entry in a doubly-linked list of control messages to be transmitted
- * to the arm service.
- *
- * The actual message is allocated at the end of this struct.
+ * Handle for interacting with ARM.
*/
-struct ARMControlMessage
+struct GNUNET_ARM_Handle
{
/**
- * This is a doubly-linked list.
+ * Our connection to the ARM service.
*/
- struct ARMControlMessage *next;
+ struct GNUNET_MQ_Handle *mq;
/**
- * This is a doubly-linked list.
+ * The configuration that we are using.
*/
- struct ARMControlMessage *prev;
+ const struct GNUNET_CONFIGURATION_Handle *cfg;
/**
- * Callback for service state change requests.
+ * Head of doubly-linked list of pending operations.
*/
- GNUNET_ARM_ResultCallback result_cont;
+ struct GNUNET_ARM_Operation *operation_pending_head;
/**
- * Callback for service list requests.
+ * Tail of doubly-linked list of pending operations.
*/
- GNUNET_ARM_ServiceListCallback list_cont;
+ struct GNUNET_ARM_Operation *operation_pending_tail;
/**
- * Closure for 'result_cont' or 'list_cont'.
+ * Callback to invoke on connection/disconnection.
*/
- void *cont_cls;
+ GNUNET_ARM_ConnectionStatusCallback conn_status;
/**
- * Timeout for the operation.
+ * Closure for @e conn_status.
*/
- struct GNUNET_TIME_Absolute timeout;
+ void *conn_status_cls;
/**
- * Type of the request expressed as a message type (start, stop or list).
+ * ARM operation where the goal is to wait for ARM shutdown to
+ * complete. This operation is special in that it waits for an
+ * error on the @e mq. So we complete it by calling the
+ * continuation in the #mq_error_handler(). Note that the operation
+ * is no longer in the @e operation_pending_head DLL once it is
+ * referenced from this field.
*/
- uint16_t type;
+ struct GNUNET_ARM_Operation *thm;
/**
- * Flags for passing std descriptors to ARM (when starting ARM).
+ * ID of the reconnect task (if any).
*/
- enum GNUNET_OS_InheritStdioFlags std_inheritance;
+ struct GNUNET_SCHEDULER_Task *reconnect_task;
/**
- * ARM handle.
+ * Current delay we use for re-trying to connect to core.
*/
- struct GNUNET_ARM_Handle *h;
+ struct GNUNET_TIME_Relative retry_backoff;
/**
- * Message to send.
+ * Counter for request identifiers. They are used to match replies
+ * from ARM to operations in the @e operation_pending_head DLL.
*/
- struct GNUNET_ARM_Message *msg;
+ uint64_t request_id_counter;
/**
- * Task to run when request times out.
+ * Have we detected that ARM is up?
*/
- GNUNET_SCHEDULER_TaskIdentifier timeout_task_id;
+ int currently_up;
+
};
-static void
-client_notify_handler (void *cls, const struct GNUNET_MessageHeader *msg);
+/**
+ * Connect to arm.
+ *
+ * @param h arm handle
+ * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
+ */
static int
reconnect_arm (struct GNUNET_ARM_Handle *h);
-static void
-trigger_next_request (struct GNUNET_ARM_Handle *h, int ignore_currently_down);
-
/**
* Task scheduled to try to re-connect to arm.
*
- * @param cls the 'struct GNUNET_ARM_Handle'
- * @param tc task context
+ * @param cls the `struct GNUNET_ARM_Handle`
*/
static void
-reconnect_arm_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+reconnect_arm_task (void *cls)
{
struct GNUNET_ARM_Handle *h = cls;
- h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
- LOG (GNUNET_ERROR_TYPE_DEBUG, "Connecting to ARM service after delay\n");
+ h->reconnect_task = NULL;
reconnect_arm (h);
}
-static void
-clear_pending_messages (struct GNUNET_ARM_Handle *h, enum GNUNET_ARM_RequestStatus result)
-{
- struct ARMControlMessage *cm;
-
- LOG (GNUNET_ERROR_TYPE_DEBUG,
- "Clearing pending messages\n");
-
- while (NULL != (cm = h->control_pending_head))
- {
- GNUNET_CONTAINER_DLL_remove (h->control_pending_head,
- h->control_pending_tail, cm);
- GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != cm->timeout_task_id);
- GNUNET_SCHEDULER_cancel (cm->timeout_task_id);
- if (NULL != cm->result_cont)
- cm->result_cont (cm->cont_cls, cm->h, result, NULL, 0);
- GNUNET_free_non_null (cm->msg);
- GNUNET_free (cm);
- }
-}
-
/**
* Close down any existing connection to the ARM service and
* try re-establishing it later.
static void
reconnect_arm_later (struct GNUNET_ARM_Handle *h)
{
- if (GNUNET_NO != h->currently_down)
- return;
+ struct GNUNET_ARM_Operation *op;
- if (NULL != h->cth)
+ if (NULL != h->mq)
{
- GNUNET_CLIENT_notify_transmit_ready_cancel (h->cth);
- h->cth = NULL;
+ GNUNET_MQ_destroy (h->mq);
+ h->mq = NULL;
}
-
- if (NULL != h->client)
+ h->currently_up = GNUNET_NO;
+ GNUNET_assert (NULL == h->reconnect_task);
+ h->reconnect_task =
+ GNUNET_SCHEDULER_add_delayed (h->retry_backoff,
+ &reconnect_arm_task,
+ h);
+ while (NULL != (op = h->operation_pending_head))
{
- GNUNET_CLIENT_disconnect (h->client);
- h->client = NULL;
+ if (NULL != op->result_cont)
+ op->result_cont (op->cont_cls,
+ GNUNET_ARM_REQUEST_DISCONNECTED,
+ 0);
+ if (NULL != op->list_cont)
+ op->list_cont (op->cont_cls,
+ GNUNET_ARM_REQUEST_DISCONNECTED,
+ 0,
+ NULL);
+ GNUNET_ARM_operation_cancel (op);
}
-
+ GNUNET_assert (NULL == h->operation_pending_head);
+ h->retry_backoff = GNUNET_TIME_STD_BACKOFF (h->retry_backoff);
if (NULL != h->conn_status)
- h->conn_status (h->conn_status_cls, h, GNUNET_NO);
+ h->conn_status (h->conn_status_cls,
+ GNUNET_NO);
+}
- h->currently_down = GNUNET_YES;
- GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == h->reconnect_task);
- h->reconnect_task =
- GNUNET_SCHEDULER_add_delayed (h->retry_backoff, &reconnect_arm_task, h);
- /* Don't clear pending messages on disconnection, deliver them later
- clear_pending_messages (h, GNUNET_ARM_REQUEST_DISCONNECTED);
- GNUNET_assert (NULL == h->control_pending_head);
- */
- h->retry_backoff = GNUNET_TIME_STD_BACKOFF (h->retry_backoff);
+/**
+ * Find a control message by its unique ID.
+ *
+ * @param h ARM handle
+ * @param id unique message ID to use for the lookup
+ * @return NULL if not found
+ */
+static struct GNUNET_ARM_Operation *
+find_op_by_id (struct GNUNET_ARM_Handle *h,
+ uint64_t id)
+{
+ struct GNUNET_ARM_Operation *result;
+
+ for (result = h->operation_pending_head; NULL != result; result = result->next)
+ if (id == result->id)
+ return result;
+ return NULL;
}
+
/**
- * Transmit the next message to the arm service.
+ * Handler for ARM replies.
*
- * @param cls closure with the 'struct GNUNET_ARM_Handle'
- * @param size number of bytes available in buf
- * @param buf where the callee should write the message
- * @return number of bytes written to buf
+ * @param cls our `struct GNUNET_ARM_Handle`
+ * @param res the message received from the arm service
*/
-static size_t
-transmit_arm_message (void *cls, size_t size, void *buf)
+static void
+handle_arm_result (void *cls,
+ const struct GNUNET_ARM_ResultMessage *res)
{
struct GNUNET_ARM_Handle *h = cls;
- struct ARMControlMessage *cm;
- struct GNUNET_ARM_Message *arm_msg;
- uint16_t msize;
- uint64_t request_id;
+ struct GNUNET_ARM_Operation *op;
+ uint64_t id;
+ enum GNUNET_ARM_Result result;
+ GNUNET_ARM_ResultCallback result_cont;
+ void *result_cont_cls;
- LOG (GNUNET_ERROR_TYPE_DEBUG,
- "transmit_arm_message is running with %p buffer of size %lu. ARM is known to be %s\n",
- buf, size, h->currently_down ? "unconnected" : "connected");
- GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == h->reconnect_task);
- h->cth = NULL;
- if ((GNUNET_YES == h->currently_down) && (NULL != buf))
- {
- h->currently_down = GNUNET_NO;
- if (NULL != h->conn_status)
- h->conn_status (h->conn_status_cls, h, GNUNET_YES);
- h->retry_backoff = GNUNET_TIME_UNIT_MILLISECONDS;
- GNUNET_CLIENT_receive (h->client, &client_notify_handler, h,
- GNUNET_TIME_UNIT_FOREVER_REL);
- }
- if (NULL == buf)
+ id = GNUNET_ntohll (res->arm_msg.request_id);
+ op = find_op_by_id (h,
+ id);
+ if (NULL == op)
{
LOG (GNUNET_ERROR_TYPE_DEBUG,
- "Transmission failed, initiating reconnect\n");
- reconnect_arm_later (h);
- return 0;
+ "Message with unknown id %llu\n",
+ (unsigned long long) id);
+ return;
}
- if (NULL == (cm = h->control_pending_head))
+
+ result = (enum GNUNET_ARM_Result) ntohl (res->result);
+ if ( (GNUNET_YES == op->is_arm_stop) &&
+ (GNUNET_ARM_RESULT_STOPPING == result) )
{
- LOG (GNUNET_ERROR_TYPE_DEBUG, "Queue is empty, not sending anything\n");
- return 0;
+ /* special case: if we are stopping 'gnunet-service-arm', we do not just
+ wait for the result message, but also wait for the service to close
+ the connection (and then we have to close our client handle as well);
+ this is done by installing a different receive handler, waiting for
+ the connection to go down */
+ if (NULL != h->thm)
+ {
+ GNUNET_break (0);
+ op->result_cont (h->thm->cont_cls,
+ GNUNET_ARM_REQUEST_SENT_OK,
+ GNUNET_ARM_RESULT_IS_NOT_KNOWN);
+ GNUNET_free (h->thm);
+ }
+ GNUNET_CONTAINER_DLL_remove (h->operation_pending_head,
+ h->operation_pending_tail,
+ op);
+ h->thm = op;
+ return;
}
+ result_cont = op->result_cont;
+ result_cont_cls = op->cont_cls;
+ GNUNET_ARM_operation_cancel (op);
+ if (NULL != result_cont)
+ result_cont (result_cont_cls,
+ GNUNET_ARM_REQUEST_SENT_OK,
+ result);
+}
- GNUNET_assert (NULL != cm->msg);
- msize = ntohs (cm->msg->header.size);
- if (size < msize)
+
+/**
+ * Checked that list result message is well-formed.
+ *
+ * @param cls our `struct GNUNET_ARM_Handle`
+ * @param lres the message received from the arm service
+ * @return #GNUNET_OK if message is well-formed
+ */
+static int
+check_arm_list_result (void *cls,
+ const struct GNUNET_ARM_ListResultMessage *lres)
+{
+ const char *pos = (const char *) &lres[1];
+ uint16_t rcount = ntohs (lres->count);
+ uint16_t msize = ntohs (lres->arm_msg.header.size);
+ uint16_t size_check;
+
+ size_check = 0;
+ for (unsigned int i = 0; i < rcount; i++)
{
- LOG (GNUNET_ERROR_TYPE_DEBUG,
- "Request is too big (%u < %u), not sending it\n", size, msize);
- trigger_next_request (h, GNUNET_NO);
- return 0;
+ const char *end = memchr (pos, 0, msize - size_check);
+ if (NULL == end)
+ {
+ GNUNET_break (0);
+ return GNUNET_SYSERR;
+ }
+ size_check += (end - pos) + 1;
+ pos = end + 1;
}
- arm_msg = cm->msg;
- if (0 == h->request_id_counter)
- h->request_id_counter++;
- request_id = h->request_id_counter++;
- LOG (GNUNET_ERROR_TYPE_DEBUG,
- "Transmitting control message with %u bytes of type %u to arm with id %llu\n",
- (unsigned int) msize, (unsigned int) ntohs (cm->msg->header.type), request_id);
- arm_msg->request_id = GNUNET_htonll (request_id);
- memcpy (buf, cm->msg, msize);
- /* Otherwise we won't be able to find it later! */
- arm_msg->request_id = request_id;
-
- GNUNET_CONTAINER_DLL_remove (h->control_pending_head,
- h->control_pending_tail, cm);
- GNUNET_CONTAINER_DLL_insert_tail (h->control_sent_head,
- h->control_sent_tail, cm);
-
- /* Don't free msg, keep it around (kind of wasteful, but then we don't
- * really have many messages to handle, and it'll be freed when it times
- * out anyway.
- */
- trigger_next_request (h, GNUNET_NO);
- return msize;
+ return GNUNET_OK;
}
/**
- * Check the list of pending requests, send the next
- * one to the arm.
+ * Handler for ARM list replies.
*
- * @param h arm handle
- * @param ignore_currently_down transmit message even if not initialized?
+ * @param cls our `struct GNUNET_ARM_Handle`
+ * @param lres the message received from the arm service
*/
static void
-trigger_next_request (struct GNUNET_ARM_Handle *h, int ignore_currently_down)
+handle_arm_list_result (void *cls,
+ const struct GNUNET_ARM_ListResultMessage *lres)
{
- uint16_t msize;
+ struct GNUNET_ARM_Handle *h = cls;
+ uint16_t rcount = ntohs (lres->count);
+ const char *list[rcount];
+ const char *pos = (const char *) &lres[1];
+ uint16_t msize = ntohs (lres->arm_msg.header.size);
+ struct GNUNET_ARM_Operation *op;
+ uint16_t size_check;
+ uint64_t id;
- if ((GNUNET_YES == h->currently_down) && (ignore_currently_down == GNUNET_NO))
+ id = GNUNET_ntohll (lres->arm_msg.request_id);
+ op = find_op_by_id (h,
+ id);
+ if (NULL == op)
{
LOG (GNUNET_ERROR_TYPE_DEBUG,
- "ARM connection down, not processing queue\n");
+ "Message with unknown id %llu\n",
+ (unsigned long long) id);
return;
}
- if (NULL != h->cth)
+ size_check = 0;
+ for (unsigned int i = 0; i < rcount; i++)
{
- LOG (GNUNET_ERROR_TYPE_DEBUG, "Request pending, not processing queue\n");
- return;
+ const char *end = memchr (pos,
+ 0,
+ msize - size_check);
+
+ /* Assert, as this was already checked in #check_arm_list_result() */
+ GNUNET_assert (NULL != end);
+ list[i] = pos;
+ size_check += (end - pos) + 1;
+ pos = end + 1;
}
- if (NULL != h->control_pending_head)
- msize =
- ntohs (((struct GNUNET_MessageHeader *) &h->
- control_pending_head[1])->size);
- else if (GNUNET_NO == ignore_currently_down)
+ if (NULL != op->list_cont)
+ op->list_cont (op->cont_cls,
+ GNUNET_ARM_REQUEST_SENT_OK,
+ rcount,
+ list);
+ GNUNET_ARM_operation_cancel (op);
+}
+
+
+/**
+ * Receive confirmation from test, ARM service is up.
+ *
+ * @param cls closure with the `struct GNUNET_ARM_Handle`
+ * @param msg message received
+ */
+static void
+handle_confirm (void *cls,
+ const struct GNUNET_MessageHeader *msg)
+{
+ struct GNUNET_ARM_Handle *h = cls;
+
+ LOG (GNUNET_ERROR_TYPE_DEBUG,
+ "Got confirmation from ARM that we are up!\n");
+ if (GNUNET_NO == h->currently_up)
{
- LOG (GNUNET_ERROR_TYPE_DEBUG,
- "Request queue empty, not processing queue\n");
- return; /* no pending message */
+ h->currently_up = GNUNET_YES;
+ if (NULL != h->conn_status)
+ h->conn_status (h->conn_status_cls,
+ GNUNET_YES);
}
- h->cth =
- GNUNET_CLIENT_notify_transmit_ready (h->client, msize,
- GNUNET_TIME_UNIT_FOREVER_REL,
- GNUNET_NO, &transmit_arm_message, h);
+}
+
+
+/**
+ * Generic error handler, called with the appropriate error code and
+ * the same closure specified at the creation of the message queue.
+ * Not every message queue implementation supports an error handler.
+ *
+ * @param cls closure with the `struct GNUNET_ARM_Handle *`
+ * @param error error code
+ */
+static void
+mq_error_handler (void *cls,
+ enum GNUNET_MQ_Error error)
+{
+ struct GNUNET_ARM_Handle *h = cls;
+ struct GNUNET_ARM_Operation *op;
+
+ h->currently_up = GNUNET_NO;
+ if (NULL != (op = h->thm))
+ {
+ h->thm = NULL;
+ op->result_cont (op->cont_cls,
+ GNUNET_ARM_REQUEST_SENT_OK,
+ GNUNET_ARM_RESULT_STOPPED);
+ GNUNET_free (op);
+ }
+ reconnect_arm_later (h);
}
* Connect to arm.
*
* @param h arm handle
+ * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
*/
static int
reconnect_arm (struct GNUNET_ARM_Handle *h)
{
- GNUNET_assert (NULL == h->client);
- GNUNET_assert (GNUNET_YES == h->currently_down);
- h->client = GNUNET_CLIENT_connect ("arm", h->cfg);
- if (NULL == h->client)
+ GNUNET_MQ_hd_fixed_size (arm_result,
+ GNUNET_MESSAGE_TYPE_ARM_RESULT,
+ struct GNUNET_ARM_ResultMessage);
+ GNUNET_MQ_hd_var_size (arm_list_result,
+ GNUNET_MESSAGE_TYPE_ARM_LIST_RESULT,
+ struct GNUNET_ARM_ListResultMessage);
+ GNUNET_MQ_hd_fixed_size (confirm,
+ GNUNET_MESSAGE_TYPE_TEST,
+ struct GNUNET_MessageHeader);
+ struct GNUNET_MQ_MessageHandler handlers[] = {
+ make_arm_result_handler (h),
+ make_arm_list_result_handler (h),
+ make_confirm_handler (h),
+ GNUNET_MQ_handler_end ()
+ };
+ struct GNUNET_MessageHeader *test;
+ struct GNUNET_MQ_Envelope *env;
+
+ if (NULL != h->mq)
+ return GNUNET_OK;
+ GNUNET_assert (GNUNET_NO == h->currently_up);
+ h->mq = GNUNET_CLIENT_connecT (h->cfg,
+ "arm",
+ handlers,
+ &mq_error_handler,
+ h);
+ if (NULL == h->mq)
{
LOG (GNUNET_ERROR_TYPE_DEBUG,
- "arm_api, GNUNET_CLIENT_connect returned NULL\n");
+ "GNUNET_CLIENT_connect returned NULL\n");
if (NULL != h->conn_status)
- h->conn_status (h->conn_status_cls, h, GNUNET_SYSERR);
+ h->conn_status (h->conn_status_cls,
+ GNUNET_SYSERR);
return GNUNET_SYSERR;
}
LOG (GNUNET_ERROR_TYPE_DEBUG,
- "arm_api, GNUNET_CLIENT_connect returned non-NULL\n");
- trigger_next_request (h, GNUNET_YES);
+ "Sending TEST message to ARM\n");
+ env = GNUNET_MQ_msg (test,
+ GNUNET_MESSAGE_TYPE_TEST);
+ GNUNET_MQ_send (h->mq,
+ env);
return GNUNET_OK;
}
* the ARM service may internally use a different
* configuration to determine how to start the service).
* @param conn_status will be called when connecting/disconnecting
- * @param cls closure for conn_status
+ * @param conn_status_cls closure for @a conn_status
* @return context to use for further ARM operations, NULL on error.
*/
struct GNUNET_ARM_Handle *
GNUNET_ARM_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
- GNUNET_ARM_ConnectionStatusCallback conn_status, void *cls)
+ GNUNET_ARM_ConnectionStatusCallback conn_status,
+ void *conn_status_cls)
{
struct GNUNET_ARM_Handle *h;
- h = GNUNET_malloc (sizeof (struct GNUNET_ARM_Handle));
- h->cfg = GNUNET_CONFIGURATION_dup (cfg);
- h->currently_down = GNUNET_YES;
- h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
+ h = GNUNET_new (struct GNUNET_ARM_Handle);
+ h->cfg = cfg;
h->conn_status = conn_status;
- h->conn_status_cls = cls;
+ h->conn_status_cls = conn_status_cls;
if (GNUNET_OK != reconnect_arm (h))
{
GNUNET_free (h);
/**
* Disconnect from the ARM service (if connected) and destroy the context.
- * Don't call inside an ARM callback!
*
* @param h the handle that was being used
*/
void
-GNUNET_ARM_disconnect_and_free (struct GNUNET_ARM_Handle *h)
+GNUNET_ARM_disconnect (struct GNUNET_ARM_Handle *h)
{
- LOG (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting from ARM service\n");
- if (NULL != h->cth)
+ struct GNUNET_ARM_Operation *op;
+
+ LOG (GNUNET_ERROR_TYPE_DEBUG,
+ "Disconnecting from ARM service\n");
+ while (NULL != (op = h->operation_pending_head))
{
- GNUNET_CLIENT_notify_transmit_ready_cancel (h->cth);
- h->cth = NULL;
+ GNUNET_CONTAINER_DLL_remove (h->operation_pending_head,
+ h->operation_pending_tail,
+ op);
+ if (NULL != op->result_cont)
+ op->result_cont (op->cont_cls,
+ GNUNET_ARM_REQUEST_DISCONNECTED,
+ 0);
+ if (NULL != op->list_cont)
+ op->list_cont (op->cont_cls,
+ GNUNET_ARM_REQUEST_DISCONNECTED,
+ 0,
+ NULL);
+ if (NULL != op->async)
+ {
+ GNUNET_SCHEDULER_cancel (op->async);
+ op->async = NULL;
+ }
+ GNUNET_free (op);
}
- clear_pending_messages (h, GNUNET_ARM_REQUEST_DISCONNECTED);
- if (NULL != h->client)
+ if (NULL != h->mq)
{
- GNUNET_CLIENT_disconnect (h->client);
- h->client = NULL;
+ GNUNET_MQ_destroy (h->mq);
+ h->mq = NULL;
}
- if (GNUNET_SCHEDULER_NO_TASK != h->reconnect_task)
+ if (NULL != h->reconnect_task)
{
GNUNET_SCHEDULER_cancel (h->reconnect_task);
- h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
- }
- if (GNUNET_NO == h->service_test_is_active)
- {
- GNUNET_CONFIGURATION_destroy (h->cfg);
- GNUNET_free (h);
+ h->reconnect_task = NULL;
}
+ GNUNET_free (h);
}
-/**
- * Message timed out. Remove it from the queue.
- *
- * @param cls the message (struct ARMControlMessage *)
- * @param tc task context
- */
-static void
-control_message_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
-{
- struct ARMControlMessage *cm = cls;
- struct GNUNET_ARM_Message *arm_msg;
- LOG (GNUNET_ERROR_TYPE_DEBUG,
- "Control message timed out\n");
- arm_msg = cm->msg;
- if ((NULL == arm_msg) || (0 == arm_msg->request_id))
- {
- GNUNET_CONTAINER_DLL_remove (cm->h->control_pending_head,
- cm->h->control_pending_tail, cm);
- }
- else
- {
- GNUNET_CONTAINER_DLL_remove (cm->h->control_sent_head,
- cm->h->control_sent_tail, cm);
- }
- if (NULL != cm->result_cont)
- cm->result_cont (cm->cont_cls, cm->h, GNUNET_ARM_REQUEST_TIMEOUT, NULL, 0);
- else if (NULL != cm->list_cont)
- cm->list_cont (cm->cont_cls, cm->h, GNUNET_ARM_REQUEST_TIMEOUT, 0, NULL);
- GNUNET_free_non_null (cm->msg);
- GNUNET_free (cm);
-}
-
-
-#include "do_start_process.c"
-
-
/**
* A client specifically requested starting of ARM itself.
- * This function is called with information about whether
- * or not ARM is running; if it is, report success. If
- * it is not, start the ARM process.
+ * Starts the ARM service.
*
- * @param cls the context for the request that we will report on (struct ARMControlMessage *)
- * @param tc why were we called (reason says if ARM is running)
+ * @param h the handle with configuration details
+ * @param std_inheritance inheritance of std streams
+ * @return operation status code
*/
-static void
-arm_service_report (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+static enum GNUNET_ARM_Result
+start_arm_service (struct GNUNET_ARM_Handle *h,
+ enum GNUNET_OS_InheritStdioFlags std_inheritance)
{
- struct ARMControlMessage *cm = cls;
struct GNUNET_OS_Process *proc;
- unsigned char test_is_active;
char *cbinary;
char *binary;
+ char *quotedbinary;
char *config;
char *loprefix;
char *lopostfix;
- test_is_active = cm->h->service_test_is_active;
-
- /* FIXME: shouldn't we check for GNUNET_SCHEDULER_REASON_SHUTDOWN ? */
- if ((GNUNET_YES == test_is_active) &&
- (0 != (tc->reason & GNUNET_SCHEDULER_REASON_PREREQ_DONE)))
- {
- LOG (GNUNET_ERROR_TYPE_DEBUG, "Looks like `%s' is already running.\n",
- "gnunet-service-arm");
- /* arm is running! */
- if (cm->result_cont)
- cm->result_cont (cm->cont_cls, cm->h, GNUNET_ARM_REQUEST_SENT_OK, "arm", GNUNET_ARM_RESULT_IS_STARTED_ALREADY);
- }
- if (GNUNET_NO == test_is_active)
- {
- /* User disconnected & destroyed ARM handle in the middle of
- * the service test, so we kept the handle around until now.
- */
- GNUNET_CONFIGURATION_destroy (cm->h->cfg);
- GNUNET_free (cm->h);
- }
- if ((0 != (tc->reason & GNUNET_SCHEDULER_REASON_PREREQ_DONE)) ||
- (GNUNET_NO == test_is_active))
- {
- GNUNET_free (cm);
- return;
- }
- cm->h->service_test_is_active = GNUNET_NO;
- LOG (GNUNET_ERROR_TYPE_DEBUG,
- "Looks like `%s' is not running, will start it.\n",
- "gnunet-service-arm");
- if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (
- cm->h->cfg, "arm", "PREFIX", &loprefix))
+ if (GNUNET_OK !=
+ GNUNET_CONFIGURATION_get_value_string (h->cfg,
+ "arm",
+ "PREFIX",
+ &loprefix))
loprefix = GNUNET_strdup ("");
- if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (
- cm->h->cfg, "arm", "OPTIONS", &lopostfix))
+ if (GNUNET_OK !=
+ GNUNET_CONFIGURATION_get_value_string (h->cfg,
+ "arm",
+ "OPTIONS",
+ &lopostfix))
lopostfix = GNUNET_strdup ("");
- if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (
- cm->h->cfg, "arm", "BINARY", &cbinary))
+ if (GNUNET_OK !=
+ GNUNET_CONFIGURATION_get_value_string (h->cfg,
+ "arm",
+ "BINARY",
+ &cbinary))
{
- GNUNET_log_config_missing (GNUNET_ERROR_TYPE_WARNING, "arm", "BINARY");
- if (cm->result_cont)
- cm->result_cont (cm->cont_cls, cm->h, GNUNET_ARM_REQUEST_SENT_OK, "arm", GNUNET_ARM_RESULT_IS_NOT_KNOWN);
- GNUNET_free (cm);
+ GNUNET_log_config_missing (GNUNET_ERROR_TYPE_WARNING,
+ "arm",
+ "BINARY");
GNUNET_free (loprefix);
GNUNET_free (lopostfix);
- return;
+ return GNUNET_ARM_RESULT_IS_NOT_KNOWN;
}
- if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (
- cm->h->cfg, "arm", "CONFIG", &config))
+ if (GNUNET_OK !=
+ GNUNET_CONFIGURATION_get_value_filename (h->cfg,
+ "arm",
+ "CONFIG",
+ &config))
config = NULL;
binary = GNUNET_OS_get_libexec_binary_path (cbinary);
+ GNUNET_asprintf ("edbinary,
+ "\"%s\"",
+ binary);
GNUNET_free (cbinary);
- if ((GNUNET_YES == GNUNET_CONFIGURATION_have_value (
- cm->h->cfg, "TESTING", "WEAKRANDOM")) &&
- (GNUNET_YES == GNUNET_CONFIGURATION_get_value_yesno (
- cm->h->cfg, "TESTING", "WEAKRANDOM")) &&
- (GNUNET_NO == GNUNET_CONFIGURATION_have_value (
- cm->h->cfg, "TESTING", "HOSTFILE")))
+ if ( (GNUNET_YES ==
+ GNUNET_CONFIGURATION_have_value (h->cfg,
+ "TESTING",
+ "WEAKRANDOM")) &&
+ (GNUNET_YES ==
+ GNUNET_CONFIGURATION_get_value_yesno (h->cfg,
+ "TESTING",
+ "WEAKRANDOM")) &&
+ (GNUNET_NO ==
+ GNUNET_CONFIGURATION_have_value (h->cfg,
+ "TESTING",
+ "HOSTFILE")))
{
/* Means we are ONLY running locally */
/* we're clearly running a test, don't daemonize */
if (NULL == config)
- proc = do_start_process (GNUNET_NO, cm->std_inheritance,
- NULL, loprefix, binary,
- /* no daemonization! */
- lopostfix, NULL);
+ proc = GNUNET_OS_start_process_s (GNUNET_NO,
+ std_inheritance,
+ NULL,
+ loprefix,
+ quotedbinary,
+ /* no daemonization! */
+ lopostfix,
+ NULL);
else
- proc = do_start_process (GNUNET_NO, cm->std_inheritance,
- NULL, loprefix, binary, "-c", config,
- /* no daemonization! */
- lopostfix, NULL);
+ proc = GNUNET_OS_start_process_s (GNUNET_NO,
+ std_inheritance,
+ NULL,
+ loprefix,
+ quotedbinary,
+ "-c", config,
+ /* no daemonization! */
+ lopostfix,
+ NULL);
}
else
{
if (NULL == config)
- proc = do_start_process (GNUNET_NO, cm->std_inheritance,
- NULL, loprefix, binary,
- "-d", lopostfix, NULL);
+ proc = GNUNET_OS_start_process_s (GNUNET_NO,
+ std_inheritance,
+ NULL,
+ loprefix,
+ quotedbinary,
+ "-d", /* do daemonize */
+ lopostfix, NULL);
else
- proc = do_start_process (GNUNET_NO, cm->std_inheritance,
- NULL, loprefix, binary, "-c", config,
- "-d", lopostfix, NULL);
+ proc = GNUNET_OS_start_process_s (GNUNET_NO,
+ std_inheritance,
+ NULL,
+ loprefix,
+ quotedbinary,
+ "-c", config,
+ "-d", /* do daemonize */
+ lopostfix,
+ NULL);
}
GNUNET_free (binary);
+ GNUNET_free (quotedbinary);
GNUNET_free_non_null (config);
GNUNET_free (loprefix);
GNUNET_free (lopostfix);
if (NULL == proc)
+ return GNUNET_ARM_RESULT_START_FAILED;
+ GNUNET_OS_process_destroy (proc);
+ return GNUNET_ARM_RESULT_STARTING;
+}
+
+
+/**
+ * Abort an operation. Only prevents the callback from being
+ * called, the operation may still complete.
+ *
+ * @param op operation to cancel
+ */
+void
+GNUNET_ARM_operation_cancel (struct GNUNET_ARM_Operation *op)
+{
+ struct GNUNET_ARM_Handle *h = op->h;
+
+ if (h->thm == op)
{
- if (cm->result_cont)
- cm->result_cont (cm->cont_cls, cm->h, GNUNET_ARM_REQUEST_SENT_OK, "arm",
- GNUNET_ARM_RESULT_START_FAILED);
- GNUNET_free (cm);
+ op->result_cont = NULL;
return;
}
- if (cm->result_cont)
- cm->result_cont (cm->cont_cls, cm->h, GNUNET_ARM_REQUEST_SENT_OK, "arm",
- GNUNET_ARM_RESULT_STARTING);
- GNUNET_OS_process_destroy (proc);
- reconnect_arm (cm->h);
- GNUNET_free (cm);
+ GNUNET_CONTAINER_DLL_remove (h->operation_pending_head,
+ h->operation_pending_tail,
+ op);
+ GNUNET_free (op);
}
*
* @param h handle to ARM
* @param service_name name of the service
- * @param timeout how long to wait before failing for good
* @param cb callback to invoke when service is ready
- * @param cb_cls closure for callback
+ * @param cb_cls closure for @a cb
* @param type type of the request
+ * @return handle to queue, NULL on error
*/
-static void
-change_service (struct GNUNET_ARM_Handle *h, const char *service_name,
- struct GNUNET_TIME_Relative timeout, GNUNET_ARM_ResultCallback cb,
- void *cb_cls, uint16_t type)
+static struct GNUNET_ARM_Operation *
+change_service (struct GNUNET_ARM_Handle *h,
+ const char *service_name,
+ GNUNET_ARM_ResultCallback cb,
+ void *cb_cls,
+ uint16_t type)
{
- struct ARMControlMessage *cm;
+ struct GNUNET_ARM_Operation *op;
size_t slen;
+ struct GNUNET_MQ_Envelope *env;
struct GNUNET_ARM_Message *msg;
slen = strlen (service_name) + 1;
GNUNET_SERVER_MAX_MESSAGE_SIZE)
{
GNUNET_break (0);
- if (cb != NULL)
- cb (cb_cls, h, GNUNET_ARM_REQUEST_TOO_LONG, NULL, 0);
- return;
+ return NULL;
}
- LOG (GNUNET_ERROR_TYPE_DEBUG, "Requesting %s of service `%s'.\n",
- (GNUNET_MESSAGE_TYPE_ARM_START == type) ? "start" : "termination",
- service_name);
- cm = GNUNET_malloc (sizeof (struct ARMControlMessage) + slen);
- cm->h = h;
- cm->result_cont = cb;
- cm->cont_cls = cb_cls;
- cm->timeout = GNUNET_TIME_relative_to_absolute (timeout);
- memcpy (&cm[1], service_name, slen);
- msg = GNUNET_malloc (sizeof (struct GNUNET_ARM_Message) + slen);
- msg->header.size = htons (sizeof (struct GNUNET_ARM_Message) + slen);
- msg->header.type = htons (type);
- memcpy (&msg[1], service_name, slen);
- cm->msg = msg;
+ if (0 == h->request_id_counter)
+ h->request_id_counter++;
+ op = GNUNET_new (struct GNUNET_ARM_Operation);
+ op->h = h;
+ op->result_cont = cb;
+ op->cont_cls = cb_cls;
+ op->id = h->request_id_counter++;
+ GNUNET_CONTAINER_DLL_insert_tail (h->operation_pending_head,
+ h->operation_pending_tail,
+ op);
+ env = GNUNET_MQ_msg_extra (msg,
+ slen,
+ type);
+ msg->reserved = htonl (0);
+ msg->request_id = GNUNET_htonll (op->id);
+ memcpy (&msg[1],
+ service_name,
+ slen);
+ GNUNET_MQ_send (h->mq,
+ env);
+ return op;
+}
+
+
+/**
+ * Task run to notify application that ARM is already up.
+ *
+ * @param cls the operation that asked ARM to be started
+ */
+static void
+notify_running (void *cls)
+{
+ struct GNUNET_ARM_Operation *op = cls;
+ struct GNUNET_ARM_Handle *h = op->h;
+
+ op->async = NULL;
+ GNUNET_CONTAINER_DLL_remove (h->operation_pending_head,
+ h->operation_pending_tail,
+ op);
+ if (NULL != op->result_cont)
+ op->result_cont (op->cont_cls,
+ GNUNET_ARM_REQUEST_SENT_OK,
+ GNUNET_ARM_RESULT_IS_STARTED_ALREADY);
+ if ( (GNUNET_YES == h->currently_up) &&
+ (NULL != h->conn_status) )
+ h->conn_status (h->conn_status_cls,
+ GNUNET_YES);
+ GNUNET_free (op);
+}
+
+
+/**
+ * Task run to notify application that ARM is being started.
+ *
+ * @param cls the operation that asked ARM to be started
+ */
+static void
+notify_starting (void *cls)
+{
+ struct GNUNET_ARM_Operation *op = cls;
+ struct GNUNET_ARM_Handle *h = op->h;
+
+ op->async = NULL;
LOG (GNUNET_ERROR_TYPE_DEBUG,
- "Inserting a control message into the queue. Timeout is %llu\n",
- GNUNET_TIME_absolute_get_remaining (cm->timeout).rel_value);
- GNUNET_CONTAINER_DLL_insert_tail (h->control_pending_head,
- h->control_pending_tail, cm);
- cm->timeout_task_id =
- GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_absolute_get_remaining
- (cm->timeout), &control_message_timeout, cm);
- trigger_next_request (h, GNUNET_NO);
+ "Notifying client that we started the ARM service\n");
+ GNUNET_CONTAINER_DLL_remove (h->operation_pending_head,
+ h->operation_pending_tail,
+ op);
+ if (NULL != op->result_cont)
+ op->result_cont (op->cont_cls,
+ GNUNET_ARM_REQUEST_SENT_OK,
+ op->starting_ret);
+ GNUNET_free (op);
}
* @param h handle to ARM
* @param service_name name of the service
* @param std_inheritance inheritance of std streams
- * @param timeout how long to wait before failing for good
* @param cont callback to invoke after request is sent or not sent
- * @param cont_cls closure for callback
+ * @param cont_cls closure for @a cont
+ * @return handle for the operation, NULL on error
*/
-void
+struct GNUNET_ARM_Operation *
GNUNET_ARM_request_service_start (struct GNUNET_ARM_Handle *h,
- const char *service_name, enum GNUNET_OS_InheritStdioFlags std_inheritance,
- struct GNUNET_TIME_Relative timeout, GNUNET_ARM_ResultCallback cont,
- void *cont_cls)
+ const char *service_name,
+ enum GNUNET_OS_InheritStdioFlags std_inheritance,
+ GNUNET_ARM_ResultCallback cont,
+ void *cont_cls)
{
- struct ARMControlMessage *cm;
- size_t slen;
+ struct GNUNET_ARM_Operation *op;
+ enum GNUNET_ARM_Result ret;
LOG (GNUNET_ERROR_TYPE_DEBUG,
- "Asked to start service `%s' within %s\n", service_name,
- GNUNET_STRINGS_relative_time_to_string (timeout, GNUNET_NO));
- if (0 == strcasecmp ("arm", service_name))
+ "Starting service `%s'\n",
+ service_name);
+ if (0 != strcasecmp ("arm",
+ service_name))
+ return change_service (h,
+ service_name,
+ cont,
+ cont_cls,
+ GNUNET_MESSAGE_TYPE_ARM_START);
+
+ /* Possible cases:
+ * 1) We're connected to ARM already. Invoke the callback immediately.
+ * 2) We're not connected to ARM.
+ * Cancel any reconnection attempts temporarily, then perform
+ * a service test.
+ */
+ if (GNUNET_YES == h->currently_up)
{
- /* Possible cases:
- * 1) We're connected to ARM already. Invoke the callback immediately.
- * 2) We're not connected to ARM.
- * Cancel any reconnection attempts temporarily, then perform
- * a service test.
- */
- if (GNUNET_NO == h->currently_down)
- {
- LOG (GNUNET_ERROR_TYPE_DEBUG, "ARM is already running\n");
- if (NULL != cont)
- cont (cont_cls, h, GNUNET_ARM_REQUEST_SENT_OK, "arm", GNUNET_ARM_RESULT_IS_STARTED_ALREADY);
- }
- else if (GNUNET_NO == h->service_test_is_active)
- {
- if (NULL != h->cth)
- {
- GNUNET_CLIENT_notify_transmit_ready_cancel (h->cth);
- h->cth = NULL;
- }
- if (NULL != h->client)
- {
- GNUNET_CLIENT_disconnect (h->client);
- h->client = NULL;
- }
- if (GNUNET_SCHEDULER_NO_TASK != h->reconnect_task)
- {
- GNUNET_SCHEDULER_cancel (h->reconnect_task);
- h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
- }
-
- LOG (GNUNET_ERROR_TYPE_DEBUG,
- "Not connected to ARM, will do a service test\n");
-
- slen = strlen ("arm") + 1;
- cm = GNUNET_malloc (sizeof (struct ARMControlMessage) + slen);
- cm->h = h;
- cm->result_cont = cont;
- cm->cont_cls = cont_cls;
- cm->timeout = GNUNET_TIME_relative_to_absolute (timeout);
- cm->std_inheritance = std_inheritance;
- memcpy (&cm[1], service_name, slen);
- h->service_test_is_active = GNUNET_YES;
- GNUNET_CLIENT_service_test ("arm", h->cfg, timeout, &arm_service_report,
- cm);
- }
- else
- {
- /* Service test is already running - tell user to chill out and try
- * again later.
- */
- LOG (GNUNET_ERROR_TYPE_DEBUG, "Service test is already in progress, we're busy\n");
- if (NULL != cont)
- cont (cont_cls, h, GNUNET_ARM_REQUEST_BUSY, NULL, 0);
- }
- return;
+ LOG (GNUNET_ERROR_TYPE_DEBUG,
+ "ARM is already running\n");
+ op = GNUNET_new (struct GNUNET_ARM_Operation);
+ op->h = h;
+ op->result_cont = cont;
+ op->cont_cls = cont_cls;
+ GNUNET_CONTAINER_DLL_insert_tail (h->operation_pending_head,
+ h->operation_pending_tail,
+ op);
+ op->async = GNUNET_SCHEDULER_add_now (¬ify_running,
+ op);
+ return op;
}
- change_service (h, service_name, timeout, cont, cont_cls,
- GNUNET_MESSAGE_TYPE_ARM_START);
+ /* This is an inherently uncertain choice, as it is of course
+ theoretically possible that ARM is up and we just did not
+ yet complete the MQ handshake. However, given that users
+ are unlikely to hammer 'gnunet-arm -s' on a busy system,
+ the above check should catch 99.99% of the cases where ARM
+ is already running. */
+ LOG (GNUNET_ERROR_TYPE_DEBUG,
+ "Starting ARM service\n");
+ ret = start_arm_service (h,
+ std_inheritance);
+ if (GNUNET_ARM_RESULT_STARTING == ret)
+ reconnect_arm (h);
+ op = GNUNET_new (struct GNUNET_ARM_Operation);
+ op->h = h;
+ op->result_cont = cont;
+ op->cont_cls = cont_cls;
+ GNUNET_CONTAINER_DLL_insert_tail (h->operation_pending_head,
+ h->operation_pending_tail,
+ op);
+ op->starting_ret = ret;
+ op->async = GNUNET_SCHEDULER_add_now (¬ify_starting,
+ op);
+ return op;
}
/**
- * Request a service to be stopped.
- * Stopping arm itself will not invalidate its handle, and
- * ARM API will try to restore connection to the ARM service,
- * even if ARM connection was lost because you asked for ARM to be stopped.
- * Call GNUNET_ARM_disconnect_and_free () to free the handle and prevent
+ * Request a service to be stopped. Stopping arm itself will not
+ * invalidate its handle, and ARM API will try to restore connection
+ * to the ARM service, even if ARM connection was lost because you
+ * asked for ARM to be stopped. Call
+ * #GNUNET_ARM_disconnect() to free the handle and prevent
* further connection attempts.
*
* @param h handle to ARM
* @param service_name name of the service
- * @param timeout how long to wait before failing for good
* @param cont callback to invoke after request is sent or is not sent
- * @param cont_cls closure for callback
+ * @param cont_cls closure for @a cont
+ * @return handle for the operation, NULL on error
*/
-void
+struct GNUNET_ARM_Operation *
GNUNET_ARM_request_service_stop (struct GNUNET_ARM_Handle *h,
- const char *service_name, struct GNUNET_TIME_Relative timeout,
- GNUNET_ARM_ResultCallback cont, void *cont_cls)
+ const char *service_name,
+ GNUNET_ARM_ResultCallback cont,
+ void *cont_cls)
{
- LOG (GNUNET_ERROR_TYPE_DEBUG,
- "Stopping service `%s' within %s\n",
- service_name,
- GNUNET_STRINGS_relative_time_to_string (timeout, GNUNET_NO));
- change_service (h, service_name, timeout, cont, cont_cls,
- GNUNET_MESSAGE_TYPE_ARM_STOP);
+ struct GNUNET_ARM_Operation *op;
+
+ LOG (GNUNET_ERROR_TYPE_DEBUG,
+ "Stopping service `%s'\n",
+ service_name);
+ op = change_service (h,
+ service_name,
+ cont,
+ cont_cls,
+ GNUNET_MESSAGE_TYPE_ARM_STOP);
+ if (NULL == op)
+ return NULL;
+ /* If the service is ARM, set a flag as we will use MQ errors
+ to detect that the process is really gone. */
+ if (0 == strcasecmp (service_name,
+ "arm"))
+ op->is_arm_stop = GNUNET_YES;
+ return op;
}
* Request a list of running services.
*
* @param h handle to ARM
- * @param timeout how long to wait before failing for good
* @param cont callback to invoke after request is sent or is not sent
- * @param cont_cls closure for callback
+ * @param cont_cls closure for @a cont
+ * @return handle for the operation, NULL on error
*/
-void
+struct GNUNET_ARM_Operation *
GNUNET_ARM_request_service_list (struct GNUNET_ARM_Handle *h,
- struct GNUNET_TIME_Relative timeout,
- GNUNET_ARM_ServiceListCallback cont, void *cont_cls)
+ GNUNET_ARM_ServiceListCallback cont,
+ void *cont_cls)
{
- struct ARMControlMessage *cm;
+ struct GNUNET_ARM_Operation *op;
+ struct GNUNET_MQ_Envelope *env;
struct GNUNET_ARM_Message *msg;
- LOG (GNUNET_ERROR_TYPE_DEBUG,
- "Requesting LIST from ARM service with timeout: %s\n",
- GNUNET_STRINGS_relative_time_to_string (timeout, GNUNET_YES));
-
- cm = GNUNET_malloc (sizeof (struct ARMControlMessage));
- cm->h = h;
- cm->list_cont = cont;
- cm->cont_cls = cont_cls;
- cm->timeout = GNUNET_TIME_relative_to_absolute (timeout);
- msg = GNUNET_malloc (sizeof (struct GNUNET_ARM_Message));
- msg->header.size = htons (sizeof (struct GNUNET_ARM_Message));
- msg->header.type = htons (GNUNET_MESSAGE_TYPE_ARM_LIST);
- cm->msg = msg;
- GNUNET_CONTAINER_DLL_insert_tail (h->control_pending_head,
- h->control_pending_tail, cm);
- cm->timeout_task_id =
- GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_absolute_get_remaining
- (cm->timeout), &control_message_timeout, cm);
- trigger_next_request (h, GNUNET_NO);
-}
-
-static struct ARMControlMessage *
-find_cm_by_id (struct GNUNET_ARM_Handle *h, uint64_t id)
-{
- struct ARMControlMessage *result;
- for (result = h->control_sent_head; result; result = result->next)
- if (id == result->msg->request_id)
- return result;
- return NULL;
-}
-
-
-/**
- * Handler for ARM replies.
- *
- * @param cls our "struct GNUNET_ARM_Handle"
- * @param msg the message received from the arm service
- */
-static void
-client_notify_handler (void *cls, const struct GNUNET_MessageHeader *msg)
-{
- struct GNUNET_ARM_Handle *h = cls;
-
- uint16_t msize;
- uint64_t id;
- unsigned char fail;
-
- const struct GNUNET_ARM_Message *arm_msg;
- const struct GNUNET_ARM_ResultMessage *res;
- const struct GNUNET_ARM_ListResultMessage *lres;
- enum GNUNET_ARM_Result result;
- struct ARMControlMessage *cm;
-
- const char *pos;
- uint16_t size_check;
- uint16_t rcount;
-
- if (NULL == msg)
- {
- LOG (GNUNET_ERROR_TYPE_INFO,
- _("Client was disconnected from arm service, trying to reconnect.\n"));
- reconnect_arm_later (h);
- return;
- }
- msize = ntohs (msg->size);
LOG (GNUNET_ERROR_TYPE_DEBUG,
- "Processing message of type %u and size %u from arm service\n",
- ntohs (msg->type), msize);
- if (msize < sizeof (struct GNUNET_ARM_Message))
- {
- GNUNET_break (0);
- reconnect_arm_later (h);
- return;
- }
- arm_msg = (const struct GNUNET_ARM_Message *) msg;
- id = GNUNET_ntohll (arm_msg->request_id);
- cm = find_cm_by_id (h, id);
- if (NULL == cm)
- {
- LOG (GNUNET_ERROR_TYPE_DEBUG, "Message with unknown id %llu\n", id);
- return;
- }
-
- fail = GNUNET_NO;
- switch (ntohs (msg->type))
- {
- case GNUNET_MESSAGE_TYPE_ARM_RESULT:
- if (msize < sizeof (struct GNUNET_ARM_ResultMessage))
- {
- GNUNET_assert (0);
- fail = GNUNET_YES;
- break;
- }
- res = (const struct GNUNET_ARM_ResultMessage *) msg;
- LOG (GNUNET_ERROR_TYPE_DEBUG,
- "Received response from ARM for service `%s': %u\n",
- (const char *) &cm->msg[1], ntohs (msg->type));
- result = (enum GNUNET_ARM_Result) ntohl (res->result);
- if (NULL != cm->result_cont)
- cm->result_cont (cm->cont_cls, h, GNUNET_ARM_REQUEST_SENT_OK, (const char *) &cm->msg[1], result);
- break;
- case GNUNET_MESSAGE_TYPE_ARM_LIST_RESULT:
- if (msize < sizeof (struct GNUNET_ARM_ListResultMessage))
- {
- GNUNET_break (0);
- fail = GNUNET_YES;
- return;
- }
- else
- {
- size_check = 0;
- lres = (const struct GNUNET_ARM_ListResultMessage *) msg;
- rcount = ntohs (lres->count);
- {
- const char *list[rcount];
- unsigned int i;
-
- pos = (const char *)&lres[1];
- for (i = 0; i < rcount; i++)
- {
- const char *end = memchr (pos, 0, msize - size_check);
- if (NULL == end)
- {
- GNUNET_break (0);
- fail = GNUNET_YES;
- break;
- }
- list[i] = pos;
- size_check += (end - pos) + 1;
- pos = end + 1;
- }
- if (GNUNET_YES == fail)
- break;
- if (NULL != cm->list_cont)
- cm->list_cont (cm->cont_cls, h, GNUNET_ARM_REQUEST_SENT_OK, rcount, list);
- }
- }
- break;
- default:
- fail = GNUNET_YES;
- return;
- }
-
- GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != cm->timeout_task_id);
- GNUNET_SCHEDULER_cancel (cm->timeout_task_id);
- GNUNET_CONTAINER_DLL_remove (h->control_sent_head,
- h->control_sent_tail, cm);
- GNUNET_free (cm->msg);
- GNUNET_free (cm);
-
- if (GNUNET_YES == fail)
- reconnect_arm_later (h);
- else
- GNUNET_CLIENT_receive (h->client, &client_notify_handler, h,
- GNUNET_TIME_UNIT_FOREVER_REL);
+ "Requesting LIST from ARM service\n");
+ if (0 == h->request_id_counter)
+ h->request_id_counter++;
+ op = GNUNET_new (struct GNUNET_ARM_Operation);
+ op->h = h;
+ op->list_cont = cont;
+ op->cont_cls = cont_cls;
+ op->id = h->request_id_counter++;
+ GNUNET_CONTAINER_DLL_insert_tail (h->operation_pending_head,
+ h->operation_pending_tail,
+ op);
+ env = GNUNET_MQ_msg (msg,
+ GNUNET_MESSAGE_TYPE_ARM_LIST);
+ msg->reserved = htonl (0);
+ msg->request_id = GNUNET_htonll (op->id);
+ GNUNET_MQ_send (h->mq,
+ env);
+ return op;
}
+
/* end of arm_api.c */