-fix channel data range to make -1 legal value
[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  * The size of the buffer we fill before sending out the message
54  */
55 #define BUFFER_SIZE GNUNET_SERVER_MAX_MESSAGE_SIZE
56
57 /**
58  * The message queue for sending messages to the controller service
59  */
60 struct 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
89 /**
90  * Connection handle for the logger service
91  */
92 struct GNUNET_TESTBED_LOGGER_Handle
93 {
94   /**
95    * Client connection
96    */
97   struct GNUNET_CLIENT_Connection *client;
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
114   /**
115    * Flush completion callback
116    */
117   GNUNET_TESTBED_LOGGER_FlushCompletion cb;
118
119   /**
120    * Closure for the above callback
121    */
122   void *cb_cls;
123
124   /**
125    * Local buffer for data to be transmitted
126    */
127   void *buf;
128
129   /**
130    * The size of the local buffer
131    */
132   size_t bs;
133
134   /**
135    * Number of bytes wrote since last flush
136    */
137   size_t bwrote;
138
139   /**
140    * How long after should we retry sending a message to the service?
141    */
142   struct GNUNET_TIME_Relative retry_backoff;
143
144   /**
145    * Task to call the flush completion callback
146    */
147   GNUNET_SCHEDULER_TaskIdentifier flush_completion_task;
148
149   /**
150    * Task to be executed when flushing takes too long
151    */
152   GNUNET_SCHEDULER_TaskIdentifier timeout_flush_task;
153 };
154
155
156 /**
157  * Cancels the flush timeout task
158  *
159  * @param h handle to the logger
160  */
161 static void
162 cancel_timeout_flush (struct GNUNET_TESTBED_LOGGER_Handle *h)
163 {
164   GNUNET_SCHEDULER_cancel (h->timeout_flush_task);
165   h->timeout_flush_task = GNUNET_SCHEDULER_NO_TASK;
166 }
167
168
169 /**
170  * Task to call the flush completion notification
171  *
172  * @param cls the logger handle
173  * @param tc the scheduler task context
174  */
175 static void
176 call_flush_completion (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
177 {
178   struct GNUNET_TESTBED_LOGGER_Handle *h = cls;
179   GNUNET_TESTBED_LOGGER_FlushCompletion cb;
180   void *cb_cls;
181   size_t bw;
182
183   h->flush_completion_task = GNUNET_SCHEDULER_NO_TASK;
184   bw = h->bwrote;
185   h->bwrote = 0;
186   cb = h->cb;
187   h->cb = NULL;
188   cb_cls = h->cb_cls;
189   h->cb_cls = NULL;
190   if (GNUNET_SCHEDULER_NO_TASK != h->timeout_flush_task)
191     cancel_timeout_flush (h);
192   if (NULL != cb)
193     cb (cb_cls, bw);
194 }
195
196
197 /**
198  * Schedule the flush completion notification task
199  *
200  * @param h logger handle
201  */
202 static void
203 trigger_flush_notification (struct GNUNET_TESTBED_LOGGER_Handle *h)
204 {
205   if (GNUNET_SCHEDULER_NO_TASK != 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);
208 }
209
210
211 /**
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.
215  *
216  * @param cls closure
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
220  */
221 static size_t
222 transmit_ready_notify (void *cls, size_t size, void *buf)
223 {
224   struct GNUNET_TESTBED_LOGGER_Handle *h = cls;
225   struct MessageQueue *mq;
226
227   h->th = NULL;
228   mq = h->mq_head;
229   GNUNET_assert (NULL != mq);
230   if ((0 == size) && (NULL == buf))     /* Timeout */
231   {
232     LOG_DEBUG ("Message sending timed out -- retrying\n");
233     h->retry_backoff = GNUNET_TIME_STD_BACKOFF (h->retry_backoff);
234     h->th =
235         GNUNET_CLIENT_notify_transmit_ready (h->client,
236                                              ntohs (mq->msg->size),
237                                              h->retry_backoff, GNUNET_YES,
238                                              &transmit_ready_notify, h);
239     return 0;
240   }
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);
249   GNUNET_free (mq);
250   h->bwrote += (size - sizeof (struct GNUNET_MessageHeader));
251   mq = h->mq_head;
252   if (NULL != mq)
253   {
254     h->retry_backoff = GNUNET_TIME_STD_BACKOFF (h->retry_backoff);
255     h->th =
256         GNUNET_CLIENT_notify_transmit_ready (h->client,
257                                              ntohs (mq->msg->size),
258                                              h->retry_backoff, GNUNET_YES,
259                                              &transmit_ready_notify, h);
260     return size;
261   }
262   if (NULL != h->cb)
263     trigger_flush_notification (h);       /* Call the flush completion callback */
264   return size;
265 }
266
267
268 /**
269  * Queues a message in send queue of the logger handle
270  *
271  * @param h the logger handle
272  * @param msg the message to queue
273  */
274 static void
275 queue_message (struct GNUNET_TESTBED_LOGGER_Handle *h,
276                struct GNUNET_MessageHeader *msg)
277 {
278   struct MessageQueue *mq;
279   uint16_t type;
280   uint16_t size;
281
282   type = ntohs (msg->type);
283   size = ntohs (msg->size);
284   mq = GNUNET_new (struct MessageQueue);
285   mq->msg = msg;
286   LOG (GNUNET_ERROR_TYPE_DEBUG,
287        "Queueing message of type %u, size %u for sending\n", type,
288        ntohs (msg->size));
289   GNUNET_CONTAINER_DLL_insert_tail (h->mq_head, h->mq_tail, mq);
290   if (NULL == h->th)
291   {
292     h->retry_backoff = GNUNET_TIME_STD_BACKOFF (h->retry_backoff);
293     h->th =
294         GNUNET_CLIENT_notify_transmit_ready (h->client, size,
295                                              h->retry_backoff, GNUNET_YES,
296                                              &transmit_ready_notify,
297                                              h);
298   }
299 }
300
301
302 /**
303  * Send the buffered data to the service
304  *
305  * @param h the logger handle
306  */
307 static void
308 dispatch_buffer (struct GNUNET_TESTBED_LOGGER_Handle *h)
309 {
310   struct GNUNET_MessageHeader *msg;
311   size_t msize;
312
313   msize = sizeof (struct GNUNET_MessageHeader) + h->bs;
314   msg = GNUNET_realloc (h->buf, msize);
315   h->buf = NULL;
316   memmove (&msg[1], msg, h->bs);
317   h->bs = 0;
318   msg->type = htons (GNUNET_MESSAGE_TYPE_TESTBED_LOGGER_MSG);
319   msg->size = htons (msize);
320   queue_message (h, msg);
321 }
322
323
324 /**
325  * Connect to the testbed logger service
326  *
327  * @param cfg configuration to use
328  * @return the handle which can be used for sending data to the service; NULL
329  *           upon any error
330  */
331 struct GNUNET_TESTBED_LOGGER_Handle *
332 GNUNET_TESTBED_LOGGER_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
333 {
334   struct GNUNET_TESTBED_LOGGER_Handle *h;
335   struct GNUNET_CLIENT_Connection *client;
336
337   client = GNUNET_CLIENT_connect ("testbed-logger", cfg);
338   if (NULL == client)
339     return NULL;
340   h = GNUNET_new (struct GNUNET_TESTBED_LOGGER_Handle);
341   h->client = client;
342   return h;
343 }
344
345
346 /**
347  * Disconnect from the logger service.
348  *
349  * @param h the logger handle
350  */
351 void
352 GNUNET_TESTBED_LOGGER_disconnect (struct GNUNET_TESTBED_LOGGER_Handle *h)
353 {
354   struct MessageQueue *mq;
355   unsigned int lost;
356
357   if (GNUNET_SCHEDULER_NO_TASK != h->flush_completion_task)
358     GNUNET_SCHEDULER_cancel (h->flush_completion_task);
359   lost = 0;
360   while (NULL != (mq = h->mq_head))
361   {
362     GNUNET_CONTAINER_DLL_remove (h->mq_head, h->mq_tail, mq);
363     GNUNET_free (mq->msg);
364     GNUNET_free (mq);
365     lost++;
366   }
367   if (0 != lost)
368     LOG (GNUNET_ERROR_TYPE_WARNING, "Cleaning up %u unsent logger message[s]\n",
369          lost);
370   GNUNET_CLIENT_disconnect (h->client);
371   GNUNET_free (h);
372 }
373
374
375 /**
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.
379  *
380  * @param h the logger handle
381  * @param data the data to send;
382  * @param size how many bytes of data to send
383  */
384 void
385 GNUNET_TESTBED_LOGGER_write (struct GNUNET_TESTBED_LOGGER_Handle *h,
386                              const void *data, size_t size)
387 {
388   size_t fit_size;
389
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 )
395     dispatch_buffer (h);
396   if (NULL == h->buf)
397   {
398     h->buf = GNUNET_malloc (size);
399     h->bs = size;
400     memcpy (h->buf, data, size);
401     goto dispatch_ready;
402   }
403   h->buf = GNUNET_realloc (h->buf, h->bs + size);
404   memcpy (h->buf + h->bs, data, size);
405   h->bs += size;
406
407  dispatch_ready:
408   if (BUFFER_SIZE == fit_size)
409     dispatch_buffer (h);
410 }
411
412
413 /**
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.
417  *
418  * @param cls the logger handle
419  * @param tc scheduler task context
420  */
421 static void
422 timeout_flush (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
423 {
424   struct GNUNET_TESTBED_LOGGER_Handle *h = cls;
425   GNUNET_TESTBED_LOGGER_FlushCompletion cb;
426   void *cb_cls;
427
428   h->timeout_flush_task = GNUNET_SCHEDULER_NO_TASK;
429   cb = h->cb;
430   h->cb = NULL;
431   cb_cls = h->cb_cls;
432   h->cb_cls = NULL;
433   if (GNUNET_SCHEDULER_NO_TASK != h->flush_completion_task)
434   {
435     GNUNET_SCHEDULER_cancel (h->flush_completion_task);
436     h->flush_completion_task = GNUNET_SCHEDULER_NO_TASK;
437   }
438   if (NULL != cb)
439     cb (cb_cls, 0);
440 }
441
442
443 /**
444  * Flush the buffered data to the logger service
445  *
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
450  */
451 void
452 GNUNET_TESTBED_LOGGER_flush (struct GNUNET_TESTBED_LOGGER_Handle *h,
453                              struct GNUNET_TIME_Relative timeout,
454                              GNUNET_TESTBED_LOGGER_FlushCompletion cb,
455                              void *cb_cls)
456 {
457   h->cb = cb;
458   h->cb_cls = cb_cls;
459   GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == h->timeout_flush_task);
460   h->timeout_flush_task =
461       GNUNET_SCHEDULER_add_delayed (timeout, &timeout_flush, h);
462   if (NULL == h->buf)
463   {
464     trigger_flush_notification (h);
465     return;
466   }
467   dispatch_buffer (h);
468 }
469
470
471 /**
472  * Cancel notification upon flush.  Should only be used when the flush
473  * completion callback given to GNUNET_TESTBED_LOGGER_flush() is not already
474  * called.
475  *
476  * @param h the logger handle
477  */
478 void
479 GNUNET_TESTBED_LOGGER_flush_cancel (struct GNUNET_TESTBED_LOGGER_Handle *h)
480 {
481   if (GNUNET_SCHEDULER_NO_TASK != h->flush_completion_task)
482   {
483     GNUNET_SCHEDULER_cancel (h->flush_completion_task);
484     h->flush_completion_task = GNUNET_SCHEDULER_NO_TASK;
485   }
486   if (GNUNET_SCHEDULER_NO_TASK != h->timeout_flush_task)
487     cancel_timeout_flush (h);
488   h->cb = NULL;
489   h->cb_cls = NULL;
490 }
491
492 /* End of testbed_logger_api.c */