Missing declarations
[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   GNUNET_STREAM_close (peer1.socket);
94   if (NULL != peer2.socket)
95     GNUNET_STREAM_close (peer2.socket);
96   if (NULL != peer2_listen_socket)
97     GNUNET_STREAM_listen_close (peer2_listen_socket); /* Close listen socket */
98   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: shutdown\n");
99   if (GNUNET_SCHEDULER_NO_TASK != abort_task)
100     GNUNET_SCHEDULER_cancel (abort_task);
101   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Wait\n");
102   GNUNET_SCHEDULER_shutdown ();
103 }
104
105
106 /**
107  * Something went wrong and timed out. Kill everything and set error flag
108  */
109 static void
110 do_abort (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
111 {
112   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: ABORT\n");
113   if (GNUNET_SCHEDULER_NO_TASK != read_task)
114       GNUNET_SCHEDULER_cancel (read_task);
115   result = GNUNET_SYSERR;
116   abort_task = GNUNET_SCHEDULER_NO_TASK;
117   do_shutdown (cls, tc);
118 }
119
120
121 /**
122  * The write completion function; called upon writing some data to stream or
123  * upon error
124  *
125  * @param cls the closure from GNUNET_STREAM_write/read
126  * @param status the status of the stream at the time this function is called
127  * @param size the number of bytes read or written
128  */
129 static void 
130 write_completion (void *cls,
131                   enum GNUNET_STREAM_Status status,
132                   size_t size)
133 {
134   struct PeerData *peer;
135
136   peer = (struct PeerData *) cls;
137   GNUNET_assert (GNUNET_STREAM_OK == status);
138   GNUNET_assert (size <= DATA_SIZE);
139   peer->bytes_wrote += size;
140
141   if (peer->bytes_wrote < DATA_SIZE) /* Have more data to send */
142     {
143       peer->io_write_handle =
144         GNUNET_STREAM_write (peer->socket,
145                              ((void *) data) + peer->bytes_wrote,
146                              sizeof (data) - peer->bytes_wrote,
147                              GNUNET_TIME_relative_multiply
148                              (GNUNET_TIME_UNIT_SECONDS, 5),
149                              &write_completion,
150                              cls);
151       GNUNET_assert (NULL != peer->io_write_handle);
152     }
153   else
154     {
155       LOG (GNUNET_ERROR_TYPE_DEBUG, "Writing successfully finished\n");
156       result = GNUNET_OK;
157       GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
158     }
159 }
160
161
162 /**
163  * Task for calling STREAM_write with a chunk of random data
164  *
165  * @param cls the peer data entity
166  * @param tc the task context
167  */
168 static void
169 stream_write_task (void *cls,
170                    const struct GNUNET_SCHEDULER_TaskContext *tc)
171 {
172   struct PeerData *peer=cls;
173   unsigned int count;
174
175   write_task = GNUNET_SCHEDULER_NO_TASK;
176   for (count=0; count < DATA_SIZE / 4; count++)
177     {
178       data[count]=GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
179                                             UINT32_MAX);
180     }
181   LOG (GNUNET_ERROR_TYPE_DEBUG, "Generation of random data complete\n");
182   peer->io_write_handle = GNUNET_STREAM_write (peer->socket,
183                                                data,
184                                                sizeof (data),
185                                                GNUNET_TIME_relative_multiply
186                                                (GNUNET_TIME_UNIT_SECONDS, 10),
187                                                &write_completion,
188                                                peer);
189   GNUNET_assert (NULL != peer->io_write_handle);
190 }
191
192
193 /**
194  * Function executed after stream has been established
195  *
196  * @param cls the closure from GNUNET_STREAM_open
197  * @param socket socket to use to communicate with the other side (read/write)
198  */
199 static void 
200 stream_open_cb (void *cls,
201                 struct GNUNET_STREAM_Socket *socket)
202 {
203   struct PeerData *peer;
204
205   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stream established from peer1\n");
206   peer = (struct PeerData *) cls;
207   peer->bytes_wrote = 0;
208   GNUNET_assert (socket == peer1.socket);
209   GNUNET_assert (socket == peer->socket);
210   write_task = GNUNET_SCHEDULER_add_now (&stream_write_task, peer);
211 }
212
213
214 /**
215  * Scheduler call back; to be executed when a new stream is connected
216  * Called from listen connect for peer2
217  */
218 static void
219 stream_read_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
220
221
222 /**
223  * Input processor
224  *
225  * @param cls peer2
226  * @param status the status of the stream at the time this function is called
227  * @param data traffic from the other side
228  * @param size the number of bytes available in data read 
229  * @return number of bytes of processed from 'data' (any data remaining should be
230  *         given to the next time the read processor is called).
231  */
232 static size_t
233 input_processor (void *cls,
234                  enum GNUNET_STREAM_Status status,
235                  const void *input_data,
236                  size_t size)
237 {
238   struct PeerData *peer = cls;
239
240   GNUNET_assert (GNUNET_STREAM_OK == status);
241   GNUNET_assert (&peer2 == peer);
242   GNUNET_assert (size < DATA_SIZE);
243   GNUNET_assert (0 == memcmp (((void *)data ) + peer->bytes_read, 
244                               input_data, size));
245   peer->bytes_read += size;
246   
247   if (peer->bytes_read < DATA_SIZE)
248   {
249     GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == read_task);
250     read_task = GNUNET_SCHEDULER_add_now (&stream_read_task, &peer2);
251     /* peer->io_read_handle = GNUNET_STREAM_read ((struct GNUNET_STREAM_Socket *) */
252     /*                                            peer->socket, */
253     /*                                            GNUNET_TIME_relative_multiply */
254     /*                                            (GNUNET_TIME_UNIT_SECONDS, 5), */
255     /*                                            &input_processor, */
256     /*                                            cls); */
257     /* GNUNET_assert (NULL != peer->io_read_handle); */
258   }
259   else 
260   {
261     /* Peer2 has completed reading*/
262     LOG (GNUNET_ERROR_TYPE_DEBUG, "Reading finished successfully\n");
263   } 
264   return size;
265 }
266
267   
268 /**
269  * Scheduler call back; to be executed when a new stream is connected
270  * Called from listen connect for peer2
271  */
272 static void
273 stream_read_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
274 {
275   struct PeerData *peer = cls;
276
277   read_task = GNUNET_SCHEDULER_NO_TASK;
278   GNUNET_assert (&peer2 == peer);  
279   peer->io_read_handle =
280     GNUNET_STREAM_read (peer->socket,
281                         GNUNET_TIME_relative_multiply
282                         (GNUNET_TIME_UNIT_SECONDS, 10),
283                         &input_processor,
284                         peer);
285   GNUNET_assert (NULL != peer->io_read_handle);
286 }
287
288
289 /**
290  * Functions of this type are called upon new stream connection from other peers
291  *
292  * @param cls the closure from GNUNET_STREAM_listen
293  * @param socket the socket representing the stream
294  * @param initiator the identity of the peer who wants to establish a stream
295  *            with us
296  * @return GNUNET_OK to keep the socket open, GNUNET_SYSERR to close the
297  *             stream (the socket will be invalid after the call)
298  */
299 static int
300 stream_listen_cb (void *cls,
301                   struct GNUNET_STREAM_Socket *socket,
302                   const struct GNUNET_PeerIdentity *initiator)
303 {
304   GNUNET_assert (NULL != socket);
305   GNUNET_assert (socket != peer1.socket);
306
307   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
308               "Peer connected: %s\n", GNUNET_i2s(initiator));
309
310   peer2.socket = socket;
311   peer2.bytes_read = 0;
312   read_task = GNUNET_SCHEDULER_add_now (&stream_read_task, &peer2);
313   return GNUNET_OK;
314 }
315
316
317 /**
318  * Listen success callback; connects a peer to stream as client
319  */
320 static void
321 stream_connect (void)
322 {
323   struct PeerData *peer = &peer1;
324
325   /* Connect to stream */
326   peer->socket = GNUNET_STREAM_open (config,
327                                      &peer2.self,         /* Null for local peer? */
328                                      10,           /* App port */
329                                      &stream_open_cb, &peer1,
330                                      GNUNET_STREAM_OPTION_MAX_PAYLOAD_SIZE, 500,
331                                      GNUNET_STREAM_OPTION_END);
332   GNUNET_assert (NULL != peer->socket);
333 }
334
335
336 /**
337  * Initialize framework and start test
338  *
339  * @param cls closure
340  * @param cfg configuration of the peer that was started
341  * @param peer identity of the peer that was created
342  */
343 static void
344 run (void *cls, 
345      const struct GNUNET_CONFIGURATION_Handle *cfg,
346      struct GNUNET_TESTING_Peer *peer)
347 {
348   struct GNUNET_PeerIdentity self;
349   
350   GNUNET_TESTING_peer_get_identity (peer, &self);
351   config = cfg;  
352   peer2_listen_socket = 
353     GNUNET_STREAM_listen (config,
354                           10, /* App port */
355                           &stream_listen_cb,
356                           NULL,
357                           GNUNET_STREAM_OPTION_SIGNAL_LISTEN_SUCCESS,
358                           &stream_connect,
359                           GNUNET_STREAM_OPTION_END);
360   GNUNET_assert (NULL != peer2_listen_socket);
361   peer1.self = self;
362   peer2.self = self;
363   abort_task =
364     GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
365                                   (GNUNET_TIME_UNIT_SECONDS, 60), &do_abort,
366                                   NULL);
367 }
368
369 /**
370  * Main function
371  */
372 int main (int argc, char **argv)
373 {
374   if (0 != GNUNET_TESTING_peer_run ("test_stream_big",
375                                     "test_stream_local.conf",
376                                     &run, NULL))
377     return 1;
378   return (GNUNET_SYSERR == result) ? 1 : 0;
379 }
380
381 /* end of test_stream_big.c */