indentation
[oweals/gnunet.git] / src / nse / nse_api.c
1 /*
2      This file is part of GNUnet.
3      (C) 2009, 2010, 2011 Christian Grothoff (and other contributing authors)
4
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.
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      General Public License for more details.
14
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.
19 */
20
21 /**
22  * @file nse/nse_api.c
23  * @brief api to get information from the network size estimation service
24  * @author Nathan Evans
25  */
26 #include "platform.h"
27 #include "gnunet_client_lib.h"
28 #include "gnunet_constants.h"
29 #include "gnunet_container_lib.h"
30 #include "gnunet_arm_service.h"
31 #include "gnunet_hello_lib.h"
32 #include "gnunet_protocols.h"
33 #include "gnunet_server_lib.h"
34 #include "gnunet_time_lib.h"
35 #include "gnunet_nse_service.h"
36 #include "nse.h"
37
38
39 /**
40  * Handle for the service.
41  */
42 struct GNUNET_NSE_Handle
43 {
44   /**
45    * Configuration to use.
46    */
47   const struct GNUNET_CONFIGURATION_Handle *cfg;
48
49   /**
50    * Socket (if available).
51    */
52   struct GNUNET_CLIENT_Connection *client;
53
54   /**
55    * Currently pending transmission request.
56    */
57   struct GNUNET_CLIENT_TransmitHandle *th;
58
59   /**
60    * Task doing exponential back-off trying to reconnect.
61    */
62   GNUNET_SCHEDULER_TaskIdentifier reconnect_task;
63
64   /**
65    * Time for next connect retry.
66    */
67   struct GNUNET_TIME_Relative reconnect_delay;
68
69   /**
70    * Callback function to call when message is received.
71    */
72   GNUNET_NSE_Callback recv_cb;
73
74   /**
75    * Closure to pass to callback.
76    */
77   void *recv_cb_cls;
78
79 };
80
81
82 /**
83  * Try again to connect to network size estimation service.
84  *
85  * @param cls the handle to the transport service
86  * @param tc scheduler context
87  */
88 static void
89 reconnect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
90
91
92 /**
93  * Type of a function to call when we receive a message
94  * from the service.
95  *
96  * @param cls closure
97  * @param msg message received, NULL on timeout or fatal error
98  */
99 static void
100 message_handler (void *cls, const struct GNUNET_MessageHeader *msg)
101 {
102   struct GNUNET_NSE_Handle *h = cls;
103   const struct GNUNET_NSE_ClientMessage *client_msg;
104
105   if (msg == NULL)
106   {
107     /* Error, timeout, death */
108     GNUNET_CLIENT_disconnect (h->client, GNUNET_NO);
109     h->client = NULL;
110     h->reconnect_task = GNUNET_SCHEDULER_add_delayed (h->reconnect_delay,
111                                                       &reconnect, h);
112     return;
113   }
114   if ((ntohs (msg->size) != sizeof (struct GNUNET_NSE_ClientMessage)) ||
115       (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_NSE_ESTIMATE))
116   {
117     GNUNET_break (0);
118     return;
119   }
120   client_msg = (const struct GNUNET_NSE_ClientMessage *) msg;
121   h->recv_cb (h->recv_cb_cls,
122               GNUNET_TIME_absolute_ntoh (client_msg->timestamp),
123               client_msg->size_estimate, client_msg->std_deviation);
124   GNUNET_CLIENT_receive (h->client,
125                          &message_handler, h, GNUNET_TIME_UNIT_FOREVER_REL);
126 }
127
128
129
130 /**
131  * Reschedule a connect attempt to the service.
132  *
133  * @param h transport service to reconnect
134  */
135 static void
136 reschedule_connect (struct GNUNET_NSE_Handle *h)
137 {
138   GNUNET_assert (h->reconnect_task == GNUNET_SCHEDULER_NO_TASK);
139
140   if (NULL != h->th)
141   {
142     GNUNET_CLIENT_notify_transmit_ready_cancel (h->th);
143     h->th = NULL;
144   }
145   if (NULL != h->client)
146   {
147     GNUNET_CLIENT_disconnect (h->client, GNUNET_NO);
148     h->client = NULL;
149   }
150
151 #if DEBUG_NSE
152   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
153               "Scheduling task to reconnect to nse service in %llu ms.\n",
154               h->reconnect_delay.rel_value);
155 #endif
156   h->reconnect_task
157       = GNUNET_SCHEDULER_add_delayed (h->reconnect_delay, &reconnect, h);
158   if (h->reconnect_delay.rel_value == 0)
159   {
160     h->reconnect_delay = GNUNET_TIME_UNIT_MILLISECONDS;
161   }
162   else
163   {
164     h->reconnect_delay = GNUNET_TIME_relative_multiply (h->reconnect_delay, 2);
165     h->reconnect_delay = GNUNET_TIME_relative_min (GNUNET_TIME_UNIT_SECONDS,
166                                                    h->reconnect_delay);
167   }
168 }
169
170
171 /**
172  * Transmit START message to service.
173  *
174  * @param cls unused
175  * @param size number of bytes available in buf
176  * @param buf where to copy the message
177  * @return number of bytes copied to buf
178  */
179 static size_t
180 send_start (void *cls, size_t size, void *buf)
181 {
182   struct GNUNET_NSE_Handle *h = cls;
183   struct GNUNET_MessageHeader *msg;
184
185   h->th = NULL;
186   if (buf == NULL)
187   {
188     /* Connect error... */
189 #if DEBUG_NSE
190     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
191                 "Shutdown while trying to transmit `%s' request.\n", "START");
192 #endif
193     reschedule_connect (h);
194     return 0;
195   }
196 #if DEBUG_NSE
197   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Transmitting `%s' request.\n", "START");
198 #endif
199   GNUNET_assert (size >= sizeof (struct GNUNET_MessageHeader));
200
201   msg = (struct GNUNET_MessageHeader *) buf;
202   msg->size = htons (sizeof (struct GNUNET_MessageHeader));
203   msg->type = htons (GNUNET_MESSAGE_TYPE_NSE_START);
204   GNUNET_CLIENT_receive (h->client,
205                          &message_handler, h, GNUNET_TIME_UNIT_FOREVER_REL);
206   return sizeof (struct GNUNET_MessageHeader);
207 }
208
209
210 /**
211  * Try again to connect to network size estimation service.
212  *
213  * @param cls the handle to the transport service
214  * @param tc scheduler context
215  */
216 static void
217 reconnect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
218 {
219   struct GNUNET_NSE_Handle *h = cls;
220
221   h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
222   if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
223   {
224     /* shutdown, just give up */
225     return;
226   }
227 #if DEBUG_NSE
228   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
229               "Connecting to network size estimation service.\n");
230 #endif
231   GNUNET_assert (h->client == NULL);
232   h->client = GNUNET_CLIENT_connect ("nse", h->cfg);
233   GNUNET_assert (h->client != NULL);
234
235   h->th =
236       GNUNET_CLIENT_notify_transmit_ready (h->client,
237                                            sizeof (struct GNUNET_MessageHeader),
238                                            GNUNET_TIME_UNIT_FOREVER_REL,
239                                            GNUNET_NO, &send_start, h);
240   GNUNET_assert (h->th != NULL);
241 }
242
243
244 /**
245  * Connect to the network size estimation service.
246  *
247  * @param cfg the configuration to use
248  * @param func funtion to call with network size estimate
249  * @param func_cls closure to pass for network size estimate callback
250  *
251  * @return handle to use
252  */
253 struct GNUNET_NSE_Handle *
254 GNUNET_NSE_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
255                     GNUNET_NSE_Callback func, void *func_cls)
256 {
257   struct GNUNET_NSE_Handle *ret;
258
259   GNUNET_assert (func != NULL);
260   ret = GNUNET_malloc (sizeof (struct GNUNET_NSE_Handle));
261   ret->cfg = cfg;
262   ret->recv_cb = func;
263   ret->recv_cb_cls = func_cls;
264   ret->reconnect_delay = GNUNET_TIME_UNIT_ZERO;
265   ret->reconnect_task = GNUNET_SCHEDULER_add_now (&reconnect, ret);
266   return ret;
267 }
268
269
270 /**
271  * Disconnect from network size estimation service
272  *
273  * @param h handle to destroy
274  */
275 void
276 GNUNET_NSE_disconnect (struct GNUNET_NSE_Handle *h)
277 {
278   GNUNET_assert (h != NULL);
279   if (h->reconnect_task != GNUNET_SCHEDULER_NO_TASK)
280   {
281     GNUNET_SCHEDULER_cancel (h->reconnect_task);
282     h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
283   }
284   if (h->th != NULL)
285   {
286     GNUNET_CLIENT_notify_transmit_ready_cancel (h->th);
287     h->th = NULL;
288   }
289   if (h->client != NULL)
290   {
291     GNUNET_CLIENT_disconnect (h->client, GNUNET_NO);
292     h->client = NULL;
293   }
294   GNUNET_free (h);
295 }
296
297 /* end of nse_api.c */