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