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