]> git.meshlink.io Git - meshlink/blob - test/basicpp.cpp
34c0bacbf2a7b45d13b227a99378d3bbb0af67a9
[meshlink] / test / basicpp.cpp
1 #include <cstring>
2 #include <iostream>
3
4 #include "meshlink++.h"
5
6 using namespace std;
7
8 int main(int argc, char *argv[]) {
9         // Open a new meshlink instance.
10
11         meshlink::mesh mesh;
12         mesh.open("basicpp_conf", "foo", "basicpp", DEV_CLASS_BACKBONE);
13
14         // Check that our own node exists.
15
16         meshlink::node *self = mesh.get_node("foo");
17         if(!self) {
18                 cerr << "Foo does not know about itself\n";
19                 return 1;
20         }
21         if(strcmp(self->name, "foo")) {
22                 cerr << "Foo thinks its name is " << self->name << "\n";
23                 return 1;
24         }
25
26         // Start and stop the mesh.
27
28         if(!mesh.start()) {
29                 cerr << "Foo could not start\n";
30                 return 1;
31         }
32         mesh.stop();
33
34         // Make sure we can start and stop the mesh again.
35
36         if(!mesh.start()) {
37                 cerr << "Foo could not start twice\n";
38                 return 1;
39         }
40         mesh.stop();
41
42         // Close the mesh and open it again, now with a different name parameter.
43
44         mesh.close();
45
46         // Check that the name is ignored now, and that we still are "foo".
47
48         mesh.open("basic_conf", "bar", "basicpp", DEV_CLASS_BACKBONE);
49
50         if(mesh.get_node("bar")) {
51                 cerr << "Foo knows about bar, it shouldn't\n";
52                 return 1;
53         }
54
55         self = mesh.get_node("foo");
56         if(!self) {
57                 cerr << "Foo doesn't know about itself the second time\n";
58                 return 1;
59         }
60         if(strcmp(self->name, "foo")) {
61                 cerr << "Foo thinks its name is " << self->name << " the second time\n";
62                 return 1;
63         }
64
65         // Start and stop the mesh.
66
67         if(!mesh.start()) {
68                 cerr << "Foo could not start a third time\n";
69                 return 1;
70         }
71
72         mesh.stop();
73
74         return 0;
75 }