small API change: do no longer pass rarely needed GNUNET_SCHEDULER_TaskContext to...
[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 * shutdown_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   }
72   if (NULL != ch)
73   {
74     GNUNET_CADET_channel_destroy (ch);
75   }
76   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnect client 1\n");
77   if (NULL != cadet_peer_1)
78   {
79     GNUNET_CADET_disconnect (cadet_peer_1);
80   }
81   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnect client 2\n");
82   if (NULL != cadet_peer_2)
83   {
84     GNUNET_CADET_disconnect (cadet_peer_2);
85   }
86 }
87
88
89 /**
90  * Something went wrong and timed out. Kill everything and set error flag
91  */
92 static void
93 do_abort (void *cls)
94 {
95   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ABORT\n");
96   result = GNUNET_SYSERR;
97   abort_task = NULL;
98   if (NULL != shutdown_task)
99   {
100     GNUNET_SCHEDULER_cancel (shutdown_task);
101     shutdown_task = NULL;
102   }
103   do_shutdown (cls);
104 }
105
106
107 /**
108  * Function is called whenever a message is received.
109  *
110  * @param cls closure (set from GNUNET_CADET_connect)
111  * @param channel connection to the other end
112  * @param channel_ctx place to store local state associated with the channel
113  * @param message the actual message
114  *
115  * @return GNUNET_OK to keep the connection open,
116  *         GNUNET_SYSERR to close it (signal serious error)
117  */
118 static int
119 data_callback (void *cls, struct GNUNET_CADET_Channel *channel,
120                void **channel_ctx,
121                const struct GNUNET_MessageHeader *message)
122 {
123   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Data callback! Shutting down.\n");
124   got_data = GNUNET_YES;
125   if (NULL != shutdown_task)
126     GNUNET_SCHEDULER_cancel (shutdown_task);
127   shutdown_task =
128     GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
129                                   &do_shutdown,
130                                   NULL);
131   GNUNET_CADET_receive_done (channel);
132   return GNUNET_OK;
133 }
134
135
136 /**
137  * Method called whenever another peer has added us to a channel
138  * the other peer initiated.
139  *
140  * @param cls closure
141  * @param channel new handle to the channel
142  * @param initiator peer that started the channel
143  * @param port port number
144  * @param options channel options
145  * @return initial channel context for the channel
146  *         (can be NULL -- that's not an error)
147  */
148 static void *
149 inbound_channel (void *cls,
150                  struct GNUNET_CADET_Channel *channel,
151                  const struct GNUNET_PeerIdentity *initiator,
152                  uint32_t port,
153                  enum GNUNET_CADET_ChannelOption options)
154 {
155   long id = (long) cls;
156
157   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
158               "received incoming channel on peer %d, port %u\n",
159               id, port);
160   if (id != 2L)
161   {
162     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
163                 "wrong peer\n");
164     result = GNUNET_SYSERR;
165   }
166   return NULL;
167 }
168
169
170 /**
171  * Function called whenever an channel is destroyed.  Should clean up
172  * any associated state.
173  *
174  * @param cls closure (set from GNUNET_CADET_connect)
175  * @param channel connection to the other end (henceforth invalid)
176  * @param channel_ctx place where local state associated
177  *                    with the channel is stored
178  */
179 static void
180 channel_end (void *cls, const struct GNUNET_CADET_Channel *channel,
181              void *channel_ctx)
182 {
183   long id = (long) cls;
184
185   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
186               "incoming channel closed at peer %ld\n",
187               id);
188   if (NULL != mth)
189   {
190     GNUNET_CADET_notify_transmit_ready_cancel (mth);
191     mth = NULL;
192   }
193   if (GNUNET_NO == got_data)
194   {
195     GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (
196                                   GNUNET_TIME_UNIT_SECONDS,
197                                   2),
198                                   &do_connect, NULL);
199   }
200 }
201
202
203 /**
204  * Handler array for traffic received on peer1
205  */
206 static struct GNUNET_CADET_MessageHandler handlers1[] = {
207   {&data_callback, 1, 0},
208   {NULL, 0, 0}
209 };
210
211
212 /**
213  * Handler array for traffic received on peer2 (none expected)
214  */
215 static struct GNUNET_CADET_MessageHandler handlers2[] = {
216   {&data_callback, 1, 0},
217   {NULL, 0, 0}
218 };
219
220
221 /**
222  * Data send callback: fillbuffer with test packet.
223  *
224  * @param cls Closure (unused).
225  * @param size Buffer size.
226  * @param buf Buffer to fill.
227  *
228  * @return size of test packet.
229  */
230 static size_t
231 do_send (void *cls, size_t size, void *buf)
232 {
233   struct GNUNET_MessageHeader *m = buf;
234
235   mth = NULL;
236   if (NULL == buf)
237   {
238     GNUNET_break (0);
239     result = GNUNET_SYSERR;
240     return 0;
241   }
242   m->size = htons (sizeof (struct GNUNET_MessageHeader));
243   m->type = htons (1);
244   GNUNET_assert (size >= sizeof (struct GNUNET_MessageHeader));
245   return sizeof (struct GNUNET_MessageHeader);
246 }
247
248 /**
249  * Connect to other client and send data
250  *
251  * @param cls Closue (unused).
252  */
253 static void
254 do_connect (void *cls)
255 {
256   struct GNUNET_PeerIdentity id;
257   const struct GNUNET_SCHEDULER_TaskContext *tc;
258
259   tc = GNUNET_SCHEDULER_get_task_context ();
260   if (NULL != tc && 0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
261     return;
262
263   GNUNET_TESTING_peer_get_identity (me, &id);
264   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CONNECT BY PORT\n");
265   ch = GNUNET_CADET_channel_create (cadet_peer_1, NULL, &id, 1,
266                                    GNUNET_CADET_OPTION_DEFAULT);
267   mth = GNUNET_CADET_notify_transmit_ready (ch, GNUNET_NO,
268                                            GNUNET_TIME_UNIT_FOREVER_REL,
269                                            sizeof (struct GNUNET_MessageHeader),
270                                            &do_send, NULL);
271 }
272
273
274 /**
275  * Initialize framework and start test
276  *
277  * @param cls Closure (unused).
278  * @param cfg Configuration handle.
279  * @param peer Testing peer handle.
280  */
281 static void
282 run (void *cls,
283      const struct GNUNET_CONFIGURATION_Handle *cfg,
284      struct GNUNET_TESTING_Peer *peer)
285 {
286   static uint32_t ports[] = {1, 0};
287
288   me = peer;
289   abort_task =
290       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
291                                     (GNUNET_TIME_UNIT_SECONDS, 15), &do_abort,
292                                     NULL);
293   cadet_peer_1 = GNUNET_CADET_connect (cfg,       /* configuration */
294                                      (void *) 1L,       /* cls */
295                                      NULL,              /* inbound new hndlr */
296                                      &channel_end,      /* channel end hndlr */
297                                      handlers1, /* traffic handlers */
298                                      NULL);     /* ports offered */
299
300   cadet_peer_2 = GNUNET_CADET_connect (cfg,       /* configuration */
301                                      (void *) 2L,     /* cls */
302                                      &inbound_channel,   /* inbound new hndlr */
303                                      &channel_end,      /* channel end hndlr */
304                                      handlers2, /* traffic handlers */
305                                      ports);     /* ports offered */
306   if (NULL == cadet_peer_1 || NULL == cadet_peer_2)
307   {
308     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Couldn't connect to cadet :(\n");
309     result = GNUNET_SYSERR;
310     return;
311   }
312   else
313   {
314     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "YAY! CONNECTED TO CADET :D\n");
315   }
316   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (
317                                   GNUNET_TIME_UNIT_SECONDS,
318                                   2),
319                                 &do_connect, NULL);
320 }
321
322
323 /**
324  * Main
325  */
326 int
327 main (int argc, char *argv[])
328 {
329   if (0 != GNUNET_TESTING_peer_run ("test-cadet-local",
330                                     "test_cadet.conf",
331                                 &run, NULL))
332   {
333     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "run failed\n");
334     return 2;
335   }
336   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Final result: %d\n", result);
337   return (result == GNUNET_OK) ? 0 : 1;
338 }
339
340 /* end of test_cadet_local_1.c */