]> git.meshlink.io Git - meshlink/blob - test/basicpp.cpp
Add a function to destroy a MeshLink instance.
[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(int argc, char *argv[]) {
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_node("foo");
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         // Start and stop the mesh.
29
30         if(!mesh.start()) {
31                 cerr << "Foo could not start\n";
32                 return 1;
33         }
34         mesh.stop();
35
36         // Make sure we can start and stop the mesh again.
37
38         if(!mesh.start()) {
39                 cerr << "Foo could not start twice\n";
40                 return 1;
41         }
42         mesh.stop();
43
44         // Close the mesh and open it again, now with a different name parameter.
45
46         mesh.close();
47
48         // Check that the name is ignored now, and that we still are "foo".
49
50         mesh.open("basic_conf", "bar", "basicpp", DEV_CLASS_BACKBONE);
51
52         if(mesh.get_node("bar")) {
53                 cerr << "Foo knows about bar, it shouldn't\n";
54                 return 1;
55         }
56
57         self = mesh.get_node("foo");
58         if(!self) {
59                 cerr << "Foo doesn't know about itself the second time\n";
60                 return 1;
61         }
62         if(strcmp(self->name, "foo")) {
63                 cerr << "Foo thinks its name is " << self->name << " the second time\n";
64                 return 1;
65         }
66
67         // Start and stop the mesh.
68
69         if(!mesh.start()) {
70                 cerr << "Foo could not start a third time\n";
71                 return 1;
72         }
73
74         mesh.stop();
75
76         if(!meshlink::destroy("basicpp_conf")) {
77                 cerr << "Could not destroy configuration\n";
78                 return 1;
79         }
80
81         if(!access("basic.conf", F_OK) || errno != ENOENT) {
82                 cerr << "Configuration not fully destroyed\n";
83                 return 1;
84         }
85
86         return 0;
87 }