7 #include "../src/meshlink.h"
9 volatile bool bar_reachable = false;
10 volatile bool bar_responded = false;
12 void log_cb(meshlink_handle_t *mesh, meshlink_log_level_t level, const char *text) {
13 static struct timeval tv0;
17 gettimeofday(&tv0, NULL);
20 gettimeofday(&tv, NULL);
21 fprintf(stderr, "%u.%.03u ", (unsigned int)(tv.tv_sec - tv0.tv_sec), (unsigned int)tv.tv_usec / 1000);
24 fprintf(stderr, "(%s) ", mesh->name);
27 fprintf(stderr, "[%d] %s\n", level, text);
30 void status_cb(meshlink_handle_t *mesh, meshlink_node_t *node, bool reachable) {
33 printf("status_cb: %s %sreachable\n", node->name, reachable ? "" : "un");
35 if(!strcmp(node->name, "bar")) {
36 bar_reachable = reachable;
40 void foo_receive_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, const void *data, size_t len) {
44 printf("foo_receive_cb %zu: ", len);
45 fwrite(data, 1, len, stdout);
48 if(len == 5 && !memcmp(data, "Hello", 5)) {
53 void bar_receive_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, const void *data, size_t len) {
54 printf("bar_receive_cb %zu: ", len);
55 fwrite(data, 1, len, stdout);
57 // Echo the data back.
58 meshlink_channel_send(mesh, channel, data, len);
61 bool reject_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, uint16_t port, const void *data, size_t len) {
71 bool accept_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, uint16_t port, const void *data, size_t len) {
72 printf("accept_cb: (from %s on port %u) ", channel->node->name, (unsigned int)port);
75 fwrite(data, 1, len, stdout);
84 meshlink_set_channel_receive_cb(mesh, channel, bar_receive_cb);
87 bar_receive_cb(mesh, channel, data, len);
93 void poll_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, size_t len) {
96 meshlink_set_channel_poll_cb(mesh, channel, NULL);
98 if(meshlink_channel_send(mesh, channel, "Hello", 5) != 5) {
99 fprintf(stderr, "Could not send whole message\n");
104 meshlink_set_log_cb(NULL, MESHLINK_DEBUG, log_cb);
106 // Open two new meshlink instance.
108 meshlink_handle_t *mesh1 = meshlink_open("channels_conf.1", "foo", "channels", DEV_CLASS_BACKBONE);
111 fprintf(stderr, "Could not initialize configuration for foo\n");
115 meshlink_handle_t *mesh2 = meshlink_open("channels_conf.2", "bar", "channels", DEV_CLASS_BACKBONE);
118 fprintf(stderr, "Could not initialize configuration for bar\n");
122 meshlink_enable_discovery(mesh1, false);
123 meshlink_enable_discovery(mesh2, false);
124 meshlink_set_log_cb(mesh1, MESHLINK_DEBUG, log_cb);
125 meshlink_set_log_cb(mesh2, MESHLINK_DEBUG, log_cb);
127 // Import and export both side's data
129 meshlink_add_address(mesh1, "localhost");
131 char *data = meshlink_export(mesh1);
134 fprintf(stderr, "Foo could not export its configuration\n");
138 if(!meshlink_import(mesh2, data)) {
139 fprintf(stderr, "Bar could not import foo's configuration\n");
145 data = meshlink_export(mesh2);
148 fprintf(stderr, "Bar could not export its configuration\n");
152 if(!meshlink_import(mesh1, data)) {
153 fprintf(stderr, "Foo could not import bar's configuration\n");
159 // Set the callbacks.
161 meshlink_set_channel_accept_cb(mesh1, reject_cb);
162 meshlink_set_channel_accept_cb(mesh2, accept_cb);
164 meshlink_set_node_status_cb(mesh1, status_cb);
166 // Start both instances
168 if(!meshlink_start(mesh1)) {
169 fprintf(stderr, "Foo could not start\n");
173 if(!meshlink_start(mesh2)) {
174 fprintf(stderr, "Bar could not start\n");
178 // Wait for the two to connect.
180 for(int i = 0; i < 20; i++) {
189 fprintf(stderr, "Bar not reachable for foo after 20 seconds\n");
193 // Open a channel from foo to bar.
195 meshlink_node_t *bar = meshlink_get_node(mesh1, "bar");
198 fprintf(stderr, "Foo could not find bar\n");
202 meshlink_channel_t *channel = meshlink_channel_open(mesh1, bar, 7, foo_receive_cb, NULL, 0);
203 meshlink_set_channel_poll_cb(mesh1, channel, poll_cb);
205 for(int i = 0; i < 20; i++) {
214 fprintf(stderr, "Bar did not respond to foo's channel message\n");
218 meshlink_channel_close(mesh1, channel);
222 meshlink_stop(mesh2);
223 meshlink_stop(mesh1);
224 meshlink_close(mesh2);
225 meshlink_close(mesh1);