]> git.meshlink.io Git - meshlink/blob - test/channels-failure.c
Update UTCP to correctly handle timeouts sending data.
[meshlink] / test / channels-failure.c
1 #include <stdio.h>
2 #include <unistd.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include <sys/time.h>
6 #include <assert.h>
7
8 #include "../src/meshlink.h"
9 #include "utils.h"
10
11 static void log_cb(meshlink_handle_t *mesh, meshlink_log_level_t level, const char *text) {
12         static struct timeval tv0;
13         struct timeval tv;
14
15         if(tv0.tv_sec == 0) {
16                 gettimeofday(&tv0, NULL);
17         }
18
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         }
25
26         fprintf(stderr, "[%d] %s\n", level, text);
27 }
28
29 static bool accept_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, uint16_t port, const void *data, size_t len) {
30         (void)mesh;
31         (void)channel;
32         (void)port;
33         (void)data;
34         (void)len;
35
36         return true;
37 }
38
39 static struct sync_flag poll_flag;
40 static size_t poll_len;
41
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);
44         poll_len = len;
45         set_sync_flag(&poll_flag, true);
46 }
47
48 static struct sync_flag receive_flag;
49 static size_t receive_len;
50
51 static void receive_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, const void *data, size_t len) {
52         (void)mesh;
53         (void)channel;
54         (void)data;
55
56         receive_len = len;
57         set_sync_flag(&receive_flag, true);
58 }
59
60 int main() {
61         meshlink_set_log_cb(NULL, MESHLINK_DEBUG, log_cb);
62
63         // Open three meshlink instances.
64
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);
67
68         assert(mesh1);
69         assert(mesh2);
70
71         meshlink_enable_discovery(mesh1, false);
72         meshlink_enable_discovery(mesh2, false);
73
74         meshlink_set_log_cb(mesh1, MESHLINK_DEBUG, log_cb);
75         meshlink_set_log_cb(mesh2, MESHLINK_DEBUG, log_cb);
76
77         // Import and export both side's data
78
79         meshlink_add_address(mesh1, "localhost");
80         meshlink_add_address(mesh2, "localhost");
81
82         char *data1 = meshlink_export(mesh1);
83         char *data2 = meshlink_export(mesh2);
84
85         assert(data1);
86         assert(data2);
87
88         assert(meshlink_import(mesh1, data2));
89         assert(meshlink_import(mesh2, data1));
90
91         free(data1);
92         free(data2);
93
94         // Set the callbacks.
95
96         meshlink_set_channel_accept_cb(mesh2, accept_cb);
97
98         // Open a channel from foo to bar
99
100         meshlink_node_t *bar = meshlink_get_node(mesh1, "bar");
101         assert(bar);
102
103         meshlink_channel_t *channel = meshlink_channel_open(mesh1, bar, 7, receive_cb, NULL, 0);
104         assert(channel);
105
106         meshlink_set_channel_poll_cb(mesh1, channel, poll_cb);
107
108         // Start both instances
109
110         assert(meshlink_start(mesh1));
111         assert(meshlink_start(mesh2));
112
113         // Wait for the channel to be established
114
115         assert(wait_sync_flag(&poll_flag, 10));
116         assert(poll_len != 0);
117
118         sleep(1);
119
120         // Stop mesh2, then try to send data to it. We should get a notification that the channel has closed after a while.
121
122         assert(!check_sync_flag(&receive_flag));
123
124         meshlink_stop(mesh2);
125
126         assert(meshlink_channel_send(mesh1, channel, "hello", 5) == 5);
127
128         assert(wait_sync_flag(&receive_flag, 70));
129         assert(receive_len == 0);
130
131         meshlink_channel_close(mesh1, channel);
132
133         // Try setting up a new channel while bar is still down.
134
135         poll_flag.flag = false;
136         receive_flag.flag = false;
137
138         channel = meshlink_channel_open(mesh1, bar, 7, NULL, NULL, 0);
139         assert(channel);
140
141         meshlink_set_channel_poll_cb(mesh1, channel, poll_cb);
142
143         assert(wait_sync_flag(&poll_flag, 70));
144         assert(poll_len == 0);
145
146         // Clean up.
147
148         meshlink_close(mesh1);
149         meshlink_close(mesh2);
150
151         return 0;
152 }