]> git.meshlink.io Git - meshlink/blob - test/invite-join.c
Format the examples and test suite.
[meshlink] / test / invite-join.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 baz_reachable = false;
9
10 void status_cb(meshlink_handle_t *mesh, meshlink_node_t *node, bool reachable) {
11         if(!strcmp(node->name, "baz")) {
12                 baz_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("invite_join_conf.1", "foo", "invite-join", 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("invite_join_conf.2", "bar", "invite-join", 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         // Start the first instance and have it generate an invitation.
39
40         meshlink_set_node_status_cb(mesh1, status_cb);
41
42         if(!meshlink_start(mesh1)) {
43                 fprintf(stderr, "Foo could not start\n");
44                 return 1;
45         }
46
47         meshlink_add_address(mesh1, "localhost");
48         char *url = meshlink_invite(mesh1, "baz");
49
50         if(!url) {
51                 fprintf(stderr, "Foo could not generate an invitation for baz\n");
52                 return 1;
53         }
54
55         // Have the second instance join the first.
56
57         if(!meshlink_join(mesh2, url)) {
58                 fprintf(stderr, "Baz could not join foo's mesh\n");
59                 return 1;
60         }
61
62         free(url);
63
64         if(!meshlink_start(mesh2)) {
65                 fprintf(stderr, "Baz could not start\n");
66                 return 1;
67         }
68
69         // Wait for the two to connect.
70
71         for(int i = 0; i < 60; i++) {
72                 sleep(1);
73
74                 if(baz_reachable) {
75                         break;
76                 }
77         }
78
79         if(!baz_reachable) {
80                 fprintf(stderr, "Baz not reachable for foo after 20 seconds\n");
81                 return 1;
82         }
83
84         int pmtu = meshlink_get_pmtu(mesh1, meshlink_get_node(mesh1, "baz"));
85
86         for(int i = 0; i < 10 && !pmtu; i++) {
87                 sleep(1);
88                 pmtu = meshlink_get_pmtu(mesh1, meshlink_get_node(mesh1, "baz"));
89         }
90
91         if(!pmtu) {
92                 fprintf(stderr, "UDP communication with baz not possible after 10 seconds\n");
93                 return 1;
94         }
95
96         // Clean up.
97
98         meshlink_stop(mesh2);
99         meshlink_stop(mesh1);
100         meshlink_close(mesh2);
101         meshlink_close(mesh1);
102
103         return 0;
104 }