-remove trailing whitespace
[oweals/gnunet.git] / src / testbed / testbed_logger_api.c
1 /*
2       This file is part of GNUnet
3       (C) 2008--2013 Christian Grothoff (and other contributing authors)
4
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.
9
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.
14
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.
19  */
20
21 /**
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>
25  */
26
27 #include "platform.h"
28 #include "gnunet_util_lib.h"
29 #include "gnunet_testbed_logger_service.h"
30
31 /**
32  * Generic logging shorthand
33  */
34 #define LOG(kind, ...)                          \
35   GNUNET_log_from (kind, "testbed-logger-api", __VA_ARGS__)
36
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
52
53 /**
54  * The message queue for sending messages to the controller service
55  */
56 struct MessageQueue
57 {
58   /**
59    * next pointer for DLL
60    */
61   struct MessageQueue *next;
62
63   /**
64    * prev pointer for DLL
65    */
66   struct MessageQueue *prev;
67
68   /**
69    * The message to be sent
70    */
71   struct GNUNET_MessageHeader *msg;
72
73   /**
74    * Completion callback
75    */
76   GNUNET_TESTBED_LOGGER_FlushCompletion cb;
77
78   /**
79    * callback closure
80    */
81   void *cb_cls;
82 };
83
84
85 /**
86  * Connection handle for the logger service
87  */
88 struct GNUNET_TESTBED_LOGGER_Handle
89 {
90   /**
91    * Client connection
92    */
93   struct GNUNET_CLIENT_Connection *client;
94
95   /**
96    * The transport handle
97    */
98   struct GNUNET_CLIENT_TransmitHandle *th;
99
100   /**
101    * DLL head for the message queue
102    */
103   struct MessageQueue *mq_head;
104
105   /**
106    * DLL tail for the message queue
107    */
108   struct MessageQueue *mq_tail;
109
110   /**
111    * Flush completion callback
112    */
113   GNUNET_TESTBED_LOGGER_FlushCompletion cb;
114
115   /**
116    * Closure for the above callback
117    */
118   void *cb_cls;
119
120   /**
121    * Local buffer for data to be transmitted
122    */
123   void *buf;
124
125   /**
126    * The size of the local buffer
127    */
128   size_t bs;
129
130   /**
131    * Number of bytes wrote since last flush
132    */
133   size_t bwrote;
134
135   /**
136    * How long after should we retry sending a message to the service?
137    */
138   struct GNUNET_TIME_Relative retry_backoff;
139
140   /**
141    * Task to call the flush completion callback
142    */
143   GNUNET_SCHEDULER_TaskIdentifier flush_completion_task;
144
145   /**
146    * Task to be executed when flushing takes too long
147    */
148   GNUNET_SCHEDULER_TaskIdentifier timeout_flush_task;
149 };
150
151
152 /**
153  * Cancels the flush timeout task
154  *
155  * @param h handle to the logger
156  */
157 static void
158 cancel_timeout_flush (struct GNUNET_TESTBED_LOGGER_Handle *h)
159 {
160   GNUNET_SCHEDULER_cancel (h->timeout_flush_task);
161   h->timeout_flush_task = GNUNET_SCHEDULER_NO_TASK;
162 }
163
164
165 /**
166  * Task to call the flush completion notification
167  *
168  * @param cls the logger handle
169  * @param tc the scheduler task context
170  */
171 static void
172 call_flush_completion (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
173 {
174   struct GNUNET_TESTBED_LOGGER_Handle *h = cls;
175   GNUNET_TESTBED_LOGGER_FlushCompletion cb;
176   void *cb_cls;
177   size_t bw;
178
179   h->flush_completion_task = GNUNET_SCHEDULER_NO_TASK;
180   bw = h->bwrote;
181   h->bwrote = 0;
182   cb = h->cb;
183   h->cb = NULL;
184   cb_cls = h->cb_cls;
185   h->cb_cls = NULL;
186   if (GNUNET_SCHEDULER_NO_TASK != h->timeout_flush_task)
187     cancel_timeout_flush (h);
188   if (NULL != cb)
189     cb (cb_cls, bw);
190 }
191
192
193 /**
194  * Schedule the flush completion notification task
195  *
196  * @param h logger handle
197  */
198 static void
199 trigger_flush_notification (struct GNUNET_TESTBED_LOGGER_Handle *h)
200 {
201   if (GNUNET_SCHEDULER_NO_TASK != h->flush_completion_task)
202     GNUNET_SCHEDULER_cancel (h->flush_completion_task);
203   h->flush_completion_task = GNUNET_SCHEDULER_add_now (&call_flush_completion, h);
204 }
205
206
207 /**
208  * Function called to notify a client about the connection begin ready to queue
209  * more data.  "buf" will be NULL and "size" zero if the connection was closed
210  * for writing in the meantime.
211  *
212  * @param cls closure
213  * @param size number of bytes available in buf
214  * @param buf where the callee should write the message
215  * @return number of bytes written to buf
216  */
217 static size_t
218 transmit_ready_notify (void *cls, size_t size, void *buf)
219 {
220   struct GNUNET_TESTBED_LOGGER_Handle *h = cls;
221   struct MessageQueue *mq;
222
223   h->th = NULL;
224   mq = h->mq_head;
225   GNUNET_assert (NULL != mq);
226   if ((0 == size) && (NULL == buf))     /* Timeout */
227   {
228     LOG_DEBUG ("Message sending timed out -- retrying\n");
229     h->retry_backoff = GNUNET_TIME_STD_BACKOFF (h->retry_backoff);
230     h->th =
231         GNUNET_CLIENT_notify_transmit_ready (h->client,
232                                              ntohs (mq->msg->size),
233                                              h->retry_backoff, GNUNET_YES,
234                                              &transmit_ready_notify, h);
235     return 0;
236   }
237   h->retry_backoff = GNUNET_TIME_UNIT_ZERO;
238   GNUNET_assert (ntohs (mq->msg->size) <= size);
239   size = ntohs (mq->msg->size);
240   memcpy (buf, mq->msg, size);
241   LOG_DEBUG ("Message of type: %u and size: %u sent\n",
242              ntohs (mq->msg->type), size);
243   GNUNET_free (mq->msg);
244   GNUNET_CONTAINER_DLL_remove (h->mq_head, h->mq_tail, mq);
245   GNUNET_free (mq);
246   h->bwrote += (size - sizeof (struct GNUNET_MessageHeader));
247   mq = h->mq_head;
248   if (NULL != mq)
249   {
250     h->retry_backoff = GNUNET_TIME_STD_BACKOFF (h->retry_backoff);
251     h->th =
252         GNUNET_CLIENT_notify_transmit_ready (h->client,
253                                              ntohs (mq->msg->size),
254                                              h->retry_backoff, GNUNET_YES,
255                                              &transmit_ready_notify, h);
256     return size;
257   }
258   if (NULL != h->cb)
259     trigger_flush_notification (h);       /* Call the flush completion callback */
260   return size;
261 }
262
263
264 /**
265  * Queues a message in send queue of the logger handle
266  *
267  * @param h the logger handle
268  * @param msg the message to queue
269  */
270 static void
271 queue_message (struct GNUNET_TESTBED_LOGGER_Handle *h,
272                struct GNUNET_MessageHeader *msg)
273 {
274   struct MessageQueue *mq;
275   uint16_t type;
276   uint16_t size;
277
278   type = ntohs (msg->type);
279   size = ntohs (msg->size);
280   mq = GNUNET_malloc (sizeof (struct MessageQueue));
281   mq->msg = msg;
282   LOG (GNUNET_ERROR_TYPE_DEBUG,
283        "Queueing message of type %u, size %u for sending\n", type,
284        ntohs (msg->size));
285   GNUNET_CONTAINER_DLL_insert_tail (h->mq_head, h->mq_tail, mq);
286   if (NULL == h->th)
287   {
288     h->retry_backoff = GNUNET_TIME_STD_BACKOFF (h->retry_backoff);
289     h->th =
290         GNUNET_CLIENT_notify_transmit_ready (h->client, size,
291                                              h->retry_backoff, GNUNET_YES,
292                                              &transmit_ready_notify,
293                                              h);
294   }
295 }
296
297
298 /**
299  * Send the buffered data to the service
300  *
301  * @param h the logger handle
302  */
303 static void
304 dispatch_buffer (struct GNUNET_TESTBED_LOGGER_Handle *h)
305 {
306   struct GNUNET_MessageHeader *msg;
307   size_t msize;
308
309   msize = sizeof (struct GNUNET_MessageHeader) + h->bs;
310   msg = GNUNET_realloc (h->buf, msize);
311   h->buf = NULL;
312   memmove (&msg[1], msg, h->bs);
313   h->bs = 0;
314   msg->type = htons (GNUNET_MESSAGE_TYPE_TESTBED_LOGGER_MSG);
315   msg->size = htons (msize);
316   queue_message (h, msg);
317 }
318
319
320 /**
321  * Connect to the testbed logger service
322  *
323  * @param cfg configuration to use
324  * @return the handle which can be used for sending data to the service; NULL
325  *           upon any error
326  */
327 struct GNUNET_TESTBED_LOGGER_Handle *
328 GNUNET_TESTBED_LOGGER_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
329 {
330   struct GNUNET_TESTBED_LOGGER_Handle *h;
331   struct GNUNET_CLIENT_Connection *client;
332
333   client = GNUNET_CLIENT_connect ("testbed-logger", cfg);
334   if (NULL == client)
335     return NULL;
336   h = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_LOGGER_Handle));
337   h->client = client;
338   return h;
339 }
340
341
342 /**
343  * Disconnect from the logger service.
344  *
345  * @param h the logger handle
346  */
347 void
348 GNUNET_TESTBED_LOGGER_disconnect (struct GNUNET_TESTBED_LOGGER_Handle *h)
349 {
350   struct MessageQueue *mq;
351
352   if (GNUNET_SCHEDULER_NO_TASK != h->flush_completion_task)
353     GNUNET_SCHEDULER_cancel (h->flush_completion_task);
354   while (NULL != (mq = h->mq_head))
355   {
356     GNUNET_CONTAINER_DLL_remove (h->mq_head, h->mq_tail, mq);
357     GNUNET_free (mq->msg);
358     GNUNET_free (mq);
359   }
360   GNUNET_CLIENT_disconnect (h->client);
361   GNUNET_free (h);
362 }
363
364
365 /**
366  * Send data to be logged to the logger service.  The data will be buffered and
367  * will be sent upon an explicit call to GNUNET_TESTBED_LOGGER_flush() or upon
368  * exceeding a threshold size.
369  *
370  * @param h the logger handle
371  * @param data the data to send;
372  * @param size how many bytes of data to send
373  */
374 void
375 GNUNET_TESTBED_LOGGER_write (struct GNUNET_TESTBED_LOGGER_Handle *h,
376                              const void *data, size_t size)
377 {
378   size_t fit_size;
379
380   GNUNET_assert (0 != size);
381   GNUNET_assert (NULL != data);
382   GNUNET_assert (size < (GNUNET_SERVER_MAX_MESSAGE_SIZE
383                          - sizeof (struct GNUNET_MessageHeader)));
384   fit_size = sizeof (struct GNUNET_MessageHeader) + h->bs + size;
385   if ( GNUNET_SERVER_MAX_MESSAGE_SIZE < fit_size )
386     dispatch_buffer (h);
387   if (NULL == h->buf)
388   {
389     h->buf = GNUNET_malloc (size);
390     h->bs = size;
391     memcpy (h->buf, data, size);
392     return;
393   }
394   h->buf = GNUNET_realloc (h->buf, h->bs + size);
395   memcpy (h->buf + h->bs, data, size);
396   h->bs += size;
397   return;
398 }
399
400
401 /**
402  * Task to be executed when flushing our local buffer takes longer than timeout
403  * given to GNUNET_TESTBED_LOGGER_flush().  The flush completion callback will
404  * be called with 0 as the amount of data sent.
405  *
406  * @param cls the logger handle
407  * @param tc scheduler task context
408  */
409 static void
410 timeout_flush (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
411 {
412   struct GNUNET_TESTBED_LOGGER_Handle *h = cls;
413   GNUNET_TESTBED_LOGGER_FlushCompletion cb;
414   void *cb_cls;
415
416   h->timeout_flush_task = GNUNET_SCHEDULER_NO_TASK;
417   cb = h->cb;
418   h->cb = NULL;
419   cb_cls = h->cb_cls;
420   h->cb_cls = NULL;
421   if (GNUNET_SCHEDULER_NO_TASK != h->flush_completion_task)
422   {
423     GNUNET_SCHEDULER_cancel (h->flush_completion_task);
424     h->flush_completion_task = GNUNET_SCHEDULER_NO_TASK;
425   }
426   if (NULL != cb)
427     cb (cb_cls, 0);
428 }
429
430
431 /**
432  * Flush the buffered data to the logger service
433  *
434  * @param h the logger handle
435  * @param timeout how long to wait before calling the flust completion callback
436  * @param cb the callback to call after the data is flushed
437  * @param cb_cls the closure for the above callback
438  */
439 void
440 GNUNET_TESTBED_LOGGER_flush (struct GNUNET_TESTBED_LOGGER_Handle *h,
441                              struct GNUNET_TIME_Relative timeout,
442                              GNUNET_TESTBED_LOGGER_FlushCompletion cb,
443                              void *cb_cls)
444 {
445   h->cb = cb;
446   h->cb_cls = cb_cls;
447   GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == h->timeout_flush_task);
448   h->timeout_flush_task =
449       GNUNET_SCHEDULER_add_delayed (timeout, &timeout_flush, h);
450   if (NULL == h->buf)
451   {
452     trigger_flush_notification (h);
453     return;
454   }
455   dispatch_buffer (h);
456 }
457
458
459 /**
460  * Cancel notification upon flush.  Should only be used when the flush
461  * completion callback given to GNUNET_TESTBED_LOGGER_flush() is not already
462  * called.
463  *
464  * @param h the logger handle
465  */
466 void
467 GNUNET_TESTBED_LOGGER_flush_cancel (struct GNUNET_TESTBED_LOGGER_Handle *h)
468 {
469   if (GNUNET_SCHEDULER_NO_TASK != h->flush_completion_task)
470   {
471     GNUNET_SCHEDULER_cancel (h->flush_completion_task);
472     h->flush_completion_task = GNUNET_SCHEDULER_NO_TASK;
473   }
474   if (GNUNET_SCHEDULER_NO_TASK != h->timeout_flush_task)
475     cancel_timeout_flush (h);
476   h->cb = NULL;
477   h->cb_cls = NULL;
478 }
479
480 /* End of testbed_logger_api.c */