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_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"
35 #define LOG(kind,...) GNUNET_log_from (kind, "nse-api",__VA_ARGS__)
38 * Handle for talking with the NSE service.
40 struct GNUNET_NSE_Handle
43 * Configuration to use.
45 const struct GNUNET_CONFIGURATION_Handle *cfg;
48 * Socket (if available).
50 struct GNUNET_CLIENT_Connection *client;
53 * Currently pending transmission request.
55 struct GNUNET_CLIENT_TransmitHandle *th;
58 * Task doing exponential back-off trying to reconnect.
60 struct GNUNET_SCHEDULER_Task * reconnect_task;
63 * Time for next connect retry.
65 struct GNUNET_TIME_Relative reconnect_delay;
68 * Callback function to call when message is received.
70 GNUNET_NSE_Callback recv_cb;
73 * Closure to pass to @e recv_cb callback.
81 * Try again to connect to network size estimation service.
83 * @param cls the handle to the transport service
84 * @param tc scheduler context
87 reconnect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
91 * Type of a function to call when we receive a message
95 * @param msg message received, NULL on timeout or fatal error
98 message_handler (void *cls, const struct GNUNET_MessageHeader *msg)
100 struct GNUNET_NSE_Handle *h = cls;
101 const struct GNUNET_NSE_ClientMessage *client_msg;
105 /* Error, timeout, death */
106 GNUNET_CLIENT_disconnect (h->client);
109 GNUNET_SCHEDULER_add_delayed (h->reconnect_delay,
113 if ((ntohs (msg->size) != sizeof (struct GNUNET_NSE_ClientMessage)) ||
114 (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_NSE_ESTIMATE))
119 client_msg = (const struct GNUNET_NSE_ClientMessage *) msg;
120 h->recv_cb (h->recv_cb_cls, GNUNET_TIME_absolute_ntoh (client_msg->timestamp),
121 GNUNET_ntoh_double (client_msg->size_estimate),
122 GNUNET_ntoh_double (client_msg->std_deviation));
123 GNUNET_CLIENT_receive (h->client, &message_handler, h,
124 GNUNET_TIME_UNIT_FOREVER_REL);
129 * Reschedule a connect attempt to the service.
131 * @param h transport service to reconnect
134 reschedule_connect (struct GNUNET_NSE_Handle *h)
136 GNUNET_assert (h->reconnect_task == NULL);
140 GNUNET_CLIENT_notify_transmit_ready_cancel (h->th);
143 if (NULL != h->client)
145 GNUNET_CLIENT_disconnect (h->client);
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,
154 GNUNET_SCHEDULER_add_delayed (h->reconnect_delay, &reconnect, h);
155 h->reconnect_delay = GNUNET_TIME_STD_BACKOFF (h->reconnect_delay);
160 * Transmit START message to service.
162 * @param cls the `struct GNUNET_NSE_Handle *`
163 * @param size number of bytes available in @a buf
164 * @param buf where to copy the message
165 * @return number of bytes copied to @a buf
168 send_start (void *cls, size_t size, void *buf)
170 struct GNUNET_NSE_Handle *h = cls;
171 struct GNUNET_MessageHeader *msg;
176 /* Connect error... */
177 LOG (GNUNET_ERROR_TYPE_DEBUG,
178 "Error while trying to transmit `%s' request.\n",
180 reschedule_connect (h);
183 LOG (GNUNET_ERROR_TYPE_DEBUG,
184 "Transmitting `%s' request.\n",
186 GNUNET_assert (size >= sizeof (struct GNUNET_MessageHeader));
188 msg = (struct GNUNET_MessageHeader *) buf;
189 msg->size = htons (sizeof (struct GNUNET_MessageHeader));
190 msg->type = htons (GNUNET_MESSAGE_TYPE_NSE_START);
191 GNUNET_CLIENT_receive (h->client, &message_handler, h,
192 GNUNET_TIME_UNIT_FOREVER_REL);
193 return sizeof (struct GNUNET_MessageHeader);
198 * Try again to connect to network size estimation service.
200 * @param cls the `struct GNUNET_NSE_Handle *`
201 * @param tc scheduler context
204 reconnect (void *cls,
205 const struct GNUNET_SCHEDULER_TaskContext *tc)
207 struct GNUNET_NSE_Handle *h = cls;
209 h->reconnect_task = NULL;
210 LOG (GNUNET_ERROR_TYPE_DEBUG,
211 "Connecting to network size estimation service.\n");
212 GNUNET_assert (NULL == h->client);
213 h->client = GNUNET_CLIENT_connect ("nse", h->cfg);
214 GNUNET_assert (NULL != h->client);
215 GNUNET_assert (NULL == h->th);
217 GNUNET_CLIENT_notify_transmit_ready (h->client,
218 sizeof (struct GNUNET_MessageHeader),
219 GNUNET_TIME_UNIT_FOREVER_REL,
220 GNUNET_NO, &send_start, h);
221 GNUNET_assert (NULL != h->th);
226 * Connect to the network size estimation service.
228 * @param cfg the configuration to use
229 * @param func funtion to call with network size estimate
230 * @param func_cls closure to pass to @a func
231 * @return handle to use
233 struct GNUNET_NSE_Handle *
234 GNUNET_NSE_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
235 GNUNET_NSE_Callback func, void *func_cls)
237 struct GNUNET_NSE_Handle *ret;
239 GNUNET_assert (func != NULL);
240 ret = GNUNET_new (struct GNUNET_NSE_Handle);
243 ret->recv_cb_cls = func_cls;
244 ret->reconnect_delay = GNUNET_TIME_UNIT_ZERO;
245 ret->reconnect_task = GNUNET_SCHEDULER_add_now (&reconnect, ret);
251 * Disconnect from network size estimation service
253 * @param h handle to destroy
256 GNUNET_NSE_disconnect (struct GNUNET_NSE_Handle *h)
258 GNUNET_assert (NULL != h);
259 if (h->reconnect_task != NULL)
261 GNUNET_SCHEDULER_cancel (h->reconnect_task);
262 h->reconnect_task = NULL;
266 GNUNET_CLIENT_notify_transmit_ready_cancel (h->th);
269 if (NULL != h->client)
271 GNUNET_CLIENT_disconnect (h->client);
277 /* end of nse_api.c */