testbed_api_barriers.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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/testbed_api_barriers.c
  18. * @brief API implementation for testbed barriers
  19. * @author Sree Harsha Totakura <sreeharsha@totakura.in>
  20. */
  21. #include "platform.h"
  22. #include "gnunet_testbed_service.h"
  23. #include "testbed_api.h"
  24. /**
  25. * Logging shorthand
  26. */
  27. #define LOG(type, ...) \
  28. GNUNET_log_from (type, "testbed-api-barriers", __VA_ARGS__);
  29. /**
  30. * Debug logging shorthand
  31. */
  32. #define LOG_DEBUG(...) \
  33. LOG (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__);
  34. /**
  35. * Barrier wait handle
  36. */
  37. struct GNUNET_TESTBED_BarrierWaitHandle
  38. {
  39. /**
  40. * The name of the barrier
  41. */
  42. char *name;
  43. /**
  44. * Then configuration used for the client connection
  45. */
  46. struct GNUNET_CONFIGURATION_Handle *cfg;
  47. /**
  48. * The testbed-barrier service message queue.
  49. */
  50. struct GNUNET_MQ_Handle *mq;
  51. /**
  52. * The barrier wait callback
  53. */
  54. GNUNET_TESTBED_barrier_wait_cb cb;
  55. /**
  56. * The closure for @e cb.
  57. */
  58. void *cb_cls;
  59. };
  60. /**
  61. * Check if barrier status message is well-formed.
  62. *
  63. * @param cls closure
  64. * @param msg received message
  65. * @return #GNUNET_OK if the message is well-formed.
  66. */
  67. static int
  68. check_status (void *cls,
  69. const struct GNUNET_TESTBED_BarrierStatusMsg *msg)
  70. {
  71. /* FIXME: this fails to actually check that the message
  72. follows the protocol spec (0-terminations!). However,
  73. not critical as #handle_status() doesn't interpret the
  74. variable-size part anyway right now. */
  75. return GNUNET_OK;
  76. }
  77. /**
  78. * Type of a function to call when we receive a message
  79. * from the service.
  80. *
  81. * @param cls closure
  82. * @param msg received message
  83. */
  84. static void
  85. handle_status (void *cls,
  86. const struct GNUNET_TESTBED_BarrierStatusMsg *msg)
  87. {
  88. struct GNUNET_TESTBED_BarrierWaitHandle *h = cls;
  89. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  90. "Got barrier status %d\n",
  91. (int) ntohs (msg->status));
  92. switch (ntohs (msg->status))
  93. {
  94. case GNUNET_TESTBED_BARRIERSTATUS_ERROR:
  95. h->cb (h->cb_cls,
  96. h->name,
  97. GNUNET_SYSERR);
  98. break;
  99. case GNUNET_TESTBED_BARRIERSTATUS_INITIALISED:
  100. h->cb (h->cb_cls,
  101. h->name,
  102. GNUNET_SYSERR);
  103. GNUNET_break (0);
  104. break;
  105. case GNUNET_TESTBED_BARRIERSTATUS_CROSSED:
  106. h->cb (h->cb_cls,
  107. h->name,
  108. GNUNET_OK);
  109. break;
  110. default:
  111. GNUNET_break_op (0);
  112. h->cb (h->cb_cls,
  113. h->name,
  114. GNUNET_SYSERR);
  115. break;
  116. }
  117. GNUNET_TESTBED_barrier_wait_cancel (h);
  118. }
  119. /**
  120. * Generic error handler, called with the appropriate error code and
  121. * the same closure specified at the creation of the message queue.
  122. * Not every message queue implementation supports an error handler.
  123. *
  124. * @param cls closure with the `struct GNUNET_TESTBED_BarrierWaitHandle *`
  125. * @param error error code
  126. */
  127. static void
  128. mq_error_handler (void *cls,
  129. enum GNUNET_MQ_Error error)
  130. {
  131. struct GNUNET_TESTBED_BarrierWaitHandle *h = cls;
  132. h->cb (h->cb_cls,
  133. h->name,
  134. GNUNET_SYSERR);
  135. GNUNET_TESTBED_barrier_wait_cancel (h);
  136. }
  137. /**
  138. * Wait for a barrier to be crossed. This function should be called by the
  139. * peers which have been started by the testbed. If the peer is not started by
  140. * testbed this function may return error
  141. *
  142. * @param name the name of the barrier
  143. * @param cb the barrier wait callback
  144. * @param cb_cls the closure for @a cb
  145. * @return barrier wait handle which can be used to cancel the waiting at
  146. * anytime before the callback is called. NULL upon error.
  147. */
  148. struct GNUNET_TESTBED_BarrierWaitHandle *
  149. GNUNET_TESTBED_barrier_wait (const char *name,
  150. GNUNET_TESTBED_barrier_wait_cb cb,
  151. void *cb_cls)
  152. {
  153. struct GNUNET_TESTBED_BarrierWaitHandle *h
  154. = GNUNET_new (struct GNUNET_TESTBED_BarrierWaitHandle);
  155. struct GNUNET_MQ_MessageHandler handlers[] = {
  156. GNUNET_MQ_hd_var_size (status,
  157. GNUNET_MESSAGE_TYPE_TESTBED_BARRIER_STATUS,
  158. struct GNUNET_TESTBED_BarrierStatusMsg,
  159. h),
  160. GNUNET_MQ_handler_end ()
  161. };
  162. struct GNUNET_MQ_Envelope *env;
  163. struct GNUNET_TESTBED_BarrierWait *msg;
  164. const char *cfg_filename;
  165. size_t name_len;
  166. GNUNET_assert (NULL != cb);
  167. cfg_filename = getenv (ENV_TESTBED_CONFIG);
  168. if (NULL == cfg_filename)
  169. {
  170. LOG (GNUNET_ERROR_TYPE_ERROR,
  171. "Are you running under testbed?\n");
  172. GNUNET_free (h);
  173. return NULL;
  174. }
  175. h->cfg = GNUNET_CONFIGURATION_create ();
  176. if (GNUNET_OK !=
  177. GNUNET_CONFIGURATION_load (h->cfg,
  178. cfg_filename))
  179. {
  180. LOG (GNUNET_ERROR_TYPE_ERROR,
  181. "Unable to load configuration from file `%s'\n",
  182. cfg_filename);
  183. GNUNET_CONFIGURATION_destroy (h->cfg);
  184. GNUNET_free (h);
  185. return NULL;
  186. }
  187. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  188. "Waiting on barrier `%s'\n",
  189. name);
  190. h->name = GNUNET_strdup (name);
  191. h->cb = cb;
  192. h->cb_cls = cb_cls;
  193. h->mq = GNUNET_CLIENT_connect (h->cfg,
  194. "testbed-barrier",
  195. handlers,
  196. &mq_error_handler,
  197. h);
  198. if (NULL == h->mq)
  199. {
  200. LOG (GNUNET_ERROR_TYPE_ERROR,
  201. "Unable to connect to local testbed-barrier service\n");
  202. GNUNET_TESTBED_barrier_wait_cancel (h);
  203. return NULL;
  204. }
  205. name_len = strlen (name); /* NOTE: unusual to not have 0-termination, change? */
  206. env = GNUNET_MQ_msg_extra (msg,
  207. name_len,
  208. GNUNET_MESSAGE_TYPE_TESTBED_BARRIER_WAIT);
  209. GNUNET_memcpy (msg->name,
  210. name,
  211. name_len);
  212. GNUNET_MQ_send (h->mq,
  213. env);
  214. return h;
  215. }
  216. /**
  217. * Cancel a barrier wait handle
  218. *
  219. * @param h the barrier wait handle
  220. */
  221. void
  222. GNUNET_TESTBED_barrier_wait_cancel (struct GNUNET_TESTBED_BarrierWaitHandle *h)
  223. {
  224. if (NULL != h->mq)
  225. {
  226. GNUNET_MQ_destroy (h->mq);
  227. h->mq = NULL;
  228. }
  229. GNUNET_free (h->name);
  230. GNUNET_CONFIGURATION_destroy (h->cfg);
  231. GNUNET_free (h);
  232. }
  233. /* end of testbed_api_barriers.c */