aboutsummaryrefslogtreecommitdiff
path: root/src/transport/transport-testing-send.c
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2016-07-15 21:41:11 +0000
committerChristian Grothoff <christian@grothoff.org>2016-07-15 21:41:11 +0000
commitd0fdba92b0c8c861d223312f19f9278ba63e7f98 (patch)
treeb3794d2fa0490917513c2faeeaf89d4b075ee23c /src/transport/transport-testing-send.c
parent948b042072a8e50f36ad63a0f0c5b78c9140f59d (diff)
downloadgnunet-d0fdba92b0c8c861d223312f19f9278ba63e7f98.tar.gz
gnunet-d0fdba92b0c8c861d223312f19f9278ba63e7f98.zip
towards having sending in transport-testing API (not yet flexible enough)
Diffstat (limited to 'src/transport/transport-testing-send.c')
-rw-r--r--src/transport/transport-testing-send.c241
1 files changed, 241 insertions, 0 deletions
diff --git a/src/transport/transport-testing-send.c b/src/transport/transport-testing-send.c
new file mode 100644
index 000000000..20d1f2fbc
--- /dev/null
+++ b/src/transport/transport-testing-send.c
@@ -0,0 +1,241 @@
1/*
2 This file is part of GNUnet.
3 Copyright (C) 2016 GNUnet e.V.
4
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.
9
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.
14
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.
19*/
20/**
21 * @file transport-testing-send.c
22 * @brief convenience transmission function for tests
23 * @author Christian Grothoff
24 */
25#include "transport-testing.h"
26
27/**
28 * Acceptable transmission delay.
29 */
30#define TIMEOUT_TRANSMIT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
31
32
33static size_t
34notify_ready (void *cls,
35 size_t size,
36 void *buf)
37{
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;
44
45 sj->th = NULL;
46 GNUNET_CONTAINER_DLL_remove (tth->sj_head,
47 tth->sj_tail,
48 sj);
49 if (NULL == buf)
50 {
51 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
52 "Timeout occurred while waiting for transmit_ready\n");
53 GNUNET_SCHEDULER_shutdown ();
54 GNUNET_free (sj);
55 return 0;
56 }
57
58 GNUNET_assert (size >= msize);
59 if (NULL != buf)
60 {
61 memset (buf, '\0', msize);
62 test = buf;
63 test->header.size = htons (msize);
64 test->header.type = htons (sj->mtype);
65 test->num = htonl (sj->num);
66 }
67
68 {
69 char *ps = GNUNET_strdup (GNUNET_i2s (&sender->id));
70
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,
74 sender->no,
75 ps,
76 sj->mtype,
77 msize,
78 receiver->no,
79 GNUNET_i2s (&receiver->id));
80 GNUNET_free (ps);
81 }
82 GNUNET_free (sj);
83 return msize;
84}
85
86
87/**
88 * Return @a cx in @a cls.
89 */
90static void
91find_cr (void *cls,
92 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cx)
93{
94 struct GNUNET_TRANSPORT_TESTING_ConnectRequest **cr = cls;
95
96 *cr = cx;
97}
98
99
100/**
101 * Send a test message of type @a mtype and size @a msize from
102 * peer @a sender to peer @a receiver. The peers should be
103 * connected when this function is called.
104 *
105 * @param sender the sending peer
106 * @param receiver the receiving peer
107 * @param mtype message type to use
108 * @param msize size of the message, at least `sizeof (struct GNUNET_TRANSPORT_TESTING_TestMessage)`
109 * @param num unique message number
110 * @return #GNUNET_OK if message was queued,
111 * #GNUNET_NO if peers are not connected
112 * #GNUNET_SYSERR if @a msize is illegal
113 */
114int
115GNUNET_TRANSPORT_TESTING_send (struct GNUNET_TRANSPORT_TESTING_PeerContext *sender,
116 struct GNUNET_TRANSPORT_TESTING_PeerContext *receiver,
117 uint16_t mtype,
118 uint16_t msize,
119 uint32_t num)
120{
121 struct GNUNET_TRANSPORT_TESTING_Handle *tth = sender->tth;
122 struct TRANSPORT_TESTING_SendJob *sj;
123 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cr;
124
125 if (msize < sizeof (struct GNUNET_TRANSPORT_TESTING_TestMessage))
126 {
127 GNUNET_break (0);
128 return GNUNET_SYSERR;
129 }
130 cr = NULL;
131 GNUNET_TRANSPORT_TESTING_find_connecting_context (sender,
132 receiver,
133 &find_cr,
134 &cr);
135 if (NULL == cr)
136 GNUNET_TRANSPORT_TESTING_find_connecting_context (receiver,
137 sender,
138 &find_cr,
139 &cr);
140 if ( (NULL == cr) ||
141 (GNUNET_YES != cr->connected) )
142 {
143 GNUNET_break (0);
144 return GNUNET_NO;
145 }
146 sj = GNUNET_new (struct TRANSPORT_TESTING_SendJob);
147 sj->num = num;
148 sj->sender = sender;
149 sj->receiver = receiver;
150 sj->mtype = mtype;
151 sj->msize = msize;
152 GNUNET_CONTAINER_DLL_insert (tth->sj_head,
153 tth->sj_tail,
154 sj);
155 {
156 char *receiver_s = GNUNET_strdup (GNUNET_i2s (&receiver->id));
157
158 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
159 "Sending message from peer %u (`%s') -> peer %u (`%s') !\n",
160 sender->no,
161 GNUNET_i2s (&sender->id),
162 receiver->no,
163 receiver_s);
164 GNUNET_free (receiver_s);
165 }
166 sj->th = GNUNET_TRANSPORT_notify_transmit_ready (sender->th,
167 &receiver->id,
168 msize,
169 TIMEOUT_TRANSMIT,
170 &notify_ready,
171 sj);
172 GNUNET_assert (NULL != sj->th);
173 return GNUNET_OK;
174}
175
176
177/**
178 * Task that sends a test message from the
179 * first peer to the second peer.
180 *
181 * @param ccc context which should contain at least two peers, the
182 * first two of which should be currently connected
183 */
184static void
185do_send (struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext *ccc,
186 uint16_t size)
187{
188 int ret;
189
190 ccc->global_ret = GNUNET_SYSERR;
191 ret = GNUNET_TRANSPORT_TESTING_send (ccc->p[0],
192 ccc->p[1],
193 GNUNET_TRANSPORT_TESTING_SIMPLE_MTYPE,
194 size,
195 ccc->send_num_gen++);
196 GNUNET_assert (GNUNET_SYSERR != ret);
197 if (GNUNET_NO == ret)
198 {
199 GNUNET_break (0);
200 ccc->global_ret = GNUNET_SYSERR;
201 GNUNET_SCHEDULER_shutdown ();
202 }
203}
204
205
206/**
207 * Task that sends a minimalistic test message from the
208 * first peer to the second peer.
209 *
210 * @param cls the `struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext`
211 * which should contain at least two peers, the first two
212 * of which should be currently connected
213 */
214void
215GNUNET_TRANSPORT_TESTING_simple_send (void *cls)
216{
217 struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext *ccc = cls;
218
219 do_send (ccc,
220 sizeof (struct GNUNET_MessageHeader));
221}
222
223
224/**
225 * Task that sends a large test message from the
226 * first peer to the second peer.
227 *
228 * @param cls the `struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext`
229 * which should contain at least two peers, the first two
230 * of which should be currently connected
231 */
232void
233GNUNET_TRANSPORT_TESTING_large_send (void *cls)
234{
235 struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext *ccc = cls;
236
237 do_send (ccc,
238 2600);
239}
240
241/* end of transport-testing-send.c */