674a170d36e5725ee9405c0dcf3c30655d5e4835
[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   struct GNUNET_CLIENT_TransmitHandle *th;
96
97   /**
98    * DLL head for the message queue
99    */
100   struct MessageQueue *mq_head;
101
102   /**
103    * DLL tail for the message queue
104    */
105   struct MessageQueue *mq_tail;
106
107   GNUNET_SCHEDULER_TaskIdentifier flush_completion_task;
108
109   GNUNET_TESTBED_LOGGER_FlushCompletion cb;
110
111   void *cb_cls;
112
113   void *buf;
114
115   size_t bs;
116
117   size_t bwrote;
118
119   struct GNUNET_TIME_Relative retry_backoff;
120 };
121
122
123 /**
124  * Task to call the flush completion notification
125  *
126  * @param cls the logger handle
127  * @param tc the scheduler task context
128  */
129 static void
130 call_flush_completion (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
131 {
132   struct GNUNET_TESTBED_LOGGER_Handle *h = cls; 
133   GNUNET_TESTBED_LOGGER_FlushCompletion cb;
134   void *cb_cls;
135   size_t bw;
136
137   h->flush_completion_task = GNUNET_SCHEDULER_NO_TASK;
138   bw = h->bwrote;
139   h->bwrote = 0;
140   cb = h->cb;
141   h->cb = NULL;
142   cb_cls = h->cb_cls;
143   h->cb_cls = NULL;
144   if (NULL != cb)
145     cb (cb_cls, bw);
146 }
147
148
149 /**
150  * Schedule the flush completion notification task
151  *
152  * @param h logger handle
153  */
154 static void
155 trigger_flush_notification (struct GNUNET_TESTBED_LOGGER_Handle *h)
156 {
157   if (GNUNET_SCHEDULER_NO_TASK != h->flush_completion_task)
158     GNUNET_SCHEDULER_cancel (h->flush_completion_task);
159   h->flush_completion_task = GNUNET_SCHEDULER_add_now (&call_flush_completion, h);
160 }
161
162
163 /**
164  * Function called to notify a client about the connection begin ready to queue
165  * more data.  "buf" will be NULL and "size" zero if the connection was closed
166  * for writing in the meantime.
167  *
168  * @param cls closure
169  * @param size number of bytes available in buf
170  * @param buf where the callee should write the message
171  * @return number of bytes written to buf
172  */
173 static size_t
174 transmit_ready_notify (void *cls, size_t size, void *buf)
175 {
176   struct GNUNET_TESTBED_LOGGER_Handle *h = cls;
177   struct MessageQueue *mq;
178
179   h->th = NULL;
180   mq = h->mq_head;
181   GNUNET_assert (NULL != mq);
182   if ((0 == size) && (NULL == buf))     /* Timeout */
183   {
184     LOG_DEBUG ("Message sending timed out -- retrying\n");
185     h->retry_backoff = GNUNET_TIME_STD_BACKOFF (h->retry_backoff);
186     h->th =
187         GNUNET_CLIENT_notify_transmit_ready (h->client,
188                                              ntohs (mq->msg->size),
189                                              h->retry_backoff, GNUNET_YES,
190                                              &transmit_ready_notify, h);
191     return 0;
192   }
193   h->retry_backoff = GNUNET_TIME_UNIT_ZERO;
194   GNUNET_assert (ntohs (mq->msg->size) <= size);
195   size = ntohs (mq->msg->size);
196   memcpy (buf, mq->msg, size);
197   LOG_DEBUG ("Message of type: %u and size: %u sent\n",
198              ntohs (mq->msg->type), size);
199   GNUNET_free (mq->msg);
200   GNUNET_CONTAINER_DLL_remove (h->mq_head, h->mq_tail, mq);
201   GNUNET_free (mq);
202   h->bwrote += (size - sizeof (struct GNUNET_MessageHeader));
203   mq = h->mq_head;
204   if (NULL != mq)
205   {
206     h->retry_backoff = GNUNET_TIME_STD_BACKOFF (h->retry_backoff);
207     h->th =
208         GNUNET_CLIENT_notify_transmit_ready (h->client,
209                                              ntohs (mq->msg->size),
210                                              h->retry_backoff, GNUNET_YES,
211                                              &transmit_ready_notify, h);
212     return size;
213   }
214   if (NULL != h->cb)
215     trigger_flush_notification (h);       /* Call the flush completion callback */
216   return size;
217 }
218
219
220 /**
221  * Queues a message in send queue of the logger handle
222  *
223  * @param h the logger handle
224  * @param msg the message to queue
225  */
226 static void
227 queue_message (struct GNUNET_TESTBED_LOGGER_Handle *h,
228                struct GNUNET_MessageHeader *msg)
229 {
230   struct MessageQueue *mq;
231   uint16_t type;
232   uint16_t size;
233
234   type = ntohs (msg->type);
235   size = ntohs (msg->size);
236   mq = GNUNET_malloc (sizeof (struct MessageQueue));
237   mq->msg = msg;
238   LOG (GNUNET_ERROR_TYPE_DEBUG,
239        "Queueing message of type %u, size %u for sending\n", type,
240        ntohs (msg->size));
241   GNUNET_CONTAINER_DLL_insert_tail (h->mq_head, h->mq_tail, mq);
242   if (NULL == h->th)
243   {
244     h->retry_backoff = GNUNET_TIME_STD_BACKOFF (h->retry_backoff);
245     h->th =
246         GNUNET_CLIENT_notify_transmit_ready (h->client, size,
247                                              h->retry_backoff, GNUNET_YES,
248                                              &transmit_ready_notify,
249                                              h);
250   }
251 }
252
253
254 /**
255  * Send the buffered data to the service
256  *
257  * @param h the logger handle
258  */
259 static void
260 dispatch_buffer (struct GNUNET_TESTBED_LOGGER_Handle *h)
261 {
262   struct GNUNET_MessageHeader *msg;
263   size_t msize;
264
265   msize = sizeof (struct GNUNET_MessageHeader) + h->bs;
266   msg = GNUNET_realloc (h->buf, msize);
267   h->buf = NULL;
268   memmove (&msg[1], msg, h->bs);
269   h->bs = 0;    
270   msg->type = htons (GNUNET_MESSAGE_TYPE_TESTBED_LOGGER_MSG);
271   msg->size = htons (msize);
272   queue_message (h, msg);
273 }
274
275
276 /**
277  * Connect to the testbed logger service
278  *
279  * @param cfg configuration to use
280  * @return the handle which can be used for sending data to the service; NULL
281  *           upon any error
282  */
283 struct GNUNET_TESTBED_LOGGER_Handle *
284 GNUNET_TESTBED_LOGGER_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
285 {
286   struct GNUNET_TESTBED_LOGGER_Handle *h;
287   struct GNUNET_CLIENT_Connection *client;
288   
289   client = GNUNET_CLIENT_connect ("testbed-logger", cfg);
290   if (NULL == client)
291     return NULL;
292   h = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_LOGGER_Handle));
293   h->client = client;
294   return h;
295 }
296
297
298 /**
299  * Disconnect from the logger service.
300  *
301  * @param h the logger handle
302  */
303 void
304 GNUNET_TESTBED_LOGGER_disconnect (struct GNUNET_TESTBED_LOGGER_Handle *h)
305 {
306   struct MessageQueue *mq;
307
308   if (GNUNET_SCHEDULER_NO_TASK != h->flush_completion_task)
309     GNUNET_SCHEDULER_cancel (h->flush_completion_task);
310   while (NULL != (mq = h->mq_head))
311   {
312     GNUNET_CONTAINER_DLL_remove (h->mq_head, h->mq_tail, mq);
313     GNUNET_free (mq->msg);
314     GNUNET_free (mq);
315   }
316   GNUNET_CLIENT_disconnect (h->client);
317   GNUNET_free (h);
318 }
319
320
321 /**
322  * Send data to be logged to the logger service.  The data will be buffered and
323  * will be sent upon an explicit call to GNUNET_TESTBED_LOGGER_flush() or upon
324  * exceeding a threshold size.
325  *
326  * @param h the logger handle
327  * @param data the data to send;
328  * @param size how many bytes of data to send
329  */
330 void
331 GNUNET_TESTBED_LOGGER_write (struct GNUNET_TESTBED_LOGGER_Handle *h,
332                              const void *data, size_t size)
333 {  
334   size_t fit_size;
335
336   GNUNET_assert (0 != size);
337   GNUNET_assert (NULL != data);
338   GNUNET_assert (size < (GNUNET_SERVER_MAX_MESSAGE_SIZE
339                          - sizeof (struct GNUNET_MessageHeader)));
340   fit_size = sizeof (struct GNUNET_MessageHeader) + h->bs + size;
341   if ( GNUNET_SERVER_MAX_MESSAGE_SIZE < fit_size )
342     dispatch_buffer (h);
343   if (NULL == h->buf)
344   {
345     h->buf = GNUNET_malloc (size);
346     h->bs = size;
347     memcpy (h->buf, data, size);
348     return;
349   }
350   h->buf = GNUNET_realloc (h->buf, h->bs + size);
351   memcpy (h->buf + h->bs, data, size);
352   h->bs += size;
353   return;
354 }
355
356
357 /**
358  * Flush the buffered data to the logger service
359  *
360  * @param h the logger handle
361  * @param cb the callback to call after the data is flushed
362  * @param cb_cls the closure for the above callback
363  */
364 void
365 GNUNET_TESTBED_LOGGER_flush (struct GNUNET_TESTBED_LOGGER_Handle *h,
366                              GNUNET_TESTBED_LOGGER_FlushCompletion cb,
367                              void *cb_cls)
368 {
369   h->cb = cb;
370   h->cb_cls = cb_cls;
371   if (NULL == h->buf)
372   {
373     trigger_flush_notification (h);
374     return;
375   }
376   dispatch_buffer (h);
377 }
378
379
380 /**
381  * Cancel notification upon flush.
382  *
383  * @param h the logger handle
384  */
385 void
386 GNUNET_TESTBED_LOGGER_flush_cancel (struct GNUNET_TESTBED_LOGGER_Handle *h)
387 {
388   if (GNUNET_SCHEDULER_NO_TASK != h->flush_completion_task)
389   {
390     GNUNET_SCHEDULER_cancel (h->flush_completion_task);
391     h->flush_completion_task = GNUNET_SCHEDULER_NO_TASK;
392   }
393   h->cb = NULL;
394   h->cb_cls = NULL;
395 }
396
397 /* End of testbed_logger_api.c */