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