indentation
[oweals/gnunet.git] / src / mesh / test_mesh_api.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_api.c
23  * @brief test mesh api: dummy test of callbacks
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;
37 static int result;
38 GNUNET_SCHEDULER_TaskIdentifier abort_task;
39 GNUNET_SCHEDULER_TaskIdentifier test_task;
40
41 /**
42  * Function is called whenever a message is received.
43  *
44  * @param cls closure (set from GNUNET_MESH_connect)
45  * @param tunnel connection to the other end
46  * @param tunnel_ctx place to store local state associated with the tunnel
47  * @param sender who sent the message
48  * @param message the actual message
49  * @param atsi performance data for the connection
50  * @return GNUNET_OK to keep the connection open,
51  *         GNUNET_SYSERR to close it (signal serious error)
52  */
53 static int
54 callback (void *cls,
55           struct GNUNET_MESH_Tunnel *tunnel,
56           void **tunnel_ctx,
57           const struct GNUNET_PeerIdentity *sender,
58           const struct GNUNET_MessageHeader *message,
59           const struct GNUNET_TRANSPORT_ATS_Information *atsi)
60 {
61   return 0;
62 }
63
64 static struct GNUNET_MESH_MessageHandler handlers[] = { {&callback, 1, 0},
65 {NULL, 0, 0}
66 };
67
68
69 static void
70 do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
71 {
72   if (0 != abort_task)
73   {
74     GNUNET_SCHEDULER_cancel (abort_task);
75   }
76   if (NULL != mesh)
77   {
78     GNUNET_MESH_disconnect (mesh);
79   }
80   if (0 != GNUNET_OS_process_kill (arm_pid, SIGTERM))
81   {
82     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
83   }
84   GNUNET_assert (GNUNET_OK == GNUNET_OS_process_wait (arm_pid));
85   GNUNET_OS_process_close (arm_pid);
86 }
87
88 static void
89 do_abort (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
90 {
91   if (0 != test_task)
92   {
93     GNUNET_SCHEDULER_cancel (test_task);
94   }
95   result = GNUNET_SYSERR;
96   abort_task = 0;
97   do_shutdown (cls, tc);
98 }
99
100 static void
101 test (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
102 {
103   struct GNUNET_CONFIGURATION_Handle *cfg = cls;
104   GNUNET_MESH_ApplicationType app[3];
105
106   test_task = (GNUNET_SCHEDULER_TaskIdentifier) 0;
107   app[0] = (GNUNET_MESH_ApplicationType) 1;
108   app[1] = (GNUNET_MESH_ApplicationType) 2;
109   app[2] = (GNUNET_MESH_ApplicationType) 0;
110   mesh = GNUNET_MESH_connect (cfg, NULL, NULL, handlers, &app);
111   if (NULL == mesh)
112   {
113     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Couldn't connect to mesh :(\n");
114     return;
115   }
116   else
117   {
118     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "YAY! CONNECTED TO MESH :D\n");
119   }
120
121   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
122                                 (GNUNET_TIME_UNIT_SECONDS, 1), &do_shutdown,
123                                 NULL);
124 }
125
126
127 static void
128 run (void *cls,
129      char *const *args,
130      const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
131 {
132   arm_pid = GNUNET_OS_start_process (NULL, NULL,
133                                      "gnunet-service-arm", "gnunet-service-arm",
134 #if VERBOSE_ARM
135                                      "-L", "DEBUG",
136 #endif
137                                      "-c", "test_mesh.conf", NULL);
138
139   abort_task =
140       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
141                                     (GNUNET_TIME_UNIT_SECONDS, 20), &do_abort,
142                                     NULL);
143   test_task =
144       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
145                                     (GNUNET_TIME_UNIT_SECONDS, 1), &test,
146                                     (void *) cfg);
147
148 }
149
150
151 int
152 main (int argc, char *argv[])
153 {
154   int ret;
155
156   char *const argv2[] = { "test-mesh-api",
157     "-c", "test_mesh.conf",
158 #if VERBOSE
159     "-L", "DEBUG",
160 #endif
161     NULL
162   };
163   struct GNUNET_GETOPT_CommandLineOption options[] = {
164     GNUNET_GETOPT_OPTION_END
165   };
166   GNUNET_log_setup ("test-mesh-api",
167 #if VERBOSE
168                     "DEBUG",
169 #else
170                     "WARNING",
171 #endif
172                     NULL);
173   ret = GNUNET_PROGRAM_run ((sizeof (argv2) / sizeof (char *)) - 1,
174                             argv2, "test-mesh-api", "nohelp",
175                             options, &run, NULL);
176
177   if (GNUNET_OK != ret)
178   {
179     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
180                 "run failed with error code %d\n", ret);
181     return 1;
182   }
183   if (GNUNET_SYSERR == result)
184   {
185     GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "test failed\n");
186     return 1;
187   }
188   return 0;
189 }