2 This file is part of GNUnet.
3 (C) 2011, 2012 Christian Grothoff (and other contributing authors)
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., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file stream/test_stream_big.c
23 * @brief large data transfer using stream API between local peers
24 * @author Sree Harsha Totakura
30 #include "gnunet_util_lib.h"
31 #include "gnunet_stream_lib.h"
32 #include "gnunet_testing_lib.h"
34 #define LOG(kind, ...) \
35 GNUNET_log (kind, __VA_ARGS__);
37 #define TIME_REL_SECS(sec) \
38 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, sec)
42 * Structure for holding peer's sockets and IO Handles
47 * Peer's stream socket
49 struct GNUNET_STREAM_Socket *socket;
52 * Peer's io write handle
54 struct GNUNET_STREAM_IOWriteHandle *io_write_handle;
57 * Peer's io read handle
59 struct GNUNET_STREAM_IOReadHandle *io_read_handle;
62 * Bytes the peer has written
64 unsigned int bytes_wrote;
67 * Byte the peer has read
69 unsigned int bytes_read;
72 static struct GNUNET_OS_Process *arm_pid;
73 static struct PeerData peer1;
74 static struct PeerData peer2;
75 static struct GNUNET_STREAM_ListenSocket *peer2_listen_socket;
76 static struct GNUNET_CONFIGURATION_Handle *config;
78 static GNUNET_SCHEDULER_TaskIdentifier abort_task;
79 static GNUNET_SCHEDULER_TaskIdentifier test_task;
80 static GNUNET_SCHEDULER_TaskIdentifier read_task;
81 static GNUNET_SCHEDULER_TaskIdentifier write_task;
83 #define DATA_SIZE 65536 /* 64KB */
84 static uint32_t data[DATA_SIZE / 4]; /* 64KB array */
91 do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
93 GNUNET_STREAM_close (peer1.socket);
94 if (NULL != peer2.socket)
95 GNUNET_STREAM_close (peer2.socket);
96 if (NULL != peer2_listen_socket)
97 GNUNET_STREAM_listen_close (peer2_listen_socket); /* Close listen socket */
98 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: shutdown\n");
101 GNUNET_SCHEDULER_cancel (abort_task);
103 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: arm\n");
104 if (0 != GNUNET_OS_process_kill (arm_pid, SIGTERM))
106 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
108 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Wait\n");
109 /* Free the duplicated configuration */
110 GNUNET_CONFIGURATION_destroy (config);
111 GNUNET_assert (GNUNET_OK == GNUNET_OS_process_wait (arm_pid));
112 GNUNET_OS_process_destroy (arm_pid);
117 * Something went wrong and timed out. Kill everything and set error flag
120 do_abort (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
122 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: ABORT\n");
125 GNUNET_SCHEDULER_cancel (test_task);
129 GNUNET_SCHEDULER_cancel (read_task);
131 result = GNUNET_SYSERR;
133 do_shutdown (cls, tc);
138 * The write completion function; called upon writing some data to stream or
141 * @param cls the closure from GNUNET_STREAM_write/read
142 * @param status the status of the stream at the time this function is called
143 * @param size the number of bytes read or written
146 write_completion (void *cls,
147 enum GNUNET_STREAM_Status status,
150 struct PeerData *peer;
152 peer = (struct PeerData *) cls;
153 GNUNET_assert (GNUNET_STREAM_OK == status);
154 GNUNET_assert (size < DATA_SIZE);
155 peer->bytes_wrote += size;
157 if (peer->bytes_wrote < DATA_SIZE) /* Have more data to send */
159 peer->io_write_handle =
160 GNUNET_STREAM_write (peer->socket,
161 ((void *) data) + peer->bytes_wrote,
162 DATA_SIZE - peer->bytes_wrote,
163 GNUNET_TIME_relative_multiply
164 (GNUNET_TIME_UNIT_SECONDS, 5),
167 GNUNET_assert (NULL != peer->io_write_handle);
171 LOG (GNUNET_ERROR_TYPE_DEBUG, "Writing successfully finished\n");
173 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
179 * Task for calling STREAM_write with a chunk of random data
181 * @param cls the peer data entity
182 * @param tc the task context
185 stream_write_task (void *cls,
186 const struct GNUNET_SCHEDULER_TaskContext *tc)
188 struct PeerData *peer=cls;
191 write_task = GNUNET_SCHEDULER_NO_TASK;
192 for (count=0; count < DATA_SIZE / 4; count++)
194 data[count]=GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
197 LOG (GNUNET_ERROR_TYPE_DEBUG, "Generation of random data complete\n");
198 peer->io_write_handle = GNUNET_STREAM_write (peer->socket,
201 GNUNET_TIME_relative_multiply
202 (GNUNET_TIME_UNIT_SECONDS, 10),
205 GNUNET_assert (NULL != peer->io_write_handle);
210 * Function executed after stream has been established
212 * @param cls the closure from GNUNET_STREAM_open
213 * @param socket socket to use to communicate with the other side (read/write)
216 stream_open_cb (void *cls,
217 struct GNUNET_STREAM_Socket *socket)
219 struct PeerData *peer;
221 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stream established from peer1\n");
222 peer = (struct PeerData *) cls;
223 peer->bytes_wrote = 0;
224 GNUNET_assert (socket == peer1.socket);
225 GNUNET_assert (socket == peer->socket);
226 write_task = GNUNET_SCHEDULER_add_now (&stream_write_task, peer);
231 * Scheduler call back; to be executed when a new stream is connected
232 * Called from listen connect for peer2
235 stream_read_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
242 * @param status the status of the stream at the time this function is called
243 * @param data traffic from the other side
244 * @param size the number of bytes available in data read
245 * @return number of bytes of processed from 'data' (any data remaining should be
246 * given to the next time the read processor is called).
249 input_processor (void *cls,
250 enum GNUNET_STREAM_Status status,
251 const void *input_data,
254 struct PeerData *peer = cls;
256 GNUNET_assert (GNUNET_STREAM_OK == status);
257 GNUNET_assert (&peer2 == peer);
258 GNUNET_assert (size < DATA_SIZE);
259 GNUNET_assert (0 == memcmp (((void *)data ) + peer->bytes_read,
261 peer->bytes_read += size;
263 if (peer->bytes_read < DATA_SIZE)
265 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == read_task);
266 read_task = GNUNET_SCHEDULER_add_now (&stream_read_task, &peer2);
267 /* peer->io_read_handle = GNUNET_STREAM_read ((struct GNUNET_STREAM_Socket *) */
269 /* GNUNET_TIME_relative_multiply */
270 /* (GNUNET_TIME_UNIT_SECONDS, 5), */
271 /* &input_processor, */
273 /* GNUNET_assert (NULL != peer->io_read_handle); */
277 /* Peer2 has completed reading*/
278 LOG (GNUNET_ERROR_TYPE_DEBUG, "Reading finished successfully\n");
285 * Scheduler call back; to be executed when a new stream is connected
286 * Called from listen connect for peer2
289 stream_read_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
291 struct PeerData *peer = cls;
293 read_task = GNUNET_SCHEDULER_NO_TASK;
294 GNUNET_assert (&peer2 == peer);
295 peer->io_read_handle =
296 GNUNET_STREAM_read (peer->socket,
297 GNUNET_TIME_relative_multiply
298 (GNUNET_TIME_UNIT_SECONDS, 10),
301 GNUNET_assert (NULL != peer->io_read_handle);
306 * Functions of this type are called upon new stream connection from other peers
308 * @param cls the closure from GNUNET_STREAM_listen
309 * @param socket the socket representing the stream
310 * @param initiator the identity of the peer who wants to establish a stream
312 * @return GNUNET_OK to keep the socket open, GNUNET_SYSERR to close the
313 * stream (the socket will be invalid after the call)
316 stream_listen_cb (void *cls,
317 struct GNUNET_STREAM_Socket *socket,
318 const struct GNUNET_PeerIdentity *initiator)
320 GNUNET_assert (NULL != socket);
321 GNUNET_assert (socket != peer1.socket);
323 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
324 "Peer connected: %s\n", GNUNET_i2s(initiator));
326 peer2.socket = socket;
327 peer2.bytes_read = 0;
328 read_task = GNUNET_SCHEDULER_add_now (&stream_read_task, &peer2);
334 * Task for connecting the peer to stream as client
336 * @param cls PeerData
337 * @param tc the TaskContext
340 stream_connect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
342 struct PeerData *peer = cls;
343 struct GNUNET_PeerIdentity self;
345 GNUNET_assert (&peer1 == peer);
346 GNUNET_assert (GNUNET_OK == GNUNET_TESTING_get_peer_identity (config,
348 /* Connect to stream */
349 peer->socket = GNUNET_STREAM_open (config,
350 &self, /* Null for local peer? */
354 GNUNET_STREAM_OPTION_END);
355 GNUNET_assert (NULL != peer->socket);
363 * @param tc the task context
366 test (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
368 struct GNUNET_PeerIdentity self;
370 test_task = GNUNET_SCHEDULER_NO_TASK;
371 /* Get our identity */
372 GNUNET_assert (GNUNET_OK == GNUNET_TESTING_get_peer_identity (config,
375 peer2_listen_socket = GNUNET_STREAM_listen (config,
379 GNUNET_STREAM_OPTION_END);
380 GNUNET_assert (NULL != peer2_listen_socket);
381 GNUNET_SCHEDULER_add_delayed (TIME_REL_SECS(2), &stream_connect, &peer1);
386 * Initialize framework and start test
389 run (void *cls, char *const *args, const char *cfgfile,
390 const struct GNUNET_CONFIGURATION_Handle *cfg)
392 /* Duplicate the configuration */
393 config = GNUNET_CONFIGURATION_dup (cfg);
395 GNUNET_OS_start_process (GNUNET_YES, NULL, NULL, "gnunet-service-arm",
396 "gnunet-service-arm",
400 "-c", "test_stream_local.conf", NULL);
403 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
404 (GNUNET_TIME_UNIT_SECONDS, 60), &do_abort,
406 test_task = GNUNET_SCHEDULER_add_delayed (TIME_REL_SECS(3), &test, NULL);
412 int main (int argc, char **argv)
416 char *const argv2[] = { "test-stream-big",
417 "-c", "test_stream_local.conf",
422 struct GNUNET_GETOPT_CommandLineOption options[] = {
423 GNUNET_GETOPT_OPTION_END
427 GNUNET_PROGRAM_run ((sizeof (argv2) / sizeof (char *)) - 1, argv2,
428 "test-stream-big", "nohelp", options, &run, NULL);
430 if (GNUNET_OK != ret)
432 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "run failed with error code %d\n",
436 if (GNUNET_SYSERR == result)
438 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "test failed\n");
441 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test ok\n");