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.
22 * @file arm/arm_monitor_api.c
23 * @brief API for monitoring 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 INIT_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
34 #define LOG(kind,...) GNUNET_log_from (kind, "arm-monitor-api",__VA_ARGS__)
37 * Handle for interacting with ARM.
39 struct GNUNET_ARM_MonitorHandle
43 * Our control connection to the ARM service.
45 struct GNUNET_CLIENT_Connection *monitor;
48 * The configuration that we are using.
50 struct GNUNET_CONFIGURATION_Handle *cfg;
53 * Handle for our current transmission request.
55 struct GNUNET_CLIENT_TransmitHandle *cth;
58 * ID of the reconnect task (if any).
60 GNUNET_SCHEDULER_TaskIdentifier reconnect_task;
63 * Current delay we use for re-trying to connect to core.
65 struct GNUNET_TIME_Relative retry_backoff;
68 * Are we currently disconnected and hence unable to send?
70 unsigned char currently_down;
73 * Callback to invoke on status updates.
75 GNUNET_ARM_ServiceStatusCallback service_status;
78 * Closure for service_status.
83 * ID of a task to run if we fail to get a reply to the init message in time.
85 GNUNET_SCHEDULER_TaskIdentifier init_timeout_task_id;
89 monitor_notify_handler (void *cls, const struct GNUNET_MessageHeader *msg);
92 reconnect_arm_monitor (struct GNUNET_ARM_MonitorHandle *h);
95 * Task scheduled to try to re-connect to arm.
97 * @param cls the 'struct GNUNET_ARM_MonitorHandle'
98 * @param tc task context
101 reconnect_arm_monitor_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
103 struct GNUNET_ARM_MonitorHandle *h = cls;
105 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
106 LOG (GNUNET_ERROR_TYPE_DEBUG, "Connecting to ARM service for monitoring after delay\n");
107 reconnect_arm_monitor (h);
112 * Close down any existing connection to the ARM service and
113 * try re-establishing it later.
115 * @param h our handle
118 reconnect_arm_monitor_later (struct GNUNET_ARM_MonitorHandle *h)
122 GNUNET_CLIENT_notify_transmit_ready_cancel (h->cth);
126 if (NULL != h->monitor)
128 GNUNET_CLIENT_disconnect (h->monitor);
132 if (GNUNET_SCHEDULER_NO_TASK != h->init_timeout_task_id)
134 GNUNET_SCHEDULER_cancel (h->init_timeout_task_id);
135 h->init_timeout_task_id = GNUNET_SCHEDULER_NO_TASK;
138 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == h->reconnect_task);
140 GNUNET_SCHEDULER_add_delayed (h->retry_backoff, &reconnect_arm_monitor_task, h);
142 h->retry_backoff = GNUNET_TIME_STD_BACKOFF (h->retry_backoff);
147 * Init message timed out. Disconnect and try again.
149 * @param cls arm monitor handle
150 * @param tc task context
153 init_timeout_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
155 struct GNUNET_ARM_MonitorHandle *h = cls;
156 LOG (GNUNET_ERROR_TYPE_DEBUG,
157 "Init message timed out\n");
159 h->init_timeout_task_id = GNUNET_SCHEDULER_NO_TASK;
160 reconnect_arm_monitor_later (h);
165 * Transmit the monitoring initialization message to the arm service.
167 * @param cls closure with the 'struct GNUNET_ARM_MonitorHandle'
168 * @param size number of bytes available in buf
169 * @param buf where the callee should write the message
170 * @return number of bytes written to buf
173 transmit_monitoring_init_message (void *cls, size_t size, void *buf)
175 struct GNUNET_ARM_MonitorHandle *h = cls;
176 struct GNUNET_MessageHeader *msg;
179 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == h->reconnect_task);
180 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == h->init_timeout_task_id);
184 LOG (GNUNET_ERROR_TYPE_DEBUG,
185 "Transmission failed, initiating reconnect\n");
186 reconnect_arm_monitor_later (h);
189 msize = sizeof (struct GNUNET_MessageHeader);
192 LOG (GNUNET_ERROR_TYPE_DEBUG,
193 "Request is too big (%u < %u), not sending it\n", size, msize);
194 h->cth = GNUNET_CLIENT_notify_transmit_ready (h->monitor, msize,
195 GNUNET_TIME_UNIT_FOREVER_REL, GNUNET_NO,
196 transmit_monitoring_init_message, h);
201 msg->size = htons (msize);
202 msg->type = htons (GNUNET_MESSAGE_TYPE_ARM_MONITOR);
203 LOG (GNUNET_ERROR_TYPE_DEBUG,
204 "Transmitting ARM monitoring init message with %u bytes to arm.\n",
205 (unsigned int) msize);
207 h->init_timeout_task_id = GNUNET_SCHEDULER_add_delayed (
208 INIT_TIMEOUT, init_timeout_task, h);
209 GNUNET_CLIENT_receive (h->monitor, &monitor_notify_handler, h,
210 GNUNET_TIME_UNIT_FOREVER_REL);
216 reconnect_arm_monitor (struct GNUNET_ARM_MonitorHandle *h)
218 GNUNET_assert (NULL == h->monitor);
219 h->monitor = GNUNET_CLIENT_connect ("arm", h->cfg);
220 if (NULL == h->monitor)
222 LOG (GNUNET_ERROR_TYPE_DEBUG,
223 "arm_api, GNUNET_CLIENT_connect returned NULL\n");
224 if (NULL != h->service_status)
225 h->service_status (h->cls, NULL, GNUNET_ARM_SERVICE_STOPPED);
226 return GNUNET_SYSERR;
228 LOG (GNUNET_ERROR_TYPE_DEBUG,
229 "arm_api, GNUNET_CLIENT_connect returned non-NULL\n");
230 h->cth = GNUNET_CLIENT_notify_transmit_ready (h->monitor,
231 sizeof (struct GNUNET_MessageHeader), GNUNET_TIME_UNIT_FOREVER_REL,
232 GNUNET_NO, &transmit_monitoring_init_message, h);
238 * Setup a context for monitoring ARM, then
239 * start connecting to the ARM service for monitoring using that context.
241 * @param cfg configuration to use (needed to contact ARM;
242 * the ARM service may internally use a different
243 * configuration to determine how to start the service).
244 * @param cont callback to invoke on status updates
245 * @param cont_cls closure
246 * @return context to use for further ARM monitor operations, NULL on error.
248 struct GNUNET_ARM_MonitorHandle *
249 GNUNET_ARM_monitor (const struct GNUNET_CONFIGURATION_Handle *cfg,
250 GNUNET_ARM_ServiceStatusCallback cont, void *cont_cls)
252 struct GNUNET_ARM_MonitorHandle *h;
254 h = GNUNET_new (struct GNUNET_ARM_MonitorHandle);
255 h->cfg = GNUNET_CONFIGURATION_dup (cfg);
256 h->currently_down = GNUNET_YES;
257 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
258 h->init_timeout_task_id = GNUNET_SCHEDULER_NO_TASK;
259 h->service_status = cont;
261 if (GNUNET_OK != reconnect_arm_monitor (h))
271 * Disconnect from the ARM service (if connected) and destroy the context.
273 * @param h the handle that was being used
276 GNUNET_ARM_monitor_disconnect_and_free (struct GNUNET_ARM_MonitorHandle *h)
278 LOG (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting from ARM service\n");
281 GNUNET_CLIENT_notify_transmit_ready_cancel (h->cth);
284 if (GNUNET_SCHEDULER_NO_TASK != h->init_timeout_task_id)
286 GNUNET_SCHEDULER_cancel (h->init_timeout_task_id);
287 h->init_timeout_task_id = GNUNET_SCHEDULER_NO_TASK;
289 if (NULL != h->monitor)
291 GNUNET_CLIENT_disconnect (h->monitor);
294 if (GNUNET_SCHEDULER_NO_TASK != h->reconnect_task)
296 GNUNET_SCHEDULER_cancel (h->reconnect_task);
297 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
299 GNUNET_CONFIGURATION_destroy (h->cfg);
305 * Handler for notification messages received from ARM.
307 * @param cls our "struct GNUNET_ARM_MonitorHandle"
308 * @param msg the message received from the arm service
311 monitor_notify_handler (void *cls, const struct GNUNET_MessageHeader *msg)
313 struct GNUNET_ARM_MonitorHandle *h = cls;
315 const struct GNUNET_ARM_StatusMessage *res;
316 enum GNUNET_ARM_ServiceStatus status;
320 LOG (GNUNET_ERROR_TYPE_INFO,
321 _("Monitoring client was disconnected from arm service, trying to reconnect.\n"));
322 reconnect_arm_monitor_later (h);
325 msize = ntohs (msg->size);
326 LOG (GNUNET_ERROR_TYPE_DEBUG,
327 "Processing message of type %u and size %u from arm service\n",
328 ntohs (msg->type), msize);
329 switch (ntohs (msg->type))
331 case GNUNET_MESSAGE_TYPE_ARM_STATUS:
332 if (msize <= sizeof (struct GNUNET_ARM_StatusMessage))
335 reconnect_arm_monitor_later (h);
338 if (GNUNET_SCHEDULER_NO_TASK != h->init_timeout_task_id)
340 GNUNET_SCHEDULER_cancel (h->init_timeout_task_id);
341 h->init_timeout_task_id = GNUNET_SCHEDULER_NO_TASK;
343 res = (const struct GNUNET_ARM_StatusMessage *) msg;
344 LOG (GNUNET_ERROR_TYPE_DEBUG,
345 "Received response from ARM for service `%s': %u\n",
346 (const char *) &res[1], ntohs (msg->type));
347 status = (enum GNUNET_ARM_ServiceStatus) ntohl (res->status);
348 GNUNET_CLIENT_receive (h->monitor, &monitor_notify_handler, h,
349 GNUNET_TIME_UNIT_FOREVER_REL);
350 if (NULL != h->service_status)
351 h->service_status (h->cls, (const char *) &res[1], status);
354 reconnect_arm_monitor_later (h);
360 /* end of arm_api.c */