2 This file is part of GNUnet.
3 Copyright (C) 2016 GNUnet e.V.
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
21 * @file transport-testing-send.c
22 * @brief convenience transmission function for tests
23 * @author Christian Grothoff
25 #include "transport-testing.h"
28 * Acceptable transmission delay.
30 #define TIMEOUT_TRANSMIT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
34 notify_ready (void *cls,
38 struct TRANSPORT_TESTING_SendJob *sj = cls;
39 struct GNUNET_TRANSPORT_TESTING_PeerContext *sender = sj->sender;
40 struct GNUNET_TRANSPORT_TESTING_PeerContext *receiver = sj->receiver;
41 struct GNUNET_TRANSPORT_TESTING_Handle *tth = sender->tth;
42 uint16_t msize = sj->msize;
43 struct GNUNET_TRANSPORT_TESTING_TestMessage *test;
46 GNUNET_CONTAINER_DLL_remove (tth->sj_head,
51 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
52 "Timeout occurred while waiting for transmit_ready\n");
53 GNUNET_SCHEDULER_shutdown ();
58 GNUNET_assert (size >= msize);
61 memset (buf, sj->num, msize);
63 test->header.size = htons (msize);
64 test->header.type = htons (sj->mtype);
65 test->num = htonl (sj->num);
69 char *ps = GNUNET_strdup (GNUNET_i2s (&sender->id));
71 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
72 "Sending message %u from %u (%s) with type %u and size %u bytes to peer %u (%s)\n",
73 (unsigned int) sj->num,
79 GNUNET_i2s (&receiver->id));
83 GNUNET_SCHEDULER_add_now (sj->cont,
91 * Return @a cx in @a cls.
95 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cx)
97 struct GNUNET_TRANSPORT_TESTING_ConnectRequest **cr = cls;
104 * Send a test message of type @a mtype and size @a msize from
105 * peer @a sender to peer @a receiver. The peers should be
106 * connected when this function is called.
108 * @param sender the sending peer
109 * @param receiver the receiving peer
110 * @param mtype message type to use
111 * @param msize size of the message, at least `sizeof (struct GNUNET_TRANSPORT_TESTING_TestMessage)`
112 * @param num unique message number
113 * @param cont continuation to call after transmission
114 * @param cont_cls closure for @a cont
115 * @return #GNUNET_OK if message was queued,
116 * #GNUNET_NO if peers are not connected
117 * #GNUNET_SYSERR if @a msize is illegal
120 GNUNET_TRANSPORT_TESTING_send (struct GNUNET_TRANSPORT_TESTING_PeerContext *sender,
121 struct GNUNET_TRANSPORT_TESTING_PeerContext *receiver,
125 GNUNET_SCHEDULER_TaskCallback cont,
128 struct GNUNET_TRANSPORT_TESTING_Handle *tth = sender->tth;
129 struct TRANSPORT_TESTING_SendJob *sj;
130 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cr;
132 if (msize < sizeof (struct GNUNET_TRANSPORT_TESTING_TestMessage))
135 return GNUNET_SYSERR;
138 GNUNET_TRANSPORT_TESTING_find_connecting_context (sender,
143 GNUNET_TRANSPORT_TESTING_find_connecting_context (receiver,
148 (GNUNET_YES != cr->connected) )
153 sj = GNUNET_new (struct TRANSPORT_TESTING_SendJob);
156 sj->receiver = receiver;
158 sj->cont_cls = cont_cls;
161 GNUNET_CONTAINER_DLL_insert (tth->sj_head,
165 char *receiver_s = GNUNET_strdup (GNUNET_i2s (&receiver->id));
167 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
168 "Sending message from peer %u (`%s') -> peer %u (`%s') !\n",
170 GNUNET_i2s (&sender->id),
173 GNUNET_free (receiver_s);
175 sj->th = GNUNET_TRANSPORT_notify_transmit_ready (sender->th,
181 GNUNET_assert (NULL != sj->th);
187 * Task that sends a test message from the
188 * first peer to the second peer.
190 * @param ccc context which should contain at least two peers, the
191 * first two of which should be currently connected
192 * @param size desired message size
193 * @param cont continuation to call after transmission
194 * @param cont_cls closure for @a cont
197 do_send (struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext *ccc,
199 GNUNET_SCHEDULER_TaskCallback cont,
204 ccc->global_ret = GNUNET_SYSERR;
205 ret = GNUNET_TRANSPORT_TESTING_send (ccc->p[0],
207 GNUNET_TRANSPORT_TESTING_SIMPLE_MTYPE,
212 GNUNET_assert (GNUNET_SYSERR != ret);
213 if (GNUNET_NO == ret)
216 ccc->global_ret = GNUNET_SYSERR;
217 GNUNET_SCHEDULER_shutdown ();
223 * Task that sends a minimalistic test message from the
224 * first peer to the second peer.
226 * @param cls the `struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext`
227 * which should contain at least two peers, the first two
228 * of which should be currently connected
231 GNUNET_TRANSPORT_TESTING_simple_send (void *cls)
233 struct GNUNET_TRANSPORT_TESTING_SendClosure *sc = cls;
237 if (0 < sc->num_messages)
240 done = (0 == sc->num_messages);
244 done = 0; /* infinite loop */
246 msize = sizeof (struct GNUNET_TRANSPORT_TESTING_TestMessage);
247 if (NULL != sc->get_size_cb)
248 msize = sc->get_size_cb (sc->num_messages);
249 /* if this was the last message, call the continuation,
250 otherwise call this function again */
253 done ? sc->cont : &GNUNET_TRANSPORT_TESTING_simple_send,
254 done ? sc->cont_cls : sc);
259 * Task that sends a large test message from the
260 * first peer to the second peer.
262 * @param cls the `struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext`
263 * which should contain at least two peers, the first two
264 * of which should be currently connected
267 GNUNET_TRANSPORT_TESTING_large_send (void *cls)
269 struct GNUNET_TRANSPORT_TESTING_SendClosure *sc = cls;
273 if (0 < sc->num_messages)
276 done = (0 == sc->num_messages);
280 done = 0; /* infinite loop */
283 if (NULL != sc->get_size_cb)
284 msize = sc->get_size_cb (sc->num_messages);
285 /* if this was the last message, call the continuation,
286 otherwise call this function again */
289 done ? sc->cont : &GNUNET_TRANSPORT_TESTING_large_send,
290 done ? sc->cont_cls : sc);
293 /* end of transport-testing-send.c */