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
88 static void reconnect (void *cls,
89 const struct GNUNET_SCHEDULER_TaskContext *tc);
93 * Type of a function to call when we receive a message
97 * @param msg message received, NULL on timeout or fatal error
100 message_handler (void *cls, const struct GNUNET_MessageHeader *msg)
102 struct GNUNET_NSE_Handle *h = cls;
103 const struct GNUNET_NSE_ClientMessage *client_msg;
107 /* Error, timeout, death */
108 GNUNET_CLIENT_disconnect (h->client, GNUNET_NO);
111 GNUNET_SCHEDULER_add_delayed (h->reconnect_delay, &reconnect, h);
114 if ((ntohs (msg->size) != sizeof (struct GNUNET_NSE_ClientMessage)) ||
115 (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_NSE_ESTIMATE))
120 client_msg = (const struct GNUNET_NSE_ClientMessage *) msg;
121 h->recv_cb (h->recv_cb_cls, GNUNET_TIME_absolute_ntoh (client_msg->timestamp),
122 client_msg->size_estimate, client_msg->std_deviation);
123 GNUNET_CLIENT_receive (h->client, &message_handler, h,
124 GNUNET_TIME_UNIT_FOREVER_REL);
130 * Reschedule a connect attempt to the service.
132 * @param h transport service to reconnect
135 reschedule_connect (struct GNUNET_NSE_Handle *h)
137 GNUNET_assert (h->reconnect_task == GNUNET_SCHEDULER_NO_TASK);
141 GNUNET_CLIENT_notify_transmit_ready_cancel (h->th);
144 if (NULL != h->client)
146 GNUNET_CLIENT_disconnect (h->client, GNUNET_NO);
151 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
152 "Scheduling task to reconnect to nse service in %llu ms.\n",
153 h->reconnect_delay.rel_value);
156 GNUNET_SCHEDULER_add_delayed (h->reconnect_delay, &reconnect, h);
157 if (h->reconnect_delay.rel_value == 0)
159 h->reconnect_delay = GNUNET_TIME_UNIT_MILLISECONDS;
163 h->reconnect_delay = GNUNET_TIME_relative_multiply (h->reconnect_delay, 2);
165 GNUNET_TIME_relative_min (GNUNET_TIME_UNIT_SECONDS, h->reconnect_delay);
171 * Transmit START message to service.
174 * @param size number of bytes available in buf
175 * @param buf where to copy the message
176 * @return number of bytes copied to buf
179 send_start (void *cls, size_t size, void *buf)
181 struct GNUNET_NSE_Handle *h = cls;
182 struct GNUNET_MessageHeader *msg;
187 /* Connect error... */
189 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
190 "Shutdown while trying to transmit `%s' request.\n", "START");
192 reschedule_connect (h);
196 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Transmitting `%s' request.\n", "START");
198 GNUNET_assert (size >= sizeof (struct GNUNET_MessageHeader));
200 msg = (struct GNUNET_MessageHeader *) buf;
201 msg->size = htons (sizeof (struct GNUNET_MessageHeader));
202 msg->type = htons (GNUNET_MESSAGE_TYPE_NSE_START);
203 GNUNET_CLIENT_receive (h->client, &message_handler, h,
204 GNUNET_TIME_UNIT_FOREVER_REL);
205 return sizeof (struct GNUNET_MessageHeader);
210 * Try again to connect to network size estimation service.
212 * @param cls the handle to the transport service
213 * @param tc scheduler context
216 reconnect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
218 struct GNUNET_NSE_Handle *h = cls;
220 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
221 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
223 /* shutdown, just give up */
227 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
228 "Connecting to network size estimation service.\n");
230 GNUNET_assert (h->client == NULL);
231 h->client = GNUNET_CLIENT_connect ("nse", h->cfg);
232 GNUNET_assert (h->client != NULL);
235 GNUNET_CLIENT_notify_transmit_ready (h->client,
236 sizeof (struct GNUNET_MessageHeader),
237 GNUNET_TIME_UNIT_FOREVER_REL,
238 GNUNET_NO, &send_start, h);
239 GNUNET_assert (h->th != NULL);
244 * Connect to the network size estimation service.
246 * @param cfg the configuration to use
247 * @param func funtion to call with network size estimate
248 * @param func_cls closure to pass for network size estimate callback
250 * @return handle to use
252 struct GNUNET_NSE_Handle *
253 GNUNET_NSE_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
254 GNUNET_NSE_Callback func, void *func_cls)
256 struct GNUNET_NSE_Handle *ret;
258 GNUNET_assert (func != NULL);
259 ret = GNUNET_malloc (sizeof (struct GNUNET_NSE_Handle));
262 ret->recv_cb_cls = func_cls;
263 ret->reconnect_delay = GNUNET_TIME_UNIT_ZERO;
264 ret->reconnect_task = GNUNET_SCHEDULER_add_now (&reconnect, ret);
270 * Disconnect from network size estimation service
272 * @param h handle to destroy
275 GNUNET_NSE_disconnect (struct GNUNET_NSE_Handle *h)
277 GNUNET_assert (h != NULL);
278 if (h->reconnect_task != GNUNET_SCHEDULER_NO_TASK)
280 GNUNET_SCHEDULER_cancel (h->reconnect_task);
281 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
285 GNUNET_CLIENT_notify_transmit_ready_cancel (h->th);
288 if (h->client != NULL)
290 GNUNET_CLIENT_disconnect (h->client, GNUNET_NO);
296 /* end of nse_api.c */