2 This file is part of GNUnet.
3 (C) 2009, 2010, 2011 Christian Grothoff (and other contributing authors)
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.
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.
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.
23 * @brief api to get information from the network size estimation service
24 * @author Nathan Evans
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"
40 * Handle for the service.
42 struct GNUNET_NSE_Handle
45 * Configuration to use.
47 const struct GNUNET_CONFIGURATION_Handle *cfg;
50 * Socket (if available).
52 struct GNUNET_CLIENT_Connection *client;
55 * Currently pending transmission request.
57 struct GNUNET_CLIENT_TransmitHandle *th;
60 * Task doing exponential back-off trying to reconnect.
62 GNUNET_SCHEDULER_TaskIdentifier reconnect_task;
65 * Time for next connect retry.
67 struct GNUNET_TIME_Relative reconnect_delay;
70 * Callback function to call when message is received.
72 GNUNET_NSE_Callback recv_cb;
75 * Closure to pass to callback.
83 * Try again to connect to network size estimation service.
85 * @param cls the handle to the transport service
86 * @param tc scheduler context
90 const struct GNUNET_SCHEDULER_TaskContext *tc);
94 * Type of a function to call when we receive a message
98 * @param msg message received, NULL on timeout or fatal error
101 message_handler (void *cls,
102 const struct GNUNET_MessageHeader * msg)
104 struct GNUNET_NSE_Handle *h = cls;
105 const struct GNUNET_NSE_ClientMessage *client_msg;
109 /* Error, timeout, death */
110 GNUNET_CLIENT_disconnect (h->client, GNUNET_NO);
112 h->reconnect_task = GNUNET_SCHEDULER_add_delayed (h->reconnect_delay,
117 if ( (ntohs (msg->size) != sizeof(struct GNUNET_NSE_ClientMessage)) ||
118 (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_NSE_ESTIMATE) )
123 client_msg = (const struct GNUNET_NSE_ClientMessage *)msg;
124 h->recv_cb (h->recv_cb_cls,
125 GNUNET_TIME_absolute_ntoh(client_msg->timestamp),
126 client_msg->size_estimate,
127 client_msg->std_deviation);
128 GNUNET_CLIENT_receive (h->client,
131 GNUNET_TIME_UNIT_FOREVER_REL);
137 * Reschedule a connect attempt to the service.
139 * @param h transport service to reconnect
142 reschedule_connect (struct GNUNET_NSE_Handle *h)
144 GNUNET_assert (h->reconnect_task == GNUNET_SCHEDULER_NO_TASK);
148 GNUNET_CLIENT_notify_transmit_ready_cancel (h->th);
151 if (NULL != h->client)
153 GNUNET_CLIENT_disconnect (h->client, GNUNET_NO);
158 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
159 "Scheduling task to reconnect to nse service in %llu ms.\n",
160 h->reconnect_delay.rel_value);
163 = GNUNET_SCHEDULER_add_delayed (h->reconnect_delay,
165 if (h->reconnect_delay.rel_value == 0)
167 h->reconnect_delay = GNUNET_TIME_UNIT_MILLISECONDS;
171 h->reconnect_delay = GNUNET_TIME_relative_multiply (h->reconnect_delay, 2);
172 h->reconnect_delay = GNUNET_TIME_relative_min (GNUNET_TIME_UNIT_SECONDS,
179 * Transmit START message to service.
182 * @param size number of bytes available in buf
183 * @param buf where to copy the message
184 * @return number of bytes copied to buf
187 send_start (void *cls, size_t size, void *buf)
189 struct GNUNET_NSE_Handle *h = cls;
190 struct GNUNET_MessageHeader *msg;
195 /* Connect error... */
197 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
198 "Shutdown while trying to transmit `%s' request.\n",
201 reschedule_connect(h);
205 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
206 "Transmitting `%s' request.\n", "START");
208 GNUNET_assert (size >= sizeof (struct GNUNET_MessageHeader));
210 msg = (struct GNUNET_MessageHeader *)buf;
211 msg->size = htons (sizeof (struct GNUNET_MessageHeader));
212 msg->type = htons (GNUNET_MESSAGE_TYPE_NSE_START);
213 GNUNET_CLIENT_receive (h->client,
214 &message_handler, h, GNUNET_TIME_UNIT_FOREVER_REL);
215 return sizeof (struct GNUNET_MessageHeader);
220 * Try again to connect to network size estimation service.
222 * @param cls the handle to the transport service
223 * @param tc scheduler context
226 reconnect (void *cls,
227 const struct GNUNET_SCHEDULER_TaskContext *tc)
229 struct GNUNET_NSE_Handle *h = cls;
231 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
232 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
234 /* shutdown, just give up */
238 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
239 "Connecting to network size estimation service.\n");
241 GNUNET_assert (h->client == NULL);
242 h->client = GNUNET_CLIENT_connect ("nse", h->cfg);
243 GNUNET_assert (h->client != NULL);
246 GNUNET_CLIENT_notify_transmit_ready (h->client,
247 sizeof(struct GNUNET_MessageHeader),
248 GNUNET_TIME_UNIT_FOREVER_REL,
252 GNUNET_assert(h->th != NULL);
257 * Connect to the network size estimation service.
259 * @param cfg the configuration to use
260 * @param func funtion to call with network size estimate
261 * @param func_cls closure to pass for network size estimate callback
263 * @return handle to use
265 struct GNUNET_NSE_Handle *
266 GNUNET_NSE_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
267 GNUNET_NSE_Callback func, void *func_cls)
269 struct GNUNET_NSE_Handle *ret;
271 GNUNET_assert (func != NULL);
272 ret = GNUNET_malloc (sizeof (struct GNUNET_NSE_Handle));
275 ret->recv_cb_cls = func_cls;
276 ret->reconnect_delay = GNUNET_TIME_UNIT_ZERO;
277 ret->reconnect_task = GNUNET_SCHEDULER_add_now (&reconnect, ret);
283 * Disconnect from network size estimation service
285 * @param h handle to destroy
288 GNUNET_NSE_disconnect (struct GNUNET_NSE_Handle *h)
290 GNUNET_assert(h != NULL);
291 if (h->reconnect_task != GNUNET_SCHEDULER_NO_TASK)
293 GNUNET_SCHEDULER_cancel(h->reconnect_task);
294 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
298 GNUNET_CLIENT_notify_transmit_ready_cancel(h->th);
301 if (h->client != NULL)
303 GNUNET_CLIENT_disconnect(h->client, GNUNET_NO);
309 /* end of nse_api.c */