2 This file is part of GNUnet.
3 (C) 2011 Christian Grothoff (and other contributing authors)
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.
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.
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.
22 * @file mesh/test_mesh_local.c
23 * @brief test mesh local: test of tunnels with just one peer
24 * @author Bartlomiej Polot
28 #include "gnunet_util_lib.h"
29 #include "gnunet_dht_service.h"
30 #include "gnunet_mesh_service.h"
35 static struct GNUNET_OS_Process *arm_pid;
36 static struct GNUNET_MESH_Handle *mesh_peer_1;
37 static struct GNUNET_MESH_Handle *mesh_peer_2;
38 static struct GNUNET_MESH_Tunnel *t;
39 static unsigned int one = 1;
40 static unsigned int two = 2;
43 static GNUNET_SCHEDULER_TaskIdentifier abort_task;
44 static GNUNET_SCHEDULER_TaskIdentifier test_task;
51 do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
53 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: shutdown\n");
56 GNUNET_SCHEDULER_cancel (abort_task);
58 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: D1\n");
59 if (NULL != mesh_peer_1)
61 GNUNET_MESH_disconnect (mesh_peer_1);
63 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: D2\n");
64 if (NULL != mesh_peer_2)
66 GNUNET_MESH_disconnect (mesh_peer_2);
68 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: arm\n");
69 if (0 != GNUNET_OS_process_kill (arm_pid, SIGTERM))
71 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
73 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Wait\n");
74 GNUNET_assert (GNUNET_OK == GNUNET_OS_process_wait (arm_pid));
75 GNUNET_OS_process_close (arm_pid);
80 * Something went wrong and timed out. Kill everything and set error flag
83 do_abort (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
85 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: ABORT\n");
88 GNUNET_SCHEDULER_cancel (test_task);
90 result = GNUNET_SYSERR;
92 do_shutdown (cls, tc);
97 * Function is called whenever a message is received.
99 * @param cls closure (set from GNUNET_MESH_connect)
100 * @param tunnel connection to the other end
101 * @param tunnel_ctx place to store local state associated with the tunnel
102 * @param sender who sent the message
103 * @param message the actual message
104 * @param atsi performance data for the connection
105 * @return GNUNET_OK to keep the connection open,
106 * GNUNET_SYSERR to close it (signal serious error)
109 data_callback (void *cls, struct GNUNET_MESH_Tunnel *tunnel, void **tunnel_ctx,
110 const struct GNUNET_PeerIdentity *sender,
111 const struct GNUNET_MessageHeader *message,
112 const struct GNUNET_ATS_Information *atsi)
114 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Data callback\n");
115 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
116 (GNUNET_TIME_UNIT_SECONDS, 2), &do_shutdown,
123 * Method called whenever another peer has added us to a tunnel
124 * the other peer initiated.
127 * @param tunnel new handle to the tunnel
128 * @param initiator peer that started the tunnel
129 * @param atsi performance information for the tunnel
130 * @return initial tunnel context for the tunnel (can be NULL -- that's not an error)
133 inbound_tunnel (void *cls, struct GNUNET_MESH_Tunnel *tunnel,
134 const struct GNUNET_PeerIdentity *initiator,
135 const struct GNUNET_ATS_Information *atsi)
137 unsigned int id = *(unsigned int *) cls;
139 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: received incoming tunnel\n");
142 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
143 "test: received incoming tunnel on peer 2\n");
144 result = GNUNET_SYSERR;
151 * Function called whenever an inbound tunnel is destroyed. Should clean up
152 * any associated state.
154 * @param cls closure (set from GNUNET_MESH_connect)
155 * @param tunnel connection to the other end (henceforth invalid)
156 * @param tunnel_ctx place where local state associated
157 * with the tunnel is stored
160 inbound_end (void *cls, const struct GNUNET_MESH_Tunnel *tunnel,
163 unsigned int id = *(unsigned int *) cls;
165 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: incoming tunnel closed\n");
168 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
169 "test: received closing tunnel on peer 2\n");
170 result = GNUNET_SYSERR;
176 * Method called whenever a peer has disconnected from the tunnel.
179 * @param peer peer identity the tunnel stopped working with
182 peer_conected (void *cls, const struct GNUNET_PeerIdentity *peer,
183 const struct GNUNET_ATS_Information *atsi)
185 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: peer connected\n");
186 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &do_shutdown, NULL);
191 * Method called whenever a peer has connected to the tunnel.
194 * @param peer peer identity the tunnel was created to, NULL on timeout
195 * @param atsi performance data for the connection
198 peer_disconnected (void *cls, const struct GNUNET_PeerIdentity *peer)
200 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: peer disconnected\n");
205 * Handler array for traffic received on peer1
207 static struct GNUNET_MESH_MessageHandler handlers1[] = {
208 {&data_callback, 1, 0},
214 * Handler array for traffic received on peer2 (none expected)
216 static struct GNUNET_MESH_MessageHandler handlers2[] = { {NULL, 0, 0} };
220 * Start looking for a peer by type
223 do_find (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
225 GNUNET_MESH_peer_request_connect_by_type (t, 1);
233 test (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
235 struct GNUNET_CONFIGURATION_Handle *cfg = cls;
236 static const GNUNET_MESH_ApplicationType app1[] = { 1, 0 };
237 static const GNUNET_MESH_ApplicationType app2[] = { 0 };
239 test_task = GNUNET_SCHEDULER_NO_TASK;
240 mesh_peer_1 = GNUNET_MESH_connect (cfg, /* configuration */
242 (void *) &one, /* cls */
243 &inbound_tunnel, /* inbound new hndlr */
244 &inbound_end, /* inbound end hndlr */
245 handlers1, /* traffic handlers */
246 app1); /* apps offered */
248 mesh_peer_2 = GNUNET_MESH_connect (cfg, /* configuration */
250 (void *) &two, /* cls */
251 &inbound_tunnel, /* inbound new hndlr */
252 &inbound_end, /* inbound end hndlr */
253 handlers2, /* traffic handlers */
254 app2); /* apps offered */
255 if (NULL == mesh_peer_1 || NULL == mesh_peer_2)
257 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "test: Couldn't connect to mesh :(\n");
262 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: YAY! CONNECTED TO MESH :D\n");
265 t = GNUNET_MESH_tunnel_create (mesh_peer_2, NULL, &peer_conected,
266 &peer_disconnected, (void *) &two);
267 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &do_find, NULL);
272 * Initialize framework and start test
275 run (void *cls, char *const *args, const char *cfgfile,
276 const struct GNUNET_CONFIGURATION_Handle *cfg)
278 GNUNET_log_setup ("test_mesh_local",
286 GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
287 "gnunet-service-arm",
291 "-c", "test_mesh.conf", NULL);
294 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
295 (GNUNET_TIME_UNIT_SECONDS, 20), &do_abort,
298 test_task = GNUNET_SCHEDULER_add_now (&test, (void *) cfg);
307 main (int argc, char *argv[])
311 char *const argv2[] = { "test-mesh-local",
312 "-c", "test_mesh.conf",
318 struct GNUNET_GETOPT_CommandLineOption options[] = {
319 GNUNET_GETOPT_OPTION_END
323 GNUNET_PROGRAM_run ((sizeof (argv2) / sizeof (char *)) - 1, argv2,
324 "test-mesh-local", "nohelp", options, &run, NULL);
326 if (GNUNET_OK != ret)
328 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "run failed with error code %d\n",
332 if (GNUNET_SYSERR == result)
334 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "test failed\n");
337 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test ok\n");