]> git.meshlink.io Git - meshlink/blob - test/import-export.c
Format the examples and test suite.
[meshlink] / test / import-export.c
1 #include <stdio.h>
2 #include <unistd.h>
3 #include <stdlib.h>
4 #include <string.h>
5
6 #include "meshlink.h"
7
8 volatile bool bar_reachable = false;
9
10 void status_cb(meshlink_handle_t *mesh, meshlink_node_t *node, bool reachable) {
11         if(!strcmp(node->name, "bar")) {
12                 bar_reachable = reachable;
13         }
14 }
15
16 int main(int argc, char *argv[]) {
17         // Open two new meshlink instance.
18
19         meshlink_handle_t *mesh1 = meshlink_open("import_export_conf.1", "foo", "import-export", DEV_CLASS_BACKBONE);
20
21         if(!mesh1) {
22                 fprintf(stderr, "Could not initialize configuration for foo\n");
23                 return 1;
24         }
25
26         meshlink_handle_t *mesh2 = meshlink_open("import_export_conf.2", "bar", "import-export", DEV_CLASS_BACKBONE);
27
28         if(!mesh2) {
29                 fprintf(stderr, "Could not initialize configuration for bar\n");
30                 return 1;
31         }
32
33         // Disable local discovery
34
35         meshlink_enable_discovery(mesh1, false);
36         meshlink_enable_discovery(mesh2, false);
37
38         // Import and export both side's data
39
40         meshlink_add_address(mesh1, "localhost");
41         meshlink_add_address(mesh2, "localhost");
42
43         char *data = meshlink_export(mesh1);
44
45         if(!data) {
46                 fprintf(stderr, "Foo could not export its configuration\n");
47                 return 1;
48         }
49
50         if(!meshlink_import(mesh2, data)) {
51                 fprintf(stderr, "Bar could not import foo's configuration\n");
52                 return 1;
53         }
54
55         free(data);
56
57         data = meshlink_export(mesh2);
58
59         if(!data) {
60                 fprintf(stderr, "Bar could not export its configuration\n");
61                 return 1;
62         }
63
64
65         if(!meshlink_import(mesh1, data)) {
66                 fprintf(stderr, "Foo could not import bar's configuration\n");
67                 return 1;
68         }
69
70         free(data);
71
72         // Start both instances
73
74         meshlink_set_node_status_cb(mesh1, status_cb);
75
76         if(!meshlink_start(mesh1)) {
77                 fprintf(stderr, "Foo could not start\n");
78                 return 1;
79         }
80
81         if(!meshlink_start(mesh2)) {
82                 fprintf(stderr, "Bar could not start\n");
83                 return 1;
84         }
85
86         // Wait for the two to connect.
87
88         for(int i = 0; i < 20; i++) {
89                 sleep(1);
90
91                 if(bar_reachable) {
92                         break;
93                 }
94         }
95
96         if(!bar_reachable) {
97                 fprintf(stderr, "Bar not reachable for foo after 20 seconds\n");
98                 return 1;
99         }
100
101         int pmtu = meshlink_get_pmtu(mesh2, meshlink_get_node(mesh2, "bar"));
102
103         for(int i = 0; i < 10 && !pmtu; i++) {
104                 sleep(1);
105                 pmtu = meshlink_get_pmtu(mesh2, meshlink_get_node(mesh2, "bar"));
106         }
107
108         if(!pmtu) {
109                 fprintf(stderr, "UDP communication with bar not possible after 10 seconds\n");
110                 return 1;
111         }
112
113         // Clean up.
114
115         meshlink_stop(mesh2);
116         meshlink_stop(mesh1);
117         meshlink_close(mesh2);
118         meshlink_close(mesh1);
119
120         return 0;
121 }