- fixing compile error
[oweals/gnunet.git] / src / stream / test_stream_local.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_local.c
23  * @brief Stream API testing 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_mesh_service.h"
32 #include "gnunet_stream_lib.h"
33
34 #define VERBOSE 1
35
36 /**
37  * Structure for holding peer's sockets and IO Handles
38  */
39 struct PeerData
40 {
41   /**
42    * Peer's stream socket
43    */
44   struct GNUNET_STREAM_Socket *socket;
45
46   /**
47    * Peer's io handle
48    */
49   struct GNUNET_STREAM_IOHandle *io_handle;
50
51   /**
52    * Bytes the peer has written
53    */
54   unsigned int bytes_wrote;
55
56   /**
57    * Byte the peer has read
58    */
59   unsigned int bytes_read;
60 };
61
62 static struct GNUNET_OS_Process *arm_pid;
63 static struct PeerData peer1;
64 static struct PeerData peer2;
65 static struct GNUNET_STREAM_ListenSocket *peer2_listen_socket;
66
67 static GNUNET_SCHEDULER_TaskIdentifier abort_task;
68 static GNUNET_SCHEDULER_TaskIdentifier test_task;
69 static GNUNET_SCHEDULER_TaskIdentifier read_task;
70
71 static char *data = "ABCD";
72 static int result;
73
74 /**
75  * Shutdown nicely
76  */
77 static void
78 do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
79 {
80   GNUNET_STREAM_close (peer1.socket);
81   GNUNET_STREAM_close (peer2.socket);
82   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: shutdown\n");
83   if (0 != abort_task)
84   {
85     GNUNET_SCHEDULER_cancel (abort_task);
86   }
87   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: arm\n");
88   if (0 != GNUNET_OS_process_kill (arm_pid, SIGTERM))
89   {
90     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
91   }
92   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Wait\n");
93   GNUNET_assert (GNUNET_OK == GNUNET_OS_process_wait (arm_pid));
94   GNUNET_OS_process_close (arm_pid);
95 }
96
97
98 /**
99  * Something went wrong and timed out. Kill everything and set error flag
100  */
101 static void
102 do_abort (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
103 {
104   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: ABORT\n");
105   if (0 != test_task)
106   {
107     GNUNET_SCHEDULER_cancel (test_task);
108   }
109   if (0 != read_task)
110     {
111       GNUNET_SCHEDULER_cancel (read_task);
112     }
113   result = GNUNET_SYSERR;
114   abort_task = 0;
115   do_shutdown (cls, tc);
116 }
117
118
119 /**
120  * The write completion function; called upon writing some data to stream or
121  * upon error
122  *
123  * @param cls the closure from GNUNET_STREAM_write/read
124  * @param status the status of the stream at the time this function is called
125  * @param size the number of bytes read or written
126  */
127 void write_completion (void *cls,
128                        enum GNUNET_STREAM_Status status,
129                        size_t size)
130 {
131   struct PeerData *peer;
132
133   peer = (struct PeerData *) cls;
134   GNUNET_assert (GNUNET_STREAM_OK == status);
135   GNUNET_assert (size < strlen (data));
136   peer->bytes_wrote += size;
137
138   if (peer->bytes_wrote < strlen(data)) /* Have more data to send */
139     {
140       peer->io_handle = GNUNET_STREAM_write (peer->socket,
141                                              (void *) data,
142                                              strlen(data) - peer->bytes_wrote,
143                                              GNUNET_TIME_relative_multiply
144                                              (GNUNET_TIME_UNIT_SECONDS, 5),
145                                              &write_completion,
146                                              cls);
147       GNUNET_assert (NULL != peer->io_handle);
148     }
149   else
150     {
151       if (&peer1 == peer)   /* Peer1 has finished writing; should read now */
152         {
153           peer->io_handle = GNUNET_STREAM_read ((struct GNUNET_STREAM_Socket *)
154                                                 peer->socket,
155                                                 GNUNET_TIME_relative_multiply
156                                                 (GNUNET_TIME_UNIT_SECONDS, 5),
157                                                 &input_processor,
158                                                 cls);
159           GNUNET_assert (NULL!=peer->io_handle);
160         }
161     }
162 }
163
164
165 /**
166  * Function executed after stream has been established
167  *
168  * @param cls the closure from GNUNET_STREAM_open
169  * @param socket socket to use to communicate with the other side (read/write)
170  */
171 static void 
172 stream_open_cb (void *cls,
173                 struct GNUNET_STREAM_Socket *socket)
174 {
175   struct PeerData *peer;
176
177   peer = (struct PeerData *) cls;
178   peer->bytes_wrote = 0;
179   GNUNET_assert (socket == peer1.socket);
180   GNUNET_assert (socket == peer->socket);
181   peer->io_handle = GNUNET_STREAM_write (peer->socket, /* socket */
182                                          (void *) data, /* data */
183                                          strlen(data),
184                                          GNUNET_TIME_relative_multiply
185                                          (GNUNET_TIME_UNIT_SECONDS, 5),
186                                          &write_completion,
187                                          cls);
188   GNUNET_assert (NULL != peer->io_handle);
189 }
190
191
192 /**
193  * Input processor
194  *
195  * @param cls the closure from GNUNET_STREAM_write/read
196  * @param status the status of the stream at the time this function is called
197  * @param data traffic from the other side
198  * @param size the number of bytes available in data read 
199  * @return number of bytes of processed from 'data' (any data remaining should be
200  *         given to the next time the read processor is called).
201  */
202 static size_t
203 input_processor (void *cls,
204                  enum GNUNET_STREAM_Status status,
205                  const void *input_data,
206                  size_t size)
207 {
208   struct PeerData *peer;
209
210   peer = (struct PeerData *) cls;
211
212   GNUNET_assert (GNUNET_STERAM_OK == status);
213   GNUNET_assert (size < strlen (data));
214   GNUNET_assert (strncmp ((const char *) data + peer->bytes_read, 
215                           (const char *) input_data,
216                           size));
217   peer->bytes_read += size;
218   
219   if (peer->bytes_read < strlen (data))
220     {
221       peer->io_handle = GNUNET_STREAM_read ((struct GNUNET_STREAM_Socket *)
222                                             peer->socket,
223                                             GNUNET_TIME_relative_multiply
224                                             (GNUNET_TIME_UNIT_SECONDS, 5),
225                                             &input_processor,
226                                             cls);
227       GNUNET_assert (NULL != peer->io_handle);
228     }
229   else 
230     {
231       if (&peer2 == peer)    /* Peer2 has completed reading; should write */
232         {
233           peer->bytes_wrote = 0;
234           peer->io_handle = GNUNET_STREAM_write ((struct GNUNET_STREAM_Socket *)
235                                                  peer->socket,
236                                                  (void *) data,
237                                                  strlen(data),
238                                                  GNUNET_TIME_relative_multiply
239                                                  (GNUNET_TIME_UNIT_SECONDS, 5),
240                                                  &write_completion,
241                                                  cls);
242         }
243       else                      /* Peer1 has completed reading. End of tests */
244         {
245           GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
246         }
247     } 
248   return size;
249 }
250
251   
252 /**
253  * Scheduler call back; to be executed when a new stream is connected
254  * Called from listen connect for peer2
255  */
256 static void
257 stream_read (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
258 {
259   read_task = GNUNET_SCHEDULER_NO_TASK;
260   GNUNET_assert (NULL != cls);
261   peer2.bytes_read = 0;
262   GNUNET_STREAM_listen_close (peer2_listen_socket); /* Close listen socket */
263   peer2.io_handle = GNUNET_STREAM_read ((struct GNUNET_STREAM_Socket *) cls,
264                                         GNUNET_TIME_relative_multiply
265                                         (GNUNET_TIME_UNIT_SECONDS, 5),
266                                         &input_processor,
267                                         (void *) &peer2);
268   GNUNET_assert (NULL != peer2.io_handle);
269 }
270
271
272 /**
273  * Functions of this type are called upon new stream connection from other peers
274  *
275  * @param cls the closure from GNUNET_STREAM_listen
276  * @param socket the socket representing the stream
277  * @param initiator the identity of the peer who wants to establish a stream
278  *            with us
279  * @return GNUNET_OK to keep the socket open, GNUNET_SYSERR to close the
280  *             stream (the socket will be invalid after the call)
281  */
282 static int
283 stream_listen_cb (void *cls,
284            struct GNUNET_STREAM_Socket *socket,
285            const struct GNUNET_PeerIdentity *initiator)
286 {
287   GNUNET_assert (NULL != socket);
288   GNUNET_assert (NULL == initiator);   /* Local peer=NULL? */
289   GNUNET_assert (socket != peer1.socket);
290
291   peer2.socket = socket;
292   read_task = GNUNET_SCHEDULER_add_now (&stream_read, (void *) socket);
293   return GNUNET_OK;
294 }
295
296
297 /**
298  * Testing function
299  */
300 static void
301 test (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
302 {
303   test_task = GNUNET_SCHEDULER_NO_TASK;
304
305   /* Connect to stream library */
306   peer1.socket = GNUNET_STREAM_open (NULL,         /* Null for local peer? */
307                                      10,           /* App port */
308                                      &stream_open_cb,
309                                      (void *) &peer1);
310   GNUNET_assert (NULL != peer1.socket);
311   peer2_listen_socket = GNUNET_STREAM_listen (10 /* App port */
312                                               &stream_listen_cb,
313                                               NULL);
314   GNUNET_assert (NULL != peer2_listen_socket);
315                   
316 }
317
318 /**
319  * Initialize framework and start test
320  */
321 static void
322 run (void *cls, char *const *args, const char *cfgfile,
323      const struct GNUNET_CONFIGURATION_Handle *cfg)
324 {
325    GNUNET_log_setup ("test_stream_local",
326 #if VERBOSE
327                     "DEBUG",
328 #else
329                     "WARNING",
330 #endif
331                     NULL);
332    arm_pid =
333      GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
334                               "gnunet-service-arm",
335 #if VERBOSE_ARM
336                               "-L", "DEBUG",
337 #endif
338                               "-c", "test_stream_local.conf", NULL);
339
340    abort_task =
341      GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
342                                    (GNUNET_TIME_UNIT_SECONDS, 20), &do_abort,
343                                     NULL);
344
345    test_task = GNUNET_SCHEDULER_add_now (&test, (void *) cfg);
346
347 }
348
349 /**
350  * Main function
351  */
352 int main (int argc, char **argv)
353 {
354   int ret;
355
356   char *const argv2[] = { "test-stream-local",
357                           "-c", "test_stream.conf",
358 #if VERBOSE
359                           "-L", "DEBUG",
360 #endif
361                           NULL
362   };
363   
364   struct GNUNET_GETOPT_CommandLineOption options[] = {
365     GNUNET_GETOPT_OPTION_END
366   };
367   
368   ret =
369       GNUNET_PROGRAM_run ((sizeof (argv2) / sizeof (char *)) - 1, argv2,
370                           "test-stream-local", "nohelp", options, &run, NULL);
371
372   if (GNUNET_OK != ret)
373   {
374     GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "run failed with error code %d\n",
375                 ret);
376     return 1;
377   }
378   if (GNUNET_SYSERR == result)
379   {
380     GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "test failed\n");
381     return 1;
382   }
383   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test ok\n");
384   return 0;
385 }