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