testbed_logger_api.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. /*
  2. This file is part of GNUnet
  3. (C) 2008--2013 Christian Grothoff (and other contributing authors)
  4. GNUnet is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published
  6. by the Free Software Foundation; either version 3, or (at your
  7. option) any later version.
  8. GNUnet is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNUnet; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  15. Boston, MA 02111-1307, USA.
  16. */
  17. /**
  18. * @file testbed/testbed_logger_api.c
  19. * @brief Client-side routines for communicating with the tesbted logger service
  20. * @author Sree Harsha Totakura <sreeharsha@totakura.in>
  21. */
  22. #include "platform.h"
  23. #include "gnunet_util_lib.h"
  24. #include "gnunet_testbed_logger_service.h"
  25. /**
  26. * Generic logging shorthand
  27. */
  28. #define LOG(kind, ...) \
  29. GNUNET_log_from (kind, "testbed-logger-api", __VA_ARGS__)
  30. /**
  31. * Debug logging
  32. */
  33. #define LOG_DEBUG(...) \
  34. LOG (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
  35. #ifdef GNUNET_TIME_STD_EXPONENTIAL_BACKOFF_THRESHOLD
  36. #undef GNUNET_TIME_STD_EXPONENTIAL_BACKOFF_THRESHOLD
  37. #endif
  38. /**
  39. * Threshold after which exponential backoff should not increase (15 s).
  40. */
  41. #define GNUNET_TIME_STD_EXPONENTIAL_BACKOFF_THRESHOLD GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 3)
  42. /**
  43. * The size of the buffer we fill before sending out the message
  44. */
  45. #define BUFFER_SIZE GNUNET_SERVER_MAX_MESSAGE_SIZE
  46. /**
  47. * The message queue for sending messages to the controller service
  48. */
  49. struct MessageQueue
  50. {
  51. /**
  52. * next pointer for DLL
  53. */
  54. struct MessageQueue *next;
  55. /**
  56. * prev pointer for DLL
  57. */
  58. struct MessageQueue *prev;
  59. /**
  60. * The message to be sent
  61. */
  62. struct GNUNET_MessageHeader *msg;
  63. /**
  64. * Completion callback
  65. */
  66. GNUNET_TESTBED_LOGGER_FlushCompletion cb;
  67. /**
  68. * callback closure
  69. */
  70. void *cb_cls;
  71. };
  72. /**
  73. * Connection handle for the logger service
  74. */
  75. struct GNUNET_TESTBED_LOGGER_Handle
  76. {
  77. /**
  78. * Client connection
  79. */
  80. struct GNUNET_CLIENT_Connection *client;
  81. /**
  82. * The transport handle
  83. */
  84. struct GNUNET_CLIENT_TransmitHandle *th;
  85. /**
  86. * DLL head for the message queue
  87. */
  88. struct MessageQueue *mq_head;
  89. /**
  90. * DLL tail for the message queue
  91. */
  92. struct MessageQueue *mq_tail;
  93. /**
  94. * Flush completion callback
  95. */
  96. GNUNET_TESTBED_LOGGER_FlushCompletion cb;
  97. /**
  98. * Closure for the above callback
  99. */
  100. void *cb_cls;
  101. /**
  102. * Local buffer for data to be transmitted
  103. */
  104. void *buf;
  105. /**
  106. * The size of the local buffer
  107. */
  108. size_t bs;
  109. /**
  110. * Number of bytes wrote since last flush
  111. */
  112. size_t bwrote;
  113. /**
  114. * How long after should we retry sending a message to the service?
  115. */
  116. struct GNUNET_TIME_Relative retry_backoff;
  117. /**
  118. * Task to call the flush completion callback
  119. */
  120. GNUNET_SCHEDULER_TaskIdentifier flush_completion_task;
  121. /**
  122. * Task to be executed when flushing takes too long
  123. */
  124. GNUNET_SCHEDULER_TaskIdentifier timeout_flush_task;
  125. };
  126. /**
  127. * Cancels the flush timeout task
  128. *
  129. * @param h handle to the logger
  130. */
  131. static void
  132. cancel_timeout_flush (struct GNUNET_TESTBED_LOGGER_Handle *h)
  133. {
  134. GNUNET_SCHEDULER_cancel (h->timeout_flush_task);
  135. h->timeout_flush_task = GNUNET_SCHEDULER_NO_TASK;
  136. }
  137. /**
  138. * Task to call the flush completion notification
  139. *
  140. * @param cls the logger handle
  141. * @param tc the scheduler task context
  142. */
  143. static void
  144. call_flush_completion (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
  145. {
  146. struct GNUNET_TESTBED_LOGGER_Handle *h = cls;
  147. GNUNET_TESTBED_LOGGER_FlushCompletion cb;
  148. void *cb_cls;
  149. size_t bw;
  150. h->flush_completion_task = GNUNET_SCHEDULER_NO_TASK;
  151. bw = h->bwrote;
  152. h->bwrote = 0;
  153. cb = h->cb;
  154. h->cb = NULL;
  155. cb_cls = h->cb_cls;
  156. h->cb_cls = NULL;
  157. if (GNUNET_SCHEDULER_NO_TASK != h->timeout_flush_task)
  158. cancel_timeout_flush (h);
  159. if (NULL != cb)
  160. cb (cb_cls, bw);
  161. }
  162. /**
  163. * Schedule the flush completion notification task
  164. *
  165. * @param h logger handle
  166. */
  167. static void
  168. trigger_flush_notification (struct GNUNET_TESTBED_LOGGER_Handle *h)
  169. {
  170. if (GNUNET_SCHEDULER_NO_TASK != h->flush_completion_task)
  171. GNUNET_SCHEDULER_cancel (h->flush_completion_task);
  172. h->flush_completion_task = GNUNET_SCHEDULER_add_now (&call_flush_completion, h);
  173. }
  174. /**
  175. * Function called to notify a client about the connection begin ready to queue
  176. * more data. "buf" will be NULL and "size" zero if the connection was closed
  177. * for writing in the meantime.
  178. *
  179. * @param cls closure
  180. * @param size number of bytes available in buf
  181. * @param buf where the callee should write the message
  182. * @return number of bytes written to buf
  183. */
  184. static size_t
  185. transmit_ready_notify (void *cls, size_t size, void *buf)
  186. {
  187. struct GNUNET_TESTBED_LOGGER_Handle *h = cls;
  188. struct MessageQueue *mq;
  189. h->th = NULL;
  190. mq = h->mq_head;
  191. GNUNET_assert (NULL != mq);
  192. if ((0 == size) && (NULL == buf)) /* Timeout */
  193. {
  194. LOG_DEBUG ("Message sending timed out -- retrying\n");
  195. h->retry_backoff = GNUNET_TIME_STD_BACKOFF (h->retry_backoff);
  196. h->th =
  197. GNUNET_CLIENT_notify_transmit_ready (h->client,
  198. ntohs (mq->msg->size),
  199. h->retry_backoff, GNUNET_YES,
  200. &transmit_ready_notify, h);
  201. return 0;
  202. }
  203. h->retry_backoff = GNUNET_TIME_UNIT_ZERO;
  204. GNUNET_assert (ntohs (mq->msg->size) <= size);
  205. size = ntohs (mq->msg->size);
  206. memcpy (buf, mq->msg, size);
  207. LOG_DEBUG ("Message of type: %u and size: %u sent\n",
  208. ntohs (mq->msg->type), size);
  209. GNUNET_free (mq->msg);
  210. GNUNET_CONTAINER_DLL_remove (h->mq_head, h->mq_tail, mq);
  211. GNUNET_free (mq);
  212. h->bwrote += (size - sizeof (struct GNUNET_MessageHeader));
  213. mq = h->mq_head;
  214. if (NULL != mq)
  215. {
  216. h->retry_backoff = GNUNET_TIME_STD_BACKOFF (h->retry_backoff);
  217. h->th =
  218. GNUNET_CLIENT_notify_transmit_ready (h->client,
  219. ntohs (mq->msg->size),
  220. h->retry_backoff, GNUNET_YES,
  221. &transmit_ready_notify, h);
  222. return size;
  223. }
  224. if (NULL != h->cb)
  225. trigger_flush_notification (h); /* Call the flush completion callback */
  226. return size;
  227. }
  228. /**
  229. * Queues a message in send queue of the logger handle
  230. *
  231. * @param h the logger handle
  232. * @param msg the message to queue
  233. */
  234. static void
  235. queue_message (struct GNUNET_TESTBED_LOGGER_Handle *h,
  236. struct GNUNET_MessageHeader *msg)
  237. {
  238. struct MessageQueue *mq;
  239. uint16_t type;
  240. uint16_t size;
  241. type = ntohs (msg->type);
  242. size = ntohs (msg->size);
  243. mq = GNUNET_new (struct MessageQueue);
  244. mq->msg = msg;
  245. LOG (GNUNET_ERROR_TYPE_DEBUG,
  246. "Queueing message of type %u, size %u for sending\n", type,
  247. ntohs (msg->size));
  248. GNUNET_CONTAINER_DLL_insert_tail (h->mq_head, h->mq_tail, mq);
  249. if (NULL == h->th)
  250. {
  251. h->retry_backoff = GNUNET_TIME_STD_BACKOFF (h->retry_backoff);
  252. h->th =
  253. GNUNET_CLIENT_notify_transmit_ready (h->client, size,
  254. h->retry_backoff, GNUNET_YES,
  255. &transmit_ready_notify,
  256. h);
  257. }
  258. }
  259. /**
  260. * Send the buffered data to the service
  261. *
  262. * @param h the logger handle
  263. */
  264. static void
  265. dispatch_buffer (struct GNUNET_TESTBED_LOGGER_Handle *h)
  266. {
  267. struct GNUNET_MessageHeader *msg;
  268. size_t msize;
  269. msize = sizeof (struct GNUNET_MessageHeader) + h->bs;
  270. msg = GNUNET_realloc (h->buf, msize);
  271. h->buf = NULL;
  272. memmove (&msg[1], msg, h->bs);
  273. h->bs = 0;
  274. msg->type = htons (GNUNET_MESSAGE_TYPE_TESTBED_LOGGER_MSG);
  275. msg->size = htons (msize);
  276. queue_message (h, msg);
  277. }
  278. /**
  279. * Connect to the testbed logger service
  280. *
  281. * @param cfg configuration to use
  282. * @return the handle which can be used for sending data to the service; NULL
  283. * upon any error
  284. */
  285. struct GNUNET_TESTBED_LOGGER_Handle *
  286. GNUNET_TESTBED_LOGGER_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
  287. {
  288. struct GNUNET_TESTBED_LOGGER_Handle *h;
  289. struct GNUNET_CLIENT_Connection *client;
  290. client = GNUNET_CLIENT_connect ("testbed-logger", cfg);
  291. if (NULL == client)
  292. return NULL;
  293. h = GNUNET_new (struct GNUNET_TESTBED_LOGGER_Handle);
  294. h->client = client;
  295. return h;
  296. }
  297. /**
  298. * Disconnect from the logger service.
  299. *
  300. * @param h the logger handle
  301. */
  302. void
  303. GNUNET_TESTBED_LOGGER_disconnect (struct GNUNET_TESTBED_LOGGER_Handle *h)
  304. {
  305. struct MessageQueue *mq;
  306. unsigned int lost;
  307. if (GNUNET_SCHEDULER_NO_TASK != h->flush_completion_task)
  308. GNUNET_SCHEDULER_cancel (h->flush_completion_task);
  309. lost = 0;
  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. lost++;
  316. }
  317. if (0 != lost)
  318. LOG (GNUNET_ERROR_TYPE_WARNING, "Cleaning up %u unsent logger message[s]\n",
  319. lost);
  320. GNUNET_CLIENT_disconnect (h->client);
  321. GNUNET_free (h);
  322. }
  323. /**
  324. * Send data to be logged to the logger service. The data will be buffered and
  325. * will be sent upon an explicit call to GNUNET_TESTBED_LOGGER_flush() or upon
  326. * exceeding a threshold size.
  327. *
  328. * @param h the logger handle
  329. * @param data the data to send;
  330. * @param size how many bytes of data to send
  331. */
  332. void
  333. GNUNET_TESTBED_LOGGER_write (struct GNUNET_TESTBED_LOGGER_Handle *h,
  334. const void *data, size_t size)
  335. {
  336. size_t fit_size;
  337. GNUNET_assert (0 != size);
  338. GNUNET_assert (NULL != data);
  339. GNUNET_assert (size <= (BUFFER_SIZE - sizeof (struct GNUNET_MessageHeader)));
  340. fit_size = sizeof (struct GNUNET_MessageHeader) + h->bs + size;
  341. if ( BUFFER_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. goto dispatch_ready;
  349. }
  350. h->buf = GNUNET_realloc (h->buf, h->bs + size);
  351. memcpy (h->buf + h->bs, data, size);
  352. h->bs += size;
  353. dispatch_ready:
  354. if (BUFFER_SIZE == fit_size)
  355. dispatch_buffer (h);
  356. }
  357. /**
  358. * Task to be executed when flushing our local buffer takes longer than timeout
  359. * given to GNUNET_TESTBED_LOGGER_flush(). The flush completion callback will
  360. * be called with 0 as the amount of data sent.
  361. *
  362. * @param cls the logger handle
  363. * @param tc scheduler task context
  364. */
  365. static void
  366. timeout_flush (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
  367. {
  368. struct GNUNET_TESTBED_LOGGER_Handle *h = cls;
  369. GNUNET_TESTBED_LOGGER_FlushCompletion cb;
  370. void *cb_cls;
  371. h->timeout_flush_task = GNUNET_SCHEDULER_NO_TASK;
  372. cb = h->cb;
  373. h->cb = NULL;
  374. cb_cls = h->cb_cls;
  375. h->cb_cls = NULL;
  376. if (GNUNET_SCHEDULER_NO_TASK != h->flush_completion_task)
  377. {
  378. GNUNET_SCHEDULER_cancel (h->flush_completion_task);
  379. h->flush_completion_task = GNUNET_SCHEDULER_NO_TASK;
  380. }
  381. if (NULL != cb)
  382. cb (cb_cls, 0);
  383. }
  384. /**
  385. * Flush the buffered data to the logger service
  386. *
  387. * @param h the logger handle
  388. * @param timeout how long to wait before calling the flust completion callback
  389. * @param cb the callback to call after the data is flushed
  390. * @param cb_cls the closure for the above callback
  391. */
  392. void
  393. GNUNET_TESTBED_LOGGER_flush (struct GNUNET_TESTBED_LOGGER_Handle *h,
  394. struct GNUNET_TIME_Relative timeout,
  395. GNUNET_TESTBED_LOGGER_FlushCompletion cb,
  396. void *cb_cls)
  397. {
  398. h->cb = cb;
  399. h->cb_cls = cb_cls;
  400. GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == h->timeout_flush_task);
  401. h->timeout_flush_task =
  402. GNUNET_SCHEDULER_add_delayed (timeout, &timeout_flush, h);
  403. if (NULL == h->buf)
  404. {
  405. trigger_flush_notification (h);
  406. return;
  407. }
  408. dispatch_buffer (h);
  409. }
  410. /**
  411. * Cancel notification upon flush. Should only be used when the flush
  412. * completion callback given to GNUNET_TESTBED_LOGGER_flush() is not already
  413. * called.
  414. *
  415. * @param h the logger handle
  416. */
  417. void
  418. GNUNET_TESTBED_LOGGER_flush_cancel (struct GNUNET_TESTBED_LOGGER_Handle *h)
  419. {
  420. if (GNUNET_SCHEDULER_NO_TASK != h->flush_completion_task)
  421. {
  422. GNUNET_SCHEDULER_cancel (h->flush_completion_task);
  423. h->flush_completion_task = GNUNET_SCHEDULER_NO_TASK;
  424. }
  425. if (GNUNET_SCHEDULER_NO_TASK != h->timeout_flush_task)
  426. cancel_timeout_flush (h);
  427. h->cb = NULL;
  428. h->cb_cls = NULL;
  429. }
  430. /* End of testbed_logger_api.c */