aboutsummaryrefslogtreecommitdiff
path: root/src/testbed-logger
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2016-06-24 14:34:07 +0000
committerChristian Grothoff <christian@grothoff.org>2016-06-24 14:34:07 +0000
commit4a99626e6a02f71ed4d492d3454f22e1674c9c13 (patch)
tree482efee58be8f89cc1f5788918fa6635ff8e4c9d /src/testbed-logger
parent71c68d0bbdb546bfe84b4536b447899c53cb3812 (diff)
downloadgnunet-4a99626e6a02f71ed4d492d3454f22e1674c9c13.tar.gz
gnunet-4a99626e6a02f71ed4d492d3454f22e1674c9c13.zip
adapting testbed-logger to MQ API
Diffstat (limited to 'src/testbed-logger')
-rw-r--r--src/testbed-logger/test_testbed_logger_api.c1
-rw-r--r--src/testbed-logger/testbed_logger_api.c353
2 files changed, 103 insertions, 251 deletions
diff --git a/src/testbed-logger/test_testbed_logger_api.c b/src/testbed-logger/test_testbed_logger_api.c
index 8f7391f22..0ebe0c3f4 100644
--- a/src/testbed-logger/test_testbed_logger_api.c
+++ b/src/testbed-logger/test_testbed_logger_api.c
@@ -209,7 +209,6 @@ do_write (void *cls)
209 if (0 == i++) 209 if (0 == i++)
210 return; 210 return;
211 GNUNET_TESTBED_LOGGER_flush (h, 211 GNUNET_TESTBED_LOGGER_flush (h,
212 GNUNET_TIME_UNIT_FOREVER_REL,
213 &flush_comp, 212 &flush_comp,
214 &write_task); 213 &write_task);
215} 214}
diff --git a/src/testbed-logger/testbed_logger_api.c b/src/testbed-logger/testbed_logger_api.c
index aaf18cd33..869054cf3 100644
--- a/src/testbed-logger/testbed_logger_api.c
+++ b/src/testbed-logger/testbed_logger_api.c
@@ -1,6 +1,6 @@
1/* 1/*
2 This file is part of GNUnet 2 This file is part of GNUnet
3 Copyright (C) 2008--2013 GNUnet e.V. 3 Copyright (C) 2008--2013, 2016 GNUnet e.V.
4 4
5 GNUnet is free software; you can redistribute it and/or modify 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 6 it under the terms of the GNU General Public License as published
@@ -22,6 +22,7 @@
22 * @file testbed-logger/testbed_logger_api.c 22 * @file testbed-logger/testbed_logger_api.c
23 * @brief Client-side routines for communicating with the tesbted logger service 23 * @brief Client-side routines for communicating with the tesbted logger service
24 * @author Sree Harsha Totakura <sreeharsha@totakura.in> 24 * @author Sree Harsha Totakura <sreeharsha@totakura.in>
25 * @author Christian Grothoff
25 */ 26 */
26 27
27#include "platform.h" 28#include "platform.h"
@@ -34,57 +35,11 @@
34#define LOG(kind, ...) \ 35#define LOG(kind, ...) \
35 GNUNET_log_from (kind, "testbed-logger-api", __VA_ARGS__) 36 GNUNET_log_from (kind, "testbed-logger-api", __VA_ARGS__)
36 37
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 38
52/** 39/**
53 * The size of the buffer we fill before sending out the message 40 * The size of the buffer we fill before sending out the message
54 */ 41 */
55#define BUFFER_SIZE GNUNET_SERVER_MAX_MESSAGE_SIZE 42#define BUFFER_SIZE (GNUNET_SERVER_MAX_MESSAGE_SIZE - sizeof (struct GNUNET_MessageHeader))
56
57/**
58 * The message queue for sending messages to the controller service
59 */
60struct 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 43
89/** 44/**
90 * Connection handle for the logger service 45 * Connection handle for the logger service
@@ -94,22 +49,7 @@ struct GNUNET_TESTBED_LOGGER_Handle
94 /** 49 /**
95 * Client connection 50 * Client connection
96 */ 51 */
97 struct GNUNET_CLIENT_Connection *client; 52 struct GNUNET_MQ_Handle *mq;
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 53
114 /** 54 /**
115 * Flush completion callback 55 * Flush completion callback
@@ -117,19 +57,19 @@ struct GNUNET_TESTBED_LOGGER_Handle
117 GNUNET_TESTBED_LOGGER_FlushCompletion cb; 57 GNUNET_TESTBED_LOGGER_FlushCompletion cb;
118 58
119 /** 59 /**
120 * Closure for the above callback 60 * Closure for @e cb
121 */ 61 */
122 void *cb_cls; 62 void *cb_cls;
123 63
124 /** 64 /**
125 * Local buffer for data to be transmitted 65 * Local buffer for data to be transmitted
126 */ 66 */
127 void *buf; 67 char buf[BUFFER_SIZE];
128 68
129 /** 69 /**
130 * The size of the local buffer 70 * How many bytes in @a buf are in use?
131 */ 71 */
132 size_t bs; 72 size_t buse;
133 73
134 /** 74 /**
135 * Number of bytes wrote since last flush 75 * Number of bytes wrote since last flush
@@ -144,29 +84,16 @@ struct GNUNET_TESTBED_LOGGER_Handle
144 /** 84 /**
145 * Task to call the flush completion callback 85 * Task to call the flush completion callback
146 */ 86 */
147 struct GNUNET_SCHEDULER_Task * flush_completion_task; 87 struct GNUNET_SCHEDULER_Task *flush_completion_task;
148 88
149 /** 89 /**
150 * Task to be executed when flushing takes too long 90 * Number of entries in the MQ.
151 */ 91 */
152 struct GNUNET_SCHEDULER_Task * timeout_flush_task; 92 unsigned int mq_len;
153}; 93};
154 94
155 95
156/** 96/**
157 * Cancels the flush timeout task
158 *
159 * @param h handle to the logger
160 */
161static void
162cancel_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 97 * Task to call the flush completion notification
171 * 98 *
172 * @param cls the logger handle 99 * @param cls the logger handle
@@ -186,8 +113,6 @@ call_flush_completion (void *cls)
186 h->cb = NULL; 113 h->cb = NULL;
187 cb_cls = h->cb_cls; 114 cb_cls = h->cb_cls;
188 h->cb_cls = NULL; 115 h->cb_cls = NULL;
189 if (NULL != h->timeout_flush_task)
190 cancel_timeout_flush (h);
191 if (NULL != cb) 116 if (NULL != cb)
192 cb (cb_cls, bw); 117 cb (cb_cls, bw);
193} 118}
@@ -203,97 +128,39 @@ trigger_flush_notification (struct GNUNET_TESTBED_LOGGER_Handle *h)
203{ 128{
204 if (NULL != h->flush_completion_task) 129 if (NULL != h->flush_completion_task)
205 GNUNET_SCHEDULER_cancel (h->flush_completion_task); 130 GNUNET_SCHEDULER_cancel (h->flush_completion_task);
206 h->flush_completion_task = GNUNET_SCHEDULER_add_now (&call_flush_completion, h); 131 h->flush_completion_task
132 = GNUNET_SCHEDULER_add_now (&call_flush_completion,
133 h);
207} 134}
208 135
209 136
210/** 137/**
211 * Function called to notify a client about the connection begin ready to queue 138 * Send the buffered data to the service
212 * more data. "buf" will be NULL and "size" zero if the connection was closed
213 * for writing in the meantime.
214 * 139 *
215 * @param cls closure 140 * @param h the logger handle
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 */ 141 */
220static size_t 142static void
221transmit_ready_notify (void *cls, size_t size, void *buf) 143dispatch_buffer (struct GNUNET_TESTBED_LOGGER_Handle *h);
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 144
266 145
267/** 146/**
268 * Queues a message in send queue of the logger handle 147 * MQ successfully sent a message.
269 * 148 *
270 * @param h the logger handle 149 * @param cls our handle
271 * @param msg the message to queue
272 */ 150 */
273static void 151static void
274queue_message (struct GNUNET_TESTBED_LOGGER_Handle *h, 152notify_sent (void *cls)
275 struct GNUNET_MessageHeader *msg)
276{ 153{
277 struct MessageQueue *mq; 154 struct GNUNET_TESTBED_LOGGER_Handle *h = cls;
278 uint16_t type; 155
279 uint16_t size; 156 h->mq_len--;
280 157 if ( (0 == h->mq_len) &&
281 type = ntohs (msg->type); 158 (NULL != h->cb) )
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 { 159 {
291 h->retry_backoff = GNUNET_TIME_STD_BACKOFF (h->retry_backoff); 160 if (0 == h->buse)
292 h->th = 161 trigger_flush_notification (h);
293 GNUNET_CLIENT_notify_transmit_ready (h->client, size, 162 else
294 h->retry_backoff, GNUNET_YES, 163 dispatch_buffer (h);
295 &transmit_ready_notify,
296 h);
297 } 164 }
298} 165}
299 166
@@ -307,16 +174,40 @@ static void
307dispatch_buffer (struct GNUNET_TESTBED_LOGGER_Handle *h) 174dispatch_buffer (struct GNUNET_TESTBED_LOGGER_Handle *h)
308{ 175{
309 struct GNUNET_MessageHeader *msg; 176 struct GNUNET_MessageHeader *msg;
310 size_t msize; 177 struct GNUNET_MQ_Envelope *env;
311 178
312 msize = sizeof (struct GNUNET_MessageHeader) + h->bs; 179 env = GNUNET_MQ_msg_extra (msg,
313 msg = GNUNET_realloc (h->buf, msize); 180 h->buse,
314 h->buf = NULL; 181 GNUNET_MESSAGE_TYPE_TESTBED_LOGGER_MSG);
315 memmove (&msg[1], msg, h->bs); 182 memcpy (&msg[1],
316 h->bs = 0; 183 h->buf,
317 msg->type = htons (GNUNET_MESSAGE_TYPE_TESTBED_LOGGER_MSG); 184 h->buse);
318 msg->size = htons (msize); 185 h->bwrote += h->buse;
319 queue_message (h, msg); 186 h->buse = 0;
187 h->mq_len++;
188 GNUNET_MQ_notify_sent (env,
189 &notify_sent,
190 h);
191 GNUNET_MQ_send (h->mq,
192 env);
193}
194
195
196/**
197 * We got disconnected from the logger. Stop logging.
198 *
199 * @param cls the `struct GNUNET_TESTBED_LOGGER_Handle`
200 * @param error error code
201 */
202static void
203mq_error_handler (void *cls,
204 enum GNUNET_MQ_Error error)
205{
206 struct GNUNET_TESTBED_LOGGER_Handle *h = cls;
207
208 GNUNET_break (0);
209 GNUNET_MQ_destroy (h->mq);
210 h->mq = NULL;
320} 211}
321 212
322 213
@@ -331,13 +222,18 @@ struct GNUNET_TESTBED_LOGGER_Handle *
331GNUNET_TESTBED_LOGGER_connect (const struct GNUNET_CONFIGURATION_Handle *cfg) 222GNUNET_TESTBED_LOGGER_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
332{ 223{
333 struct GNUNET_TESTBED_LOGGER_Handle *h; 224 struct GNUNET_TESTBED_LOGGER_Handle *h;
334 struct GNUNET_CLIENT_Connection *client;
335 225
336 client = GNUNET_CLIENT_connect ("testbed-logger", cfg);
337 if (NULL == client)
338 return NULL;
339 h = GNUNET_new (struct GNUNET_TESTBED_LOGGER_Handle); 226 h = GNUNET_new (struct GNUNET_TESTBED_LOGGER_Handle);
340 h->client = client; 227 h->mq = GNUNET_CLIENT_connecT (cfg,
228 "testbed-logger",
229 NULL,
230 &mq_error_handler,
231 h);
232 if (NULL == h->mq)
233 {
234 GNUNET_free (h);
235 return NULL;
236 }
341 return h; 237 return h;
342} 238}
343 239
@@ -350,23 +246,20 @@ GNUNET_TESTBED_LOGGER_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
350void 246void
351GNUNET_TESTBED_LOGGER_disconnect (struct GNUNET_TESTBED_LOGGER_Handle *h) 247GNUNET_TESTBED_LOGGER_disconnect (struct GNUNET_TESTBED_LOGGER_Handle *h)
352{ 248{
353 struct MessageQueue *mq;
354 unsigned int lost;
355
356 if (NULL != h->flush_completion_task) 249 if (NULL != h->flush_completion_task)
250 {
357 GNUNET_SCHEDULER_cancel (h->flush_completion_task); 251 GNUNET_SCHEDULER_cancel (h->flush_completion_task);
358 lost = 0; 252 h->flush_completion_task = NULL;
359 while (NULL != (mq = h->mq_head)) 253 }
254 if (0 != h->mq_len)
255 LOG (GNUNET_ERROR_TYPE_WARNING,
256 "Disconnect lost %u logger message[s]\n",
257 h->mq_len);
258 if (NULL != h->mq)
360 { 259 {
361 GNUNET_CONTAINER_DLL_remove (h->mq_head, h->mq_tail, mq); 260 GNUNET_MQ_destroy (h->mq);
362 GNUNET_free (mq->msg); 261 h->mq = NULL;
363 GNUNET_free (mq);
364 lost++;
365 } 262 }
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); 263 GNUNET_free (h);
371} 264}
372 265
@@ -378,63 +271,28 @@ GNUNET_TESTBED_LOGGER_disconnect (struct GNUNET_TESTBED_LOGGER_Handle *h)
378 * 271 *
379 * @param h the logger handle 272 * @param h the logger handle
380 * @param data the data to send; 273 * @param data the data to send;
381 * @param size how many bytes of data to send 274 * @param size how many bytes of @a data to send
382 */ 275 */
383void 276void
384GNUNET_TESTBED_LOGGER_write (struct GNUNET_TESTBED_LOGGER_Handle *h, 277GNUNET_TESTBED_LOGGER_write (struct GNUNET_TESTBED_LOGGER_Handle *h,
385 const void *data, size_t size) 278 const void *data,
279 size_t size)
386{ 280{
387 size_t fit_size; 281 if (NULL == h->mq)
388 282 return;
389 GNUNET_assert (0 != size); 283 while (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 */
419static void
420timeout_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 { 284 {
433 GNUNET_SCHEDULER_cancel (h->flush_completion_task); 285 size_t fit_size = GNUNET_MIN (size,
434 h->flush_completion_task = NULL; 286 BUFFER_SIZE - h->buse);
287 memcpy (&h->buf[h->buse],
288 data,
289 fit_size);
290 h->buse += fit_size;
291 data += fit_size;
292 size -= fit_size;
293 if (0 != size)
294 dispatch_buffer (h);
435 } 295 }
436 if (NULL != cb)
437 cb (cb_cls, 0);
438} 296}
439 297
440 298
@@ -442,22 +300,19 @@ timeout_flush (void *cls)
442 * Flush the buffered data to the logger service 300 * Flush the buffered data to the logger service
443 * 301 *
444 * @param h the logger handle 302 * @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 303 * @param cb the callback to call after the data is flushed
447 * @param cb_cls the closure for the above callback 304 * @param cb_cls the closure for the above callback
448 */ 305 */
449void 306void
450GNUNET_TESTBED_LOGGER_flush (struct GNUNET_TESTBED_LOGGER_Handle *h, 307GNUNET_TESTBED_LOGGER_flush (struct GNUNET_TESTBED_LOGGER_Handle *h,
451 struct GNUNET_TIME_Relative timeout,
452 GNUNET_TESTBED_LOGGER_FlushCompletion cb, 308 GNUNET_TESTBED_LOGGER_FlushCompletion cb,
453 void *cb_cls) 309 void *cb_cls)
454{ 310{
311 GNUNET_assert (NULL == h->cb);
455 h->cb = cb; 312 h->cb = cb;
456 h->cb_cls = cb_cls; 313 h->cb_cls = cb_cls;
457 GNUNET_assert (NULL == h->timeout_flush_task); 314 if ( (NULL == h->mq) ||
458 h->timeout_flush_task = 315 (NULL == h->buf) )
459 GNUNET_SCHEDULER_add_delayed (timeout, &timeout_flush, h);
460 if (NULL == h->buf)
461 { 316 {
462 trigger_flush_notification (h); 317 trigger_flush_notification (h);
463 return; 318 return;
@@ -481,8 +336,6 @@ GNUNET_TESTBED_LOGGER_flush_cancel (struct GNUNET_TESTBED_LOGGER_Handle *h)
481 GNUNET_SCHEDULER_cancel (h->flush_completion_task); 336 GNUNET_SCHEDULER_cancel (h->flush_completion_task);
482 h->flush_completion_task = NULL; 337 h->flush_completion_task = NULL;
483 } 338 }
484 if (NULL != h->timeout_flush_task)
485 cancel_timeout_flush (h);
486 h->cb = NULL; 339 h->cb = NULL;
487 h->cb_cls = NULL; 340 h->cb_cls = NULL;
488} 341}