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.
28 #include "../common/common_handlers.h"
29 #include "../common/test_step.h"
30 #include "../common/mesh_event_handler.h"
31 #include "../../utils.h"
33 #define CMD_LINE_ARG_NODENAME 1
34 #define CMD_LINE_ARG_DEVCLASS 2
35 #define CMD_LINE_ARG_CLIENTID 3
36 #define CMD_LINE_ARG_IMPORTSTR 4
37 #define CMD_LINE_ARG_INVITEURL 5
38 #define CHANNEL_PORT 1234
40 static void channel_receive_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, const void *dat, size_t len);
41 static bool channel_accept(meshlink_handle_t *mesh, meshlink_channel_t *channel, uint16_t port, const void *dat, size_t len);
43 static int client_id = -1;
44 static meshlink_handle_t *mesh = NULL;
46 static struct sync_flag peer_reachable = {.mutex = PTHREAD_MUTEX_INITIALIZER, .cond = PTHREAD_COND_INITIALIZER, .flag = false};
47 static struct sync_flag start_test = {.mutex = PTHREAD_MUTEX_INITIALIZER, .cond = PTHREAD_COND_INITIALIZER, .flag = false};
48 static struct sync_flag app_reachable = {.mutex = PTHREAD_MUTEX_INITIALIZER, .cond = PTHREAD_COND_INITIALIZER, .flag = false};
49 static struct sync_flag channel_opened = {.mutex = PTHREAD_MUTEX_INITIALIZER, .cond = PTHREAD_COND_INITIALIZER, .flag = false};
50 static struct sync_flag channel_data_recieved = {.mutex = PTHREAD_MUTEX_INITIALIZER, .cond = PTHREAD_COND_INITIALIZER, .flag = false};
52 static void send_event(mesh_event_t event) {
55 for(attempts = 0; attempts < 5; attempts += 1) {
56 if(mesh_event_sock_send(client_id, event, NULL, 0)) {
66 static bool channel_accept(meshlink_handle_t *mesh, meshlink_channel_t *channel, uint16_t port, const void *dat, size_t len) {
70 assert(port == CHANNEL_PORT);
72 fprintf(stderr, "\tapp2node2 got channel request from %s\n", channel->node->name);
74 if(!strcmp(channel->node->name, "corenode1")) {
75 meshlink_set_channel_receive_cb(mesh, channel, channel_receive_cb);
84 /* channel receive callback */
85 static void channel_receive_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, const void *dat, size_t len) {
89 fprintf(stderr, "\tapp2node2 got error from %s at %lu\n", channel->node->name, time(NULL));
90 send_event(ERR_NETWORK);
94 memcpy(data, dat, len);
96 fprintf(stderr, "\tapp2node2 got message from %s as %s\n", channel->node->name, data);
98 if(!strcmp(channel->node->name, "corenode1")) {
99 if(!memcmp(dat, "Channel Message", len)) {
100 set_sync_flag(&channel_data_recieved, true);
101 } else if(!memcmp(dat, "failure", 7)) {
104 } else if(!strcmp(channel->node->name, "app2node1")) {
105 if(!memcmp(dat, "Channel Message", len)) {
106 set_sync_flag(&channel_data_recieved, true);
107 } else if(!memcmp(dat, "failure", 7)) {
117 static void poll_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, size_t len) {
118 char *message = "Channel Message";
119 char *node = (char *)channel->node->name;
121 meshlink_set_channel_poll_cb(mesh, channel, NULL);
122 fprintf(stderr, "\tapp2node2's Channel request has been accepted by %s at : %lu\n", node, time(NULL));
123 set_sync_flag(&channel_opened, true);
124 assert(meshlink_channel_send(mesh, channel, message, strlen(message)) >= 0);
129 static void node_status_cb(meshlink_handle_t *mesh, meshlink_node_t *node,
131 if(!strcasecmp(node->name, "corenode1")) {
133 fprintf(stderr, "\tNode corenode1 became reachable\n");
134 set_sync_flag(&peer_reachable, true);
136 } else if(!strcasecmp(node->name, "app2node1")) {
138 fprintf(stderr, "\tNode app2node1 became reachable\n");
139 set_sync_flag(&app_reachable, true);
146 void mesh_start_test_handler(int a) {
147 fprintf(stderr, "Starting test in app2node2\n");
148 set_sync_flag(&start_test, true);
151 int main(int argc, char *argv[]) {
153 struct timeval main_loop_wait = { 2, 0 };
154 meshlink_channel_t *channel = NULL;
155 meshlink_node_t *core_node = NULL;
156 meshlink_node_t **node_handles = NULL;
157 meshlink_submesh_t *submesh = NULL;
159 fprintf(stderr, "\tMesh node 'app2node2' starting up........\n");
161 // Import mesh event handler
163 if((argv[CMD_LINE_ARG_CLIENTID]) && (argv[CMD_LINE_ARG_IMPORTSTR])) {
164 client_id = atoi(argv[CMD_LINE_ARG_CLIENTID]);
165 mesh_event_sock_connect(argv[CMD_LINE_ARG_IMPORTSTR]);
168 // Setup required signals
171 signal(SIGIO, mesh_start_test_handler);
173 // Run peer node instance
175 mesh = meshlink_open("app2node2conf", argv[CMD_LINE_ARG_NODENAME],
176 "test_channel_conn", atoi(argv[CMD_LINE_ARG_DEVCLASS]));
178 meshlink_set_log_cb(mesh, MESHLINK_DEBUG, meshlink_callback_logger);
179 meshlink_set_channel_accept_cb(mesh, channel_accept);
180 meshlink_set_node_status_cb(mesh, node_status_cb);
182 if(argv[CMD_LINE_ARG_INVITEURL]) {
183 assert(meshlink_join(mesh, argv[CMD_LINE_ARG_INVITEURL]));
186 assert(meshlink_start(mesh));
188 send_event(NODE_STARTED);
190 // Wait for peer node to join
192 assert(wait_sync_flag(&peer_reachable, 15));
193 send_event(NODE_JOINED);
195 while(false == wait_sync_flag(&start_test, 10));
197 // Open a channel to peer node
198 core_node = meshlink_get_node(mesh, "corenode1");
200 fprintf(stderr, "\tapp2node2 Sending Channel request to corenode1 at : %lu\n", time(NULL));
201 channel = meshlink_channel_open(mesh, core_node, CHANNEL_PORT,
202 channel_receive_cb, NULL, 0);
203 meshlink_set_channel_poll_cb(mesh, channel, poll_cb);
204 assert(wait_sync_flag(&channel_opened, 30));
205 send_event(CHANNEL_OPENED);
207 assert(wait_sync_flag(&channel_data_recieved, 30));
208 send_event(CHANNEL_DATA_RECIEVED);
210 // Open a channel to peer node
211 channel_opened.flag = false;
212 channel_data_recieved.flag = false;
214 assert(wait_sync_flag(&app_reachable, 60));
216 core_node = meshlink_get_node(mesh, "app2node1");
218 fprintf(stderr, "\tapp2node2 Sending Channel request to app2node1 at : %lu\n", time(NULL));
219 channel = meshlink_channel_open(mesh, core_node, CHANNEL_PORT,
220 channel_receive_cb, NULL, 0);
221 meshlink_set_channel_poll_cb(mesh, channel, poll_cb);
222 assert(wait_sync_flag(&channel_opened, 15));
223 send_event(CHANNEL_OPENED);
225 assert(wait_sync_flag(&channel_data_recieved, 30));
226 send_event(CHANNEL_DATA_RECIEVED);
229 node_handles = meshlink_get_all_nodes(mesh, NULL, &num_nodes);
230 fprintf(stderr, "\tGot %d nodes in list with error : %s\n", num_nodes, meshlink_strerror(meshlink_errno));
231 assert(node_handles);
232 assert((num_nodes == 4));
234 for(i = 0; i < num_nodes; i++) {
235 fprintf(stderr, "\tChecking the node : %s\n", node_handles[i]->name);
237 if(0 == strcmp(node_handles[i]->name, "app1node1")) {
238 send_event(SIG_ABORT);
240 } else if(0 == strcmp(node_handles[i]->name, "app1node2")) {
241 send_event(SIG_ABORT);
246 meshlink_node_t *node = meshlink_get_self(mesh);
248 submesh = meshlink_get_node_submesh(mesh, node);
251 node_handles = meshlink_get_all_nodes_by_submesh(mesh, submesh, node_handles, &num_nodes);
252 assert(node_handles);
253 assert((num_nodes == 2));
255 for(i = 0; i < num_nodes; i++) {
256 fprintf(stderr, "\tChecking the node : %s\n", node_handles[i]->name);
258 if((0 == strcmp(node_handles[i]->name, "app1node1")) || (0 == strcmp(node_handles[i]->name, "app1node2"))) {
259 send_event(SIG_ABORT);
264 submesh = meshlink_get_submesh(mesh, "app2");
266 if(submesh == NULL) {
267 fprintf(stderr, "\tapp2node2 Got invalid submesh handle\n");
268 send_event(ERR_NETWORK);
271 submesh = meshlink_get_submesh(mesh, "app1");
273 if(submesh != NULL) {
274 fprintf(stderr, "\tapp2node2 Submesh handle should be NULL\n");
275 send_event(ERR_NETWORK);
278 send_event(MESH_EVENT_COMPLETED);
280 // All test steps executed - wait for signals to stop/start or close the mesh
282 while(test_running) {
283 select(1, NULL, NULL, NULL, &main_loop_wait);
286 meshlink_close(mesh);