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