another connect_task init missing
[oweals/gnunet.git] / src / cadet / test_cadet_local.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2011 GNUnet e.V.
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., 51 Franklin Street, Fifth Floor,
18      Boston, MA 02110-1301, USA.
19 */
20
21 /**
22  * @file cadet/test_cadet_local.c
23  * @brief test cadet local: test of cadet channels with just one peer
24  * @author Bartlomiej Polot
25  */
26
27 #include "platform.h"
28 #include "gnunet_util_lib.h"
29 #include "gnunet_dht_service.h"
30 #include "gnunet_testing_lib.h"
31 #include "gnunet_cadet_service.h"
32
33 struct GNUNET_TESTING_Peer *me;
34
35 static struct GNUNET_CADET_Handle *cadet_peer_1;
36
37 static struct GNUNET_CADET_Handle *cadet_peer_2;
38
39 static struct GNUNET_CADET_Channel *ch;
40
41 static int result = GNUNET_OK;
42
43 static int got_data = GNUNET_NO;
44
45 static struct GNUNET_SCHEDULER_Task *abort_task;
46
47 static struct GNUNET_SCHEDULER_Task *connect_task;
48
49 static struct GNUNET_CADET_TransmitHandle *mth;
50
51
52 /**
53  * Connect to other client and send data
54  *
55  * @param cls Closue (unused).
56  */
57 static void
58 do_connect (void *cls);
59
60
61 /**
62  * Shutdown nicely
63  */
64 static void
65 do_shutdown (void *cls)
66 {
67   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutdown\n");
68   if (NULL != abort_task)
69   {
70     GNUNET_SCHEDULER_cancel (abort_task);
71     abort_task = NULL;
72   }
73   if (NULL != connect_task)
74   {
75     GNUNET_SCHEDULER_cancel (connect_task);
76     connect_task = NULL;
77   }
78   if (NULL != ch)
79   {
80     GNUNET_CADET_channel_destroy (ch);
81     ch = NULL;
82   }
83   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
84               "Disconnect client 1\n");
85   if (NULL != cadet_peer_1)
86   {
87     GNUNET_CADET_disconnect (cadet_peer_1);
88     cadet_peer_1 = NULL;
89   }
90   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
91               "Disconnect client 2\n");
92   if (NULL != cadet_peer_2)
93   {
94     GNUNET_CADET_disconnect (cadet_peer_2);
95     cadet_peer_2 = NULL;
96   }
97 }
98
99
100 /**
101  * Something went wrong and timed out. Kill everything and set error flag
102  */
103 static void
104 do_abort (void *cls)
105 {
106   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ABORT\n");
107   result = GNUNET_SYSERR;
108   abort_task = NULL;
109   GNUNET_SCHEDULER_shutdown ();
110 }
111
112
113 /**
114  * Function is called whenever a message is received.
115  *
116  * @param cls closure (set from GNUNET_CADET_connect)
117  * @param channel connection to the other end
118  * @param channel_ctx place to store local state associated with the channel
119  * @param message the actual message
120  *
121  * @return GNUNET_OK to keep the connection open,
122  *         GNUNET_SYSERR to close it (signal serious error)
123  */
124 static int
125 data_callback (void *cls, struct GNUNET_CADET_Channel *channel,
126                void **channel_ctx,
127                const struct GNUNET_MessageHeader *message)
128 {
129   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
130               "Data callback! Shutting down.\n");
131   got_data = GNUNET_YES;
132   GNUNET_SCHEDULER_shutdown ();
133   GNUNET_CADET_receive_done (channel);
134   return GNUNET_OK;
135 }
136
137
138 /**
139  * Method called whenever another peer has added us to a channel
140  * the other peer initiated.
141  *
142  * @param cls closure
143  * @param channel new handle to the channel
144  * @param initiator peer that started the channel
145  * @param port port number
146  * @param options channel options
147  * @return initial channel context for the channel
148  *         (can be NULL -- that's not an error)
149  */
150 static void *
151 inbound_channel (void *cls,
152                  struct GNUNET_CADET_Channel *channel,
153                  const struct GNUNET_PeerIdentity *initiator,
154                  const struct GNUNET_HashCode *port,
155                  enum GNUNET_CADET_ChannelOption options)
156 {
157   long id = (long) cls;
158
159   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
160               "received incoming channel on peer %d, port %s\n",
161               (int) id,
162               GNUNET_h2s (port));
163   if (id != 2L)
164   {
165     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
166                 "wrong peer\n");
167     result = GNUNET_SYSERR;
168   }
169   return NULL;
170 }
171
172
173 /**
174  * Function called whenever an channel is destroyed.  Should clean up
175  * any associated state.
176  *
177  * @param cls closure (set from GNUNET_CADET_connect)
178  * @param channel connection to the other end (henceforth invalid)
179  * @param channel_ctx place where local state associated
180  *                    with the channel is stored
181  */
182 static void
183 channel_end (void *cls,
184              const struct GNUNET_CADET_Channel *channel,
185              void *channel_ctx)
186 {
187   long id = (long) cls;
188
189   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
190               "incoming channel closed at peer %ld\n",
191               id);
192   if (NULL != mth)
193   {
194     GNUNET_CADET_notify_transmit_ready_cancel (mth);
195     mth = NULL;
196   }
197   if (channel == ch)
198     ch = NULL;
199   if (GNUNET_NO == got_data)
200   {
201     if (NULL == connect_task)
202       connect_task
203         = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
204                                                                        2),
205                                         &do_connect,
206                                         NULL);
207   }
208 }
209
210
211 /**
212  * Handler array for traffic received on peer1
213  */
214 static struct GNUNET_CADET_MessageHandler handlers1[] = {
215   {&data_callback, 1, 0},
216   {NULL, 0, 0}
217 };
218
219
220 /**
221  * Handler array for traffic received on peer2 (none expected)
222  */
223 static struct GNUNET_CADET_MessageHandler handlers2[] = {
224   {&data_callback, 1, 0},
225   {NULL, 0, 0}
226 };
227
228
229 /**
230  * Data send callback: fillbuffer with test packet.
231  *
232  * @param cls Closure (unused).
233  * @param size Buffer size.
234  * @param buf Buffer to fill.
235  *
236  * @return size of test packet.
237  */
238 static size_t
239 do_send (void *cls, size_t size, void *buf)
240 {
241   struct GNUNET_MessageHeader *m = buf;
242
243   mth = NULL;
244   if (NULL == buf)
245   {
246     GNUNET_break (0);
247     result = GNUNET_SYSERR;
248     return 0;
249   }
250   m->size = htons (sizeof (struct GNUNET_MessageHeader));
251   m->type = htons (1);
252   GNUNET_assert (size >= sizeof (struct GNUNET_MessageHeader));
253   return sizeof (struct GNUNET_MessageHeader);
254 }
255
256
257 /**
258  * Connect to other client and send data
259  *
260  * @param cls Closue (unused).
261  */
262 static void
263 do_connect (void *cls)
264 {
265   struct GNUNET_PeerIdentity id;
266
267   connect_task = NULL;
268   GNUNET_TESTING_peer_get_identity (me, &id);
269   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
270               "CONNECT BY PORT\n");
271   ch = GNUNET_CADET_channel_create (cadet_peer_1,
272                                     NULL,
273                                     &id, GC_u2h (1),
274                                     GNUNET_CADET_OPTION_DEFAULT);
275   mth = GNUNET_CADET_notify_transmit_ready (ch, GNUNET_NO,
276                                             GNUNET_TIME_UNIT_FOREVER_REL,
277                                             sizeof (struct GNUNET_MessageHeader),
278                                             &do_send, NULL);
279 }
280
281
282 /**
283  * Initialize framework and start test
284  *
285  * @param cls Closure (unused).
286  * @param cfg Configuration handle.
287  * @param peer Testing peer handle.
288  */
289 static void
290 run (void *cls,
291      const struct GNUNET_CONFIGURATION_Handle *cfg,
292      struct GNUNET_TESTING_Peer *peer)
293 {
294   me = peer;
295   GNUNET_SCHEDULER_add_shutdown (&do_shutdown, NULL);
296   abort_task =
297       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
298                                     (GNUNET_TIME_UNIT_SECONDS, 15),
299                                     &do_abort,
300                                     NULL);
301   cadet_peer_1 = GNUNET_CADET_connect (cfg,       /* configuration */
302                                      (void *) 1L,       /* cls */
303                                      &channel_end,      /* channel end hndlr */
304                                      handlers1); /* traffic handlers */
305   cadet_peer_2 = GNUNET_CADET_connect (cfg,       /* configuration */
306                                      (void *) 2L,     /* cls */
307                                      &channel_end,      /* channel end hndlr */
308                                      handlers2); /* traffic handlers */
309
310   if ( (NULL == cadet_peer_1) ||
311        (NULL == cadet_peer_2) )
312   {
313     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
314                 "Couldn't connect to cadet :(\n");
315     result = GNUNET_SYSERR;
316     GNUNET_SCHEDULER_shutdown ();
317     return;
318   }
319   GNUNET_CADET_open_port (cadet_peer_2,
320                           GC_u2h (1),
321                           &inbound_channel,
322                           (void *) 2L);
323   connect_task
324     = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
325                                                                    2),
326                                     &do_connect,
327                                     NULL);
328 }
329
330
331 /**
332  * Main
333  */
334 int
335 main (int argc, char *argv[])
336 {
337   if (0 != GNUNET_TESTING_peer_run ("test-cadet-local",
338                                     "test_cadet.conf",
339                                 &run, NULL))
340   {
341     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "run failed\n");
342     return 2;
343   }
344   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Final result: %d\n", result);
345   return (result == GNUNET_OK) ? 0 : 1;
346 }
347
348 /* end of test_cadet_local_1.c */