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