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