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 it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
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_MAX_MESSAGE_SIZE - sizeof(struct \
43 GNUNET_MessageHeader))
46 * Connection handle for the logger service
48 struct GNUNET_TESTBED_LOGGER_Handle
53 struct GNUNET_MQ_Handle *mq;
56 * Flush completion callback
58 GNUNET_TESTBED_LOGGER_FlushCompletion cb;
66 * Local buffer for data to be transmitted
68 char buf[BUFFER_SIZE];
71 * How many bytes in @a buf are in use?
76 * Number of bytes wrote since last flush
81 * How long after should we retry sending a message to the service?
83 struct GNUNET_TIME_Relative retry_backoff;
86 * Task to call the flush completion callback
88 struct GNUNET_SCHEDULER_Task *flush_completion_task;
91 * Number of entries in the MQ.
98 * Task to call the flush completion notification
100 * @param cls the logger handle
103 call_flush_completion (void *cls)
105 struct GNUNET_TESTBED_LOGGER_Handle *h = cls;
106 GNUNET_TESTBED_LOGGER_FlushCompletion cb;
110 h->flush_completion_task = NULL;
123 * Schedule the flush completion notification task
125 * @param h logger handle
128 trigger_flush_notification (struct GNUNET_TESTBED_LOGGER_Handle *h)
130 if (NULL != h->flush_completion_task)
131 GNUNET_SCHEDULER_cancel (h->flush_completion_task);
132 h->flush_completion_task
133 = GNUNET_SCHEDULER_add_now (&call_flush_completion,
139 * Send the buffered data to the service
141 * @param h the logger handle
144 dispatch_buffer (struct GNUNET_TESTBED_LOGGER_Handle *h);
148 * MQ successfully sent a message.
150 * @param cls our handle
153 notify_sent (void *cls)
155 struct GNUNET_TESTBED_LOGGER_Handle *h = cls;
158 if ((0 == h->mq_len) &&
162 trigger_flush_notification (h);
170 * Send the buffered data to the service
172 * @param h the logger handle
175 dispatch_buffer (struct GNUNET_TESTBED_LOGGER_Handle *h)
177 struct GNUNET_MessageHeader *msg;
178 struct GNUNET_MQ_Envelope *env;
180 env = GNUNET_MQ_msg_extra (msg,
182 GNUNET_MESSAGE_TYPE_TESTBED_LOGGER_MSG);
183 GNUNET_memcpy (&msg[1],
186 h->bwrote += h->buse;
189 GNUNET_MQ_notify_sent (env,
192 GNUNET_MQ_send (h->mq,
198 * We got disconnected from the logger. Stop logging.
200 * @param cls the `struct GNUNET_TESTBED_LOGGER_Handle`
201 * @param error error code
204 mq_error_handler (void *cls,
205 enum GNUNET_MQ_Error error)
207 struct GNUNET_TESTBED_LOGGER_Handle *h = cls;
210 GNUNET_MQ_destroy (h->mq);
216 * Connect to the testbed logger service
218 * @param cfg configuration to use
219 * @return the handle which can be used for sending data to the service; NULL
222 struct GNUNET_TESTBED_LOGGER_Handle *
223 GNUNET_TESTBED_LOGGER_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
225 struct GNUNET_TESTBED_LOGGER_Handle *h;
227 h = GNUNET_new (struct GNUNET_TESTBED_LOGGER_Handle);
228 h->mq = GNUNET_CLIENT_connect (cfg,
243 * Disconnect from the logger service.
245 * @param h the logger handle
248 GNUNET_TESTBED_LOGGER_disconnect (struct GNUNET_TESTBED_LOGGER_Handle *h)
250 if (NULL != h->flush_completion_task)
252 GNUNET_SCHEDULER_cancel (h->flush_completion_task);
253 h->flush_completion_task = NULL;
256 LOG (GNUNET_ERROR_TYPE_WARNING,
257 "Disconnect lost %u logger message[s]\n",
261 GNUNET_MQ_destroy (h->mq);
269 * Send data to be logged to the logger service. The data will be buffered and
270 * will be sent upon an explicit call to GNUNET_TESTBED_LOGGER_flush() or upon
271 * exceeding a threshold size.
273 * @param h the logger handle
274 * @param data the data to send;
275 * @param size how many bytes of @a data to send
278 GNUNET_TESTBED_LOGGER_write (struct GNUNET_TESTBED_LOGGER_Handle *h,
286 size_t fit_size = GNUNET_MIN (size,
287 BUFFER_SIZE - h->buse);
288 GNUNET_memcpy (&h->buf[h->buse],
301 * Flush the buffered data to the logger service
303 * @param h the logger handle
304 * @param cb the callback to call after the data is flushed
305 * @param cb_cls the closure for the above callback
308 GNUNET_TESTBED_LOGGER_flush (struct GNUNET_TESTBED_LOGGER_Handle *h,
309 GNUNET_TESTBED_LOGGER_FlushCompletion cb,
312 GNUNET_assert (NULL == h->cb);
315 if ((NULL == h->mq) ||
318 trigger_flush_notification (h);
326 * Cancel notification upon flush. Should only be used when the flush
327 * completion callback given to GNUNET_TESTBED_LOGGER_flush() is not already
330 * @param h the logger handle
333 GNUNET_TESTBED_LOGGER_flush_cancel (struct GNUNET_TESTBED_LOGGER_Handle *h)
335 if (NULL != h->flush_completion_task)
337 GNUNET_SCHEDULER_cancel (h->flush_completion_task);
338 h->flush_completion_task = NULL;
345 /* End of testbed_logger_api.c */