diff options
Diffstat (limited to 'src/testbed-logger/testbed_logger_api.c')
-rw-r--r-- | src/testbed-logger/testbed_logger_api.c | 490 |
1 files changed, 490 insertions, 0 deletions
diff --git a/src/testbed-logger/testbed_logger_api.c b/src/testbed-logger/testbed_logger_api.c new file mode 100644 index 000000000..aaf18cd33 --- /dev/null +++ b/src/testbed-logger/testbed_logger_api.c | |||
@@ -0,0 +1,490 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet | ||
3 | Copyright (C) 2008--2013 GNUnet e.V. | ||
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., 51 Franklin Street, Fifth Floor, | ||
18 | Boston, MA 02110-1301, USA. | ||
19 | */ | ||
20 | |||
21 | /** | ||
22 | * @file testbed-logger/testbed_logger_api.c | ||
23 | * @brief Client-side routines for communicating with the tesbted logger service | ||
24 | * @author Sree Harsha Totakura <sreeharsha@totakura.in> | ||
25 | */ | ||
26 | |||
27 | #include "platform.h" | ||
28 | #include "gnunet_util_lib.h" | ||
29 | #include "gnunet_testbed_logger_service.h" | ||
30 | |||
31 | /** | ||
32 | * Generic logging shorthand | ||
33 | */ | ||
34 | #define LOG(kind, ...) \ | ||
35 | GNUNET_log_from (kind, "testbed-logger-api", __VA_ARGS__) | ||
36 | |||
37 | /** | ||
38 | * Debug logging | ||
39 | */ | ||
40 | #define LOG_DEBUG(...) \ | ||
41 | LOG (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__) | ||
42 | |||
43 | #ifdef GNUNET_TIME_STD_EXPONENTIAL_BACKOFF_THRESHOLD | ||
44 | #undef GNUNET_TIME_STD_EXPONENTIAL_BACKOFF_THRESHOLD | ||
45 | #endif | ||
46 | |||
47 | /** | ||
48 | * Threshold after which exponential backoff should not increase (15 s). | ||
49 | */ | ||
50 | #define GNUNET_TIME_STD_EXPONENTIAL_BACKOFF_THRESHOLD GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 3) | ||
51 | |||
52 | /** | ||
53 | * The size of the buffer we fill before sending out the message | ||
54 | */ | ||
55 | #define BUFFER_SIZE GNUNET_SERVER_MAX_MESSAGE_SIZE | ||
56 | |||
57 | /** | ||
58 | * The message queue for sending messages to the controller service | ||
59 | */ | ||
60 | struct MessageQueue | ||
61 | { | ||
62 | /** | ||
63 | * next pointer for DLL | ||
64 | */ | ||
65 | struct MessageQueue *next; | ||
66 | |||
67 | /** | ||
68 | * prev pointer for DLL | ||
69 | */ | ||
70 | struct MessageQueue *prev; | ||
71 | |||
72 | /** | ||
73 | * The message to be sent | ||
74 | */ | ||
75 | struct GNUNET_MessageHeader *msg; | ||
76 | |||
77 | /** | ||
78 | * Completion callback | ||
79 | */ | ||
80 | GNUNET_TESTBED_LOGGER_FlushCompletion cb; | ||
81 | |||
82 | /** | ||
83 | * callback closure | ||
84 | */ | ||
85 | void *cb_cls; | ||
86 | }; | ||
87 | |||
88 | |||
89 | /** | ||
90 | * Connection handle for the logger service | ||
91 | */ | ||
92 | struct GNUNET_TESTBED_LOGGER_Handle | ||
93 | { | ||
94 | /** | ||
95 | * Client connection | ||
96 | */ | ||
97 | struct GNUNET_CLIENT_Connection *client; | ||
98 | |||
99 | /** | ||
100 | * The transport handle | ||
101 | */ | ||
102 | struct GNUNET_CLIENT_TransmitHandle *th; | ||
103 | |||
104 | /** | ||
105 | * DLL head for the message queue | ||
106 | */ | ||
107 | struct MessageQueue *mq_head; | ||
108 | |||
109 | /** | ||
110 | * DLL tail for the message queue | ||
111 | */ | ||
112 | struct MessageQueue *mq_tail; | ||
113 | |||
114 | /** | ||
115 | * Flush completion callback | ||
116 | */ | ||
117 | GNUNET_TESTBED_LOGGER_FlushCompletion cb; | ||
118 | |||
119 | /** | ||
120 | * Closure for the above callback | ||
121 | */ | ||
122 | void *cb_cls; | ||
123 | |||
124 | /** | ||
125 | * Local buffer for data to be transmitted | ||
126 | */ | ||
127 | void *buf; | ||
128 | |||
129 | /** | ||
130 | * The size of the local buffer | ||
131 | */ | ||
132 | size_t bs; | ||
133 | |||
134 | /** | ||
135 | * Number of bytes wrote since last flush | ||
136 | */ | ||
137 | size_t bwrote; | ||
138 | |||
139 | /** | ||
140 | * How long after should we retry sending a message to the service? | ||
141 | */ | ||
142 | struct GNUNET_TIME_Relative retry_backoff; | ||
143 | |||
144 | /** | ||
145 | * Task to call the flush completion callback | ||
146 | */ | ||
147 | struct GNUNET_SCHEDULER_Task * flush_completion_task; | ||
148 | |||
149 | /** | ||
150 | * Task to be executed when flushing takes too long | ||
151 | */ | ||
152 | struct GNUNET_SCHEDULER_Task * timeout_flush_task; | ||
153 | }; | ||
154 | |||
155 | |||
156 | /** | ||
157 | * Cancels the flush timeout task | ||
158 | * | ||
159 | * @param h handle to the logger | ||
160 | */ | ||
161 | static void | ||
162 | cancel_timeout_flush (struct GNUNET_TESTBED_LOGGER_Handle *h) | ||
163 | { | ||
164 | GNUNET_SCHEDULER_cancel (h->timeout_flush_task); | ||
165 | h->timeout_flush_task = NULL; | ||
166 | } | ||
167 | |||
168 | |||
169 | /** | ||
170 | * Task to call the flush completion notification | ||
171 | * | ||
172 | * @param cls the logger handle | ||
173 | */ | ||
174 | static void | ||
175 | call_flush_completion (void *cls) | ||
176 | { | ||
177 | struct GNUNET_TESTBED_LOGGER_Handle *h = cls; | ||
178 | GNUNET_TESTBED_LOGGER_FlushCompletion cb; | ||
179 | void *cb_cls; | ||
180 | size_t bw; | ||
181 | |||
182 | h->flush_completion_task = NULL; | ||
183 | bw = h->bwrote; | ||
184 | h->bwrote = 0; | ||
185 | cb = h->cb; | ||
186 | h->cb = NULL; | ||
187 | cb_cls = h->cb_cls; | ||
188 | h->cb_cls = NULL; | ||
189 | if (NULL != h->timeout_flush_task) | ||
190 | cancel_timeout_flush (h); | ||
191 | if (NULL != cb) | ||
192 | cb (cb_cls, bw); | ||
193 | } | ||
194 | |||
195 | |||
196 | /** | ||
197 | * Schedule the flush completion notification task | ||
198 | * | ||
199 | * @param h logger handle | ||
200 | */ | ||
201 | static void | ||
202 | trigger_flush_notification (struct GNUNET_TESTBED_LOGGER_Handle *h) | ||
203 | { | ||
204 | if (NULL != h->flush_completion_task) | ||
205 | GNUNET_SCHEDULER_cancel (h->flush_completion_task); | ||
206 | h->flush_completion_task = GNUNET_SCHEDULER_add_now (&call_flush_completion, h); | ||
207 | } | ||
208 | |||
209 | |||
210 | /** | ||
211 | * Function called to notify a client about the connection begin ready to queue | ||
212 | * more data. "buf" will be NULL and "size" zero if the connection was closed | ||
213 | * for writing in the meantime. | ||
214 | * | ||
215 | * @param cls closure | ||
216 | * @param size number of bytes available in buf | ||
217 | * @param buf where the callee should write the message | ||
218 | * @return number of bytes written to buf | ||
219 | */ | ||
220 | static size_t | ||
221 | transmit_ready_notify (void *cls, size_t size, void *buf) | ||
222 | { | ||
223 | struct GNUNET_TESTBED_LOGGER_Handle *h = cls; | ||
224 | struct MessageQueue *mq; | ||
225 | |||
226 | h->th = NULL; | ||
227 | mq = h->mq_head; | ||
228 | GNUNET_assert (NULL != mq); | ||
229 | if ((0 == size) && (NULL == buf)) /* Timeout */ | ||
230 | { | ||
231 | LOG_DEBUG ("Message sending timed out -- retrying\n"); | ||
232 | h->retry_backoff = GNUNET_TIME_STD_BACKOFF (h->retry_backoff); | ||
233 | h->th = | ||
234 | GNUNET_CLIENT_notify_transmit_ready (h->client, | ||
235 | ntohs (mq->msg->size), | ||
236 | h->retry_backoff, GNUNET_YES, | ||
237 | &transmit_ready_notify, h); | ||
238 | return 0; | ||
239 | } | ||
240 | h->retry_backoff = GNUNET_TIME_UNIT_ZERO; | ||
241 | GNUNET_assert (ntohs (mq->msg->size) <= size); | ||
242 | size = ntohs (mq->msg->size); | ||
243 | memcpy (buf, mq->msg, size); | ||
244 | LOG_DEBUG ("Message of type: %u and size: %u sent\n", | ||
245 | ntohs (mq->msg->type), size); | ||
246 | GNUNET_free (mq->msg); | ||
247 | GNUNET_CONTAINER_DLL_remove (h->mq_head, h->mq_tail, mq); | ||
248 | GNUNET_free (mq); | ||
249 | h->bwrote += (size - sizeof (struct GNUNET_MessageHeader)); | ||
250 | mq = h->mq_head; | ||
251 | if (NULL != mq) | ||
252 | { | ||
253 | h->retry_backoff = GNUNET_TIME_STD_BACKOFF (h->retry_backoff); | ||
254 | h->th = | ||
255 | GNUNET_CLIENT_notify_transmit_ready (h->client, | ||
256 | ntohs (mq->msg->size), | ||
257 | h->retry_backoff, GNUNET_YES, | ||
258 | &transmit_ready_notify, h); | ||
259 | return size; | ||
260 | } | ||
261 | if (NULL != h->cb) | ||
262 | trigger_flush_notification (h); /* Call the flush completion callback */ | ||
263 | return size; | ||
264 | } | ||
265 | |||
266 | |||
267 | /** | ||
268 | * Queues a message in send queue of the logger handle | ||
269 | * | ||
270 | * @param h the logger handle | ||
271 | * @param msg the message to queue | ||
272 | */ | ||
273 | static void | ||
274 | queue_message (struct GNUNET_TESTBED_LOGGER_Handle *h, | ||
275 | struct GNUNET_MessageHeader *msg) | ||
276 | { | ||
277 | struct MessageQueue *mq; | ||
278 | uint16_t type; | ||
279 | uint16_t size; | ||
280 | |||
281 | type = ntohs (msg->type); | ||
282 | size = ntohs (msg->size); | ||
283 | mq = GNUNET_new (struct MessageQueue); | ||
284 | mq->msg = msg; | ||
285 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
286 | "Queueing message of type %u, size %u for sending\n", type, | ||
287 | ntohs (msg->size)); | ||
288 | GNUNET_CONTAINER_DLL_insert_tail (h->mq_head, h->mq_tail, mq); | ||
289 | if (NULL == h->th) | ||
290 | { | ||
291 | h->retry_backoff = GNUNET_TIME_STD_BACKOFF (h->retry_backoff); | ||
292 | h->th = | ||
293 | GNUNET_CLIENT_notify_transmit_ready (h->client, size, | ||
294 | h->retry_backoff, GNUNET_YES, | ||
295 | &transmit_ready_notify, | ||
296 | h); | ||
297 | } | ||
298 | } | ||
299 | |||
300 | |||
301 | /** | ||
302 | * Send the buffered data to the service | ||
303 | * | ||
304 | * @param h the logger handle | ||
305 | */ | ||
306 | static void | ||
307 | dispatch_buffer (struct GNUNET_TESTBED_LOGGER_Handle *h) | ||
308 | { | ||
309 | struct GNUNET_MessageHeader *msg; | ||
310 | size_t msize; | ||
311 | |||
312 | msize = sizeof (struct GNUNET_MessageHeader) + h->bs; | ||
313 | msg = GNUNET_realloc (h->buf, msize); | ||
314 | h->buf = NULL; | ||
315 | memmove (&msg[1], msg, h->bs); | ||
316 | h->bs = 0; | ||
317 | msg->type = htons (GNUNET_MESSAGE_TYPE_TESTBED_LOGGER_MSG); | ||
318 | msg->size = htons (msize); | ||
319 | queue_message (h, msg); | ||
320 | } | ||
321 | |||
322 | |||
323 | /** | ||
324 | * Connect to the testbed logger service | ||
325 | * | ||
326 | * @param cfg configuration to use | ||
327 | * @return the handle which can be used for sending data to the service; NULL | ||
328 | * upon any error | ||
329 | */ | ||
330 | struct GNUNET_TESTBED_LOGGER_Handle * | ||
331 | GNUNET_TESTBED_LOGGER_connect (const struct GNUNET_CONFIGURATION_Handle *cfg) | ||
332 | { | ||
333 | struct GNUNET_TESTBED_LOGGER_Handle *h; | ||
334 | struct GNUNET_CLIENT_Connection *client; | ||
335 | |||
336 | client = GNUNET_CLIENT_connect ("testbed-logger", cfg); | ||
337 | if (NULL == client) | ||
338 | return NULL; | ||
339 | h = GNUNET_new (struct GNUNET_TESTBED_LOGGER_Handle); | ||
340 | h->client = client; | ||
341 | return h; | ||
342 | } | ||
343 | |||
344 | |||
345 | /** | ||
346 | * Disconnect from the logger service. | ||
347 | * | ||
348 | * @param h the logger handle | ||
349 | */ | ||
350 | void | ||
351 | GNUNET_TESTBED_LOGGER_disconnect (struct GNUNET_TESTBED_LOGGER_Handle *h) | ||
352 | { | ||
353 | struct MessageQueue *mq; | ||
354 | unsigned int lost; | ||
355 | |||
356 | if (NULL != h->flush_completion_task) | ||
357 | GNUNET_SCHEDULER_cancel (h->flush_completion_task); | ||
358 | lost = 0; | ||
359 | while (NULL != (mq = h->mq_head)) | ||
360 | { | ||
361 | GNUNET_CONTAINER_DLL_remove (h->mq_head, h->mq_tail, mq); | ||
362 | GNUNET_free (mq->msg); | ||
363 | GNUNET_free (mq); | ||
364 | lost++; | ||
365 | } | ||
366 | if (0 != lost) | ||
367 | LOG (GNUNET_ERROR_TYPE_WARNING, "Cleaning up %u unsent logger message[s]\n", | ||
368 | lost); | ||
369 | GNUNET_CLIENT_disconnect (h->client); | ||
370 | GNUNET_free (h); | ||
371 | } | ||
372 | |||
373 | |||
374 | /** | ||
375 | * Send data to be logged to the logger service. The data will be buffered and | ||
376 | * will be sent upon an explicit call to GNUNET_TESTBED_LOGGER_flush() or upon | ||
377 | * exceeding a threshold size. | ||
378 | * | ||
379 | * @param h the logger handle | ||
380 | * @param data the data to send; | ||
381 | * @param size how many bytes of data to send | ||
382 | */ | ||
383 | void | ||
384 | GNUNET_TESTBED_LOGGER_write (struct GNUNET_TESTBED_LOGGER_Handle *h, | ||
385 | const void *data, size_t size) | ||
386 | { | ||
387 | size_t fit_size; | ||
388 | |||
389 | GNUNET_assert (0 != size); | ||
390 | GNUNET_assert (NULL != data); | ||
391 | GNUNET_assert (size <= (BUFFER_SIZE - sizeof (struct GNUNET_MessageHeader))); | ||
392 | fit_size = sizeof (struct GNUNET_MessageHeader) + h->bs + size; | ||
393 | if ( BUFFER_SIZE < fit_size ) | ||
394 | dispatch_buffer (h); | ||
395 | if (NULL == h->buf) | ||
396 | { | ||
397 | h->buf = GNUNET_malloc (size); | ||
398 | h->bs = size; | ||
399 | memcpy (h->buf, data, size); | ||
400 | goto dispatch_ready; | ||
401 | } | ||
402 | h->buf = GNUNET_realloc (h->buf, h->bs + size); | ||
403 | memcpy (h->buf + h->bs, data, size); | ||
404 | h->bs += size; | ||
405 | |||
406 | dispatch_ready: | ||
407 | if (BUFFER_SIZE == fit_size) | ||
408 | dispatch_buffer (h); | ||
409 | } | ||
410 | |||
411 | |||
412 | /** | ||
413 | * Task to be executed when flushing our local buffer takes longer than timeout | ||
414 | * given to GNUNET_TESTBED_LOGGER_flush(). The flush completion callback will | ||
415 | * be called with 0 as the amount of data sent. | ||
416 | * | ||
417 | * @param cls the logger handle | ||
418 | */ | ||
419 | static void | ||
420 | timeout_flush (void *cls) | ||
421 | { | ||
422 | struct GNUNET_TESTBED_LOGGER_Handle *h = cls; | ||
423 | GNUNET_TESTBED_LOGGER_FlushCompletion cb; | ||
424 | void *cb_cls; | ||
425 | |||
426 | h->timeout_flush_task = NULL; | ||
427 | cb = h->cb; | ||
428 | h->cb = NULL; | ||
429 | cb_cls = h->cb_cls; | ||
430 | h->cb_cls = NULL; | ||
431 | if (NULL != h->flush_completion_task) | ||
432 | { | ||
433 | GNUNET_SCHEDULER_cancel (h->flush_completion_task); | ||
434 | h->flush_completion_task = NULL; | ||
435 | } | ||
436 | if (NULL != cb) | ||
437 | cb (cb_cls, 0); | ||
438 | } | ||
439 | |||
440 | |||
441 | /** | ||
442 | * Flush the buffered data to the logger service | ||
443 | * | ||
444 | * @param h the logger handle | ||
445 | * @param timeout how long to wait before calling the flust completion callback | ||
446 | * @param cb the callback to call after the data is flushed | ||
447 | * @param cb_cls the closure for the above callback | ||
448 | */ | ||
449 | void | ||
450 | GNUNET_TESTBED_LOGGER_flush (struct GNUNET_TESTBED_LOGGER_Handle *h, | ||
451 | struct GNUNET_TIME_Relative timeout, | ||
452 | GNUNET_TESTBED_LOGGER_FlushCompletion cb, | ||
453 | void *cb_cls) | ||
454 | { | ||
455 | h->cb = cb; | ||
456 | h->cb_cls = cb_cls; | ||
457 | GNUNET_assert (NULL == h->timeout_flush_task); | ||
458 | h->timeout_flush_task = | ||
459 | GNUNET_SCHEDULER_add_delayed (timeout, &timeout_flush, h); | ||
460 | if (NULL == h->buf) | ||
461 | { | ||
462 | trigger_flush_notification (h); | ||
463 | return; | ||
464 | } | ||
465 | dispatch_buffer (h); | ||
466 | } | ||
467 | |||
468 | |||
469 | /** | ||
470 | * Cancel notification upon flush. Should only be used when the flush | ||
471 | * completion callback given to GNUNET_TESTBED_LOGGER_flush() is not already | ||
472 | * called. | ||
473 | * | ||
474 | * @param h the logger handle | ||
475 | */ | ||
476 | void | ||
477 | GNUNET_TESTBED_LOGGER_flush_cancel (struct GNUNET_TESTBED_LOGGER_Handle *h) | ||
478 | { | ||
479 | if (NULL != h->flush_completion_task) | ||
480 | { | ||
481 | GNUNET_SCHEDULER_cancel (h->flush_completion_task); | ||
482 | h->flush_completion_task = NULL; | ||
483 | } | ||
484 | if (NULL != h->timeout_flush_task) | ||
485 | cancel_timeout_flush (h); | ||
486 | h->cb = NULL; | ||
487 | h->cb_cls = NULL; | ||
488 | } | ||
489 | |||
490 | /* End of testbed_logger_api.c */ | ||