2 This file is part of GNUnet
3 Copyright (C) 2008--2013, 2016 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-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 * @author Christian Grothoff
29 #include "gnunet_util_lib.h"
30 #include "gnunet_testbed_logger_service.h"
33 * Generic logging shorthand
35 #define LOG(kind, ...) \
36 GNUNET_log_from (kind, "testbed-logger-api", __VA_ARGS__)
40 * The size of the buffer we fill before sending out the message
42 #define BUFFER_SIZE (GNUNET_SERVER_MAX_MESSAGE_SIZE - sizeof (struct GNUNET_MessageHeader))
45 * Connection handle for the logger service
47 struct GNUNET_TESTBED_LOGGER_Handle
52 struct GNUNET_MQ_Handle *mq;
55 * Flush completion callback
57 GNUNET_TESTBED_LOGGER_FlushCompletion cb;
65 * Local buffer for data to be transmitted
67 char buf[BUFFER_SIZE];
70 * How many bytes in @a buf are in use?
75 * Number of bytes wrote since last flush
80 * How long after should we retry sending a message to the service?
82 struct GNUNET_TIME_Relative retry_backoff;
85 * Task to call the flush completion callback
87 struct GNUNET_SCHEDULER_Task *flush_completion_task;
90 * Number of entries in the MQ.
97 * Task to call the flush completion notification
99 * @param cls the logger handle
102 call_flush_completion (void *cls)
104 struct GNUNET_TESTBED_LOGGER_Handle *h = cls;
105 GNUNET_TESTBED_LOGGER_FlushCompletion cb;
109 h->flush_completion_task = NULL;
122 * Schedule the flush completion notification task
124 * @param h logger handle
127 trigger_flush_notification (struct GNUNET_TESTBED_LOGGER_Handle *h)
129 if (NULL != h->flush_completion_task)
130 GNUNET_SCHEDULER_cancel (h->flush_completion_task);
131 h->flush_completion_task
132 = GNUNET_SCHEDULER_add_now (&call_flush_completion,
138 * Send the buffered data to the service
140 * @param h the logger handle
143 dispatch_buffer (struct GNUNET_TESTBED_LOGGER_Handle *h);
147 * MQ successfully sent a message.
149 * @param cls our handle
152 notify_sent (void *cls)
154 struct GNUNET_TESTBED_LOGGER_Handle *h = cls;
157 if ( (0 == h->mq_len) &&
161 trigger_flush_notification (h);
169 * Send the buffered data to the service
171 * @param h the logger handle
174 dispatch_buffer (struct GNUNET_TESTBED_LOGGER_Handle *h)
176 struct GNUNET_MessageHeader *msg;
177 struct GNUNET_MQ_Envelope *env;
179 env = GNUNET_MQ_msg_extra (msg,
181 GNUNET_MESSAGE_TYPE_TESTBED_LOGGER_MSG);
182 GNUNET_memcpy (&msg[1],
185 h->bwrote += h->buse;
188 GNUNET_MQ_notify_sent (env,
191 GNUNET_MQ_send (h->mq,
197 * We got disconnected from the logger. Stop logging.
199 * @param cls the `struct GNUNET_TESTBED_LOGGER_Handle`
200 * @param error error code
203 mq_error_handler (void *cls,
204 enum GNUNET_MQ_Error error)
206 struct GNUNET_TESTBED_LOGGER_Handle *h = cls;
209 GNUNET_MQ_destroy (h->mq);
215 * Connect to the testbed logger service
217 * @param cfg configuration to use
218 * @return the handle which can be used for sending data to the service; NULL
221 struct GNUNET_TESTBED_LOGGER_Handle *
222 GNUNET_TESTBED_LOGGER_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
224 struct GNUNET_TESTBED_LOGGER_Handle *h;
226 h = GNUNET_new (struct GNUNET_TESTBED_LOGGER_Handle);
227 h->mq = GNUNET_CLIENT_connect (cfg,
242 * Disconnect from the logger service.
244 * @param h the logger handle
247 GNUNET_TESTBED_LOGGER_disconnect (struct GNUNET_TESTBED_LOGGER_Handle *h)
249 if (NULL != h->flush_completion_task)
251 GNUNET_SCHEDULER_cancel (h->flush_completion_task);
252 h->flush_completion_task = NULL;
255 LOG (GNUNET_ERROR_TYPE_WARNING,
256 "Disconnect lost %u logger message[s]\n",
260 GNUNET_MQ_destroy (h->mq);
268 * Send data to be logged to the logger service. The data will be buffered and
269 * will be sent upon an explicit call to GNUNET_TESTBED_LOGGER_flush() or upon
270 * exceeding a threshold size.
272 * @param h the logger handle
273 * @param data the data to send;
274 * @param size how many bytes of @a data to send
277 GNUNET_TESTBED_LOGGER_write (struct GNUNET_TESTBED_LOGGER_Handle *h,
285 size_t fit_size = GNUNET_MIN (size,
286 BUFFER_SIZE - h->buse);
287 GNUNET_memcpy (&h->buf[h->buse],
300 * Flush the buffered data to the logger service
302 * @param h the logger handle
303 * @param cb the callback to call after the data is flushed
304 * @param cb_cls the closure for the above callback
307 GNUNET_TESTBED_LOGGER_flush (struct GNUNET_TESTBED_LOGGER_Handle *h,
308 GNUNET_TESTBED_LOGGER_FlushCompletion cb,
311 GNUNET_assert (NULL == h->cb);
314 if ( (NULL == h->mq) ||
317 trigger_flush_notification (h);
325 * Cancel notification upon flush. Should only be used when the flush
326 * completion callback given to GNUNET_TESTBED_LOGGER_flush() is not already
329 * @param h the logger handle
332 GNUNET_TESTBED_LOGGER_flush_cancel (struct GNUNET_TESTBED_LOGGER_Handle *h)
334 if (NULL != h->flush_completion_task)
336 GNUNET_SCHEDULER_cancel (h->flush_completion_task);
337 h->flush_completion_task = NULL;
343 /* End of testbed_logger_api.c */