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");
56 // Write data to stdout.
60 bool reject_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, uint16_t port, const void *data, size_t len) {
70 bool accept_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, uint16_t port, const void *data, size_t len) {
75 meshlink_set_channel_receive_cb(mesh, channel, bar_receive_cb);
78 bar_receive_cb(mesh, channel, data, len);
84 void poll_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, size_t len) {
87 meshlink_set_channel_poll_cb(mesh, channel, NULL);
94 meshlink_set_log_cb(NULL, debug_level, log_cb);
96 meshlink_handle_t *mesh1 = meshlink_open("echo-fork_conf.1", "foo", "echo-fork", DEV_CLASS_BACKBONE);
99 fprintf(stderr, "Could not initialize configuration for foo\n");
103 meshlink_set_log_cb(mesh1, debug_level, log_cb);
104 meshlink_set_channel_accept_cb(mesh1, reject_cb);
105 meshlink_set_node_status_cb(mesh1, status_cb);
107 if(!meshlink_start(mesh1)) {
108 fprintf(stderr, "Foo could not start\n");
112 for(int i = 0; i < 20; i++) {
121 fprintf(stderr, "Bar not reachable for foo after 20 seconds\n");
125 // Open a channel from foo to bar.
127 meshlink_node_t *bar = meshlink_get_node(mesh1, "bar");
130 fprintf(stderr, "Foo could not find bar\n");
134 meshlink_channel_t *channel = meshlink_channel_open(mesh1, bar, 7, foo_receive_cb, NULL, 0);
135 meshlink_set_channel_poll_cb(mesh1, channel, poll_cb);
137 // read and buffer stdin
138 int BUF_SIZE = 1024 * 1024;
139 char buffer[BUF_SIZE];
141 for(int i = 0; i < 5; i++) {
150 fprintf(stderr, "Bar did not respond to foo's channel message\n");
155 //fprintf(stderr, ":");
156 ssize_t len = read(0, buffer, BUF_SIZE);
165 ssize_t sent = meshlink_channel_send(mesh1, channel, p, len);
168 fprintf(stderr, "Sending message failed\n");
181 fprintf(stderr, "Foo finished sending\n");
183 meshlink_channel_close(mesh1, channel);
188 meshlink_close(mesh1);
199 meshlink_set_log_cb(NULL, debug_level, log_cb);
201 meshlink_handle_t *mesh2 = meshlink_open("echo-fork_conf.2", "bar", "echo-fork", DEV_CLASS_BACKBONE);
204 fprintf(stderr, "Could not initialize configuration for bar\n");
208 meshlink_set_log_cb(mesh2, debug_level, log_cb);
209 meshlink_set_channel_accept_cb(mesh2, accept_cb);
210 meshlink_set_node_status_cb(mesh2, status_cb);
212 if(!meshlink_start(mesh2)) {
213 fprintf(stderr, "Bar could not start\n");
223 meshlink_close(mesh2);
230 debug_level = getenv("DEBUG") ? MESHLINK_DEBUG : MESHLINK_ERROR;
232 // Initialize and exchange configuration.
234 meshlink_handle_t *foo = meshlink_open("echo-fork_conf.1", "foo", "echo-fork", DEV_CLASS_BACKBONE);
235 meshlink_handle_t *bar = meshlink_open("echo-fork_conf.2", "bar", "echo-fork", DEV_CLASS_BACKBONE);
236 meshlink_add_address(foo, "localhost");
237 meshlink_import(bar, meshlink_export(foo));
238 meshlink_import(foo, meshlink_export(bar));