transport-testing-send2.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 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 transport-testing-send.c
  18. * @brief convenience transmission function for tests
  19. * @author Christian Grothoff
  20. */
  21. #include "transport-testing2.h"
  22. /**
  23. * Acceptable transmission delay.
  24. */
  25. #define TIMEOUT_TRANSMIT GNUNET_TIME_relative_multiply ( \
  26. GNUNET_TIME_UNIT_SECONDS, 30)
  27. /**
  28. * Return @a cx in @a cls.
  29. */
  30. static void
  31. find_cr (void *cls,
  32. struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cx)
  33. {
  34. struct GNUNET_TRANSPORT_TESTING_ConnectRequest **cr = cls;
  35. if (GNUNET_NO == cx->connected)
  36. return;
  37. *cr = cx;
  38. }
  39. /**
  40. * Send a test message of type @a mtype and size @a msize from
  41. * peer @a sender to peer @a receiver. The peers should be
  42. * connected when this function is called.
  43. *
  44. * @param sender the sending peer
  45. * @param receiver the receiving peer
  46. * @param mtype message type to use
  47. * @param msize size of the message, at least `sizeof (struct GNUNET_TRANSPORT_TESTING_TestMessage)`
  48. * @param num unique message number
  49. * @param cont continuation to call after transmission
  50. * @param cont_cls closure for @a cont
  51. * @return #GNUNET_OK if message was queued,
  52. * #GNUNET_NO if peers are not connected
  53. * #GNUNET_SYSERR if @a msize is illegal
  54. */
  55. int
  56. GNUNET_TRANSPORT_TESTING_send (struct
  57. GNUNET_TRANSPORT_TESTING_PeerContext *sender,
  58. struct GNUNET_TRANSPORT_TESTING_PeerContext *
  59. receiver,
  60. uint16_t mtype,
  61. uint16_t msize,
  62. uint32_t num,
  63. GNUNET_SCHEDULER_TaskCallback cont,
  64. void *cont_cls)
  65. {
  66. struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cr;
  67. struct GNUNET_MQ_Envelope *env;
  68. struct GNUNET_TRANSPORT_TESTING_TestMessage *test;
  69. if (msize < sizeof(struct GNUNET_TRANSPORT_TESTING_TestMessage))
  70. {
  71. GNUNET_break (0);
  72. return GNUNET_SYSERR;
  73. }
  74. cr = NULL;
  75. GNUNET_TRANSPORT_TESTING_find_connecting_context (sender,
  76. receiver,
  77. &find_cr,
  78. &cr);
  79. if (NULL == cr)
  80. GNUNET_TRANSPORT_TESTING_find_connecting_context (receiver,
  81. sender,
  82. &find_cr,
  83. &cr);
  84. if (NULL == cr)
  85. {
  86. GNUNET_break (0);
  87. return GNUNET_NO;
  88. }
  89. if (NULL == cr->mq)
  90. {
  91. GNUNET_break (0);
  92. return GNUNET_NO;
  93. }
  94. {
  95. char *receiver_s = GNUNET_strdup (GNUNET_i2s (&receiver->id));
  96. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  97. "Sending message from peer %u (`%s') -> peer %u (`%s') !\n",
  98. sender->no,
  99. GNUNET_i2s (&sender->id),
  100. receiver->no,
  101. receiver_s);
  102. GNUNET_free (receiver_s);
  103. }
  104. env = GNUNET_MQ_msg_extra (test,
  105. msize - sizeof(*test),
  106. mtype);
  107. test->num = htonl (num);
  108. memset (&test[1],
  109. num,
  110. msize - sizeof(*test));
  111. GNUNET_MQ_notify_sent (env,
  112. cont,
  113. cont_cls);
  114. GNUNET_MQ_send (cr->mq,
  115. env);
  116. return GNUNET_OK;
  117. }
  118. /**
  119. * Task that sends a test message from the
  120. * first peer to the second peer.
  121. *
  122. * @param ccc context which should contain at least two peers, the
  123. * first two of which should be currently connected
  124. * @param size desired message size
  125. * @param cont continuation to call after transmission
  126. * @param cont_cls closure for @a cont
  127. */
  128. static void
  129. do_send (struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext *ccc,
  130. uint16_t size,
  131. GNUNET_SCHEDULER_TaskCallback cont,
  132. void *cont_cls)
  133. {
  134. int ret;
  135. ccc->global_ret = GNUNET_SYSERR;
  136. ret = GNUNET_TRANSPORT_TESTING_send (ccc->p[0],
  137. ccc->p[1],
  138. GNUNET_TRANSPORT_TESTING_SIMPLE_MTYPE,
  139. size,
  140. ccc->send_num_gen++,
  141. cont,
  142. cont_cls);
  143. GNUNET_assert (GNUNET_SYSERR != ret);
  144. if (GNUNET_NO == ret)
  145. {
  146. GNUNET_break (0);
  147. ccc->global_ret = GNUNET_SYSERR;
  148. GNUNET_SCHEDULER_shutdown ();
  149. }
  150. }
  151. /**
  152. * Task that sends a minimalistic test message from the
  153. * first peer to the second peer.
  154. *
  155. * @param cls the `struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext`
  156. * which should contain at least two peers, the first two
  157. * of which should be currently connected
  158. */
  159. void
  160. GNUNET_TRANSPORT_TESTING_simple_send (void *cls)
  161. {
  162. struct GNUNET_TRANSPORT_TESTING_SendClosure *sc = cls;
  163. int done;
  164. size_t msize;
  165. if (0 < sc->num_messages)
  166. {
  167. sc->num_messages--;
  168. done = (0 == sc->num_messages);
  169. }
  170. else
  171. {
  172. done = 0; /* infinite loop */
  173. }
  174. msize = sizeof(struct GNUNET_TRANSPORT_TESTING_TestMessage);
  175. if (NULL != sc->get_size_cb)
  176. msize = sc->get_size_cb (sc->num_messages);
  177. /* if this was the last message, call the continuation,
  178. otherwise call this function again */
  179. do_send (sc->ccc,
  180. msize,
  181. done ? sc->cont : &GNUNET_TRANSPORT_TESTING_simple_send,
  182. done ? sc->cont_cls : sc);
  183. }
  184. /**
  185. * Task that sends a large test message from the
  186. * first peer to the second peer.
  187. *
  188. * @param cls the `struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext`
  189. * which should contain at least two peers, the first two
  190. * of which should be currently connected
  191. */
  192. void
  193. GNUNET_TRANSPORT_TESTING_large_send (void *cls)
  194. {
  195. struct GNUNET_TRANSPORT_TESTING_SendClosure *sc = cls;
  196. int done;
  197. size_t msize;
  198. if (0 < sc->num_messages)
  199. {
  200. sc->num_messages--;
  201. done = (0 == sc->num_messages);
  202. }
  203. else
  204. {
  205. done = 0; /* infinite loop */
  206. }
  207. msize = 2600;
  208. if (NULL != sc->get_size_cb)
  209. msize = sc->get_size_cb (sc->num_messages);
  210. /* if this was the last message, call the continuation,
  211. otherwise call this function again */
  212. do_send (sc->ccc,
  213. msize,
  214. done ? sc->cont : &GNUNET_TRANSPORT_TESTING_large_send,
  215. done ? sc->cont_cls : sc);
  216. }
  217. /* end of transport-testing-send.c */