aboutsummaryrefslogtreecommitdiff
path: root/src/nse/nse_api.c
diff options
context:
space:
mode:
authorNathan S. Evans <evans@in.tum.de>2011-07-08 17:10:45 +0000
committerNathan S. Evans <evans@in.tum.de>2011-07-08 17:10:45 +0000
commit5058b5b92455e26dd740b5c0344f78e95cd0fc2e (patch)
tree0077531e5ff8c9c9348b301a10618b0328bef90e /src/nse/nse_api.c
parenta6627549cdea39c0c5eb75966598e4ee7c31cad1 (diff)
downloadgnunet-5058b5b92455e26dd740b5c0344f78e95cd0fc2e.tar.gz
gnunet-5058b5b92455e26dd740b5c0344f78e95cd0fc2e.zip
initial nse commit
Diffstat (limited to 'src/nse/nse_api.c')
-rw-r--r--src/nse/nse_api.c299
1 files changed, 299 insertions, 0 deletions
diff --git a/src/nse/nse_api.c b/src/nse/nse_api.c
new file mode 100644
index 000000000..20c28e94f
--- /dev/null
+++ b/src/nse/nse_api.c
@@ -0,0 +1,299 @@
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 * TODO:
27 */
28#include "platform.h"
29#include "gnunet_client_lib.h"
30#include "gnunet_constants.h"
31#include "gnunet_container_lib.h"
32#include "gnunet_arm_service.h"
33#include "gnunet_hello_lib.h"
34#include "gnunet_protocols.h"
35#include "gnunet_server_lib.h"
36#include "gnunet_time_lib.h"
37#include "gnunet_nse_service.h"
38#include "nse.h"
39
40
41/**
42 * Handle for the service.
43 */
44struct GNUNET_NSE_Handle
45{
46 /**
47 * Configuration to use.
48 */
49 const struct GNUNET_CONFIGURATION_Handle *cfg;
50
51 /**
52 * Socket (if available).
53 */
54 struct GNUNET_CLIENT_Connection *client;
55
56 /**
57 * Currently pending transmission request.
58 */
59 struct GNUNET_CLIENT_TransmitHandle *th;
60
61 /**
62 * Task doing exponential back-off trying to reconnect.
63 */
64 GNUNET_SCHEDULER_TaskIdentifier reconnect_task;
65
66 /**
67 * Time for next connect retry.
68 */
69 struct GNUNET_TIME_Relative reconnect_delay;
70
71 /**
72 * Should this handle auto-destruct once all actions have
73 * been processed?
74 */
75 int do_destroy;
76
77 /**
78 * Are we currently receiving from the service?
79 */
80 int receiving;
81
82 /**
83 * Callback function to call when message is received.
84 */
85 GNUNET_NSE_Callback recv_cb;
86
87 /**
88 * Closure to pass to callback.
89 */
90 void *recv_cb_cls;
91
92};
93
94
95/**
96 * Type of a function to call when we receive a message
97 * from the service.
98 *
99 * @param cls closure
100 * @param msg message received, NULL on timeout or fatal error
101 */
102void message_handler (void *cls,
103 const struct GNUNET_MessageHeader * msg)
104{
105 struct GNUNET_NSE_Handle *h = cls;
106 struct GNUNET_NSE_ClientMessage *client_msg;
107
108 if ((ntohs (msg->size) < sizeof(struct GNUNET_NSE_ClientMessage))
109 || (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_NSE_ESTIMATE))
110 {
111#if DEBUG_NSE
112 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
113 "%s: received incorrect message (size %d < %d) from service!",
114 "NSE API", ntohs (msg->size),
115 sizeof(struct GNUNET_NSE_ClientMessage));
116#endif
117 return;
118 }
119
120 client_msg = (struct GNUNET_NSE_ClientMessage *)msg;
121
122 h->recv_cb (h->recv_cb_cls, client_msg->size_estimate,
123 client_msg->std_deviation);
124
125 GNUNET_CLIENT_receive (h->client,
126 &message_handler, h, GNUNET_TIME_UNIT_FOREVER_REL);
127}
128
129static void
130reconnect (void *cls,
131 const struct GNUNET_SCHEDULER_TaskContext *tc);
132
133/**
134 * Reschedule a connect attempt to the service.
135 *
136 * @param h transport service to reconnect
137 */
138static void
139reschedule_connect (struct GNUNET_NSE_Handle *h)
140{
141 GNUNET_assert (h->reconnect_task == GNUNET_SCHEDULER_NO_TASK);
142
143 if (NULL != h->th)
144 {
145 GNUNET_CLIENT_notify_transmit_ready_cancel (h->th);
146 h->th = NULL;
147 }
148 if (NULL != h->client)
149 {
150 GNUNET_CLIENT_disconnect (h->client, GNUNET_NO);
151 h->client = NULL;
152 }
153
154#if DEBUG_NSE
155 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
156 "Scheduling task to reconnect to nse service in %llu ms.\n",
157 h->reconnect_delay.rel_value);
158#endif
159 h->reconnect_task
160 = GNUNET_SCHEDULER_add_delayed (h->reconnect_delay,
161 &reconnect, h);
162 if (h->reconnect_delay.rel_value == 0)
163 {
164 h->reconnect_delay = GNUNET_TIME_UNIT_MILLISECONDS;
165 }
166 else
167 {
168 h->reconnect_delay = GNUNET_TIME_relative_multiply (h->reconnect_delay, 2);
169 h->reconnect_delay = GNUNET_TIME_relative_min (GNUNET_TIME_UNIT_SECONDS,
170 h->reconnect_delay);
171 }
172}
173
174/**
175 * Transmit START message to service.
176 *
177 * @param cls unused
178 * @param size number of bytes available in buf
179 * @param buf where to copy the message
180 * @return number of bytes copied to buf
181 */
182static size_t
183send_start (void *cls, size_t size, void *buf)
184{
185 struct GNUNET_NSE_Handle *h = cls;
186 struct GNUNET_MessageHeader *msg;
187
188 h->th = NULL;
189 if (buf == NULL)
190 {
191 /* Connect error... */
192#if DEBUG_NSE
193 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
194 "Shutdown while trying to transmit `%s' request.\n",
195 "START");
196#endif
197 reschedule_connect(h);
198 return 0;
199 }
200#if DEBUG_NSE
201 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
202 "Transmitting `%s' request.\n", "START");
203#endif
204 GNUNET_assert (size >= sizeof (struct GNUNET_MessageHeader));
205
206 msg = (struct GNUNET_MessageHeader *)buf;
207 msg->size = htons (sizeof (struct GNUNET_MessageHeader));
208 msg->type = htons (GNUNET_MESSAGE_TYPE_NSE_START);
209 GNUNET_CLIENT_receive (h->client,
210 &message_handler, h, GNUNET_TIME_UNIT_FOREVER_REL);
211 return sizeof (struct GNUNET_MessageHeader);
212}
213
214/**
215 * Try again to connect to network size estimation service.
216 *
217 * @param cls the handle to the transport service
218 * @param tc scheduler context
219 */
220static void
221reconnect (void *cls,
222 const struct GNUNET_SCHEDULER_TaskContext *tc)
223{
224 struct GNUNET_NSE_Handle *h = cls;
225
226 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
227 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
228 {
229 /* shutdown, just give up */
230 return;
231 }
232#if DEBUG_NSE
233 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
234 "Connecting to network size estimation service.\n");
235#endif
236 GNUNET_assert (h->client == NULL);
237 h->client = GNUNET_CLIENT_connect ("nse", h->cfg);
238 GNUNET_assert (h->client != NULL);
239
240 h->th =
241 GNUNET_CLIENT_notify_transmit_ready (h->client,
242 sizeof(struct GNUNET_MessageHeader),
243 GNUNET_TIME_UNIT_FOREVER_REL,
244 GNUNET_NO,
245 &send_start,
246 h);
247 GNUNET_assert(h->th != NULL);
248}
249
250/**
251 * Connect to the network size estimation service.
252 *
253 * @param cfg the configuration to use
254 * @param func funtion to call with network size estimate
255 * @param func_cls closure to pass for network size estimate callback
256 *
257 * @return handle to use
258 */
259struct GNUNET_NSE_Handle *
260GNUNET_NSE_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
261 GNUNET_NSE_Callback func, void *func_cls)
262{
263 struct GNUNET_NSE_Handle *ret;
264
265 ret = GNUNET_malloc (sizeof (struct GNUNET_NSE_Handle));
266
267 if (func == NULL)
268 return NULL;
269
270 ret->cfg = cfg;
271 ret->recv_cb = func;
272 ret->recv_cb_cls = func_cls;
273 ret->reconnect_delay = GNUNET_TIME_UNIT_ZERO;
274 ret->reconnect_task = GNUNET_SCHEDULER_add_now (&reconnect, ret);
275 return ret;
276}
277
278/**
279 * Disconnect from network size estimation service
280 *
281 * @param h handle to destroy
282 *
283 */
284void
285GNUNET_NSE_disconnect (struct GNUNET_NSE_Handle *h)
286{
287 GNUNET_assert(h != NULL);
288 if (h->reconnect_task != GNUNET_SCHEDULER_NO_TASK)
289 {
290 GNUNET_SCHEDULER_cancel(h->reconnect_task);
291 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
292 }
293 if (h->th != NULL)
294 GNUNET_CLIENT_notify_transmit_ready_cancel(h->th);
295 if (h->client != NULL)
296 GNUNET_CLIENT_disconnect(h->client, GNUNET_NO);
297
298 GNUNET_free(h);
299}