]> git.meshlink.io Git - meshlink/blob - test/channels-aio-fd.c
Add support for AIO using filedescriptors.
[meshlink] / test / channels-aio-fd.c
1 #include <assert.h>
2 #include <stdio.h>
3 #include <unistd.h>
4 #include <stdlib.h>
5 #include <string.h>
6 #include <sys/time.h>
7 #include <limits.h>
8
9 #include "meshlink.h"
10 #include "utils.h"
11
12 static const size_t size = 1024 * 1024; // size of data to transfer
13 static const size_t nchannels = 4; // number of simultaneous channels
14
15 struct aio_info {
16         int callbacks;
17         size_t size;
18         struct timeval tv;
19         struct sync_flag flag;
20 };
21
22 struct channel_info {
23         FILE *file;
24         struct aio_info aio_infos[2];
25 };
26
27 static void aio_fd_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, int fd, size_t len, void *priv) {
28         (void)mesh;
29         (void)channel;
30         (void)fd;
31         (void)len;
32
33         struct aio_info *info = priv;
34         gettimeofday(&info->tv, NULL);
35         info->callbacks++;
36         info->size += len;
37         set_sync_flag(&info->flag, true);
38 }
39
40 static bool reject_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, uint16_t port, const void *data, size_t len) {
41         (void)mesh;
42         (void)channel;
43         (void)port;
44         (void)data;
45         (void)len;
46
47         return false;
48 }
49
50 static bool accept_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, uint16_t port, const void *data, size_t len) {
51         assert(port && port <= nchannels);
52         assert(!data);
53         assert(!len);
54
55         struct channel_info *infos = mesh->priv;
56         struct channel_info *info = &infos[port - 1];
57
58         assert(meshlink_channel_aio_fd_receive(mesh, channel, fileno(info->file), size / 4, aio_fd_cb, &info->aio_infos[0]));
59         assert(meshlink_channel_aio_fd_receive(mesh, channel, fileno(info->file), size - size / 4, aio_fd_cb, &info->aio_infos[1]));
60
61         return true;
62 }
63
64 int main(int argc, char *argv[]) {
65         (void)argc;
66         (void)argv;
67
68         // Prepare file
69
70         char *outdata = malloc(size);
71
72         assert(outdata);
73
74         for(size_t i = 0; i < size; i++) {
75                 // Human readable output
76                 outdata[i] = i % 96 ? i % 96 + 32 : '\n';
77         }
78
79         FILE *file = fopen("channels_aio_fd.in", "w");
80         assert(file);
81         assert(fwrite(outdata, size, 1, file) == 1);
82         assert(fclose(file) == 0);
83
84         struct channel_info in_infos[nchannels];
85         struct channel_info out_infos[nchannels];
86
87         memset(in_infos, 0, sizeof(in_infos));
88         memset(out_infos, 0, sizeof(out_infos));
89
90         for(size_t i = 0; i < nchannels; i++) {
91                 char filename[PATH_MAX];
92                 snprintf(filename, sizeof(filename), "channels_aio_fd.out%d", (int)i);
93                 in_infos[i].file = fopen(filename, "w");
94                 assert(in_infos[i].file);
95                 out_infos[i].file = fopen("channels_aio_fd.in", "r");
96                 assert(out_infos[i].file);
97         }
98
99         // Open two new meshlink instance.
100
101         meshlink_destroy("channels_aio_fd_conf.1");
102         meshlink_destroy("channels_aio_fd_conf.2");
103
104         meshlink_handle_t *mesh1 = meshlink_open("channels_aio_fd_conf.1", "foo", "channels", DEV_CLASS_BACKBONE);
105         assert(mesh1);
106
107         meshlink_handle_t *mesh2 = meshlink_open("channels_aio_fd_conf.2", "bar", "channels", DEV_CLASS_BACKBONE);
108         assert(mesh2);
109
110         mesh2->priv = in_infos;
111
112         meshlink_enable_discovery(mesh1, false);
113         meshlink_enable_discovery(mesh2, false);
114
115         // Import and export both side's data
116
117         meshlink_add_address(mesh1, "localhost");
118
119         char *data = meshlink_export(mesh1);
120         assert(data);
121         assert(meshlink_import(mesh2, data));
122         free(data);
123
124         data = meshlink_export(mesh2);
125         assert(data);
126         assert(meshlink_import(mesh1, data));
127         free(data);
128
129         // Set the callbacks.
130
131         meshlink_set_channel_accept_cb(mesh1, reject_cb);
132         meshlink_set_channel_accept_cb(mesh2, accept_cb);
133
134         // Start both instances
135
136         assert(meshlink_start(mesh1));
137         assert(meshlink_start(mesh2));
138
139         // Open channels from foo to bar.
140
141         meshlink_node_t *bar = meshlink_get_node(mesh1, "bar");
142         assert(bar);
143
144         meshlink_channel_t *channels[nchannels];
145
146         for(size_t i = 0; i < nchannels; i++) {
147                 channels[i] = meshlink_channel_open(mesh1, bar, i + 1, NULL, NULL, 0);
148                 assert(channels[i]);
149         }
150
151         // Send a large buffer of data on each channel.
152
153         for(size_t i = 0; i < nchannels; i++) {
154                 assert(meshlink_channel_aio_fd_send(mesh1, channels[i], fileno(out_infos[i].file), size / 3, aio_fd_cb, &out_infos[i].aio_infos[0]));
155                 assert(meshlink_channel_aio_fd_send(mesh1, channels[i], fileno(out_infos[i].file), size - size / 3, aio_fd_cb, &out_infos[i].aio_infos[1]));
156         }
157
158         // Wait for everyone to finish.
159
160         for(size_t i = 0; i < nchannels; i++) {
161                 assert(wait_sync_flag(&out_infos[i].aio_infos[0].flag, 10));
162                 assert(wait_sync_flag(&out_infos[i].aio_infos[1].flag, 10));
163                 assert(wait_sync_flag(&in_infos[i].aio_infos[0].flag, 10));
164                 assert(wait_sync_flag(&in_infos[i].aio_infos[1].flag, 10));
165         }
166
167         // Check that everything is correct.
168
169         for(size_t i = 0; i < nchannels; i++) {
170                 assert(fclose(in_infos[i].file) == 0);
171                 assert(fclose(out_infos[i].file) == 0);
172
173                 // One callback for each AIO buffer.
174                 assert(out_infos[i].aio_infos[0].callbacks == 1);
175                 assert(out_infos[i].aio_infos[1].callbacks == 1);
176                 assert(in_infos[i].aio_infos[0].callbacks == 1);
177                 assert(in_infos[i].aio_infos[1].callbacks == 1);
178
179                 // Correct size sent and received.
180                 assert(out_infos[i].aio_infos[0].size == size / 3);
181                 assert(out_infos[i].aio_infos[1].size == size - size / 3);
182                 assert(in_infos[i].aio_infos[0].size == size / 4);
183                 assert(in_infos[i].aio_infos[1].size == size - size / 4);
184
185                 // First batch of data should all be sent and received before the second batch
186                 for(size_t j = 0; j < nchannels; j++) {
187                         assert(timercmp(&out_infos[i].aio_infos[0].tv, &out_infos[j].aio_infos[1].tv, <=));
188                         assert(timercmp(&in_infos[i].aio_infos[0].tv, &in_infos[j].aio_infos[1].tv, <=));
189                 }
190
191                 // Files should be identical
192                 char command[PATH_MAX];
193                 snprintf(command, sizeof(command), "cmp channels_aio_fd.in channels_aio_fd.out%d", (int)i);
194                 assert(system(command) == 0);
195
196         }
197
198         // Clean up.
199
200         meshlink_close(mesh2);
201         meshlink_close(mesh1);
202
203         return 0;
204 }