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-new.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;
51 struct GNUNET_PeerIdentity self;
54 * Peer's io write handle
56 struct GNUNET_STREAM_IOWriteHandle *io_write_handle;
59 * Peer's io read handle
61 struct GNUNET_STREAM_IOReadHandle *io_read_handle;
64 * Bytes the peer has written
66 unsigned int bytes_wrote;
69 * Byte the peer has read
71 unsigned int bytes_read;
74 static struct PeerData peer1;
75 static struct PeerData peer2;
76 static struct GNUNET_STREAM_ListenSocket *peer2_listen_socket;
77 static const struct GNUNET_CONFIGURATION_Handle *config;
79 static GNUNET_SCHEDULER_TaskIdentifier abort_task;
80 static GNUNET_SCHEDULER_TaskIdentifier test_task;
81 static GNUNET_SCHEDULER_TaskIdentifier read_task;
82 static GNUNET_SCHEDULER_TaskIdentifier write_task;
84 #define DATA_SIZE 65536 /* 64KB */
85 static uint32_t data[DATA_SIZE / 4]; /* 64KB array */
92 do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
94 GNUNET_STREAM_close (peer1.socket);
95 if (NULL != peer2.socket)
96 GNUNET_STREAM_close (peer2.socket);
97 if (NULL != peer2_listen_socket)
98 GNUNET_STREAM_listen_close (peer2_listen_socket); /* Close listen socket */
99 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: shutdown\n");
102 GNUNET_SCHEDULER_cancel (abort_task);
104 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Wait\n");
105 GNUNET_SCHEDULER_shutdown ();
110 * Something went wrong and timed out. Kill everything and set error flag
113 do_abort (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
115 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: ABORT\n");
118 GNUNET_SCHEDULER_cancel (test_task);
122 GNUNET_SCHEDULER_cancel (read_task);
124 result = GNUNET_SYSERR;
126 do_shutdown (cls, tc);
131 * The write completion function; called upon writing some data to stream or
134 * @param cls the closure from GNUNET_STREAM_write/read
135 * @param status the status of the stream at the time this function is called
136 * @param size the number of bytes read or written
139 write_completion (void *cls,
140 enum GNUNET_STREAM_Status status,
143 struct PeerData *peer;
145 peer = (struct PeerData *) cls;
146 GNUNET_assert (GNUNET_STREAM_OK == status);
147 GNUNET_assert (size < DATA_SIZE);
148 peer->bytes_wrote += size;
150 if (peer->bytes_wrote < DATA_SIZE) /* Have more data to send */
152 peer->io_write_handle =
153 GNUNET_STREAM_write (peer->socket,
154 ((void *) data) + peer->bytes_wrote,
155 DATA_SIZE - peer->bytes_wrote,
156 GNUNET_TIME_relative_multiply
157 (GNUNET_TIME_UNIT_SECONDS, 5),
160 GNUNET_assert (NULL != peer->io_write_handle);
164 LOG (GNUNET_ERROR_TYPE_DEBUG, "Writing successfully finished\n");
166 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
172 * Task for calling STREAM_write with a chunk of random data
174 * @param cls the peer data entity
175 * @param tc the task context
178 stream_write_task (void *cls,
179 const struct GNUNET_SCHEDULER_TaskContext *tc)
181 struct PeerData *peer=cls;
184 write_task = GNUNET_SCHEDULER_NO_TASK;
185 for (count=0; count < DATA_SIZE / 4; count++)
187 data[count]=GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
190 LOG (GNUNET_ERROR_TYPE_DEBUG, "Generation of random data complete\n");
191 peer->io_write_handle = GNUNET_STREAM_write (peer->socket,
194 GNUNET_TIME_relative_multiply
195 (GNUNET_TIME_UNIT_SECONDS, 10),
198 GNUNET_assert (NULL != peer->io_write_handle);
203 * Function executed after stream has been established
205 * @param cls the closure from GNUNET_STREAM_open
206 * @param socket socket to use to communicate with the other side (read/write)
209 stream_open_cb (void *cls,
210 struct GNUNET_STREAM_Socket *socket)
212 struct PeerData *peer;
214 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stream established from peer1\n");
215 peer = (struct PeerData *) cls;
216 peer->bytes_wrote = 0;
217 GNUNET_assert (socket == peer1.socket);
218 GNUNET_assert (socket == peer->socket);
219 write_task = GNUNET_SCHEDULER_add_now (&stream_write_task, peer);
224 * Scheduler call back; to be executed when a new stream is connected
225 * Called from listen connect for peer2
228 stream_read_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
235 * @param status the status of the stream at the time this function is called
236 * @param data traffic from the other side
237 * @param size the number of bytes available in data read
238 * @return number of bytes of processed from 'data' (any data remaining should be
239 * given to the next time the read processor is called).
242 input_processor (void *cls,
243 enum GNUNET_STREAM_Status status,
244 const void *input_data,
247 struct PeerData *peer = cls;
249 GNUNET_assert (GNUNET_STREAM_OK == status);
250 GNUNET_assert (&peer2 == peer);
251 GNUNET_assert (size < DATA_SIZE);
252 GNUNET_assert (0 == memcmp (((void *)data ) + peer->bytes_read,
254 peer->bytes_read += size;
256 if (peer->bytes_read < DATA_SIZE)
258 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == read_task);
259 read_task = GNUNET_SCHEDULER_add_now (&stream_read_task, &peer2);
260 /* peer->io_read_handle = GNUNET_STREAM_read ((struct GNUNET_STREAM_Socket *) */
262 /* GNUNET_TIME_relative_multiply */
263 /* (GNUNET_TIME_UNIT_SECONDS, 5), */
264 /* &input_processor, */
266 /* GNUNET_assert (NULL != peer->io_read_handle); */
270 /* Peer2 has completed reading*/
271 LOG (GNUNET_ERROR_TYPE_DEBUG, "Reading finished successfully\n");
278 * Scheduler call back; to be executed when a new stream is connected
279 * Called from listen connect for peer2
282 stream_read_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
284 struct PeerData *peer = cls;
286 read_task = GNUNET_SCHEDULER_NO_TASK;
287 GNUNET_assert (&peer2 == peer);
288 peer->io_read_handle =
289 GNUNET_STREAM_read (peer->socket,
290 GNUNET_TIME_relative_multiply
291 (GNUNET_TIME_UNIT_SECONDS, 10),
294 GNUNET_assert (NULL != peer->io_read_handle);
299 * Functions of this type are called upon new stream connection from other peers
301 * @param cls the closure from GNUNET_STREAM_listen
302 * @param socket the socket representing the stream
303 * @param initiator the identity of the peer who wants to establish a stream
305 * @return GNUNET_OK to keep the socket open, GNUNET_SYSERR to close the
306 * stream (the socket will be invalid after the call)
309 stream_listen_cb (void *cls,
310 struct GNUNET_STREAM_Socket *socket,
311 const struct GNUNET_PeerIdentity *initiator)
313 GNUNET_assert (NULL != socket);
314 GNUNET_assert (socket != peer1.socket);
316 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
317 "Peer connected: %s\n", GNUNET_i2s(initiator));
319 peer2.socket = socket;
320 peer2.bytes_read = 0;
321 read_task = GNUNET_SCHEDULER_add_now (&stream_read_task, &peer2);
327 * Task for connecting the peer to stream as client
329 * @param cls PeerData
330 * @param tc the TaskContext
333 stream_connect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
335 struct PeerData *peer = cls;
337 /* Connect to stream */
338 peer->socket = GNUNET_STREAM_open (config,
339 &peer2.self, /* Null for local peer? */
343 GNUNET_STREAM_OPTION_END);
344 GNUNET_assert (NULL != peer->socket);
349 * Initialize framework and start test
352 * @param cfg configuration of the peer that was started
353 * @param peer identity of the peer that was created
357 const struct GNUNET_CONFIGURATION_Handle *cfg,
358 struct GNUNET_TESTING_Peer *peer)
360 struct GNUNET_PeerIdentity self;
362 GNUNET_TESTING_peer_get_identity (peer, &self);
365 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
366 (GNUNET_TIME_UNIT_SECONDS, 60), &do_abort,
368 peer2_listen_socket = GNUNET_STREAM_listen (config,
372 GNUNET_STREAM_OPTION_END);
373 GNUNET_assert (NULL != peer2_listen_socket);
376 GNUNET_SCHEDULER_add_delayed (TIME_REL_SECS(2), &stream_connect, &peer1);
382 int main (int argc, char **argv)
384 if (0 != GNUNET_TESTING_peer_run ("test_stream_big",
385 "test_stream_local.conf",
388 return (GNUNET_SYSERR == result) ? 1 : 0;
391 /* end of test_stream_big.c */