-fix double-sending in stream if finish_cb behaves in a certain way
[oweals/gnunet.git] / src / stream / test_stream_big.c
1 /*
2      This file is part of GNUnet.
3      (C) 2011, 2012 Christian Grothoff (and other contributing authors)
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., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, USA.
19 */
20
21 /**
22  * @file stream/test_stream_big.c
23  * @brief large data transfer using stream API between local peers
24  * @author Sree Harsha Totakura
25  */
26
27 #include <string.h>
28
29 #include "platform.h"
30 #include "gnunet_util_lib.h"
31 #include "gnunet_stream_lib.h"
32 #include "gnunet_testing_lib-new.h"
33
34 #define LOG(kind, ...)                         \
35   GNUNET_log (kind, __VA_ARGS__);
36
37 #define TIME_REL_SECS(sec) \
38   GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, sec)
39
40
41 /**
42  * Structure for holding peer's sockets and IO Handles
43  */
44 struct PeerData
45 {
46   /**
47    * Peer's stream socket
48    */
49   struct GNUNET_STREAM_Socket *socket;
50
51   struct GNUNET_PeerIdentity self;
52
53   /**
54    * Peer's io write handle
55    */
56   struct GNUNET_STREAM_IOWriteHandle *io_write_handle;
57
58   /**
59    * Peer's io read handle
60    */
61   struct GNUNET_STREAM_IOReadHandle *io_read_handle;
62
63   /**
64    * Peer's shutdown handle
65    */
66   struct GNUNET_STREAM_ShutdownHandle *shutdown_handle;
67
68   /**
69    * Bytes the peer has written
70    */
71   unsigned int bytes_wrote;
72
73   /**
74    * Byte the peer has read
75    */
76   unsigned int bytes_read;
77 };
78
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;
83
84 static GNUNET_SCHEDULER_TaskIdentifier abort_task;
85 static GNUNET_SCHEDULER_TaskIdentifier read_task;
86 static GNUNET_SCHEDULER_TaskIdentifier write_task;
87
88 #define DATA_SIZE 65536      /* 64KB */
89 static uint32_t data[DATA_SIZE / 4];     /* 64KB array */
90 static int result;
91
92 /**
93  * Shutdown nicely
94  */
95 static void
96 do_close (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
97 {
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 */
106 }
107
108
109 /**
110  * Something went wrong and timed out. Kill everything and set error flag
111  */
112 static void
113 do_abort (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
114 {
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;
120   do_close (cls, tc);
121 }
122
123
124 /**
125  * Completion callback for shutdown
126  *
127  * @param cls the closure from GNUNET_STREAM_shutdown call
128  * @param operation the operation that was shutdown (SHUT_RD, SHUT_WR,
129  *          SHUT_RDWR) 
130  */
131 static void 
132 shutdown_completion (void *cls,
133                      int operation)
134 {
135   static int shutdowns;
136
137   if (++shutdowns == 1)
138   {
139     peer1.shutdown_handle = NULL;
140     peer2.shutdown_handle = GNUNET_STREAM_shutdown (peer2.socket, SHUT_RDWR,
141                                                     &shutdown_completion, cls);
142     return;
143   }  
144   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "STREAM shutdown successful\n");
145   GNUNET_SCHEDULER_add_now (&do_close, cls);
146 }
147
148
149 /**
150  * Shutdown sockets gracefully
151  */
152 static void
153 do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
154 {
155   result = GNUNET_OK;
156   peer1.shutdown_handle = GNUNET_STREAM_shutdown (peer1.socket, SHUT_RDWR,
157                                                   &shutdown_completion, cls);
158 }
159
160
161 /**
162  * The write completion function; called upon writing some data to stream or
163  * upon error
164  *
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
168  */
169 static void 
170 write_completion (void *cls,
171                   enum GNUNET_STREAM_Status status,
172                   size_t size)
173 {
174   struct PeerData *peer;
175
176   peer = (struct PeerData *) cls;
177   GNUNET_assert (GNUNET_STREAM_OK == status);
178   GNUNET_assert (size <= DATA_SIZE);
179   peer->bytes_wrote += size;
180
181   if (peer->bytes_wrote < DATA_SIZE) /* Have more data to send */
182     {
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),
189                              &write_completion,
190                              cls);
191       GNUNET_assert (NULL != peer->io_write_handle);
192     }
193   else
194     {
195       LOG (GNUNET_ERROR_TYPE_DEBUG, "Writing successfully finished\n");
196       result = GNUNET_OK;
197       GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
198     }
199 }
200
201
202 /**
203  * Task for calling STREAM_write with a chunk of random data
204  *
205  * @param cls the peer data entity
206  * @param tc the task context
207  */
208 static void
209 stream_write_task (void *cls,
210                    const struct GNUNET_SCHEDULER_TaskContext *tc)
211 {
212   struct PeerData *peer=cls;
213   unsigned int count;
214
215   write_task = GNUNET_SCHEDULER_NO_TASK;
216   for (count=0; count < DATA_SIZE / 4; count++)
217     {
218       data[count]=GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
219                                             UINT32_MAX);
220     }
221   LOG (GNUNET_ERROR_TYPE_DEBUG, "Generation of random data complete\n");
222   peer->io_write_handle = GNUNET_STREAM_write (peer->socket,
223                                                data,
224                                                sizeof (data),
225                                                GNUNET_TIME_relative_multiply
226                                                (GNUNET_TIME_UNIT_SECONDS, 10),
227                                                &write_completion,
228                                                peer);
229   GNUNET_assert (NULL != peer->io_write_handle);
230 }
231
232
233 /**
234  * Function executed after stream has been established
235  *
236  * @param cls the closure from GNUNET_STREAM_open
237  * @param socket socket to use to communicate with the other side (read/write)
238  */
239 static void 
240 stream_open_cb (void *cls,
241                 struct GNUNET_STREAM_Socket *socket)
242 {
243   struct PeerData *peer;
244
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);
251 }
252
253
254 /**
255  * Scheduler call back; to be executed when a new stream is connected
256  * Called from listen connect for peer2
257  */
258 static void
259 stream_read_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
260
261
262 /**
263  * Input processor
264  *
265  * @param cls peer2
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).
271  */
272 static size_t
273 input_processor (void *cls,
274                  enum GNUNET_STREAM_Status status,
275                  const void *input_data,
276                  size_t size)
277 {
278   struct PeerData *peer = cls;
279
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, 
284                               input_data, size));
285   peer->bytes_read += size;
286   
287   if (peer->bytes_read < DATA_SIZE)
288   {
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 *) */
292     /*                                            peer->socket, */
293     /*                                            GNUNET_TIME_relative_multiply */
294     /*                                            (GNUNET_TIME_UNIT_SECONDS, 5), */
295     /*                                            &input_processor, */
296     /*                                            cls); */
297     /* GNUNET_assert (NULL != peer->io_read_handle); */
298   }
299   else 
300   {
301     /* Peer2 has completed reading*/
302     LOG (GNUNET_ERROR_TYPE_DEBUG, "Reading finished successfully\n");
303   } 
304   return size;
305 }
306
307   
308 /**
309  * Scheduler call back; to be executed when a new stream is connected
310  * Called from listen connect for peer2
311  */
312 static void
313 stream_read_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
314 {
315   struct PeerData *peer = cls;
316
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),
323                         &input_processor,
324                         peer);
325   GNUNET_assert (NULL != peer->io_read_handle);
326 }
327
328
329 /**
330  * Functions of this type are called upon new stream connection from other peers
331  *
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
335  *            with us
336  * @return GNUNET_OK to keep the socket open, GNUNET_SYSERR to close the
337  *             stream (the socket will be invalid after the call)
338  */
339 static int
340 stream_listen_cb (void *cls,
341                   struct GNUNET_STREAM_Socket *socket,
342                   const struct GNUNET_PeerIdentity *initiator)
343 {
344   if ((NULL == socket) || (NULL == initiator))
345   {
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);
350     return GNUNET_OK;
351   }
352   GNUNET_assert (NULL != socket);
353   GNUNET_assert (socket != peer1.socket);
354
355   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
356               "Peer connected: %s\n", GNUNET_i2s(initiator));
357
358   peer2.socket = socket;
359   peer2.bytes_read = 0;
360   read_task = GNUNET_SCHEDULER_add_now (&stream_read_task, &peer2);
361   return GNUNET_OK;
362 }
363
364
365 /**
366  * Listen success callback; connects a peer to stream as client
367  */
368 static void
369 stream_connect (void)
370 {
371   struct PeerData *peer = &peer1;
372
373   /* Connect to stream */
374   peer->socket = GNUNET_STREAM_open (config,
375                                      &peer2.self,         /* Null for local peer? */
376                                      10,           /* App port */
377                                      &stream_open_cb, &peer1,
378                                      GNUNET_STREAM_OPTION_MAX_PAYLOAD_SIZE, 500,
379                                      GNUNET_STREAM_OPTION_END);
380   GNUNET_assert (NULL != peer->socket);
381 }
382
383
384 /**
385  * Initialize framework and start test
386  *
387  * @param cls closure
388  * @param cfg configuration of the peer that was started
389  * @param peer identity of the peer that was created
390  */
391 static void
392 run (void *cls, 
393      const struct GNUNET_CONFIGURATION_Handle *cfg,
394      struct GNUNET_TESTING_Peer *peer)
395 {
396   struct GNUNET_PeerIdentity self;
397   
398   GNUNET_TESTING_peer_get_identity (peer, &self);
399   config = cfg;  
400   peer2_listen_socket = 
401     GNUNET_STREAM_listen (config,
402                           10, /* App port */
403                           &stream_listen_cb,
404                           NULL,
405                           GNUNET_STREAM_OPTION_SIGNAL_LISTEN_SUCCESS,
406                           &stream_connect,
407                           GNUNET_STREAM_OPTION_END);
408   GNUNET_assert (NULL != peer2_listen_socket);
409   peer1.self = self;
410   peer2.self = self;
411   abort_task =
412     GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
413                                   (GNUNET_TIME_UNIT_SECONDS, 60), &do_abort,
414                                   NULL);
415 }
416
417 /**
418  * Main function
419  */
420 int main (int argc, char **argv)
421 {
422   if (0 != GNUNET_TESTING_peer_run ("test_stream_big",
423                                     "test_stream_local.conf",
424                                     &run, NULL))
425     return 1;
426   return (GNUNET_SYSERR == result) ? 1 : 0;
427 }
428
429 /* end of test_stream_big.c */