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;
99 if (GNUNET_NO == cx->connected)
106 * Send a test message of type @a mtype and size @a msize from
107 * peer @a sender to peer @a receiver. The peers should be
108 * connected when this function is called.
110 * @param sender the sending peer
111 * @param receiver the receiving peer
112 * @param mtype message type to use
113 * @param msize size of the message, at least `sizeof (struct GNUNET_TRANSPORT_TESTING_TestMessage)`
114 * @param num unique message number
115 * @param cont continuation to call after transmission
116 * @param cont_cls closure for @a cont
117 * @return #GNUNET_OK if message was queued,
118 * #GNUNET_NO if peers are not connected
119 * #GNUNET_SYSERR if @a msize is illegal
122 GNUNET_TRANSPORT_TESTING_send (struct GNUNET_TRANSPORT_TESTING_PeerContext *sender,
123 struct GNUNET_TRANSPORT_TESTING_PeerContext *receiver,
127 GNUNET_SCHEDULER_TaskCallback cont,
130 struct GNUNET_TRANSPORT_TESTING_Handle *tth = sender->tth;
131 struct TRANSPORT_TESTING_SendJob *sj;
132 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cr;
134 if (msize < sizeof (struct GNUNET_TRANSPORT_TESTING_TestMessage))
137 return GNUNET_SYSERR;
140 GNUNET_TRANSPORT_TESTING_find_connecting_context (sender,
145 GNUNET_TRANSPORT_TESTING_find_connecting_context (receiver,
154 sj = GNUNET_new (struct TRANSPORT_TESTING_SendJob);
157 sj->receiver = receiver;
159 sj->cont_cls = cont_cls;
162 GNUNET_CONTAINER_DLL_insert (tth->sj_head,
166 char *receiver_s = GNUNET_strdup (GNUNET_i2s (&receiver->id));
168 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
169 "Sending message from peer %u (`%s') -> peer %u (`%s') !\n",
171 GNUNET_i2s (&sender->id),
174 GNUNET_free (receiver_s);
176 sj->th = GNUNET_TRANSPORT_notify_transmit_ready (sender->th,
182 GNUNET_assert (NULL != sj->th);
188 * Task that sends a test message from the
189 * first peer to the second peer.
191 * @param ccc context which should contain at least two peers, the
192 * first two of which should be currently connected
193 * @param size desired message size
194 * @param cont continuation to call after transmission
195 * @param cont_cls closure for @a cont
198 do_send (struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext *ccc,
200 GNUNET_SCHEDULER_TaskCallback cont,
205 ccc->global_ret = GNUNET_SYSERR;
206 ret = GNUNET_TRANSPORT_TESTING_send (ccc->p[0],
208 GNUNET_TRANSPORT_TESTING_SIMPLE_MTYPE,
213 GNUNET_assert (GNUNET_SYSERR != ret);
214 if (GNUNET_NO == ret)
217 ccc->global_ret = GNUNET_SYSERR;
218 GNUNET_SCHEDULER_shutdown ();
224 * Task that sends a minimalistic test message from the
225 * first peer to the second peer.
227 * @param cls the `struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext`
228 * which should contain at least two peers, the first two
229 * of which should be currently connected
232 GNUNET_TRANSPORT_TESTING_simple_send (void *cls)
234 struct GNUNET_TRANSPORT_TESTING_SendClosure *sc = cls;
238 if (0 < sc->num_messages)
241 done = (0 == sc->num_messages);
245 done = 0; /* infinite loop */
247 msize = sizeof (struct GNUNET_TRANSPORT_TESTING_TestMessage);
248 if (NULL != sc->get_size_cb)
249 msize = sc->get_size_cb (sc->num_messages);
250 /* if this was the last message, call the continuation,
251 otherwise call this function again */
254 done ? sc->cont : &GNUNET_TRANSPORT_TESTING_simple_send,
255 done ? sc->cont_cls : sc);
260 * Task that sends a large test message from the
261 * first peer to the second peer.
263 * @param cls the `struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext`
264 * which should contain at least two peers, the first two
265 * of which should be currently connected
268 GNUNET_TRANSPORT_TESTING_large_send (void *cls)
270 struct GNUNET_TRANSPORT_TESTING_SendClosure *sc = cls;
274 if (0 < sc->num_messages)
277 done = (0 == sc->num_messages);
281 done = 0; /* infinite loop */
284 if (NULL != sc->get_size_cb)
285 msize = sc->get_size_cb (sc->num_messages);
286 /* if this was the last message, call the continuation,
287 otherwise call this function again */
290 done ? sc->cont : &GNUNET_TRANSPORT_TESTING_large_send,
291 done ? sc->cont_cls : sc);
294 /* end of transport-testing-send.c */