6c3bd60a9f567a10122ff0eaa3da859b85ea2bf8
[oweals/gnunet.git] / src / mesh / test_mesh_api.c
1 #include "platform.h"
2 #include "gnunet_util_lib.h"
3 #include "gnunet_dht_service.h"
4 #include "gnunet_mesh_service_new.h"
5
6 static struct GNUNET_MESH_MessageHandler handlers[] = {
7     {NULL, 0, 0}
8 };
9
10 static struct GNUNET_OS_Process            *arm_pid;
11
12 static struct GNUNET_MESH_Handle           *mesh;
13
14 static void
15 do_shutdown (void *cls,
16              const struct GNUNET_SCHEDULER_TaskContext *tc)
17 {
18     if (NULL != mesh) {
19         GNUNET_MESH_disconnect (mesh);
20     }
21     if (0 != GNUNET_OS_process_kill (arm_pid, SIGTERM)) {
22         GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
23     }
24     GNUNET_assert (GNUNET_OK == GNUNET_OS_process_wait (arm_pid));
25     GNUNET_OS_process_close (arm_pid);
26 }
27
28
29 static void
30 error_shutdown (void *cls,
31                 const struct GNUNET_SCHEDULER_TaskContext *tc)
32 {
33     if (NULL != mesh) {
34         GNUNET_MESH_disconnect (mesh);
35     }
36     if (0 != GNUNET_OS_process_kill (arm_pid, SIGTERM)) {
37         GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
38     }
39     GNUNET_assert (GNUNET_OK == GNUNET_OS_process_wait (arm_pid));
40     GNUNET_OS_process_close (arm_pid);
41 }
42
43 static void
44 run (void *cls,
45      char *const *args,
46      const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg) {
47     GNUNET_MESH_ApplicationType         app;
48
49     arm_pid = GNUNET_OS_start_process (NULL, NULL,
50                                        "gnunet-service-arm",
51                                        "gnunet-service-arm",
52                                        "-L", "DEBUG",
53                                        "-c", "test_mesh.conf",
54                                        NULL);
55     app = 0;
56     mesh = GNUNET_MESH_connect(cfg, NULL, NULL, handlers, &app);
57     if(NULL == mesh) {
58         GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Couldn't connect to mesh :(\n");
59     } else {
60         GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "YAY! CONNECTED TO MESH :D\n");
61     }
62
63     /* do real test work here */
64     GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply(
65                                     GNUNET_TIME_UNIT_SECONDS, 5),
66                                   &do_shutdown,
67                                   NULL);
68 }
69
70
71 int main (int argc, char *argv[]) {
72     int ret;
73     char *const argv2[] = {"test-mesh-api",
74         "-c", "test_mesh.conf",
75         "-L", "DEBUG",
76         NULL
77     };
78     struct GNUNET_GETOPT_CommandLineOption options[] = {
79         GNUNET_GETOPT_OPTION_END
80     };
81       GNUNET_log_setup ("test-dht-api","DEBUG", NULL);
82     ret = GNUNET_PROGRAM_run ((sizeof (argv2) / sizeof (char *)) - 1,
83                         argv2, "test-mesh-api", "nohelp",
84                         options, &run, NULL);
85     if (ret != GNUNET_OK) {
86         GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
87                    "test-mesh-api': Failed with error code %d\n", ret);
88         return 1;
89     }
90     return 0;
91 }