2 This file is part of GNUnet.
3 Copyright (C) 2009, 2010, 2012, 2013, 2016 GNUnet e.V.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
17 * @file arm/arm_monitor_api.c
18 * @brief API for monitoring the ARM service
19 * @author Christian Grothoff
23 #include "gnunet_arm_service.h"
24 #include "gnunet_util_lib.h"
25 #include "gnunet_protocols.h"
28 #define INIT_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
30 #define LOG(kind,...) GNUNET_log_from (kind, "arm-monitor-api",__VA_ARGS__)
33 * Handle for interacting with ARM.
35 struct GNUNET_ARM_MonitorHandle
39 * Our control connection to the ARM service.
41 struct GNUNET_MQ_Handle *mq;
44 * The configuration that we are using.
46 const struct GNUNET_CONFIGURATION_Handle *cfg;
49 * ID of the reconnect task (if any).
51 struct GNUNET_SCHEDULER_Task *reconnect_task;
54 * Current delay we use for re-trying to connect to core.
56 struct GNUNET_TIME_Relative retry_backoff;
59 * Callback to invoke on status updates.
61 GNUNET_ARM_ServiceStatusCallback service_status;
64 * Closure for @e service_status.
66 void *service_status_cls;
72 * Connect to the ARM service for monitoring.
74 * @param h handle to connect
75 * @return #GNUNET_OK on success
78 reconnect_arm_monitor (struct GNUNET_ARM_MonitorHandle *h);
82 * Task scheduled to try to re-connect to arm.
84 * @param cls the `struct GNUNET_ARM_MonitorHandle`
87 reconnect_arm_monitor_task (void *cls)
89 struct GNUNET_ARM_MonitorHandle *h = cls;
91 h->reconnect_task = NULL;
92 LOG (GNUNET_ERROR_TYPE_DEBUG,
93 "Connecting to ARM service for monitoring after delay\n");
94 GNUNET_break (GNUNET_OK == reconnect_arm_monitor (h));
99 * Close down any existing connection to the ARM service and
100 * try re-establishing it later.
102 * @param h our handle
105 reconnect_arm_monitor_later (struct GNUNET_ARM_MonitorHandle *h)
109 GNUNET_MQ_destroy (h->mq);
112 GNUNET_assert (NULL == h->reconnect_task);
114 = GNUNET_SCHEDULER_add_delayed (h->retry_backoff,
115 &reconnect_arm_monitor_task, h);
116 h->retry_backoff = GNUNET_TIME_STD_BACKOFF (h->retry_backoff);
121 * Check notification messages received from ARM is well-formed.
123 * @param cls our `struct GNUNET_ARM_MonitorHandle`
124 * @param msg the message received from the arm service
125 * @return #GNUNET_OK if the message is well-formed
128 check_monitor_notify (void *cls,
129 const struct GNUNET_ARM_StatusMessage *msg)
131 size_t sl = ntohs (msg->header.size) - sizeof (struct GNUNET_ARM_StatusMessage);
132 const char *name = (const char *) &msg[1];
135 ('\0' != name[sl-1]) )
138 return GNUNET_SYSERR;
145 * Handler for notification messages received from ARM.
147 * @param cls our `struct GNUNET_ARM_MonitorHandle`
148 * @param res the message received from the arm service
151 handle_monitor_notify (void *cls,
152 const struct GNUNET_ARM_StatusMessage *res)
154 struct GNUNET_ARM_MonitorHandle *h = cls;
155 enum GNUNET_ARM_ServiceStatus status;
157 status = (enum GNUNET_ARM_ServiceStatus) ntohl (res->status);
158 LOG (GNUNET_ERROR_TYPE_DEBUG,
159 "Received notification from ARM for service `%s' with status %d\n",
160 (const char *) &res[1],
162 if (NULL != h->service_status)
163 h->service_status (h->service_status_cls,
164 (const char *) &res[1],
170 * Generic error handler, called with the appropriate error code and
171 * the same closure specified at the creation of the message queue.
172 * Not every message queue implementation supports an error handler.
174 * @param cls closure with the `struct GNUNET_ARM_MonitorHandle *`
175 * @param error error code
178 mq_error_handler (void *cls,
179 enum GNUNET_MQ_Error error)
181 struct GNUNET_ARM_MonitorHandle *h = cls;
183 reconnect_arm_monitor_later (h);
188 * Connect to the ARM service for monitoring.
190 * @param h handle to connect
191 * @return #GNUNET_OK on success
194 reconnect_arm_monitor (struct GNUNET_ARM_MonitorHandle *h)
196 struct GNUNET_MQ_MessageHandler handlers[] = {
197 GNUNET_MQ_hd_var_size (monitor_notify,
198 GNUNET_MESSAGE_TYPE_ARM_STATUS,
199 struct GNUNET_ARM_StatusMessage,
201 GNUNET_MQ_handler_end ()
203 struct GNUNET_MessageHeader *msg;
204 struct GNUNET_MQ_Envelope *env;
206 GNUNET_assert (NULL == h->mq);
207 h->mq = GNUNET_CLIENT_connect (h->cfg,
214 if (NULL != h->service_status)
215 h->service_status (h->service_status_cls,
217 GNUNET_ARM_SERVICE_STOPPED);
218 return GNUNET_SYSERR;
220 env = GNUNET_MQ_msg (msg,
221 GNUNET_MESSAGE_TYPE_ARM_MONITOR);
222 GNUNET_MQ_send (h->mq,
229 * Setup a context for monitoring ARM, then
230 * start connecting to the ARM service for monitoring using that context.
232 * @param cfg configuration to use (needed to contact ARM;
233 * the ARM service may internally use a different
234 * configuration to determine how to start the service).
235 * @param cont callback to invoke on status updates
236 * @param cont_cls closure for @a cont
237 * @return context to use for further ARM monitor operations, NULL on error.
239 struct GNUNET_ARM_MonitorHandle *
240 GNUNET_ARM_monitor_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
241 GNUNET_ARM_ServiceStatusCallback cont,
244 struct GNUNET_ARM_MonitorHandle *h;
246 h = GNUNET_new (struct GNUNET_ARM_MonitorHandle);
248 h->service_status = cont;
249 h->service_status_cls = cont_cls;
250 if (GNUNET_OK != reconnect_arm_monitor (h))
260 * Disconnect from the ARM service (if connected) and destroy the context.
262 * @param h the handle that was being used
265 GNUNET_ARM_monitor_stop (struct GNUNET_ARM_MonitorHandle *h)
269 GNUNET_MQ_destroy (h->mq);
272 if (NULL != h->reconnect_task)
274 GNUNET_SCHEDULER_cancel (h->reconnect_task);
275 h->reconnect_task = NULL;
281 /* end of arm_api.c */