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