first batch of license fixes (boring)
[oweals/gnunet.git] / src / arm / arm_monitor_api.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2009, 2010, 2012, 2013, 2016 GNUnet e.V.
4
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.
9
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.
14 */
15
16 /**
17  * @file arm/arm_monitor_api.c
18  * @brief API for monitoring the ARM service
19  * @author Christian Grothoff
20  * @author LRN
21  */
22 #include "platform.h"
23 #include "gnunet_arm_service.h"
24 #include "gnunet_util_lib.h"
25 #include "gnunet_protocols.h"
26 #include "arm.h"
27
28 #define INIT_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
29
30 #define LOG(kind,...) GNUNET_log_from (kind, "arm-monitor-api",__VA_ARGS__)
31
32 /**
33  * Handle for interacting with ARM.
34  */
35 struct GNUNET_ARM_MonitorHandle
36 {
37
38   /**
39    * Our control connection to the ARM service.
40    */
41   struct GNUNET_MQ_Handle *mq;
42
43   /**
44    * The configuration that we are using.
45    */
46   const struct GNUNET_CONFIGURATION_Handle *cfg;
47
48   /**
49    * ID of the reconnect task (if any).
50    */
51   struct GNUNET_SCHEDULER_Task *reconnect_task;
52
53   /**
54    * Current delay we use for re-trying to connect to core.
55    */
56   struct GNUNET_TIME_Relative retry_backoff;
57
58   /**
59    * Callback to invoke on status updates.
60    */
61   GNUNET_ARM_ServiceStatusCallback service_status;
62
63   /**
64    * Closure for @e service_status.
65    */
66   void *service_status_cls;
67
68 };
69
70
71 /**
72  * Connect to the ARM service for monitoring.
73  *
74  * @param h handle to connect
75  * @return #GNUNET_OK on success
76  */
77 static int
78 reconnect_arm_monitor (struct GNUNET_ARM_MonitorHandle *h);
79
80
81 /**
82  * Task scheduled to try to re-connect to arm.
83  *
84  * @param cls the `struct GNUNET_ARM_MonitorHandle`
85  */
86 static void
87 reconnect_arm_monitor_task (void *cls)
88 {
89   struct GNUNET_ARM_MonitorHandle *h = cls;
90
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));
95 }
96
97
98 /**
99  * Close down any existing connection to the ARM service and
100  * try re-establishing it later.
101  *
102  * @param h our handle
103  */
104 static void
105 reconnect_arm_monitor_later (struct GNUNET_ARM_MonitorHandle *h)
106 {
107   if (NULL != h->mq)
108   {
109     GNUNET_MQ_destroy (h->mq);
110     h->mq = NULL;
111   }
112   GNUNET_assert (NULL == h->reconnect_task);
113   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);
117 }
118
119
120 /**
121  * Check notification messages received from ARM is well-formed.
122  *
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
126  */
127 static int
128 check_monitor_notify (void *cls,
129                        const struct GNUNET_ARM_StatusMessage *msg)
130 {
131   size_t sl = ntohs (msg->header.size) - sizeof (struct GNUNET_ARM_StatusMessage);
132   const char *name = (const char *) &msg[1];
133
134   if ( (0 == sl) ||
135        ('\0' != name[sl-1]) )
136   {
137     GNUNET_break (0);
138     return GNUNET_SYSERR;
139   }
140   return GNUNET_OK;
141 }
142
143
144 /**
145  * Handler for notification messages received from ARM.
146  *
147  * @param cls our `struct GNUNET_ARM_MonitorHandle`
148  * @param res the message received from the arm service
149  */
150 static void
151 handle_monitor_notify (void *cls,
152                        const struct GNUNET_ARM_StatusMessage *res)
153 {
154   struct GNUNET_ARM_MonitorHandle *h = cls;
155   enum GNUNET_ARM_ServiceStatus status;
156
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],
161        (int) status);
162   if (NULL != h->service_status)
163     h->service_status (h->service_status_cls,
164                        (const char *) &res[1],
165                        status);
166 }
167
168
169 /**
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.
173  *
174  * @param cls closure with the `struct GNUNET_ARM_MonitorHandle *`
175  * @param error error code
176  */
177 static void
178 mq_error_handler (void *cls,
179                   enum GNUNET_MQ_Error error)
180 {
181   struct GNUNET_ARM_MonitorHandle *h = cls;
182
183   reconnect_arm_monitor_later (h);
184 }
185
186
187 /**
188  * Connect to the ARM service for monitoring.
189  *
190  * @param h handle to connect
191  * @return #GNUNET_OK on success
192  */
193 static int
194 reconnect_arm_monitor (struct GNUNET_ARM_MonitorHandle *h)
195 {
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,
200                            h),
201     GNUNET_MQ_handler_end ()
202   };
203   struct GNUNET_MessageHeader *msg;
204   struct GNUNET_MQ_Envelope *env;
205
206   GNUNET_assert (NULL == h->mq);
207   h->mq = GNUNET_CLIENT_connect (h->cfg,
208                                  "arm",
209                                  handlers,
210                                  &mq_error_handler,
211                                  h);
212   if (NULL == h->mq)
213   {
214     if (NULL != h->service_status)
215       h->service_status (h->service_status_cls,
216                          NULL,
217                          GNUNET_ARM_SERVICE_STOPPED);
218     return GNUNET_SYSERR;
219   }
220   env = GNUNET_MQ_msg (msg,
221                        GNUNET_MESSAGE_TYPE_ARM_MONITOR);
222   GNUNET_MQ_send (h->mq,
223                   env);
224   return GNUNET_OK;
225 }
226
227
228 /**
229  * Setup a context for monitoring ARM, then
230  * start connecting to the ARM service for monitoring using that context.
231  *
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.
238  */
239 struct GNUNET_ARM_MonitorHandle *
240 GNUNET_ARM_monitor_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
241                           GNUNET_ARM_ServiceStatusCallback cont,
242                           void *cont_cls)
243 {
244   struct GNUNET_ARM_MonitorHandle *h;
245
246   h = GNUNET_new (struct GNUNET_ARM_MonitorHandle);
247   h->cfg = cfg;
248   h->service_status = cont;
249   h->service_status_cls = cont_cls;
250   if (GNUNET_OK != reconnect_arm_monitor (h))
251   {
252     GNUNET_free (h);
253     return NULL;
254   }
255   return h;
256 }
257
258
259 /**
260  * Disconnect from the ARM service (if connected) and destroy the context.
261  *
262  * @param h the handle that was being used
263  */
264 void
265 GNUNET_ARM_monitor_stop (struct GNUNET_ARM_MonitorHandle *h)
266 {
267   if (NULL != h->mq)
268   {
269     GNUNET_MQ_destroy (h->mq);
270     h->mq = NULL;
271   }
272   if (NULL != h->reconnect_task)
273   {
274     GNUNET_SCHEDULER_cancel (h->reconnect_task);
275     h->reconnect_task = NULL;
276   }
277   GNUNET_free (h);
278 }
279
280
281 /* end of arm_api.c */