testbed_logger_api.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. /*
  2. This file is part of GNUnet
  3. Copyright (C) 2008--2013, 2016 GNUnet e.V.
  4. GNUnet is free software: you can redistribute it and/or modify it
  5. under the terms of the GNU Affero General Public License as published
  6. by the Free Software Foundation, either version 3 of the License,
  7. or (at your 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. Affero General Public License for more details.
  12. You should have received a copy of the GNU Affero General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. SPDX-License-Identifier: AGPL3.0-or-later
  15. */
  16. /**
  17. * @file testbed-logger/testbed_logger_api.c
  18. * @brief Client-side routines for communicating with the tesbted logger service
  19. * @author Sree Harsha Totakura <sreeharsha@totakura.in>
  20. * @author Christian Grothoff
  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. * The size of the buffer we fill before sending out the message
  32. */
  33. #define BUFFER_SIZE (GNUNET_MAX_MESSAGE_SIZE - sizeof(struct \
  34. GNUNET_MessageHeader))
  35. /**
  36. * Connection handle for the logger service
  37. */
  38. struct GNUNET_TESTBED_LOGGER_Handle
  39. {
  40. /**
  41. * Client connection
  42. */
  43. struct GNUNET_MQ_Handle *mq;
  44. /**
  45. * Flush completion callback
  46. */
  47. GNUNET_TESTBED_LOGGER_FlushCompletion cb;
  48. /**
  49. * Closure for @e cb
  50. */
  51. void *cb_cls;
  52. /**
  53. * Local buffer for data to be transmitted
  54. */
  55. char buf[BUFFER_SIZE];
  56. /**
  57. * How many bytes in @a buf are in use?
  58. */
  59. size_t buse;
  60. /**
  61. * Number of bytes wrote since last flush
  62. */
  63. size_t bwrote;
  64. /**
  65. * How long after should we retry sending a message to the service?
  66. */
  67. struct GNUNET_TIME_Relative retry_backoff;
  68. /**
  69. * Task to call the flush completion callback
  70. */
  71. struct GNUNET_SCHEDULER_Task *flush_completion_task;
  72. /**
  73. * Number of entries in the MQ.
  74. */
  75. unsigned int mq_len;
  76. };
  77. /**
  78. * Task to call the flush completion notification
  79. *
  80. * @param cls the logger handle
  81. */
  82. static void
  83. call_flush_completion (void *cls)
  84. {
  85. struct GNUNET_TESTBED_LOGGER_Handle *h = cls;
  86. GNUNET_TESTBED_LOGGER_FlushCompletion cb;
  87. void *cb_cls;
  88. size_t bw;
  89. h->flush_completion_task = NULL;
  90. bw = h->bwrote;
  91. h->bwrote = 0;
  92. cb = h->cb;
  93. h->cb = NULL;
  94. cb_cls = h->cb_cls;
  95. h->cb_cls = NULL;
  96. if (NULL != cb)
  97. cb (cb_cls, bw);
  98. }
  99. /**
  100. * Schedule the flush completion notification task
  101. *
  102. * @param h logger handle
  103. */
  104. static void
  105. trigger_flush_notification (struct GNUNET_TESTBED_LOGGER_Handle *h)
  106. {
  107. if (NULL != h->flush_completion_task)
  108. GNUNET_SCHEDULER_cancel (h->flush_completion_task);
  109. h->flush_completion_task
  110. = GNUNET_SCHEDULER_add_now (&call_flush_completion,
  111. h);
  112. }
  113. /**
  114. * Send the buffered data to the service
  115. *
  116. * @param h the logger handle
  117. */
  118. static void
  119. dispatch_buffer (struct GNUNET_TESTBED_LOGGER_Handle *h);
  120. /**
  121. * MQ successfully sent a message.
  122. *
  123. * @param cls our handle
  124. */
  125. static void
  126. notify_sent (void *cls)
  127. {
  128. struct GNUNET_TESTBED_LOGGER_Handle *h = cls;
  129. h->mq_len--;
  130. if ((0 == h->mq_len) &&
  131. (NULL != h->cb))
  132. {
  133. if (0 == h->buse)
  134. trigger_flush_notification (h);
  135. else
  136. dispatch_buffer (h);
  137. }
  138. }
  139. /**
  140. * Send the buffered data to the service
  141. *
  142. * @param h the logger handle
  143. */
  144. static void
  145. dispatch_buffer (struct GNUNET_TESTBED_LOGGER_Handle *h)
  146. {
  147. struct GNUNET_MessageHeader *msg;
  148. struct GNUNET_MQ_Envelope *env;
  149. env = GNUNET_MQ_msg_extra (msg,
  150. h->buse,
  151. GNUNET_MESSAGE_TYPE_TESTBED_LOGGER_MSG);
  152. GNUNET_memcpy (&msg[1],
  153. h->buf,
  154. h->buse);
  155. h->bwrote += h->buse;
  156. h->buse = 0;
  157. h->mq_len++;
  158. GNUNET_MQ_notify_sent (env,
  159. &notify_sent,
  160. h);
  161. GNUNET_MQ_send (h->mq,
  162. env);
  163. }
  164. /**
  165. * We got disconnected from the logger. Stop logging.
  166. *
  167. * @param cls the `struct GNUNET_TESTBED_LOGGER_Handle`
  168. * @param error error code
  169. */
  170. static void
  171. mq_error_handler (void *cls,
  172. enum GNUNET_MQ_Error error)
  173. {
  174. struct GNUNET_TESTBED_LOGGER_Handle *h = cls;
  175. GNUNET_break (0);
  176. GNUNET_MQ_destroy (h->mq);
  177. h->mq = NULL;
  178. }
  179. /**
  180. * Connect to the testbed logger service
  181. *
  182. * @param cfg configuration to use
  183. * @return the handle which can be used for sending data to the service; NULL
  184. * upon any error
  185. */
  186. struct GNUNET_TESTBED_LOGGER_Handle *
  187. GNUNET_TESTBED_LOGGER_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
  188. {
  189. struct GNUNET_TESTBED_LOGGER_Handle *h;
  190. h = GNUNET_new (struct GNUNET_TESTBED_LOGGER_Handle);
  191. h->mq = GNUNET_CLIENT_connect (cfg,
  192. "testbed-logger",
  193. NULL,
  194. &mq_error_handler,
  195. h);
  196. if (NULL == h->mq)
  197. {
  198. GNUNET_free (h);
  199. return NULL;
  200. }
  201. return h;
  202. }
  203. /**
  204. * Disconnect from the logger service.
  205. *
  206. * @param h the logger handle
  207. */
  208. void
  209. GNUNET_TESTBED_LOGGER_disconnect (struct GNUNET_TESTBED_LOGGER_Handle *h)
  210. {
  211. if (NULL != h->flush_completion_task)
  212. {
  213. GNUNET_SCHEDULER_cancel (h->flush_completion_task);
  214. h->flush_completion_task = NULL;
  215. }
  216. if (0 != h->mq_len)
  217. LOG (GNUNET_ERROR_TYPE_WARNING,
  218. "Disconnect lost %u logger message[s]\n",
  219. h->mq_len);
  220. if (NULL != h->mq)
  221. {
  222. GNUNET_MQ_destroy (h->mq);
  223. h->mq = NULL;
  224. }
  225. GNUNET_free (h);
  226. }
  227. /**
  228. * Send data to be logged to the logger service. The data will be buffered and
  229. * will be sent upon an explicit call to GNUNET_TESTBED_LOGGER_flush() or upon
  230. * exceeding a threshold size.
  231. *
  232. * @param h the logger handle
  233. * @param data the data to send;
  234. * @param size how many bytes of @a data to send
  235. */
  236. void
  237. GNUNET_TESTBED_LOGGER_write (struct GNUNET_TESTBED_LOGGER_Handle *h,
  238. const void *data,
  239. size_t size)
  240. {
  241. if (NULL == h->mq)
  242. return;
  243. while (0 != size)
  244. {
  245. size_t fit_size = GNUNET_MIN (size,
  246. BUFFER_SIZE - h->buse);
  247. GNUNET_memcpy (&h->buf[h->buse],
  248. data,
  249. fit_size);
  250. h->buse += fit_size;
  251. data += fit_size;
  252. size -= fit_size;
  253. if (0 != size)
  254. dispatch_buffer (h);
  255. }
  256. }
  257. /**
  258. * Flush the buffered data to the logger service
  259. *
  260. * @param h the logger handle
  261. * @param cb the callback to call after the data is flushed
  262. * @param cb_cls the closure for the above callback
  263. */
  264. void
  265. GNUNET_TESTBED_LOGGER_flush (struct GNUNET_TESTBED_LOGGER_Handle *h,
  266. GNUNET_TESTBED_LOGGER_FlushCompletion cb,
  267. void *cb_cls)
  268. {
  269. GNUNET_assert (NULL == h->cb);
  270. h->cb = cb;
  271. h->cb_cls = cb_cls;
  272. if ((NULL == h->mq) ||
  273. (0 == h->buse))
  274. {
  275. trigger_flush_notification (h);
  276. return;
  277. }
  278. dispatch_buffer (h);
  279. }
  280. /**
  281. * Cancel notification upon flush. Should only be used when the flush
  282. * completion callback given to GNUNET_TESTBED_LOGGER_flush() is not already
  283. * called.
  284. *
  285. * @param h the logger handle
  286. */
  287. void
  288. GNUNET_TESTBED_LOGGER_flush_cancel (struct GNUNET_TESTBED_LOGGER_Handle *h)
  289. {
  290. if (NULL != h->flush_completion_task)
  291. {
  292. GNUNET_SCHEDULER_cancel (h->flush_completion_task);
  293. h->flush_completion_task = NULL;
  294. }
  295. h->cb = NULL;
  296. h->cb_cls = NULL;
  297. }
  298. /* End of testbed_logger_api.c */