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