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;
51 struct GNUNET_PeerIdentity self;
54 * Peer's io write handle
56 struct GNUNET_STREAM_WriteHandle *io_write_handle;
59 * Peer's io read handle
61 struct GNUNET_STREAM_ReadHandle *io_read_handle;
64 * Peer's shutdown handle
66 struct GNUNET_STREAM_ShutdownHandle *shutdown_handle;
69 * Bytes the peer has written
71 unsigned int bytes_wrote;
74 * Byte the peer has read
76 unsigned int bytes_read;
79 static struct PeerData peer1;
80 static struct PeerData peer2;
81 static struct GNUNET_STREAM_ListenSocket *peer2_listen_socket;
82 static const struct GNUNET_CONFIGURATION_Handle *config;
84 static GNUNET_SCHEDULER_TaskIdentifier abort_task;
85 static GNUNET_SCHEDULER_TaskIdentifier read_task;
86 static GNUNET_SCHEDULER_TaskIdentifier write_task;
88 #define DATA_SIZE 65536 /* 64KB */
89 static uint32_t data[DATA_SIZE / 4]; /* 64KB array */
96 do_close (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
98 if (GNUNET_SCHEDULER_NO_TASK != abort_task)
99 GNUNET_SCHEDULER_cancel (abort_task);
100 if (NULL != peer1.socket)
101 GNUNET_STREAM_close (peer1.socket);
102 if (NULL != peer2.socket)
103 GNUNET_STREAM_close (peer2.socket);
104 if (NULL != peer2_listen_socket)
105 GNUNET_STREAM_listen_close (peer2_listen_socket); /* Close listen socket */
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");
116 if (GNUNET_SCHEDULER_NO_TASK != read_task)
117 GNUNET_SCHEDULER_cancel (read_task);
118 result = GNUNET_SYSERR;
119 abort_task = GNUNET_SCHEDULER_NO_TASK;
125 * Completion callback for shutdown
127 * @param cls the closure from GNUNET_STREAM_shutdown call
128 * @param operation the operation that was shutdown (SHUT_RD, SHUT_WR,
132 shutdown_completion (void *cls,
135 static int shutdowns;
137 if (++shutdowns == 1)
139 peer1.shutdown_handle = NULL;
140 peer2.shutdown_handle = GNUNET_STREAM_shutdown (peer2.socket, SHUT_RDWR,
141 &shutdown_completion, cls);
144 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "STREAM shutdown successful\n");
145 GNUNET_SCHEDULER_add_now (&do_close, cls);
150 * Shutdown sockets gracefully
153 do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
156 peer1.shutdown_handle = GNUNET_STREAM_shutdown (peer1.socket, SHUT_RDWR,
157 &shutdown_completion, cls);
162 * The write completion function; called upon writing some data to stream or
165 * @param cls the closure from GNUNET_STREAM_write/read
166 * @param status the status of the stream at the time this function is called
167 * @param size the number of bytes read or written
170 write_completion (void *cls,
171 enum GNUNET_STREAM_Status status,
174 struct PeerData *peer;
176 peer = (struct PeerData *) cls;
177 GNUNET_assert (GNUNET_STREAM_OK == status);
178 GNUNET_assert (size <= DATA_SIZE);
179 peer->bytes_wrote += size;
181 if (peer->bytes_wrote < DATA_SIZE) /* Have more data to send */
183 peer->io_write_handle =
184 GNUNET_STREAM_write (peer->socket,
185 ((void *) data) + peer->bytes_wrote,
186 sizeof (data) - peer->bytes_wrote,
187 GNUNET_TIME_relative_multiply
188 (GNUNET_TIME_UNIT_SECONDS, 5),
191 GNUNET_assert (NULL != peer->io_write_handle);
195 LOG (GNUNET_ERROR_TYPE_DEBUG, "Writing successfully finished\n");
197 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
203 * Task for calling STREAM_write with a chunk of random data
205 * @param cls the peer data entity
206 * @param tc the task context
209 stream_write_task (void *cls,
210 const struct GNUNET_SCHEDULER_TaskContext *tc)
212 struct PeerData *peer=cls;
215 write_task = GNUNET_SCHEDULER_NO_TASK;
216 for (count=0; count < DATA_SIZE / 4; count++)
218 data[count]=GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
221 LOG (GNUNET_ERROR_TYPE_DEBUG, "Generation of random data complete\n");
222 peer->io_write_handle = GNUNET_STREAM_write (peer->socket,
225 GNUNET_TIME_relative_multiply
226 (GNUNET_TIME_UNIT_SECONDS, 10),
229 GNUNET_assert (NULL != peer->io_write_handle);
234 * Function executed after stream has been established
236 * @param cls the closure from GNUNET_STREAM_open
237 * @param socket socket to use to communicate with the other side (read/write)
240 stream_open_cb (void *cls,
241 struct GNUNET_STREAM_Socket *socket)
243 struct PeerData *peer;
245 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stream established from peer1\n");
246 peer = (struct PeerData *) cls;
247 peer->bytes_wrote = 0;
248 GNUNET_assert (socket == peer1.socket);
249 GNUNET_assert (socket == peer->socket);
250 write_task = GNUNET_SCHEDULER_add_now (&stream_write_task, peer);
255 * Scheduler call back; to be executed when a new stream is connected
256 * Called from listen connect for peer2
259 stream_read_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
266 * @param status the status of the stream at the time this function is called
267 * @param data traffic from the other side
268 * @param size the number of bytes available in data read
269 * @return number of bytes of processed from 'data' (any data remaining should be
270 * given to the next time the read processor is called).
273 input_processor (void *cls,
274 enum GNUNET_STREAM_Status status,
275 const void *input_data,
278 struct PeerData *peer = cls;
280 GNUNET_assert (GNUNET_STREAM_OK == status);
281 GNUNET_assert (&peer2 == peer);
282 GNUNET_assert (size < DATA_SIZE);
283 GNUNET_assert (0 == memcmp (((void *)data ) + peer->bytes_read,
285 peer->bytes_read += size;
287 if (peer->bytes_read < DATA_SIZE)
289 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == read_task);
290 read_task = GNUNET_SCHEDULER_add_now (&stream_read_task, &peer2);
291 /* peer->io_read_handle = GNUNET_STREAM_read ((struct GNUNET_STREAM_Socket *) */
293 /* GNUNET_TIME_relative_multiply */
294 /* (GNUNET_TIME_UNIT_SECONDS, 5), */
295 /* &input_processor, */
297 /* GNUNET_assert (NULL != peer->io_read_handle); */
301 /* Peer2 has completed reading*/
302 LOG (GNUNET_ERROR_TYPE_DEBUG, "Reading finished successfully\n");
309 * Scheduler call back; to be executed when a new stream is connected
310 * Called from listen connect for peer2
313 stream_read_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
315 struct PeerData *peer = cls;
317 read_task = GNUNET_SCHEDULER_NO_TASK;
318 GNUNET_assert (&peer2 == peer);
319 peer->io_read_handle =
320 GNUNET_STREAM_read (peer->socket,
321 GNUNET_TIME_relative_multiply
322 (GNUNET_TIME_UNIT_SECONDS, 10),
325 GNUNET_assert (NULL != peer->io_read_handle);
330 * Functions of this type are called upon new stream connection from other peers
332 * @param cls the closure from GNUNET_STREAM_listen
333 * @param socket the socket representing the stream
334 * @param initiator the identity of the peer who wants to establish a stream
336 * @return GNUNET_OK to keep the socket open, GNUNET_SYSERR to close the
337 * stream (the socket will be invalid after the call)
340 stream_listen_cb (void *cls,
341 struct GNUNET_STREAM_Socket *socket,
342 const struct GNUNET_PeerIdentity *initiator)
344 if ((NULL == socket) || (NULL == initiator))
346 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Binding error\n");
347 if (GNUNET_SCHEDULER_NO_TASK != abort_task)
348 GNUNET_SCHEDULER_cancel (abort_task);
349 abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL);
352 GNUNET_assert (NULL != socket);
353 GNUNET_assert (socket != peer1.socket);
355 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
356 "Peer connected: %s\n", GNUNET_i2s(initiator));
358 peer2.socket = socket;
359 peer2.bytes_read = 0;
360 read_task = GNUNET_SCHEDULER_add_now (&stream_read_task, &peer2);
366 * Listen success callback; connects a peer to stream as client
369 stream_connect (void)
371 struct PeerData *peer = &peer1;
373 /* Connect to stream */
374 peer->socket = GNUNET_STREAM_open (config,
375 &peer2.self, /* Null for local peer? */
377 &stream_open_cb, &peer1,
378 GNUNET_STREAM_OPTION_MAX_PAYLOAD_SIZE, 500,
379 GNUNET_STREAM_OPTION_END);
380 GNUNET_assert (NULL != peer->socket);
385 * Initialize framework and start test
388 * @param cfg configuration of the peer that was started
389 * @param peer identity of the peer that was created
393 const struct GNUNET_CONFIGURATION_Handle *cfg,
394 struct GNUNET_TESTING_Peer *peer)
396 struct GNUNET_PeerIdentity self;
398 GNUNET_TESTING_peer_get_identity (peer, &self);
400 peer2_listen_socket =
401 GNUNET_STREAM_listen (config,
405 GNUNET_STREAM_OPTION_SIGNAL_LISTEN_SUCCESS,
407 GNUNET_STREAM_OPTION_END);
408 GNUNET_assert (NULL != peer2_listen_socket);
412 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
413 (GNUNET_TIME_UNIT_SECONDS, 60), &do_abort,
420 int main (int argc, char **argv)
422 if (0 != GNUNET_TESTING_peer_run ("test_stream_big",
423 "test_stream_local.conf",
426 return (GNUNET_SYSERR == result) ? 1 : 0;
429 /* end of test_stream_big.c */