run indent twice, it alternates between two 'canonical' forms, also run whitespace...
[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, struct GNUNET_MESH_Tunnel *tunnel, void **tunnel_ctx,
55           const struct GNUNET_PeerIdentity *sender,
56           const struct GNUNET_MessageHeader *message,
57           const struct GNUNET_TRANSPORT_ATS_Information *atsi)
58 {
59   return 0;
60 }
61
62 static struct GNUNET_MESH_MessageHandler handlers[] = { {&callback, 1, 0},
63 {NULL, 0, 0}
64 };
65
66
67 static void
68 do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
69 {
70   fprintf (stderr, "++++++++ STARTING SHUTDOWN\n");
71   fprintf (stderr, "+++++++++ ABORT TASK\n");
72   if (0 != abort_task)
73   {
74     GNUNET_SCHEDULER_cancel (abort_task);
75   }
76   fprintf (stderr, "+++++++++ DISCONNECT MESH\n");
77   if (NULL != mesh)
78   {
79     GNUNET_MESH_disconnect (mesh);
80   }
81   fprintf (stderr, "+++++++++ KILL PROCESS\n");
82   if (0 != GNUNET_OS_process_kill (arm_pid, SIGTERM))
83   {
84     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
85   }
86   fprintf (stderr, "+++++++++ WAIT\n");
87   GNUNET_assert (GNUNET_OK == GNUNET_OS_process_wait (arm_pid));
88   fprintf (stderr, "+++++++++ PROCESS CLOSE\n");
89   GNUNET_OS_process_close (arm_pid);
90   fprintf (stderr, "++++++++ END SHUTDOWN\n");
91 }
92
93 static void
94 do_abort (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
95 {
96   fprintf (stderr, "++++++++ STARTING ABORT\n");
97   if (0 != test_task)
98   {
99     GNUNET_SCHEDULER_cancel (test_task);
100   }
101   result = GNUNET_SYSERR;
102   abort_task = 0;
103   do_shutdown (cls, tc);
104   fprintf (stderr, "++++++++ END ABORT\n");
105 }
106
107 static void
108 test (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
109 {
110   struct GNUNET_CONFIGURATION_Handle *cfg = cls;
111   static const GNUNET_MESH_ApplicationType app[] = { 1, 2, 3, 0 };
112
113   fprintf (stderr, "++++++++ STARTING TEST\n");
114   test_task = (GNUNET_SCHEDULER_TaskIdentifier) 0;
115   mesh = GNUNET_MESH_connect (cfg, NULL, NULL, handlers, app);
116   if (NULL == mesh)
117   {
118     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Couldn't connect to mesh :(\n");
119     return;
120   }
121   else
122   {
123     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "YAY! CONNECTED TO MESH :D\n");
124   }
125
126   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
127                                 (GNUNET_TIME_UNIT_SECONDS, 3), &do_shutdown,
128                                 NULL);
129   fprintf (stderr, "++++++++ END TEST\n");
130 }
131
132
133 static void
134 run (void *cls, char *const *args, const char *cfgfile,
135      const struct GNUNET_CONFIGURATION_Handle *cfg)
136 {
137   fprintf (stderr, "++++++++ STARTING RUN\n");
138   GNUNET_log_setup ("test_mesh_small",
139 #if VERBOSE
140                     "DEBUG",
141 #else
142                     "WARNING",
143 #endif
144                     NULL);
145   arm_pid =
146       GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
147                                "gnunet-service-arm",
148 #if VERBOSE_ARM
149                                "-L", "DEBUG",
150 #endif
151                                "-c", "test_mesh.conf", NULL);
152
153   abort_task =
154       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
155                                     (GNUNET_TIME_UNIT_SECONDS, 20), &do_abort,
156                                     NULL);
157   test_task =
158       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &test,
159                                     (void *) cfg);
160 //       GNUNET_SCHEDULER_add_now (&test, (void *)cfg);
161
162   fprintf (stderr, "++++++++ END RUN\n");
163 }
164
165
166 int
167 main (int argc, char *argv[])
168 {
169   int ret;
170
171   char *const argv2[] = { "test-mesh-api",
172     "-c", "test_mesh.conf",
173 #if VERBOSE
174     "-L", "DEBUG",
175 #endif
176     NULL
177   };
178   struct GNUNET_GETOPT_CommandLineOption options[] = {
179     GNUNET_GETOPT_OPTION_END
180   };
181
182   fprintf (stderr, "++++++++ STARTING TEST_API\n");
183   ret =
184       GNUNET_PROGRAM_run ((sizeof (argv2) / sizeof (char *)) - 1, argv2,
185                           "test-mesh-api", "nohelp", options, &run, NULL);
186
187   if (GNUNET_OK != ret)
188   {
189     GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "run failed with error code %d\n",
190                 ret);
191     return 1;
192   }
193   if (GNUNET_SYSERR == result)
194   {
195     GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "test failed\n");
196     return 1;
197   }
198   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test ok\n");
199   fprintf (stderr, "++++++++ END TEST_API\n");
200   return 0;
201 }