]> git.meshlink.io Git - meshlink/blob - test/trio2.c
Clean up resources in the test cases.
[meshlink] / test / trio2.c
1 #define _GNU_SOURCE
2
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <string.h>
6 #include <unistd.h>
7 #include <errno.h>
8 #include <assert.h>
9 #include <sys/time.h>
10
11 #include "meshlink.h"
12 #include "devtools.h"
13 #include "utils.h"
14
15 static void log_cb(meshlink_handle_t *mesh, meshlink_log_level_t level, const char *text) {
16         (void)mesh;
17
18         static struct timeval tv0;
19         struct timeval tv;
20
21         if(tv0.tv_sec == 0) {
22                 gettimeofday(&tv0, NULL);
23         }
24
25         gettimeofday(&tv, NULL);
26         fprintf(stderr, "%u.%.03u ", (unsigned int)(tv.tv_sec - tv0.tv_sec), (unsigned int)tv.tv_usec / 1000);
27
28         if(mesh) {
29                 fprintf(stderr, "(%s) ", mesh->name);
30         }
31
32         fprintf(stderr, "[%d] %s\n", level, text);
33 }
34
35 static bool received = false;
36
37 static void receive_cb(meshlink_handle_t *mesh, meshlink_node_t *source, const void *data, size_t len) {
38         (void)mesh;
39         (void)source;
40
41         fprintf(stderr, "RECEIVED SOMETHING\n");
42
43         if(len == 5 && !memcmp(data, "Hello", 5)) {
44                 received = true;
45         }
46 }
47
48 int main() {
49         // Create three instances.
50
51         const char *name[3] = {"foo", "bar", "baz"};
52         meshlink_handle_t *mesh[3];
53         char *data[3];
54
55         for(int i = 0; i < 3; i++) {
56                 char *path = NULL;
57                 assert(asprintf(&path, "trio2_conf.%d", i) != -1 && path);
58
59                 mesh[i] = meshlink_open(path, name[i], "trio2", DEV_CLASS_BACKBONE);
60                 assert(mesh[i]);
61                 free(path);
62
63                 meshlink_add_address(mesh[i], "localhost");
64
65                 data[i] = meshlink_export(mesh[i]);
66                 assert(data[i]);
67         }
68
69         meshlink_set_log_cb(mesh[1], MESHLINK_DEBUG, log_cb);
70
71         // first node knows the two other nodes
72
73         for(int i = 1; i < 3; i++) {
74                 assert(meshlink_import(mesh[i], data[0]));
75                 assert(meshlink_import(mesh[0], data[i]));
76
77                 assert(meshlink_get_node(mesh[i], name[0]));
78                 assert(meshlink_get_node(mesh[0], name[i]));
79         }
80
81         // second and third node should not know each other yet
82
83         assert(!meshlink_get_node(mesh[1], name[2]));
84         assert(!meshlink_get_node(mesh[2], name[1]));
85
86         // start the nodes
87
88         for(int i = 0; i < 3; i++) {
89                 free(data[i]);
90                 assert(meshlink_start(mesh[i]));
91         }
92
93         // the nodes should now learn about each other
94
95         assert_after(meshlink_get_node(mesh[1], name[2]), 5);
96         assert_after(meshlink_get_node(mesh[2], name[1]), 5);
97
98         // Check that the second and third node autoconnect to each other
99
100         devtool_edge_t *edges = NULL;
101         size_t nedges = 0;
102         assert_after((edges = devtool_get_all_edges(mesh[1], edges, &nedges), nedges == 3), 15);
103         free(edges);
104
105         // Stop the nodes nodes
106
107         for(int i = 0; i < 3; i++) {
108                 meshlink_stop(mesh[i]);
109         }
110
111         // Start just the other two nodes
112
113         meshlink_set_log_cb(mesh[1], MESHLINK_DEBUG, log_cb);
114
115         for(int i = 1; i < 3; i++) {
116                 assert(meshlink_start(mesh[i]));
117         }
118
119         assert(meshlink_get_node(mesh[1], name[2]));
120         assert(meshlink_get_node(mesh[2], name[1]));
121
122         // Communication should still be possible
123
124         received = false;
125         meshlink_set_receive_cb(mesh[1], receive_cb);
126         assert_after((meshlink_send(mesh[2], meshlink_get_node(mesh[2], name[1]), "Hello", 5), received), 25);
127
128         // Clean up.
129
130         for(int i = 0; i < 3; i++) {
131                 meshlink_close(mesh[i]);
132         }
133 }