diff options
author | Martin Schanzenbach <schanzen@gnunet.org> | 2023-10-18 19:35:11 +0200 |
---|---|---|
committer | Martin Schanzenbach <schanzen@gnunet.org> | 2023-10-18 19:35:11 +0200 |
commit | ddfee3f564bff9c5d5719af3132d7869b8783ec4 (patch) | |
tree | e6fd7801fe6808797f3418bf081ab68d5a5ec27b /src/service/transport/transport-testing-send2.c | |
parent | 852718c2473e41bc01ada0d53ad93c7da78e6ec8 (diff) | |
download | gnunet-ddfee3f564bff9c5d5719af3132d7869b8783ec4.tar.gz gnunet-ddfee3f564bff9c5d5719af3132d7869b8783ec4.zip |
BUILD: more more components into new structure; ftbfs fix
Diffstat (limited to 'src/service/transport/transport-testing-send2.c')
-rw-r--r-- | src/service/transport/transport-testing-send2.c | 241 |
1 files changed, 241 insertions, 0 deletions
diff --git a/src/service/transport/transport-testing-send2.c b/src/service/transport/transport-testing-send2.c new file mode 100644 index 000000000..c48dc3a4a --- /dev/null +++ b/src/service/transport/transport-testing-send2.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 it | ||
6 | under the terms of the GNU Affero General Public License as published | ||
7 | by the Free Software Foundation, either version 3 of the License, | ||
8 | or (at your 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 | Affero General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU Affero General Public License | ||
16 | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
17 | |||
18 | SPDX-License-Identifier: AGPL3.0-or-later | ||
19 | */ | ||
20 | /** | ||
21 | * @file transport-testing-send.c | ||
22 | * @brief convenience transmission function for tests | ||
23 | * @author Christian Grothoff | ||
24 | */ | ||
25 | #include "platform.h" | ||
26 | #include "transport-testing2.h" | ||
27 | |||
28 | /** | ||
29 | * Acceptable transmission delay. | ||
30 | */ | ||
31 | #define TIMEOUT_TRANSMIT GNUNET_TIME_relative_multiply ( \ | ||
32 | GNUNET_TIME_UNIT_SECONDS, 30) | ||
33 | |||
34 | |||
35 | /** | ||
36 | * Return @a cx in @a cls. | ||
37 | */ | ||
38 | static void | ||
39 | find_cr (void *cls, | ||
40 | struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cx) | ||
41 | { | ||
42 | struct GNUNET_TRANSPORT_TESTING_ConnectRequest **cr = cls; | ||
43 | |||
44 | if (GNUNET_NO == cx->connected) | ||
45 | return; | ||
46 | *cr = cx; | ||
47 | } | ||
48 | |||
49 | |||
50 | /** | ||
51 | * Send a test message of type @a mtype and size @a msize from | ||
52 | * peer @a sender to peer @a receiver. The peers should be | ||
53 | * connected when this function is called. | ||
54 | * | ||
55 | * @param sender the sending peer | ||
56 | * @param receiver the receiving peer | ||
57 | * @param mtype message type to use | ||
58 | * @param msize size of the message, at least `sizeof (struct GNUNET_TRANSPORT_TESTING_TestMessage)` | ||
59 | * @param num unique message number | ||
60 | * @param cont continuation to call after transmission | ||
61 | * @param cont_cls closure for @a cont | ||
62 | * @return #GNUNET_OK if message was queued, | ||
63 | * #GNUNET_NO if peers are not connected | ||
64 | * #GNUNET_SYSERR if @a msize is illegal | ||
65 | */ | ||
66 | int | ||
67 | GNUNET_TRANSPORT_TESTING_send (struct | ||
68 | GNUNET_TRANSPORT_TESTING_PeerContext *sender, | ||
69 | struct GNUNET_TRANSPORT_TESTING_PeerContext * | ||
70 | receiver, | ||
71 | uint16_t mtype, | ||
72 | uint16_t msize, | ||
73 | uint32_t num, | ||
74 | GNUNET_SCHEDULER_TaskCallback cont, | ||
75 | void *cont_cls) | ||
76 | { | ||
77 | struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cr; | ||
78 | struct GNUNET_MQ_Envelope *env; | ||
79 | struct GNUNET_TRANSPORT_TESTING_TestMessage *test; | ||
80 | |||
81 | if (msize < sizeof(struct GNUNET_TRANSPORT_TESTING_TestMessage)) | ||
82 | { | ||
83 | GNUNET_break (0); | ||
84 | return GNUNET_SYSERR; | ||
85 | } | ||
86 | cr = NULL; | ||
87 | GNUNET_TRANSPORT_TESTING_find_connecting_context (sender, | ||
88 | receiver, | ||
89 | &find_cr, | ||
90 | &cr); | ||
91 | if (NULL == cr) | ||
92 | GNUNET_TRANSPORT_TESTING_find_connecting_context (receiver, | ||
93 | sender, | ||
94 | &find_cr, | ||
95 | &cr); | ||
96 | if (NULL == cr) | ||
97 | { | ||
98 | GNUNET_break (0); | ||
99 | return GNUNET_NO; | ||
100 | } | ||
101 | if (NULL == cr->mq) | ||
102 | { | ||
103 | GNUNET_break (0); | ||
104 | return GNUNET_NO; | ||
105 | } | ||
106 | { | ||
107 | char *receiver_s = GNUNET_strdup (GNUNET_i2s (&receiver->id)); | ||
108 | |||
109 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
110 | "Sending message from peer %u (`%s') -> peer %u (`%s') !\n", | ||
111 | sender->no, | ||
112 | GNUNET_i2s (&sender->id), | ||
113 | receiver->no, | ||
114 | receiver_s); | ||
115 | GNUNET_free (receiver_s); | ||
116 | } | ||
117 | env = GNUNET_MQ_msg_extra (test, | ||
118 | msize - sizeof(*test), | ||
119 | mtype); | ||
120 | test->num = htonl (num); | ||
121 | memset (&test[1], | ||
122 | num, | ||
123 | msize - sizeof(*test)); | ||
124 | GNUNET_MQ_notify_sent (env, | ||
125 | cont, | ||
126 | cont_cls); | ||
127 | GNUNET_MQ_send (cr->mq, | ||
128 | env); | ||
129 | return GNUNET_OK; | ||
130 | } | ||
131 | |||
132 | |||
133 | /** | ||
134 | * Task that sends a test message from the | ||
135 | * first peer to the second peer. | ||
136 | * | ||
137 | * @param ccc context which should contain at least two peers, the | ||
138 | * first two of which should be currently connected | ||
139 | * @param size desired message size | ||
140 | * @param cont continuation to call after transmission | ||
141 | * @param cont_cls closure for @a cont | ||
142 | */ | ||
143 | static void | ||
144 | do_send (struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext *ccc, | ||
145 | uint16_t size, | ||
146 | GNUNET_SCHEDULER_TaskCallback cont, | ||
147 | void *cont_cls) | ||
148 | { | ||
149 | int ret; | ||
150 | |||
151 | ccc->global_ret = GNUNET_SYSERR; | ||
152 | ret = GNUNET_TRANSPORT_TESTING_send (ccc->p[0], | ||
153 | ccc->p[1], | ||
154 | GNUNET_TRANSPORT_TESTING_SIMPLE_MTYPE, | ||
155 | size, | ||
156 | ccc->send_num_gen++, | ||
157 | cont, | ||
158 | cont_cls); | ||
159 | GNUNET_assert (GNUNET_SYSERR != ret); | ||
160 | if (GNUNET_NO == ret) | ||
161 | { | ||
162 | GNUNET_break (0); | ||
163 | ccc->global_ret = GNUNET_SYSERR; | ||
164 | GNUNET_SCHEDULER_shutdown (); | ||
165 | } | ||
166 | } | ||
167 | |||
168 | |||
169 | /** | ||
170 | * Task that sends a minimalistic test message from the | ||
171 | * first peer to the second peer. | ||
172 | * | ||
173 | * @param cls the `struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext` | ||
174 | * which should contain at least two peers, the first two | ||
175 | * of which should be currently connected | ||
176 | */ | ||
177 | void | ||
178 | GNUNET_TRANSPORT_TESTING_simple_send (void *cls) | ||
179 | { | ||
180 | struct GNUNET_TRANSPORT_TESTING_SendClosure *sc = cls; | ||
181 | int done; | ||
182 | size_t msize; | ||
183 | |||
184 | if (0 < sc->num_messages) | ||
185 | { | ||
186 | sc->num_messages--; | ||
187 | done = (0 == sc->num_messages); | ||
188 | } | ||
189 | else | ||
190 | { | ||
191 | done = 0; /* infinite loop */ | ||
192 | } | ||
193 | msize = sizeof(struct GNUNET_TRANSPORT_TESTING_TestMessage); | ||
194 | if (NULL != sc->get_size_cb) | ||
195 | msize = sc->get_size_cb (sc->num_messages); | ||
196 | /* if this was the last message, call the continuation, | ||
197 | otherwise call this function again */ | ||
198 | do_send (sc->ccc, | ||
199 | msize, | ||
200 | done ? sc->cont : &GNUNET_TRANSPORT_TESTING_simple_send, | ||
201 | done ? sc->cont_cls : sc); | ||
202 | } | ||
203 | |||
204 | |||
205 | /** | ||
206 | * Task that sends a large test message from the | ||
207 | * first peer to the second peer. | ||
208 | * | ||
209 | * @param cls the `struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext` | ||
210 | * which should contain at least two peers, the first two | ||
211 | * of which should be currently connected | ||
212 | */ | ||
213 | void | ||
214 | GNUNET_TRANSPORT_TESTING_large_send (void *cls) | ||
215 | { | ||
216 | struct GNUNET_TRANSPORT_TESTING_SendClosure *sc = cls; | ||
217 | int done; | ||
218 | size_t msize; | ||
219 | |||
220 | if (0 < sc->num_messages) | ||
221 | { | ||
222 | sc->num_messages--; | ||
223 | done = (0 == sc->num_messages); | ||
224 | } | ||
225 | else | ||
226 | { | ||
227 | done = 0; /* infinite loop */ | ||
228 | } | ||
229 | msize = 2600; | ||
230 | if (NULL != sc->get_size_cb) | ||
231 | msize = sc->get_size_cb (sc->num_messages); | ||
232 | /* if this was the last message, call the continuation, | ||
233 | otherwise call this function again */ | ||
234 | do_send (sc->ccc, | ||
235 | msize, | ||
236 | done ? sc->cont : &GNUNET_TRANSPORT_TESTING_large_send, | ||
237 | done ? sc->cont_cls : sc); | ||
238 | } | ||
239 | |||
240 | |||
241 | /* end of transport-testing-send.c */ | ||