commenting out dead test
[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    * Bytes the peer has written
65    */
66   unsigned int bytes_wrote;
67
68   /**
69    * Byte the peer has read
70    */
71   unsigned int bytes_read;
72 };
73
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;
78
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;
83
84 #define DATA_SIZE 65536      /* 64KB */
85 static uint32_t data[DATA_SIZE / 4];     /* 64KB array */
86 static int result;
87
88 /**
89  * Shutdown nicely
90  */
91 static void
92 do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
93 {
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");
100   if (0 != abort_task)
101   {
102     GNUNET_SCHEDULER_cancel (abort_task);
103   }
104   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Wait\n");
105   GNUNET_SCHEDULER_shutdown ();
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 (0 != test_task)
117   {
118     GNUNET_SCHEDULER_cancel (test_task);
119   }
120   if (0 != read_task)
121     {
122       GNUNET_SCHEDULER_cancel (read_task);
123     }
124   result = GNUNET_SYSERR;
125   abort_task = 0;
126   do_shutdown (cls, tc);
127 }
128
129
130 /**
131  * The write completion function; called upon writing some data to stream or
132  * upon error
133  *
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
137  */
138 static void 
139 write_completion (void *cls,
140                   enum GNUNET_STREAM_Status status,
141                   size_t size)
142 {
143   struct PeerData *peer;
144
145   peer = (struct PeerData *) cls;
146   GNUNET_assert (GNUNET_STREAM_OK == status);
147   GNUNET_assert (size < DATA_SIZE);
148   peer->bytes_wrote += size;
149
150   if (peer->bytes_wrote < DATA_SIZE) /* Have more data to send */
151     {
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),
158                              &write_completion,
159                              cls);
160       GNUNET_assert (NULL != peer->io_write_handle);
161     }
162   else
163     {
164       LOG (GNUNET_ERROR_TYPE_DEBUG, "Writing successfully finished\n");
165       result = GNUNET_OK;
166       GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
167     }
168 }
169
170
171 /**
172  * Task for calling STREAM_write with a chunk of random data
173  *
174  * @param cls the peer data entity
175  * @param tc the task context
176  */
177 static void
178 stream_write_task (void *cls,
179                    const struct GNUNET_SCHEDULER_TaskContext *tc)
180 {
181   struct PeerData *peer=cls;
182   unsigned int count;
183
184   write_task = GNUNET_SCHEDULER_NO_TASK;
185   for (count=0; count < DATA_SIZE / 4; count++)
186     {
187       data[count]=GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
188                                             UINT32_MAX);
189     }
190   LOG (GNUNET_ERROR_TYPE_DEBUG, "Generation of random data complete\n");
191   peer->io_write_handle = GNUNET_STREAM_write (peer->socket,
192                                                data,
193                                                DATA_SIZE,
194                                                GNUNET_TIME_relative_multiply
195                                                (GNUNET_TIME_UNIT_SECONDS, 10),
196                                                &write_completion,
197                                                peer);
198   GNUNET_assert (NULL != peer->io_write_handle);
199 }
200
201
202 /**
203  * Function executed after stream has been established
204  *
205  * @param cls the closure from GNUNET_STREAM_open
206  * @param socket socket to use to communicate with the other side (read/write)
207  */
208 static void 
209 stream_open_cb (void *cls,
210                 struct GNUNET_STREAM_Socket *socket)
211 {
212   struct PeerData *peer;
213
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);
220 }
221
222
223 /**
224  * Scheduler call back; to be executed when a new stream is connected
225  * Called from listen connect for peer2
226  */
227 static void
228 stream_read_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
229
230
231 /**
232  * Input processor
233  *
234  * @param cls peer2
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).
240  */
241 static size_t
242 input_processor (void *cls,
243                  enum GNUNET_STREAM_Status status,
244                  const void *input_data,
245                  size_t size)
246 {
247   struct PeerData *peer = cls;
248
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, 
253                               input_data, size));
254   peer->bytes_read += size;
255   
256   if (peer->bytes_read < DATA_SIZE)
257   {
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 *) */
261     /*                                            peer->socket, */
262     /*                                            GNUNET_TIME_relative_multiply */
263     /*                                            (GNUNET_TIME_UNIT_SECONDS, 5), */
264     /*                                            &input_processor, */
265     /*                                            cls); */
266     /* GNUNET_assert (NULL != peer->io_read_handle); */
267   }
268   else 
269   {
270     /* Peer2 has completed reading*/
271     LOG (GNUNET_ERROR_TYPE_DEBUG, "Reading finished successfully\n");
272   } 
273   return size;
274 }
275
276   
277 /**
278  * Scheduler call back; to be executed when a new stream is connected
279  * Called from listen connect for peer2
280  */
281 static void
282 stream_read_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
283 {
284   struct PeerData *peer = cls;
285
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),
292                         &input_processor,
293                         peer);
294   GNUNET_assert (NULL != peer->io_read_handle);
295 }
296
297
298 /**
299  * Functions of this type are called upon new stream connection from other peers
300  *
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
304  *            with us
305  * @return GNUNET_OK to keep the socket open, GNUNET_SYSERR to close the
306  *             stream (the socket will be invalid after the call)
307  */
308 static int
309 stream_listen_cb (void *cls,
310                   struct GNUNET_STREAM_Socket *socket,
311                   const struct GNUNET_PeerIdentity *initiator)
312 {
313   GNUNET_assert (NULL != socket);
314   GNUNET_assert (socket != peer1.socket);
315
316   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
317               "Peer connected: %s\n", GNUNET_i2s(initiator));
318
319   peer2.socket = socket;
320   peer2.bytes_read = 0;
321   read_task = GNUNET_SCHEDULER_add_now (&stream_read_task, &peer2);
322   return GNUNET_OK;
323 }
324
325
326 /**
327  * Listen success callback; connects a peer to stream as client
328  */
329 static void
330 stream_connect (void)
331 {
332   struct PeerData *peer = &peer1;
333
334   /* Connect to stream */
335   peer->socket = GNUNET_STREAM_open (config,
336                                      &peer2.self,         /* Null for local peer? */
337                                      10,           /* App port */
338                                      &stream_open_cb,
339                                      &peer1,
340                                      GNUNET_STREAM_OPTION_END);
341   GNUNET_assert (NULL != peer->socket);
342 }
343
344
345 /**
346  * Initialize framework and start test
347  *
348  * @param cls closure
349  * @param cfg configuration of the peer that was started
350  * @param peer identity of the peer that was created
351  */
352 static void
353 run (void *cls, 
354      const struct GNUNET_CONFIGURATION_Handle *cfg,
355      struct GNUNET_TESTING_Peer *peer)
356 {
357   struct GNUNET_PeerIdentity self;
358   
359   GNUNET_TESTING_peer_get_identity (peer, &self);
360   config = cfg;  
361   peer2_listen_socket = 
362     GNUNET_STREAM_listen (config,
363                           10, /* App port */
364                           &stream_listen_cb,
365                           NULL,
366                           GNUNET_STREAM_OPTION_SIGNAL_LISTEN_SUCCESS,
367                           &stream_connect,
368                           GNUNET_STREAM_OPTION_END);
369   GNUNET_assert (NULL != peer2_listen_socket);
370   peer1.self = self;
371   peer2.self = self;
372   abort_task =
373     GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
374                                   (GNUNET_TIME_UNIT_SECONDS, 60), &do_abort,
375                                   NULL);
376 }
377
378 /**
379  * Main function
380  */
381 int main (int argc, char **argv)
382 {
383   if (0 != GNUNET_TESTING_peer_run ("test_stream_big",
384                                     "test_stream_local.conf",
385                                     &run, NULL))
386     return 1;
387   return (GNUNET_SYSERR == result) ? 1 : 0;
388 }
389
390 /* end of test_stream_big.c */