move to config instead of hard-coding for testing
[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,
90            const struct GNUNET_SCHEDULER_TaskContext *tc);
91
92
93 /**
94  * Type of a function to call when we receive a message
95  * from the service.
96  *
97  * @param cls closure
98  * @param msg message received, NULL on timeout or fatal error
99  */
100 static void 
101 message_handler (void *cls,
102                  const struct GNUNET_MessageHeader * msg)
103 {
104   struct GNUNET_NSE_Handle *h = cls;
105   const struct GNUNET_NSE_ClientMessage *client_msg;
106
107   if (msg == NULL)
108     {
109       /* Error, timeout, death */
110       GNUNET_CLIENT_disconnect (h->client, GNUNET_NO);
111       h->client = NULL;
112       h->reconnect_task = GNUNET_SCHEDULER_add_delayed (h->reconnect_delay,
113                                                         &reconnect,
114                                                         h);
115       return;
116     }
117   if ( (ntohs (msg->size) != sizeof(struct GNUNET_NSE_ClientMessage)) || 
118        (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_NSE_ESTIMATE) )
119     {
120       GNUNET_break (0);
121       return;
122     }
123   client_msg = (const struct GNUNET_NSE_ClientMessage *)msg;
124   h->recv_cb (h->recv_cb_cls, 
125               client_msg->size_estimate,
126               client_msg->std_deviation);
127   GNUNET_CLIENT_receive (h->client,
128                          &message_handler,
129                          h, 
130                          GNUNET_TIME_UNIT_FOREVER_REL);
131 }
132
133
134
135 /**
136  * Reschedule a connect attempt to the service.
137  *
138  * @param h transport service to reconnect
139  */
140 static void
141 reschedule_connect (struct GNUNET_NSE_Handle *h)
142 {
143   GNUNET_assert (h->reconnect_task == GNUNET_SCHEDULER_NO_TASK);
144
145   if (NULL != h->th)
146     {
147       GNUNET_CLIENT_notify_transmit_ready_cancel (h->th);
148       h->th = NULL;
149     }
150   if (NULL != h->client)
151     {
152       GNUNET_CLIENT_disconnect (h->client, GNUNET_NO);
153       h->client = NULL;
154     }
155
156 #if DEBUG_NSE
157   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
158               "Scheduling task to reconnect to nse service in %llu ms.\n",
159               h->reconnect_delay.rel_value);
160 #endif
161   h->reconnect_task
162     = GNUNET_SCHEDULER_add_delayed (h->reconnect_delay,
163                                     &reconnect, h);
164   if (h->reconnect_delay.rel_value == 0)
165     {
166       h->reconnect_delay = GNUNET_TIME_UNIT_MILLISECONDS;
167     }
168   else
169     {
170       h->reconnect_delay = GNUNET_TIME_relative_multiply (h->reconnect_delay, 2);
171       h->reconnect_delay = GNUNET_TIME_relative_min (GNUNET_TIME_UNIT_SECONDS,
172                                                      h->reconnect_delay);
173     }
174 }
175
176
177 /**
178  * Transmit START message to service.
179  *
180  * @param cls unused
181  * @param size number of bytes available in buf
182  * @param buf where to copy the message
183  * @return number of bytes copied to buf
184  */
185 static size_t
186 send_start (void *cls, size_t size, void *buf)
187 {
188   struct GNUNET_NSE_Handle *h = cls;
189   struct GNUNET_MessageHeader *msg;
190
191   h->th = NULL;
192   if (buf == NULL)
193     {
194       /* Connect error... */
195 #if DEBUG_NSE
196       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
197                   "Shutdown while trying to transmit `%s' request.\n",
198                   "START");
199 #endif
200       reschedule_connect(h);
201       return 0;
202     }
203 #if DEBUG_NSE
204   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
205               "Transmitting `%s' request.\n", "START");
206 #endif
207   GNUNET_assert (size >= sizeof (struct GNUNET_MessageHeader));
208
209   msg = (struct GNUNET_MessageHeader *)buf;
210   msg->size = htons (sizeof (struct GNUNET_MessageHeader));
211   msg->type = htons (GNUNET_MESSAGE_TYPE_NSE_START);
212   GNUNET_CLIENT_receive (h->client,
213                          &message_handler, h, GNUNET_TIME_UNIT_FOREVER_REL);
214   return sizeof (struct GNUNET_MessageHeader);
215 }
216
217
218 /**
219  * Try again to connect to network size estimation service.
220  *
221  * @param cls the handle to the transport service
222  * @param tc scheduler context
223  */
224 static void
225 reconnect (void *cls,
226            const struct GNUNET_SCHEDULER_TaskContext *tc)
227 {
228   struct GNUNET_NSE_Handle *h = cls;
229
230   h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
231   if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
232     {
233       /* shutdown, just give up */
234       return;
235     }
236 #if DEBUG_NSE
237   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
238               "Connecting to network size estimation service.\n");
239 #endif
240   GNUNET_assert (h->client == NULL);
241   h->client = GNUNET_CLIENT_connect ("nse", h->cfg);
242   GNUNET_assert (h->client != NULL);
243
244   h->th =
245     GNUNET_CLIENT_notify_transmit_ready (h->client,
246                                          sizeof(struct GNUNET_MessageHeader),
247                                          GNUNET_TIME_UNIT_FOREVER_REL,
248                                          GNUNET_NO,
249                                          &send_start,
250                                          h);
251   GNUNET_assert(h->th != NULL);
252 }
253
254
255 /**
256  * Connect to the network size estimation service.
257  *
258  * @param cfg the configuration to use
259  * @param func funtion to call with network size estimate
260  * @param func_cls closure to pass for network size estimate callback
261  *
262  * @return handle to use
263  */
264 struct GNUNET_NSE_Handle *
265 GNUNET_NSE_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
266                     GNUNET_NSE_Callback func, void *func_cls)
267 {
268   struct GNUNET_NSE_Handle *ret;
269
270   GNUNET_assert (func != NULL);
271   ret = GNUNET_malloc (sizeof (struct GNUNET_NSE_Handle));
272   ret->cfg = cfg;
273   ret->recv_cb = func;
274   ret->recv_cb_cls = func_cls;
275   ret->reconnect_delay = GNUNET_TIME_UNIT_ZERO;
276   ret->reconnect_task = GNUNET_SCHEDULER_add_now (&reconnect, ret);
277   return ret;
278 }
279
280
281 /**
282  * Disconnect from network size estimation service
283  *
284  * @param h handle to destroy
285  */
286 void
287 GNUNET_NSE_disconnect (struct GNUNET_NSE_Handle *h)
288 {
289   GNUNET_assert(h != NULL);
290   if (h->reconnect_task != GNUNET_SCHEDULER_NO_TASK)
291     {
292       GNUNET_SCHEDULER_cancel(h->reconnect_task);
293       h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
294     }
295   if (h->th != NULL)
296     {
297       GNUNET_CLIENT_notify_transmit_ready_cancel(h->th);
298       h->th = NULL;
299     }
300   if (h->client != NULL)
301     {
302       GNUNET_CLIENT_disconnect(h->client, GNUNET_NO);
303       h->client = NULL;
304     }
305   GNUNET_free(h);
306 }
307
308 /* end of nse_api.c */