]> git.meshlink.io Git - meshlink/blob - test/basicpp.cpp
Never automatically try to bind to ports >= 32768.
[meshlink] / test / basicpp.cpp
1 #include <cstring>
2 #include <iostream>
3 #include <unistd.h>
4 #include <cerrno>
5
6 #include "meshlink++.h"
7
8 using namespace std;
9
10 int main() {
11         // Open a new meshlink instance.
12
13         meshlink::mesh mesh;
14         mesh.open("basicpp_conf", "foo", "basicpp", DEV_CLASS_BACKBONE);
15
16         // Check that our own node exists.
17
18         meshlink::node *self = mesh.get_self();
19         if(!self) {
20                 cerr << "Foo does not know about itself\n";
21                 return 1;
22         }
23         if(strcmp(self->name, "foo")) {
24                 cerr << "Foo thinks its name is " << self->name << "\n";
25                 return 1;
26         }
27
28         // Disable local discovery.
29
30         mesh.enable_discovery(false);
31
32         // Start and stop the mesh.
33
34         if(!mesh.start()) {
35                 cerr << "Foo could not start\n";
36                 return 1;
37         }
38         mesh.stop();
39
40         // Make sure we can start and stop the mesh again.
41
42         if(!mesh.start()) {
43                 cerr << "Foo could not start twice\n";
44                 return 1;
45         }
46         mesh.stop();
47
48         // Close the mesh and open it again, now with a different name parameter.
49
50         mesh.close();
51
52         // Check that the name is ignored now, and that we still are "foo".
53
54         mesh.open("basicpp_conf", "bar", "basicpp", DEV_CLASS_BACKBONE);
55
56         if(mesh.get_node("bar")) {
57                 cerr << "Foo knows about bar, it shouldn't\n";
58                 return 1;
59         }
60
61         self = mesh.get_self();
62         if(!self) {
63                 cerr << "Foo doesn't know about itself the second time\n";
64                 return 1;
65         }
66         if(strcmp(self->name, "foo")) {
67                 cerr << "Foo thinks its name is " << self->name << " the second time\n";
68                 return 1;
69         }
70
71         // Start and stop the mesh.
72
73         mesh.enable_discovery(false);
74
75         if(!mesh.start()) {
76                 cerr << "Foo could not start a third time\n";
77                 return 1;
78         }
79
80         mesh.stop();
81
82         if(!meshlink::destroy("basicpp_conf")) {
83                 cerr << "Could not destroy configuration\n";
84                 return 1;
85         }
86
87         if(!access("basic.conf", F_OK) || errno != ENOENT) {
88                 cerr << "Configuration not fully destroyed\n";
89                 return 1;
90         }
91
92         return 0;
93 }