2 This file is part of GNUnet
3 Copyright (C) 2008--2013 GNUnet e.V.
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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
22 * @file testbed/testbed_logger_api.c
23 * @brief Client-side routines for communicating with the tesbted logger service
24 * @author Sree Harsha Totakura <sreeharsha@totakura.in>
28 #include "gnunet_util_lib.h"
29 #include "gnunet_testbed_logger_service.h"
32 * Generic logging shorthand
34 #define LOG(kind, ...) \
35 GNUNET_log_from (kind, "testbed-logger-api", __VA_ARGS__)
40 #define LOG_DEBUG(...) \
41 LOG (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
43 #ifdef GNUNET_TIME_STD_EXPONENTIAL_BACKOFF_THRESHOLD
44 #undef GNUNET_TIME_STD_EXPONENTIAL_BACKOFF_THRESHOLD
48 * Threshold after which exponential backoff should not increase (15 s).
50 #define GNUNET_TIME_STD_EXPONENTIAL_BACKOFF_THRESHOLD GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 3)
53 * The size of the buffer we fill before sending out the message
55 #define BUFFER_SIZE GNUNET_SERVER_MAX_MESSAGE_SIZE
58 * The message queue for sending messages to the controller service
63 * next pointer for DLL
65 struct MessageQueue *next;
68 * prev pointer for DLL
70 struct MessageQueue *prev;
73 * The message to be sent
75 struct GNUNET_MessageHeader *msg;
80 GNUNET_TESTBED_LOGGER_FlushCompletion cb;
90 * Connection handle for the logger service
92 struct GNUNET_TESTBED_LOGGER_Handle
97 struct GNUNET_CLIENT_Connection *client;
100 * The transport handle
102 struct GNUNET_CLIENT_TransmitHandle *th;
105 * DLL head for the message queue
107 struct MessageQueue *mq_head;
110 * DLL tail for the message queue
112 struct MessageQueue *mq_tail;
115 * Flush completion callback
117 GNUNET_TESTBED_LOGGER_FlushCompletion cb;
120 * Closure for the above callback
125 * Local buffer for data to be transmitted
130 * The size of the local buffer
135 * Number of bytes wrote since last flush
140 * How long after should we retry sending a message to the service?
142 struct GNUNET_TIME_Relative retry_backoff;
145 * Task to call the flush completion callback
147 struct GNUNET_SCHEDULER_Task * flush_completion_task;
150 * Task to be executed when flushing takes too long
152 struct GNUNET_SCHEDULER_Task * timeout_flush_task;
157 * Cancels the flush timeout task
159 * @param h handle to the logger
162 cancel_timeout_flush (struct GNUNET_TESTBED_LOGGER_Handle *h)
164 GNUNET_SCHEDULER_cancel (h->timeout_flush_task);
165 h->timeout_flush_task = NULL;
170 * Task to call the flush completion notification
172 * @param cls the logger handle
175 call_flush_completion (void *cls)
177 struct GNUNET_TESTBED_LOGGER_Handle *h = cls;
178 GNUNET_TESTBED_LOGGER_FlushCompletion cb;
182 h->flush_completion_task = NULL;
189 if (NULL != h->timeout_flush_task)
190 cancel_timeout_flush (h);
197 * Schedule the flush completion notification task
199 * @param h logger handle
202 trigger_flush_notification (struct GNUNET_TESTBED_LOGGER_Handle *h)
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);
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.
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
221 transmit_ready_notify (void *cls, size_t size, void *buf)
223 struct GNUNET_TESTBED_LOGGER_Handle *h = cls;
224 struct MessageQueue *mq;
228 GNUNET_assert (NULL != mq);
229 if ((0 == size) && (NULL == buf)) /* Timeout */
231 LOG_DEBUG ("Message sending timed out -- retrying\n");
232 h->retry_backoff = GNUNET_TIME_STD_BACKOFF (h->retry_backoff);
234 GNUNET_CLIENT_notify_transmit_ready (h->client,
235 ntohs (mq->msg->size),
236 h->retry_backoff, GNUNET_YES,
237 &transmit_ready_notify, h);
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);
249 h->bwrote += (size - sizeof (struct GNUNET_MessageHeader));
253 h->retry_backoff = GNUNET_TIME_STD_BACKOFF (h->retry_backoff);
255 GNUNET_CLIENT_notify_transmit_ready (h->client,
256 ntohs (mq->msg->size),
257 h->retry_backoff, GNUNET_YES,
258 &transmit_ready_notify, h);
262 trigger_flush_notification (h); /* Call the flush completion callback */
268 * Queues a message in send queue of the logger handle
270 * @param h the logger handle
271 * @param msg the message to queue
274 queue_message (struct GNUNET_TESTBED_LOGGER_Handle *h,
275 struct GNUNET_MessageHeader *msg)
277 struct MessageQueue *mq;
281 type = ntohs (msg->type);
282 size = ntohs (msg->size);
283 mq = GNUNET_new (struct MessageQueue);
285 LOG (GNUNET_ERROR_TYPE_DEBUG,
286 "Queueing message of type %u, size %u for sending\n", type,
288 GNUNET_CONTAINER_DLL_insert_tail (h->mq_head, h->mq_tail, mq);
291 h->retry_backoff = GNUNET_TIME_STD_BACKOFF (h->retry_backoff);
293 GNUNET_CLIENT_notify_transmit_ready (h->client, size,
294 h->retry_backoff, GNUNET_YES,
295 &transmit_ready_notify,
302 * Send the buffered data to the service
304 * @param h the logger handle
307 dispatch_buffer (struct GNUNET_TESTBED_LOGGER_Handle *h)
309 struct GNUNET_MessageHeader *msg;
312 msize = sizeof (struct GNUNET_MessageHeader) + h->bs;
313 msg = GNUNET_realloc (h->buf, msize);
315 memmove (&msg[1], msg, h->bs);
317 msg->type = htons (GNUNET_MESSAGE_TYPE_TESTBED_LOGGER_MSG);
318 msg->size = htons (msize);
319 queue_message (h, msg);
324 * Connect to the testbed logger service
326 * @param cfg configuration to use
327 * @return the handle which can be used for sending data to the service; NULL
330 struct GNUNET_TESTBED_LOGGER_Handle *
331 GNUNET_TESTBED_LOGGER_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
333 struct GNUNET_TESTBED_LOGGER_Handle *h;
334 struct GNUNET_CLIENT_Connection *client;
336 client = GNUNET_CLIENT_connect ("testbed-logger", cfg);
339 h = GNUNET_new (struct GNUNET_TESTBED_LOGGER_Handle);
346 * Disconnect from the logger service.
348 * @param h the logger handle
351 GNUNET_TESTBED_LOGGER_disconnect (struct GNUNET_TESTBED_LOGGER_Handle *h)
353 struct MessageQueue *mq;
356 if (NULL != h->flush_completion_task)
357 GNUNET_SCHEDULER_cancel (h->flush_completion_task);
359 while (NULL != (mq = h->mq_head))
361 GNUNET_CONTAINER_DLL_remove (h->mq_head, h->mq_tail, mq);
362 GNUNET_free (mq->msg);
367 LOG (GNUNET_ERROR_TYPE_WARNING, "Cleaning up %u unsent logger message[s]\n",
369 GNUNET_CLIENT_disconnect (h->client);
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.
379 * @param h the logger handle
380 * @param data the data to send;
381 * @param size how many bytes of data to send
384 GNUNET_TESTBED_LOGGER_write (struct GNUNET_TESTBED_LOGGER_Handle *h,
385 const void *data, size_t size)
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 )
397 h->buf = GNUNET_malloc (size);
399 memcpy (h->buf, data, size);
402 h->buf = GNUNET_realloc (h->buf, h->bs + size);
403 memcpy (h->buf + h->bs, data, size);
407 if (BUFFER_SIZE == fit_size)
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.
417 * @param cls the logger handle
420 timeout_flush (void *cls)
422 struct GNUNET_TESTBED_LOGGER_Handle *h = cls;
423 GNUNET_TESTBED_LOGGER_FlushCompletion cb;
426 h->timeout_flush_task = NULL;
431 if (NULL != h->flush_completion_task)
433 GNUNET_SCHEDULER_cancel (h->flush_completion_task);
434 h->flush_completion_task = NULL;
442 * Flush the buffered data to the logger service
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
450 GNUNET_TESTBED_LOGGER_flush (struct GNUNET_TESTBED_LOGGER_Handle *h,
451 struct GNUNET_TIME_Relative timeout,
452 GNUNET_TESTBED_LOGGER_FlushCompletion cb,
457 GNUNET_assert (NULL == h->timeout_flush_task);
458 h->timeout_flush_task =
459 GNUNET_SCHEDULER_add_delayed (timeout, &timeout_flush, h);
462 trigger_flush_notification (h);
470 * Cancel notification upon flush. Should only be used when the flush
471 * completion callback given to GNUNET_TESTBED_LOGGER_flush() is not already
474 * @param h the logger handle
477 GNUNET_TESTBED_LOGGER_flush_cancel (struct GNUNET_TESTBED_LOGGER_Handle *h)
479 if (NULL != h->flush_completion_task)
481 GNUNET_SCHEDULER_cancel (h->flush_completion_task);
482 h->flush_completion_task = NULL;
484 if (NULL != h->timeout_flush_task)
485 cancel_timeout_flush (h);
490 /* End of testbed_logger_api.c */