Fixed compiler whining about test
[oweals/gnunet.git] / src / mesh / test_mesh_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 mesh/test_mesh_local.c
23  * @brief test mesh local: test of tunnels 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_mesh_service_new.h"
31
32 #define VERBOSE 1
33 #define VERBOSE_ARM 0
34
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;
40 static int result;
41 static GNUNET_SCHEDULER_TaskIdentifier abort_task;
42 static GNUNET_SCHEDULER_TaskIdentifier test_task;
43
44
45 /**
46  * Function is called whenever a message is received.
47  *
48  * @param cls closure (set from GNUNET_MESH_connect)
49  * @param tunnel connection to the other end
50  * @param tunnel_ctx place to store local state associated with the tunnel
51  * @param sender who sent the message
52  * @param message the actual message
53  * @param atsi performance data for the connection
54  * @return GNUNET_OK to keep the connection open,
55  *         GNUNET_SYSERR to close it (signal serious error)
56  */
57 static int
58 callback (void *cls, struct GNUNET_MESH_Tunnel *tunnel, void **tunnel_ctx,
59           const struct GNUNET_PeerIdentity *sender,
60           const struct GNUNET_MessageHeader *message,
61           const struct GNUNET_TRANSPORT_ATS_Information *atsi)
62 {
63   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Data callback\n");
64   return GNUNET_OK;
65 }
66
67
68 /**
69  * Method called whenever another peer has added us to a tunnel
70  * the other peer initiated.
71  *
72  * @param cls closure
73  * @param tunnel new handle to the tunnel
74  * @param initiator peer that started the tunnel
75  * @param atsi performance information for the tunnel
76  * @return initial tunnel context for the tunnel (can be NULL -- that's not an error)
77  */
78 static void *
79 inbound_tunnel (void *cls, struct GNUNET_MESH_Tunnel *tunnel,
80                 const struct GNUNET_PeerIdentity *initiator,
81                 const struct GNUNET_TRANSPORT_ATS_Information * atsi)
82 {
83   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: \n");
84   return NULL;
85 }
86
87 static struct GNUNET_MESH_MessageHandler handlers1[] = {
88   {&callback, 1, 0},
89   {NULL, 0, 0}
90 };
91
92 static struct GNUNET_MESH_MessageHandler handlers2[] = { {NULL, 0, 0} };
93
94
95
96
97
98
99
100
101 static void
102 do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
103 {
104   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: shutdown\n");
105   if (0 != abort_task)
106   {
107     GNUNET_SCHEDULER_cancel (abort_task);
108   }
109   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: D1\n");
110   if (NULL != mesh_peer_1)
111   {
112     GNUNET_MESH_disconnect (mesh_peer_1);
113   }
114   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: D2\n");
115   if (NULL != mesh_peer_2)
116   {
117     GNUNET_MESH_disconnect (mesh_peer_2);
118   }
119   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: arm\n");
120   if (0 != GNUNET_OS_process_kill (arm_pid, SIGTERM))
121   {
122     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
123   }
124   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Wait\n");
125   GNUNET_assert (GNUNET_OK == GNUNET_OS_process_wait (arm_pid));
126   GNUNET_OS_process_close (arm_pid);
127 }
128
129 static void
130 do_abort (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
131 {
132   if (0 != test_task)
133   {
134     GNUNET_SCHEDULER_cancel (test_task);
135   }
136   result = GNUNET_SYSERR;
137   abort_task = 0;
138   do_shutdown (cls, tc);
139 }
140
141 static void
142 test (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
143 {
144   struct GNUNET_CONFIGURATION_Handle *cfg = cls;
145   static const GNUNET_MESH_ApplicationType app1[] =
146       { 1, 2, 3, 4, 5, 6, 7, 8, 0 };
147   static const GNUNET_MESH_ApplicationType app2[] = { 0 };
148
149   test_task = (GNUNET_SCHEDULER_TaskIdentifier) 0;
150   mesh_peer_1 = GNUNET_MESH_connect (cfg,
151                                      10,
152                                      (void *)1,
153                                      &inbound_tunnel,
154                                      NULL,
155                                      handlers1,
156                                      app1);
157   mesh_peer_2 = GNUNET_MESH_connect (cfg,
158                                      10,
159                                      (void *)2,
160                                      NULL,
161                                      NULL,
162                                      handlers2,
163                                      app2);
164   if (NULL == mesh_peer_1 || NULL == mesh_peer_2)
165   {
166     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "test: Couldn't connect to mesh :(\n");
167     return;
168   }
169   else
170   {
171     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: YAY! CONNECTED TO MESH :D\n");
172   }
173
174   t_1 = GNUNET_MESH_tunnel_create (mesh_peer_1, NULL, NULL, NULL, (void *)1);
175 //   t_2 = GNUNET_MESH_tunnel_create (mesh_peer_2, NULL, NULL, NULL, 2);
176
177   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
178                                 (GNUNET_TIME_UNIT_SECONDS, 2), &do_shutdown,
179                                 NULL);
180 }
181
182
183 static void
184 run (void *cls, char *const *args, const char *cfgfile,
185      const struct GNUNET_CONFIGURATION_Handle *cfg)
186 {
187   GNUNET_log_setup ("test_mesh_local",
188 #if VERBOSE
189                     "DEBUG",
190 #else
191                     "WARNING",
192 #endif
193                     NULL);
194   arm_pid =
195       GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
196                                "gnunet-service-arm",
197 #if VERBOSE_ARM
198                                "-L", "DEBUG",
199 #endif
200                                "-c", "test_mesh.conf", NULL);
201
202   abort_task =
203       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
204                                     (GNUNET_TIME_UNIT_SECONDS, 20), &do_abort,
205                                     NULL);
206
207   test_task = GNUNET_SCHEDULER_add_now (&test, (void *) cfg);
208
209 }
210
211
212 int
213 main (int argc, char *argv[])
214 {
215   int ret;
216
217   char *const argv2[] = { "test-mesh-local",
218     "-c", "test_mesh.conf",
219 #if VERBOSE
220     "-L", "DEBUG",
221 #endif
222     NULL
223   };
224   struct GNUNET_GETOPT_CommandLineOption options[] = {
225     GNUNET_GETOPT_OPTION_END
226   };
227
228   ret =
229       GNUNET_PROGRAM_run ((sizeof (argv2) / sizeof (char *)) - 1, argv2,
230                           "test-mesh-local", "nohelp", options, &run, NULL);
231
232   if (GNUNET_OK != ret)
233   {
234     GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "run failed with error code %d\n",
235                 ret);
236     return 1;
237   }
238   if (GNUNET_SYSERR == result)
239   {
240     GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "test failed\n");
241     return 1;
242   }
243   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test ok\n");
244   return 0;
245 }