testbed_api_cmd_tng_service.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /*
  2. This file is part of GNUnet
  3. Copyright (C) 2021 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_cmd_tng.c
  18. * @brief Command to start the transport service of a peer.
  19. * @author t3sserakt
  20. */
  21. #include "platform.h"
  22. #include "gnunet_util_lib.h"
  23. #include "gnunet_testing_ng_lib.h"
  24. #include "gnunet-service-testbed.h"
  25. #include "testbed_api_hosts.h"
  26. #include "gnunet_testbed_ng_service.h"
  27. /**
  28. * Generic logging shortcut
  29. */
  30. #define LOG(kind, ...) \
  31. GNUNET_log (kind, __VA_ARGS__)
  32. /**
  33. * abort task to run on test timed out
  34. *
  35. * @param cls NULL
  36. * @param tc the task context
  37. */
  38. static void
  39. do_abort (void *cls)
  40. {
  41. struct TngState *ts = cls;
  42. if (GNUNET_NO == ts->service_ready)
  43. {
  44. LOG (GNUNET_ERROR_TYPE_WARNING, "Test timedout -- Aborting\n");
  45. ts->abort_task = NULL;
  46. GNUNET_TESTBED_shutdown_service (ts);
  47. }
  48. }
  49. /**
  50. *
  51. *
  52. * @param cls closure
  53. * @param cmd current CMD being cleaned up.
  54. */
  55. static void
  56. tng_service_cleanup (void *cls,
  57. const struct GNUNET_TESTING_Command *cmd)
  58. {
  59. (void) cls;
  60. }
  61. /**
  62. *
  63. *
  64. * @param cls closure.
  65. * @param[out] ret result
  66. * @param trait name of the trait.
  67. * @param index index number of the object to offer.
  68. * @return #GNUNET_OK on success.
  69. */
  70. static int
  71. tng_service_traits (void *cls,
  72. const void **ret,
  73. const char *trait,
  74. unsigned int index)
  75. {
  76. (void) cls;
  77. return GNUNET_OK;
  78. }
  79. static void *
  80. notify_connect (void *cls,
  81. const struct GNUNET_PeerIdentity *peer,
  82. struct GNUNET_MQ_Handle *mq)
  83. {
  84. struct TngState *ts = cls;
  85. if (NULL != emsg)
  86. {
  87. LOG (GNUNET_ERROR_TYPE_ERROR,
  88. "There was an error starting the transport subsystem: %s\n",
  89. emsg);
  90. }
  91. GNUNET_TESTING_interpreter_next (ps->is);
  92. return ts->nc (ts->cb_cls);
  93. }
  94. static void
  95. notify_disconnect (void *cls,
  96. const struct GNUNET_PeerIdentity *peer,
  97. void *handler_cls)
  98. {
  99. }
  100. /**
  101. * Adapter function called to establish a connection to
  102. * a service.
  103. *
  104. * @param cls closure
  105. * @param cfg configuration of the peer to connect to; will be available until
  106. * GNUNET_TESTBED_operation_done() is called on the operation returned
  107. * from GNUNET_TESTBED_service_connect()
  108. * @return service handle to return in 'op_result', NULL on error
  109. */
  110. static void *
  111. connect_adapter (void *cls,
  112. const struct GNUNET_CONFIGURATION_Handle *cfg)
  113. {
  114. struct TngState *ts = cls;
  115. service_handle = GNUNET_TRANSPORT_core_connect (cfg,
  116. ts->peer_identity,
  117. ts->handlers,
  118. ts,
  119. &notify_connect,
  120. &notify_disconnect);
  121. return service_handle;
  122. }
  123. /**
  124. * Adapter function called to destroy a connection to
  125. * a service.
  126. *
  127. * @param cls closure
  128. * @param op_result service handle returned from the connect adapter
  129. */
  130. static void
  131. disconnect_adapter (void *cls,
  132. void *op_result)
  133. {
  134. }
  135. /**
  136. * Callback to be called when a service connect operation is completed
  137. *
  138. * @param cls the callback closure from functions generating an operation
  139. * @param op the operation that has been finished
  140. * @param ca_result the service handle returned from GNUNET_TESTBED_ConnectAdapter()
  141. * @param emsg error message in case the operation has failed; will be NULL if
  142. * operation has executed successfully.
  143. */
  144. static void
  145. service_connect_comp_cb (void *cls,
  146. struct GNUNET_TESTBED_Operation *op,
  147. void *ca_result,
  148. const char *emsg)
  149. {
  150. struct TngState *ts = cls;
  151. if (NULL != emsg)
  152. {
  153. LOG (GNUNET_ERROR_TYPE_DEBUG,
  154. "An error occured connecting to service %s\n",
  155. emsg);
  156. GNUNET_TESTBED_operation_done (ts->operation);
  157. }
  158. }
  159. /**
  160. * Callback to be called when the requested peer information is available
  161. *
  162. * @param cls the closure from GNUNET_TESTBED_peer_getinformation()
  163. * @param op the operation this callback corresponds to
  164. * @param pinfo the result; will be NULL if the operation has failed
  165. * @param emsg error message if the operation has failed;
  166. * NULL if the operation is successfull
  167. */
  168. static void
  169. pi_cb (void *cls,
  170. struct GNUNET_TESTBED_Operation *op,
  171. const struct GNUNET_TESTBED_PeerInformation *pinfo,
  172. const char *emsg)
  173. {
  174. struct TngState *ts = cls;
  175. ts->peer_identity = pinfo->id;
  176. ts->operation =
  177. GNUNET_TESTBED_service_connect (NULL, peer, NULL,
  178. &service_connect_comp_cb, ts,
  179. &connect_adapter,
  180. &disconnect_adapter,
  181. ts);
  182. }
  183. static void
  184. tng_service_run (void *cls,
  185. const struct GNUNET_TESTING_Command *cmd,
  186. struct GNUNET_TESTING_Interpreter *is)
  187. {
  188. struct TngState *ts = cls;
  189. struct GNUNET_TESTBED_Peer *peer;
  190. const struct GNUNET_TESTING_Command *peer_cmd;
  191. ts->is = is;
  192. peer_cmd = GNUNET_TESTING_interpreter_lookup_command (
  193. ts->peer_label);
  194. GNUNET_TESTBED_get_trait_peer (peer_cmd,
  195. &peer);
  196. ts->operation = GNUNET_TESTBED_peer_get_information (peer,
  197. GNUNET_TESTBED_PIT_IDENTITY,
  198. &pi_cb,
  199. ts);
  200. }
  201. /**
  202. * Shutdown nicely
  203. *
  204. * @param cs service state.
  205. */
  206. void
  207. GNUNET_TESTBED_shutdown_service (struct TngState *cs)
  208. {
  209. LOG (GNUNET_ERROR_TYPE_DEBUG,
  210. "Shutting down...\n");
  211. }
  212. struct GNUNET_TESTING_Command
  213. GNUNET_TESTBED_cmd_tng_service (const char *label,
  214. const char *peer_label,
  215. const struct GNUNET_MQ_MessageHandler *handlers,
  216. GNUNET_TRANSPORT_NotifyConnect nc,
  217. void *cb_cls)
  218. {
  219. struct TngState *ts;
  220. ts = GNUNET_new (struct TngState);
  221. ts->servicename = servicename;
  222. ts->peer_label = peer_label;
  223. ts->handlers = handlers;
  224. ts->nc = nc;
  225. ts->nd = nd;
  226. ts->cb_cls;
  227. struct GNUNET_TESTING_Command cmd = {
  228. .cls = ts,
  229. .label = label,
  230. .run = &tng_service_run,
  231. .cleanup = &tmg_service_cleanup,
  232. .traits = &tng_service_traits
  233. };
  234. return cmd;
  235. }