08a3b1c3cd9d4a33e69e1051bf1468ec76a9b9d1
[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_MESH_MessageHandler        handlers[] = {{NULL, 0, 0}};
36 static struct GNUNET_OS_Process                 *arm_pid;
37 static struct GNUNET_MESH_Handle                *mesh;
38 static int                                      result;
39 GNUNET_SCHEDULER_TaskIdentifier                 abort_task;
40 GNUNET_SCHEDULER_TaskIdentifier                 test_task;
41
42 static void
43 do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
44 {
45     if (0 != abort_task) {
46         GNUNET_SCHEDULER_cancel(abort_task);
47     }
48     if (NULL != mesh) {
49         GNUNET_MESH_disconnect (mesh);
50     }
51     if (0 != GNUNET_OS_process_kill (arm_pid, SIGTERM)) {
52         GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
53     }
54     GNUNET_assert (GNUNET_OK == GNUNET_OS_process_wait (arm_pid));
55     GNUNET_OS_process_close (arm_pid);
56 }
57
58 static void
59 do_abort (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
60 {
61     if (0 != test_task) {
62         GNUNET_SCHEDULER_cancel(test_task);
63     }
64     result = GNUNET_SYSERR;
65     abort_task = 0;
66     do_shutdown(cls, tc);
67 }
68
69 static void
70 test (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
71 {
72     struct GNUNET_CONFIGURATION_Handle  *cfg = cls;
73     GNUNET_MESH_ApplicationType         app;
74
75     test_task = (GNUNET_SCHEDULER_TaskIdentifier) 0;
76     app = (GNUNET_MESH_ApplicationType) 0;
77     mesh = GNUNET_MESH_connect(cfg, NULL, NULL, handlers, &app);
78     if(NULL == mesh) {
79         GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Couldn't connect to mesh :(\n");
80         return;
81     } else {
82         GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "YAY! CONNECTED TO MESH :D\n");
83     }
84
85     GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply(
86                                     GNUNET_TIME_UNIT_SECONDS, 1),
87                                     &do_shutdown,
88                                     NULL);
89 }
90
91
92 static void
93 run (void *cls,
94      char *const *args,
95      const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
96 {
97     arm_pid = GNUNET_OS_start_process (NULL, NULL,
98                                        "gnunet-service-arm",
99                                        "gnunet-service-arm",
100 #if VERBOSE_ARM
101                                        "-L", "DEBUG",
102 #endif
103                                        "-c", "test_mesh.conf",
104                                        NULL);
105
106     abort_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply(
107                                                 GNUNET_TIME_UNIT_SECONDS, 5),
108                                                 &do_abort,
109                                                 NULL);
110     test_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply(
111                                                 GNUNET_TIME_UNIT_SECONDS, 1),
112                                                 &test,
113                                                 (void *)cfg);
114
115 }
116
117
118 int
119 main (int argc, char *argv[])
120 {
121     int ret;
122     char *const argv2[] = {"test-mesh-api",
123         "-c", "test_mesh.conf",
124 #if VERBOSE
125         "-L", "DEBUG",
126 #endif
127         NULL
128     };
129     struct GNUNET_GETOPT_CommandLineOption options[] = {
130         GNUNET_GETOPT_OPTION_END
131     };
132     GNUNET_log_setup ("test-mesh-api",
133 #if VERBOSE
134                     "DEBUG",
135 #else
136                     "WARNING",
137 #endif
138                     NULL);
139     ret = GNUNET_PROGRAM_run ((sizeof (argv2) / sizeof (char *)) - 1,
140                         argv2, "test-mesh-api", "nohelp",
141                         options, &run, NULL);
142
143     if ( GNUNET_OK != ret ) {
144         GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
145                    "run failed with error code %d\n", ret);
146         return 1;
147     }
148     if ( GNUNET_SYSERR == result ) {
149         GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
150                    "test failed\n");
151         return 1;
152     }
153     return 0;
154 }