7 #include "../src/meshlink.h"
10 * To run this test case, direct a large file to strd
13 volatile bool bar_reachable = false;
14 volatile bool bar_responded = false;
15 volatile bool foo_closed = false;
18 void log_cb(meshlink_handle_t *mesh, meshlink_log_level_t level, const char *text) {
20 fprintf(stderr, "(%s) ", mesh->name);
23 fprintf(stderr, "[%d] %s\n", level, text);
26 void status_cb(meshlink_handle_t *mesh, meshlink_node_t *node, bool reachable) {
29 if(!strcmp(node->name, "bar")) {
30 bar_reachable = reachable;
31 } else if(!strcmp(node->name, "foo"))
37 void foo_receive_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, const void *data, size_t len) {
46 void bar_receive_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, const void *data, size_t len) {
51 fprintf(stderr, "Connection closed by foo\n");
58 // Write data to stdout.
59 ret_val = write(1, data, len);
62 bool reject_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, uint16_t port, const void *data, size_t len) {
72 bool accept_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, uint16_t port, const void *data, size_t len) {
77 meshlink_set_channel_receive_cb(mesh, channel, bar_receive_cb);
80 bar_receive_cb(mesh, channel, data, len);
86 void poll_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, size_t len) {
89 meshlink_set_channel_poll_cb(mesh, channel, NULL);
96 meshlink_set_log_cb(NULL, debug_level, log_cb);
98 meshlink_handle_t *mesh1 = meshlink_open("echo-fork_conf.1", "foo", "echo-fork", DEV_CLASS_BACKBONE);
101 fprintf(stderr, "Could not initialize configuration for foo\n");
105 meshlink_set_log_cb(mesh1, debug_level, log_cb);
106 meshlink_set_channel_accept_cb(mesh1, reject_cb);
107 meshlink_set_node_status_cb(mesh1, status_cb);
109 if(!meshlink_start(mesh1)) {
110 fprintf(stderr, "Foo could not start\n");
114 for(int i = 0; i < 20; i++) {
123 fprintf(stderr, "Bar not reachable for foo after 20 seconds\n");
127 // Open a channel from foo to bar.
129 meshlink_node_t *bar = meshlink_get_node(mesh1, "bar");
132 fprintf(stderr, "Foo could not find bar\n");
136 meshlink_channel_t *channel = meshlink_channel_open(mesh1, bar, 7, foo_receive_cb, NULL, 0);
137 meshlink_set_channel_poll_cb(mesh1, channel, poll_cb);
139 // read and buffer stdin
140 int BUF_SIZE = 1024 * 1024;
141 char buffer[BUF_SIZE];
143 for(int i = 0; i < 5; i++) {
152 fprintf(stderr, "Bar did not respond to foo's channel message\n");
157 //fprintf(stderr, ":");
158 ssize_t len = read(0, buffer, BUF_SIZE);
167 ssize_t sent = meshlink_channel_send(mesh1, channel, p, len);
170 fprintf(stderr, "Sending message failed\n");
183 fprintf(stderr, "Foo finished sending\n");
185 meshlink_channel_close(mesh1, channel);
190 meshlink_close(mesh1);
201 meshlink_set_log_cb(NULL, debug_level, log_cb);
203 meshlink_handle_t *mesh2 = meshlink_open("echo-fork_conf.2", "bar", "echo-fork", DEV_CLASS_BACKBONE);
206 fprintf(stderr, "Could not initialize configuration for bar\n");
210 meshlink_set_log_cb(mesh2, debug_level, log_cb);
211 meshlink_set_channel_accept_cb(mesh2, accept_cb);
212 meshlink_set_node_status_cb(mesh2, status_cb);
214 if(!meshlink_start(mesh2)) {
215 fprintf(stderr, "Bar could not start\n");
225 meshlink_close(mesh2);
232 debug_level = getenv("DEBUG") ? MESHLINK_DEBUG : MESHLINK_ERROR;
234 // Initialize and exchange configuration.
236 meshlink_handle_t *foo = meshlink_open("echo-fork_conf.1", "foo", "echo-fork", DEV_CLASS_BACKBONE);
237 meshlink_handle_t *bar = meshlink_open("echo-fork_conf.2", "bar", "echo-fork", DEV_CLASS_BACKBONE);
238 meshlink_add_address(foo, "localhost");
239 meshlink_import(bar, meshlink_export(foo));
240 meshlink_import(foo, meshlink_export(bar));