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