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"
38 #define LOG(kind,...) GNUNET_log_from (kind, "nse-api",__VA_ARGS__)
41 * Handle for the service.
43 struct GNUNET_NSE_Handle
46 * Configuration to use.
48 const struct GNUNET_CONFIGURATION_Handle *cfg;
51 * Socket (if available).
53 struct GNUNET_CLIENT_Connection *client;
56 * Currently pending transmission request.
58 struct GNUNET_CLIENT_TransmitHandle *th;
61 * Task doing exponential back-off trying to reconnect.
63 GNUNET_SCHEDULER_TaskIdentifier reconnect_task;
66 * Time for next connect retry.
68 struct GNUNET_TIME_Relative reconnect_delay;
71 * Callback function to call when message is received.
73 GNUNET_NSE_Callback recv_cb;
76 * Closure to pass to callback.
84 * Try again to connect to network size estimation service.
86 * @param cls the handle to the transport service
87 * @param tc scheduler context
90 reconnect (void *cls, 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, const struct GNUNET_MessageHeader *msg)
103 struct GNUNET_NSE_Handle *h = cls;
104 const struct GNUNET_NSE_ClientMessage *client_msg;
108 /* Error, timeout, death */
109 GNUNET_CLIENT_disconnect (h->client);
112 GNUNET_SCHEDULER_add_delayed (h->reconnect_delay, &reconnect, h);
115 if ((ntohs (msg->size) != sizeof (struct GNUNET_NSE_ClientMessage)) ||
116 (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_NSE_ESTIMATE))
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);
132 * Reschedule a connect attempt to the service.
134 * @param h transport service to reconnect
137 reschedule_connect (struct GNUNET_NSE_Handle *h)
139 GNUNET_assert (h->reconnect_task == GNUNET_SCHEDULER_NO_TASK);
143 GNUNET_CLIENT_notify_transmit_ready_cancel (h->th);
146 if (NULL != h->client)
148 GNUNET_CLIENT_disconnect (h->client);
152 LOG (GNUNET_ERROR_TYPE_DEBUG,
153 "Scheduling task to reconnect to nse service in %llu ms.\n",
154 h->reconnect_delay.rel_value);
156 GNUNET_SCHEDULER_add_delayed (h->reconnect_delay, &reconnect, h);
157 h->reconnect_delay = GNUNET_TIME_STD_BACKOFF (h->reconnect_delay);
162 * Transmit START message to service.
165 * @param size number of bytes available in buf
166 * @param buf where to copy the message
167 * @return number of bytes copied to buf
170 send_start (void *cls, size_t size, void *buf)
172 struct GNUNET_NSE_Handle *h = cls;
173 struct GNUNET_MessageHeader *msg;
178 /* Connect error... */
179 LOG (GNUNET_ERROR_TYPE_DEBUG,
180 "Shutdown while trying to transmit `%s' request.\n", "START");
181 reschedule_connect (h);
184 LOG (GNUNET_ERROR_TYPE_DEBUG, "Transmitting `%s' request.\n", "START");
185 GNUNET_assert (size >= sizeof (struct GNUNET_MessageHeader));
187 msg = (struct GNUNET_MessageHeader *) buf;
188 msg->size = htons (sizeof (struct GNUNET_MessageHeader));
189 msg->type = htons (GNUNET_MESSAGE_TYPE_NSE_START);
190 GNUNET_CLIENT_receive (h->client, &message_handler, h,
191 GNUNET_TIME_UNIT_FOREVER_REL);
192 return sizeof (struct GNUNET_MessageHeader);
197 * Try again to connect to network size estimation service.
199 * @param cls the handle to the transport service
200 * @param tc scheduler context
203 reconnect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
205 struct GNUNET_NSE_Handle *h = cls;
207 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
208 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
210 /* shutdown, just give up */
213 LOG (GNUNET_ERROR_TYPE_DEBUG,
214 "Connecting to network size estimation service.\n");
215 GNUNET_assert (h->client == NULL);
216 h->client = GNUNET_CLIENT_connect ("nse", h->cfg);
217 GNUNET_assert (h->client != NULL);
220 GNUNET_CLIENT_notify_transmit_ready (h->client,
221 sizeof (struct GNUNET_MessageHeader),
222 GNUNET_TIME_UNIT_FOREVER_REL,
223 GNUNET_NO, &send_start, h);
224 GNUNET_assert (h->th != NULL);
229 * Connect to the network size estimation service.
231 * @param cfg the configuration to use
232 * @param func funtion to call with network size estimate
233 * @param func_cls closure to pass for network size estimate callback
235 * @return handle to use
237 struct GNUNET_NSE_Handle *
238 GNUNET_NSE_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
239 GNUNET_NSE_Callback func, void *func_cls)
241 struct GNUNET_NSE_Handle *ret;
243 GNUNET_assert (func != NULL);
244 ret = GNUNET_malloc (sizeof (struct GNUNET_NSE_Handle));
247 ret->recv_cb_cls = func_cls;
248 ret->reconnect_delay = GNUNET_TIME_UNIT_ZERO;
249 ret->reconnect_task = GNUNET_SCHEDULER_add_now (&reconnect, ret);
255 * Disconnect from network size estimation service
257 * @param h handle to destroy
260 GNUNET_NSE_disconnect (struct GNUNET_NSE_Handle *h)
262 GNUNET_assert (h != NULL);
263 if (h->reconnect_task != GNUNET_SCHEDULER_NO_TASK)
265 GNUNET_SCHEDULER_cancel (h->reconnect_task);
266 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
270 GNUNET_CLIENT_notify_transmit_ready_cancel (h->th);
273 if (h->client != NULL)
275 GNUNET_CLIENT_disconnect (h->client);
281 /* end of nse_api.c */