regex profiler cleanup
[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 read_task;
81 static GNUNET_SCHEDULER_TaskIdentifier write_task;
82
83 #define DATA_SIZE 65536      /* 64KB */
84 static uint32_t data[DATA_SIZE / 4];     /* 64KB array */
85 static int result;
86
87 /**
88  * Shutdown nicely
89  */
90 static void
91 do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
92 {
93   if (NULL != peer1.socket)
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 (GNUNET_SCHEDULER_NO_TASK != abort_task)
101     GNUNET_SCHEDULER_cancel (abort_task);
102   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Wait\n");
103   GNUNET_SCHEDULER_shutdown ();
104 }
105
106
107 /**
108  * Something went wrong and timed out. Kill everything and set error flag
109  */
110 static void
111 do_abort (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
112 {
113   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: ABORT\n");
114   if (GNUNET_SCHEDULER_NO_TASK != read_task)
115       GNUNET_SCHEDULER_cancel (read_task);
116   result = GNUNET_SYSERR;
117   abort_task = GNUNET_SCHEDULER_NO_TASK;
118   do_shutdown (cls, tc);
119 }
120
121
122 /**
123  * The write completion function; called upon writing some data to stream or
124  * upon error
125  *
126  * @param cls the closure from GNUNET_STREAM_write/read
127  * @param status the status of the stream at the time this function is called
128  * @param size the number of bytes read or written
129  */
130 static void 
131 write_completion (void *cls,
132                   enum GNUNET_STREAM_Status status,
133                   size_t size)
134 {
135   struct PeerData *peer;
136
137   peer = (struct PeerData *) cls;
138   GNUNET_assert (GNUNET_STREAM_OK == status);
139   GNUNET_assert (size <= DATA_SIZE);
140   peer->bytes_wrote += size;
141
142   if (peer->bytes_wrote < DATA_SIZE) /* Have more data to send */
143     {
144       peer->io_write_handle =
145         GNUNET_STREAM_write (peer->socket,
146                              ((void *) data) + peer->bytes_wrote,
147                              sizeof (data) - peer->bytes_wrote,
148                              GNUNET_TIME_relative_multiply
149                              (GNUNET_TIME_UNIT_SECONDS, 5),
150                              &write_completion,
151                              cls);
152       GNUNET_assert (NULL != peer->io_write_handle);
153     }
154   else
155     {
156       LOG (GNUNET_ERROR_TYPE_DEBUG, "Writing successfully finished\n");
157       result = GNUNET_OK;
158       GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
159     }
160 }
161
162
163 /**
164  * Task for calling STREAM_write with a chunk of random data
165  *
166  * @param cls the peer data entity
167  * @param tc the task context
168  */
169 static void
170 stream_write_task (void *cls,
171                    const struct GNUNET_SCHEDULER_TaskContext *tc)
172 {
173   struct PeerData *peer=cls;
174   unsigned int count;
175
176   write_task = GNUNET_SCHEDULER_NO_TASK;
177   for (count=0; count < DATA_SIZE / 4; count++)
178     {
179       data[count]=GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
180                                             UINT32_MAX);
181     }
182   LOG (GNUNET_ERROR_TYPE_DEBUG, "Generation of random data complete\n");
183   peer->io_write_handle = GNUNET_STREAM_write (peer->socket,
184                                                data,
185                                                sizeof (data),
186                                                GNUNET_TIME_relative_multiply
187                                                (GNUNET_TIME_UNIT_SECONDS, 10),
188                                                &write_completion,
189                                                peer);
190   GNUNET_assert (NULL != peer->io_write_handle);
191 }
192
193
194 /**
195  * Function executed after stream has been established
196  *
197  * @param cls the closure from GNUNET_STREAM_open
198  * @param socket socket to use to communicate with the other side (read/write)
199  */
200 static void 
201 stream_open_cb (void *cls,
202                 struct GNUNET_STREAM_Socket *socket)
203 {
204   struct PeerData *peer;
205
206   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stream established from peer1\n");
207   peer = (struct PeerData *) cls;
208   peer->bytes_wrote = 0;
209   GNUNET_assert (socket == peer1.socket);
210   GNUNET_assert (socket == peer->socket);
211   write_task = GNUNET_SCHEDULER_add_now (&stream_write_task, peer);
212 }
213
214
215 /**
216  * Scheduler call back; to be executed when a new stream is connected
217  * Called from listen connect for peer2
218  */
219 static void
220 stream_read_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
221
222
223 /**
224  * Input processor
225  *
226  * @param cls peer2
227  * @param status the status of the stream at the time this function is called
228  * @param data traffic from the other side
229  * @param size the number of bytes available in data read 
230  * @return number of bytes of processed from 'data' (any data remaining should be
231  *         given to the next time the read processor is called).
232  */
233 static size_t
234 input_processor (void *cls,
235                  enum GNUNET_STREAM_Status status,
236                  const void *input_data,
237                  size_t size)
238 {
239   struct PeerData *peer = cls;
240
241   GNUNET_assert (GNUNET_STREAM_OK == status);
242   GNUNET_assert (&peer2 == peer);
243   GNUNET_assert (size < DATA_SIZE);
244   GNUNET_assert (0 == memcmp (((void *)data ) + peer->bytes_read, 
245                               input_data, size));
246   peer->bytes_read += size;
247   
248   if (peer->bytes_read < DATA_SIZE)
249   {
250     GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == read_task);
251     read_task = GNUNET_SCHEDULER_add_now (&stream_read_task, &peer2);
252     /* peer->io_read_handle = GNUNET_STREAM_read ((struct GNUNET_STREAM_Socket *) */
253     /*                                            peer->socket, */
254     /*                                            GNUNET_TIME_relative_multiply */
255     /*                                            (GNUNET_TIME_UNIT_SECONDS, 5), */
256     /*                                            &input_processor, */
257     /*                                            cls); */
258     /* GNUNET_assert (NULL != peer->io_read_handle); */
259   }
260   else 
261   {
262     /* Peer2 has completed reading*/
263     LOG (GNUNET_ERROR_TYPE_DEBUG, "Reading finished successfully\n");
264   } 
265   return size;
266 }
267
268   
269 /**
270  * Scheduler call back; to be executed when a new stream is connected
271  * Called from listen connect for peer2
272  */
273 static void
274 stream_read_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
275 {
276   struct PeerData *peer = cls;
277
278   read_task = GNUNET_SCHEDULER_NO_TASK;
279   GNUNET_assert (&peer2 == peer);  
280   peer->io_read_handle =
281     GNUNET_STREAM_read (peer->socket,
282                         GNUNET_TIME_relative_multiply
283                         (GNUNET_TIME_UNIT_SECONDS, 10),
284                         &input_processor,
285                         peer);
286   GNUNET_assert (NULL != peer->io_read_handle);
287 }
288
289
290 /**
291  * Functions of this type are called upon new stream connection from other peers
292  *
293  * @param cls the closure from GNUNET_STREAM_listen
294  * @param socket the socket representing the stream
295  * @param initiator the identity of the peer who wants to establish a stream
296  *            with us
297  * @return GNUNET_OK to keep the socket open, GNUNET_SYSERR to close the
298  *             stream (the socket will be invalid after the call)
299  */
300 static int
301 stream_listen_cb (void *cls,
302                   struct GNUNET_STREAM_Socket *socket,
303                   const struct GNUNET_PeerIdentity *initiator)
304 {
305   if ((NULL == socket) || (NULL == initiator))
306   {
307     GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Binding error\n");
308     if (GNUNET_SCHEDULER_NO_TASK != abort_task)
309       GNUNET_SCHEDULER_cancel (abort_task);
310     abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL);
311     return GNUNET_OK;
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, &peer1,
339                                      GNUNET_STREAM_OPTION_MAX_PAYLOAD_SIZE, 500,
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 */