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_new.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_1;
39 static struct GNUNET_MESH_Tunnel *t_2;
41 static GNUNET_SCHEDULER_TaskIdentifier abort_task;
42 static GNUNET_SCHEDULER_TaskIdentifier test_task;
45 * Function is called whenever a message is received.
47 * @param cls closure (set from GNUNET_MESH_connect)
48 * @param tunnel connection to the other end
49 * @param tunnel_ctx place to store local state associated with the tunnel
50 * @param sender who sent the message
51 * @param message the actual message
52 * @param atsi performance data for the connection
53 * @return GNUNET_OK to keep the connection open,
54 * GNUNET_SYSERR to close it (signal serious error)
57 callback (void *cls, struct GNUNET_MESH_Tunnel *tunnel, void **tunnel_ctx,
58 const struct GNUNET_PeerIdentity *sender,
59 const struct GNUNET_MessageHeader *message,
60 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
65 static struct GNUNET_MESH_MessageHandler handlers1[] = {
70 static struct GNUNET_MESH_MessageHandler handlers2[] = {{NULL, 0, 0}};
74 do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
76 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: shutdown\n");
79 GNUNET_SCHEDULER_cancel (abort_task);
81 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: D1\n");
82 if (NULL != mesh_peer_1)
84 GNUNET_MESH_disconnect (mesh_peer_1);
86 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: D2\n");
87 if (NULL != mesh_peer_2)
89 GNUNET_MESH_disconnect (mesh_peer_2);
91 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: arm\n");
92 if (0 != GNUNET_OS_process_kill (arm_pid, SIGTERM))
94 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
96 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Wait\n");
97 GNUNET_assert (GNUNET_OK == GNUNET_OS_process_wait (arm_pid));
98 GNUNET_OS_process_close (arm_pid);
102 do_abort (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
106 GNUNET_SCHEDULER_cancel (test_task);
108 result = GNUNET_SYSERR;
110 do_shutdown (cls, tc);
114 test (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
116 struct GNUNET_CONFIGURATION_Handle *cfg = cls;
117 static const GNUNET_MESH_ApplicationType app1[] =
118 { 1, 2, 3, 4, 5, 6, 7, 8, 0 };
119 static const GNUNET_MESH_ApplicationType app2[] =
122 test_task = (GNUNET_SCHEDULER_TaskIdentifier) 0;
123 mesh_peer_1 = GNUNET_MESH_connect (cfg, 10, 1, NULL, NULL, handlers1, app1);
124 mesh_peer_2 = GNUNET_MESH_connect (cfg, 10, 2, NULL, NULL, handlers2, app2);
125 if (NULL == mesh_peer_1 || NULL == mesh_peer_2)
127 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "test: Couldn't connect to mesh :(\n");
132 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: YAY! CONNECTED TO MESH :D\n");
135 t_1 = GNUNET_MESH_tunnel_create (mesh_peer_1, NULL, NULL, NULL, 1);
136 // t_2 = GNUNET_MESH_tunnel_create (mesh_peer_2, NULL, NULL, NULL, 2);
138 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply(
139 GNUNET_TIME_UNIT_SECONDS,
147 run (void *cls, char *const *args, const char *cfgfile,
148 const struct GNUNET_CONFIGURATION_Handle *cfg)
150 GNUNET_log_setup ("test_mesh_local",
158 GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
159 "gnunet-service-arm",
163 "-c", "test_mesh.conf", NULL);
166 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
167 (GNUNET_TIME_UNIT_SECONDS, 20), &do_abort,
170 test_task = GNUNET_SCHEDULER_add_now (&test, (void *) cfg);
176 main (int argc, char *argv[])
180 char *const argv2[] = { "test-mesh-local",
181 "-c", "test_mesh.conf",
187 struct GNUNET_GETOPT_CommandLineOption options[] = {
188 GNUNET_GETOPT_OPTION_END
192 GNUNET_PROGRAM_run ((sizeof (argv2) / sizeof (char *)) - 1, argv2,
193 "test-mesh-local", "nohelp", options, &run, NULL);
195 if (GNUNET_OK != ret)
197 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "run failed with error code %d\n",
201 if (GNUNET_SYSERR == result)
203 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "test failed\n");
206 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test ok\n");