2 This file is part of GNUnet
3 (C) 2008--2013 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.
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
173 * @param tc the scheduler task context
176 call_flush_completion (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
178 struct GNUNET_TESTBED_LOGGER_Handle *h = cls;
179 GNUNET_TESTBED_LOGGER_FlushCompletion cb;
183 h->flush_completion_task = NULL;
190 if (NULL != h->timeout_flush_task)
191 cancel_timeout_flush (h);
198 * Schedule the flush completion notification task
200 * @param h logger handle
203 trigger_flush_notification (struct GNUNET_TESTBED_LOGGER_Handle *h)
205 if (NULL != h->flush_completion_task)
206 GNUNET_SCHEDULER_cancel (h->flush_completion_task);
207 h->flush_completion_task = GNUNET_SCHEDULER_add_now (&call_flush_completion, h);
212 * Function called to notify a client about the connection begin ready to queue
213 * more data. "buf" will be NULL and "size" zero if the connection was closed
214 * for writing in the meantime.
217 * @param size number of bytes available in buf
218 * @param buf where the callee should write the message
219 * @return number of bytes written to buf
222 transmit_ready_notify (void *cls, size_t size, void *buf)
224 struct GNUNET_TESTBED_LOGGER_Handle *h = cls;
225 struct MessageQueue *mq;
229 GNUNET_assert (NULL != mq);
230 if ((0 == size) && (NULL == buf)) /* Timeout */
232 LOG_DEBUG ("Message sending timed out -- retrying\n");
233 h->retry_backoff = GNUNET_TIME_STD_BACKOFF (h->retry_backoff);
235 GNUNET_CLIENT_notify_transmit_ready (h->client,
236 ntohs (mq->msg->size),
237 h->retry_backoff, GNUNET_YES,
238 &transmit_ready_notify, h);
241 h->retry_backoff = GNUNET_TIME_UNIT_ZERO;
242 GNUNET_assert (ntohs (mq->msg->size) <= size);
243 size = ntohs (mq->msg->size);
244 memcpy (buf, mq->msg, size);
245 LOG_DEBUG ("Message of type: %u and size: %u sent\n",
246 ntohs (mq->msg->type), size);
247 GNUNET_free (mq->msg);
248 GNUNET_CONTAINER_DLL_remove (h->mq_head, h->mq_tail, mq);
250 h->bwrote += (size - sizeof (struct GNUNET_MessageHeader));
254 h->retry_backoff = GNUNET_TIME_STD_BACKOFF (h->retry_backoff);
256 GNUNET_CLIENT_notify_transmit_ready (h->client,
257 ntohs (mq->msg->size),
258 h->retry_backoff, GNUNET_YES,
259 &transmit_ready_notify, h);
263 trigger_flush_notification (h); /* Call the flush completion callback */
269 * Queues a message in send queue of the logger handle
271 * @param h the logger handle
272 * @param msg the message to queue
275 queue_message (struct GNUNET_TESTBED_LOGGER_Handle *h,
276 struct GNUNET_MessageHeader *msg)
278 struct MessageQueue *mq;
282 type = ntohs (msg->type);
283 size = ntohs (msg->size);
284 mq = GNUNET_new (struct MessageQueue);
286 LOG (GNUNET_ERROR_TYPE_DEBUG,
287 "Queueing message of type %u, size %u for sending\n", type,
289 GNUNET_CONTAINER_DLL_insert_tail (h->mq_head, h->mq_tail, mq);
292 h->retry_backoff = GNUNET_TIME_STD_BACKOFF (h->retry_backoff);
294 GNUNET_CLIENT_notify_transmit_ready (h->client, size,
295 h->retry_backoff, GNUNET_YES,
296 &transmit_ready_notify,
303 * Send the buffered data to the service
305 * @param h the logger handle
308 dispatch_buffer (struct GNUNET_TESTBED_LOGGER_Handle *h)
310 struct GNUNET_MessageHeader *msg;
313 msize = sizeof (struct GNUNET_MessageHeader) + h->bs;
314 msg = GNUNET_realloc (h->buf, msize);
316 memmove (&msg[1], msg, h->bs);
318 msg->type = htons (GNUNET_MESSAGE_TYPE_TESTBED_LOGGER_MSG);
319 msg->size = htons (msize);
320 queue_message (h, msg);
325 * Connect to the testbed logger service
327 * @param cfg configuration to use
328 * @return the handle which can be used for sending data to the service; NULL
331 struct GNUNET_TESTBED_LOGGER_Handle *
332 GNUNET_TESTBED_LOGGER_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
334 struct GNUNET_TESTBED_LOGGER_Handle *h;
335 struct GNUNET_CLIENT_Connection *client;
337 client = GNUNET_CLIENT_connect ("testbed-logger", cfg);
340 h = GNUNET_new (struct GNUNET_TESTBED_LOGGER_Handle);
347 * Disconnect from the logger service.
349 * @param h the logger handle
352 GNUNET_TESTBED_LOGGER_disconnect (struct GNUNET_TESTBED_LOGGER_Handle *h)
354 struct MessageQueue *mq;
357 if (NULL != h->flush_completion_task)
358 GNUNET_SCHEDULER_cancel (h->flush_completion_task);
360 while (NULL != (mq = h->mq_head))
362 GNUNET_CONTAINER_DLL_remove (h->mq_head, h->mq_tail, mq);
363 GNUNET_free (mq->msg);
368 LOG (GNUNET_ERROR_TYPE_WARNING, "Cleaning up %u unsent logger message[s]\n",
370 GNUNET_CLIENT_disconnect (h->client);
376 * Send data to be logged to the logger service. The data will be buffered and
377 * will be sent upon an explicit call to GNUNET_TESTBED_LOGGER_flush() or upon
378 * exceeding a threshold size.
380 * @param h the logger handle
381 * @param data the data to send;
382 * @param size how many bytes of data to send
385 GNUNET_TESTBED_LOGGER_write (struct GNUNET_TESTBED_LOGGER_Handle *h,
386 const void *data, size_t size)
390 GNUNET_assert (0 != size);
391 GNUNET_assert (NULL != data);
392 GNUNET_assert (size <= (BUFFER_SIZE - sizeof (struct GNUNET_MessageHeader)));
393 fit_size = sizeof (struct GNUNET_MessageHeader) + h->bs + size;
394 if ( BUFFER_SIZE < fit_size )
398 h->buf = GNUNET_malloc (size);
400 memcpy (h->buf, data, size);
403 h->buf = GNUNET_realloc (h->buf, h->bs + size);
404 memcpy (h->buf + h->bs, data, size);
408 if (BUFFER_SIZE == fit_size)
414 * Task to be executed when flushing our local buffer takes longer than timeout
415 * given to GNUNET_TESTBED_LOGGER_flush(). The flush completion callback will
416 * be called with 0 as the amount of data sent.
418 * @param cls the logger handle
419 * @param tc scheduler task context
422 timeout_flush (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
424 struct GNUNET_TESTBED_LOGGER_Handle *h = cls;
425 GNUNET_TESTBED_LOGGER_FlushCompletion cb;
428 h->timeout_flush_task = NULL;
433 if (NULL != h->flush_completion_task)
435 GNUNET_SCHEDULER_cancel (h->flush_completion_task);
436 h->flush_completion_task = NULL;
444 * Flush the buffered data to the logger service
446 * @param h the logger handle
447 * @param timeout how long to wait before calling the flust completion callback
448 * @param cb the callback to call after the data is flushed
449 * @param cb_cls the closure for the above callback
452 GNUNET_TESTBED_LOGGER_flush (struct GNUNET_TESTBED_LOGGER_Handle *h,
453 struct GNUNET_TIME_Relative timeout,
454 GNUNET_TESTBED_LOGGER_FlushCompletion cb,
459 GNUNET_assert (NULL == h->timeout_flush_task);
460 h->timeout_flush_task =
461 GNUNET_SCHEDULER_add_delayed (timeout, &timeout_flush, h);
464 trigger_flush_notification (h);
472 * Cancel notification upon flush. Should only be used when the flush
473 * completion callback given to GNUNET_TESTBED_LOGGER_flush() is not already
476 * @param h the logger handle
479 GNUNET_TESTBED_LOGGER_flush_cancel (struct GNUNET_TESTBED_LOGGER_Handle *h)
481 if (NULL != h->flush_completion_task)
483 GNUNET_SCHEDULER_cancel (h->flush_completion_task);
484 h->flush_completion_task = NULL;
486 if (NULL != h->timeout_flush_task)
487 cancel_timeout_flush (h);
492 /* End of testbed_logger_api.c */