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 channel_opened = {.mutex = PTHREAD_MUTEX_INITIALIZER, .cond = PTHREAD_COND_INITIALIZER, .flag = false};
54 static struct sync_flag channel_data_recieved = {.mutex = PTHREAD_MUTEX_INITIALIZER, .cond = PTHREAD_COND_INITIALIZER, .flag = false};
56 static void send_event(mesh_event_t event) {
59 for(attempts = 0; attempts < 5; attempts += 1) {
60 if(mesh_event_sock_send(client_id, event, NULL, 0)) {
70 static bool channel_accept(meshlink_handle_t *mesh, meshlink_channel_t *channel, uint16_t port, const void *dat, size_t len) {
74 assert(port == CHANNEL_PORT);
76 fprintf(stderr, "\tapp2node1 got channel request from %s\n", channel->node->name);
78 if(!strcmp(channel->node->name, "corenode1")) {
79 meshlink_set_channel_receive_cb(mesh, channel, channel_receive_cb);
83 } else if(!strcmp(channel->node->name, "app2node2")) {
84 fprintf(stderr, "\tapp2node1 accepting channel request from %s at %lu\n", channel->node->name, time(NULL));
85 meshlink_set_channel_receive_cb(mesh, channel, channel_receive_cb);
91 fprintf(stderr, "\tapp2node1 rejecting channel request from %s at %lu\n", channel->node->name, time(NULL));
96 /* channel receive callback */
97 static void channel_receive_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, const void *dat, size_t len) {
98 char *message = "Channel Message";
102 send_event(ERR_NETWORK);
106 memcpy(data, dat, len);
108 fprintf(stderr, "\tapp2node1 got message from %s as %s\n", channel->node->name, data);
110 if(!strcmp(channel->node->name, "corenode1")) {
111 if(!memcmp(dat, "Channel Message", len)) {
112 set_sync_flag(&channel_data_recieved, true);
113 } else if(!memcmp(dat, "failure", 7)) {
116 } else if(!strcmp(channel->node->name, "app2node2")) {
117 if(!memcmp(dat, "Channel Message", len)) {
118 assert(meshlink_channel_send(mesh, channel, message, strlen(message)) >= 0);
119 } else if(!memcmp(dat, "failure", 7)) {
127 static void poll_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, size_t len) {
128 char *message = "Channel Message";
129 char *node = (char *)channel->node->name;
131 meshlink_set_channel_poll_cb(mesh, channel, NULL);
132 fprintf(stderr, "\tapp2node1's Channel request has been accepted by corenode1 at : %lu\n", time(NULL));
134 if(0 == strcmp("corenode1", node)) {
135 set_sync_flag(&channel_opened, true);
138 assert(meshlink_channel_send(mesh, channel, message, strlen(message)) >= 0);
143 static void node_status_cb(meshlink_handle_t *mesh, meshlink_node_t *node, bool reachable) {
146 if(!strcasecmp(node->name, "corenode1")) {
148 fprintf(stderr, "\tNode corenode1 became reachable\n");
149 set_sync_flag(&peer_reachable, true);
156 void mesh_start_test_handler(int signum) {
159 fprintf(stderr, "Starting test in app2node1\n");
160 set_sync_flag(&start_test, true);
163 int main(int argc, char *argv[]) {
166 struct timeval main_loop_wait = { 2, 0 };
167 meshlink_channel_t *channel = NULL;
168 meshlink_node_t *core_node = NULL;
170 fprintf(stderr, "\tMesh node 'app2node1' starting up........\n");
172 // Import mesh event handler
174 if((argv[CMD_LINE_ARG_CLIENTID]) && (argv[CMD_LINE_ARG_IMPORTSTR])) {
175 client_id = atoi(argv[CMD_LINE_ARG_CLIENTID]);
176 mesh_event_sock_connect(argv[CMD_LINE_ARG_IMPORTSTR]);
179 // Setup required signals
182 signal(SIGIO, mesh_start_test_handler);
184 // Run peer node instance
186 mesh = meshlink_open("app2node1conf", argv[CMD_LINE_ARG_NODENAME],
187 "test_channel_conn", atoi(argv[CMD_LINE_ARG_DEVCLASS]));
189 meshlink_set_log_cb(mesh, MESHLINK_DEBUG, meshlink_callback_logger);
190 meshlink_set_channel_accept_cb(mesh, channel_accept);
191 meshlink_set_node_status_cb(mesh, node_status_cb);
193 if(argv[CMD_LINE_ARG_INVITEURL]) {
194 assert(meshlink_join(mesh, argv[CMD_LINE_ARG_INVITEURL]));
197 assert(meshlink_start(mesh));
199 send_event(NODE_STARTED);
201 // Wait for peer node to join
203 assert(wait_sync_flag(&peer_reachable, 15));
204 send_event(NODE_JOINED);
206 while(false == wait_sync_flag(&start_test, 10));
208 // Open a channel to peer node
209 core_node = meshlink_get_node(mesh, "corenode1");
211 fprintf(stderr, "\tapp2node1 Sending Channel request to corenode1 at : %lu\n", time(NULL));
212 channel = meshlink_channel_open(mesh, core_node, CHANNEL_PORT,
213 channel_receive_cb, NULL, 0);
214 meshlink_set_channel_poll_cb(mesh, channel, poll_cb);
215 assert(wait_sync_flag(&channel_opened, 15));
216 send_event(CHANNEL_OPENED);
218 assert(wait_sync_flag(&channel_data_recieved, 30));
219 send_event(CHANNEL_DATA_RECIEVED);
221 // All test steps executed - wait for signals to stop/start or close the mesh
223 while(test_running) {
224 select(1, NULL, NULL, NULL, &main_loop_wait);
227 meshlink_close(mesh);