2 node_sim_peer.c -- Implementation of Node Simulation for Meshlink Testing
3 for meta connection test case 01 - re-connection of
4 two nodes when relay node goes down
5 Copyright (C) 2018 Guus Sliepen <guus@meshlink.io>
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License along
18 with this program; if not, write to the Free Software Foundation, Inc.,
19 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
33 #include "../common/common_handlers.h"
34 #include "../common/test_step.h"
35 #include "../common/mesh_event_handler.h"
36 #include "../../utils.h"
38 #define CMD_LINE_ARG_NODENAME 1
39 #define CMD_LINE_ARG_DEVCLASS 2
40 #define CMD_LINE_ARG_CLIENTID 3
41 #define CMD_LINE_ARG_IMPORTSTR 4
42 #define CMD_LINE_ARG_INVITEURL 5
43 #define CHANNEL_PORT 1234
45 static void channel_receive_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, const void *dat, size_t len);
46 static bool channel_accept(meshlink_handle_t *mesh, meshlink_channel_t *channel, uint16_t port, const void *dat, size_t len);
48 static int client_id = -1;
49 static meshlink_handle_t *mesh = NULL;
51 static struct sync_flag peer_reachable = {.mutex = PTHREAD_MUTEX_INITIALIZER, .cond = PTHREAD_COND_INITIALIZER, .flag = false};
52 static struct sync_flag start_test = {.mutex = PTHREAD_MUTEX_INITIALIZER, .cond = PTHREAD_COND_INITIALIZER, .flag = false};
53 static struct sync_flag app_reachable = {.mutex = PTHREAD_MUTEX_INITIALIZER, .cond = PTHREAD_COND_INITIALIZER, .flag = false};
54 static struct sync_flag channel_opened = {.mutex = PTHREAD_MUTEX_INITIALIZER, .cond = PTHREAD_COND_INITIALIZER, .flag = false};
55 static struct sync_flag channel_data_recieved = {.mutex = PTHREAD_MUTEX_INITIALIZER, .cond = PTHREAD_COND_INITIALIZER, .flag = false};
57 static void send_event(mesh_event_t event) {
60 for(attempts = 0; attempts < 5; attempts += 1) {
61 if(mesh_event_sock_send(client_id, event, NULL, 0)) {
71 static bool channel_accept(meshlink_handle_t *mesh, meshlink_channel_t *channel, uint16_t port, const void *dat, size_t len) {
75 assert(port == CHANNEL_PORT);
77 fprintf(stderr, "\tapp2node2 got channel request from %s\n", channel->node->name);
79 if(!strcmp(channel->node->name, "corenode1")) {
80 meshlink_set_channel_receive_cb(mesh, channel, channel_receive_cb);
89 /* channel receive callback */
90 static void channel_receive_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, const void *dat, size_t len) {
96 fprintf(stderr, "\tapp2node2 got error from %s at %lu\n", channel->node->name, time(NULL));
97 send_event(ERR_NETWORK);
101 memcpy(data, dat, len);
103 fprintf(stderr, "\tapp2node2 got message from %s as %s\n", channel->node->name, data);
105 if(!strcmp(channel->node->name, "corenode1")) {
106 if(!memcmp(dat, "Channel Message", len)) {
107 set_sync_flag(&channel_data_recieved, true);
108 } else if(!memcmp(dat, "failure", 7)) {
111 } else if(!strcmp(channel->node->name, "app2node1")) {
112 if(!memcmp(dat, "Channel Message", len)) {
113 set_sync_flag(&channel_data_recieved, true);
114 } else if(!memcmp(dat, "failure", 7)) {
124 static void poll_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, size_t len) {
125 char *message = "Channel Message";
126 char *node = (char *)channel->node->name;
128 meshlink_set_channel_poll_cb(mesh, channel, NULL);
129 fprintf(stderr, "\tapp2node2's Channel request has been accepted by %s at : %lu\n", node, time(NULL));
130 set_sync_flag(&channel_opened, true);
131 assert(meshlink_channel_send(mesh, channel, message, strlen(message)) >= 0);
136 static void node_status_cb(meshlink_handle_t *mesh, meshlink_node_t *node, bool reachable) {
139 if(!strcasecmp(node->name, "corenode1")) {
141 fprintf(stderr, "\tNode corenode1 became reachable\n");
142 set_sync_flag(&peer_reachable, true);
144 } else if(!strcasecmp(node->name, "app2node1")) {
146 fprintf(stderr, "\tNode app2node1 became reachable\n");
147 set_sync_flag(&app_reachable, true);
154 void mesh_start_test_handler(int signum) {
157 fprintf(stderr, "Starting test in app2node2\n");
158 set_sync_flag(&start_test, true);
161 int main(int argc, char *argv[]) {
165 struct timeval main_loop_wait = { 2, 0 };
166 meshlink_channel_t *channel = NULL;
167 meshlink_node_t *core_node = NULL;
168 meshlink_node_t **node_handles = NULL;
169 meshlink_submesh_t *submesh = NULL;
171 fprintf(stderr, "\tMesh node 'app2node2' starting up........\n");
173 // Import mesh event handler
175 if((argv[CMD_LINE_ARG_CLIENTID]) && (argv[CMD_LINE_ARG_IMPORTSTR])) {
176 client_id = atoi(argv[CMD_LINE_ARG_CLIENTID]);
177 mesh_event_sock_connect(argv[CMD_LINE_ARG_IMPORTSTR]);
180 // Setup required signals
183 signal(SIGIO, mesh_start_test_handler);
185 // Run peer node instance
187 mesh = meshlink_open("app2node2conf", argv[CMD_LINE_ARG_NODENAME],
188 "test_channel_conn", atoi(argv[CMD_LINE_ARG_DEVCLASS]));
190 meshlink_set_log_cb(mesh, MESHLINK_DEBUG, meshlink_callback_logger);
191 meshlink_set_channel_accept_cb(mesh, channel_accept);
192 meshlink_set_node_status_cb(mesh, node_status_cb);
194 if(argv[CMD_LINE_ARG_INVITEURL]) {
195 assert(meshlink_join(mesh, argv[CMD_LINE_ARG_INVITEURL]));
198 assert(meshlink_start(mesh));
200 send_event(NODE_STARTED);
202 // Wait for peer node to join
204 assert(wait_sync_flag(&peer_reachable, 15));
205 send_event(NODE_JOINED);
207 while(false == wait_sync_flag(&start_test, 10));
209 // Open a channel to peer node
210 core_node = meshlink_get_node(mesh, "corenode1");
212 fprintf(stderr, "\tapp2node2 Sending Channel request to corenode1 at : %lu\n", time(NULL));
213 channel = meshlink_channel_open(mesh, core_node, CHANNEL_PORT,
214 channel_receive_cb, NULL, 0);
215 meshlink_set_channel_poll_cb(mesh, channel, poll_cb);
216 assert(wait_sync_flag(&channel_opened, 30));
217 send_event(CHANNEL_OPENED);
219 assert(wait_sync_flag(&channel_data_recieved, 30));
220 send_event(CHANNEL_DATA_RECIEVED);
222 // Open a channel to peer node
223 channel_opened.flag = false;
224 channel_data_recieved.flag = false;
226 assert(wait_sync_flag(&app_reachable, 60));
228 core_node = meshlink_get_node(mesh, "app2node1");
230 fprintf(stderr, "\tapp2node2 Sending Channel request to app2node1 at : %lu\n", time(NULL));
231 channel = meshlink_channel_open(mesh, core_node, CHANNEL_PORT,
232 channel_receive_cb, NULL, 0);
233 meshlink_set_channel_poll_cb(mesh, channel, poll_cb);
234 assert(wait_sync_flag(&channel_opened, 15));
235 send_event(CHANNEL_OPENED);
237 assert(wait_sync_flag(&channel_data_recieved, 30));
238 send_event(CHANNEL_DATA_RECIEVED);
241 node_handles = meshlink_get_all_nodes(mesh, NULL, &num_nodes);
242 fprintf(stderr, "\tGot %d nodes in list with error : %s\n", (int)num_nodes, meshlink_strerror(meshlink_errno));
243 assert(node_handles);
244 assert((num_nodes == 4));
246 for(i = 0; i < num_nodes; i++) {
247 fprintf(stderr, "\tChecking the node : %s\n", node_handles[i]->name);
249 if(0 == strcmp(node_handles[i]->name, "app1node1")) {
250 send_event(SIG_ABORT);
252 } else if(0 == strcmp(node_handles[i]->name, "app1node2")) {
253 send_event(SIG_ABORT);
258 meshlink_node_t *node = meshlink_get_self(mesh);
260 submesh = meshlink_get_node_submesh(mesh, node);
263 node_handles = meshlink_get_all_nodes_by_submesh(mesh, submesh, node_handles, &num_nodes);
264 assert(node_handles);
265 assert((num_nodes == 2));
267 for(i = 0; i < num_nodes; i++) {
268 fprintf(stderr, "\tChecking the node : %s\n", node_handles[i]->name);
270 if((0 == strcmp(node_handles[i]->name, "app1node1")) || (0 == strcmp(node_handles[i]->name, "app1node2"))) {
271 send_event(SIG_ABORT);
276 submesh = meshlink_get_submesh(mesh, "app2");
278 if(submesh == NULL) {
279 fprintf(stderr, "\tapp2node2 Got invalid submesh handle\n");
280 send_event(ERR_NETWORK);
283 submesh = meshlink_get_submesh(mesh, "app1");
285 if(submesh != NULL) {
286 fprintf(stderr, "\tapp2node2 Submesh handle should be NULL\n");
287 send_event(ERR_NETWORK);
290 send_event(MESH_EVENT_COMPLETED);
292 // All test steps executed - wait for signals to stop/start or close the mesh
294 while(test_running) {
295 select(1, NULL, NULL, NULL, &main_loop_wait);
298 meshlink_close(mesh);