8 #include "../src/meshlink.h"
11 static void log_cb(meshlink_handle_t *mesh, meshlink_log_level_t level, const char *text) {
12 static struct timeval tv0;
16 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);
23 fprintf(stderr, "(%s) ", mesh->name);
26 fprintf(stderr, "[%d] %s\n", level, text);
29 static bool accept_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, uint16_t port, const void *data, size_t len) {
39 static struct sync_flag poll_flag;
40 static size_t poll_len;
42 static void poll_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, size_t len) {
43 meshlink_set_channel_poll_cb(mesh, channel, NULL);
45 set_sync_flag(&poll_flag, true);
48 static struct sync_flag receive_flag;
49 static size_t receive_len;
51 static void receive_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, const void *data, size_t len) {
57 set_sync_flag(&receive_flag, true);
61 meshlink_set_log_cb(NULL, MESHLINK_DEBUG, log_cb);
63 // Open three meshlink instances.
65 meshlink_handle_t *mesh1 = meshlink_open("channels_failure_conf.1", "foo", "channels", DEV_CLASS_BACKBONE);
66 meshlink_handle_t *mesh2 = meshlink_open("channels_failure_conf.2", "bar", "channels", DEV_CLASS_BACKBONE);
71 meshlink_enable_discovery(mesh1, false);
72 meshlink_enable_discovery(mesh2, false);
74 meshlink_set_log_cb(mesh1, MESHLINK_DEBUG, log_cb);
75 meshlink_set_log_cb(mesh2, MESHLINK_DEBUG, log_cb);
77 // Import and export both side's data
79 meshlink_add_address(mesh1, "localhost");
80 meshlink_add_address(mesh2, "localhost");
82 char *data1 = meshlink_export(mesh1);
83 char *data2 = meshlink_export(mesh2);
88 assert(meshlink_import(mesh1, data2));
89 assert(meshlink_import(mesh2, data1));
96 meshlink_set_channel_accept_cb(mesh2, accept_cb);
98 // Open a channel from foo to bar
100 meshlink_node_t *bar = meshlink_get_node(mesh1, "bar");
103 meshlink_channel_t *channel = meshlink_channel_open(mesh1, bar, 7, receive_cb, NULL, 0);
106 meshlink_set_channel_poll_cb(mesh1, channel, poll_cb);
108 // Start both instances
110 assert(meshlink_start(mesh1));
111 assert(meshlink_start(mesh2));
113 // Wait for the channel to be established
115 assert(wait_sync_flag(&poll_flag, 10));
116 assert(poll_len != 0);
120 // Stop mesh2. We should get a notification that the channel has closed after a while.
122 meshlink_stop(mesh2);
124 assert(wait_sync_flag(&receive_flag, 70));
125 assert(receive_len == 0);
127 meshlink_channel_close(mesh1, channel);
129 // Try setting up a new channel while bar is still down.
131 poll_flag.flag = false;
132 receive_flag.flag = false;
134 channel = meshlink_channel_open(mesh1, bar, 7, NULL, NULL, 0);
137 meshlink_set_channel_poll_cb(mesh1, channel, poll_cb);
139 assert(wait_sync_flag(&poll_flag, 70));
140 assert(poll_len == 0);
144 meshlink_close(mesh1);
145 meshlink_close(mesh2);