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 channel_opened = {.mutex = PTHREAD_MUTEX_INITIALIZER, .cond = PTHREAD_COND_INITIALIZER, .flag = false};
49 static struct sync_flag channel_data_recieved = {.mutex = PTHREAD_MUTEX_INITIALIZER, .cond = PTHREAD_COND_INITIALIZER, .flag = false};
51 static void send_event(mesh_event_t event) {
54 for(attempts = 0; attempts < 5; attempts += 1) {
55 if(mesh_event_sock_send(client_id, event, NULL, 0)) {
65 static bool channel_accept(meshlink_handle_t *mesh, meshlink_channel_t *channel, uint16_t port, const void *dat, size_t len) {
69 assert(port == CHANNEL_PORT);
71 fprintf(stderr, "\tapp1node1 got channel request from %s\n", channel->node->name);
73 if(!strcmp(channel->node->name, "corenode1")) {
74 fprintf(stderr, "\tapp1node1 accepting channel request from %s at %lu\n", channel->node->name, time(NULL));
75 meshlink_set_channel_receive_cb(mesh, channel, channel_receive_cb);
79 } else if(!strcmp(channel->node->name, "app1node2")) {
80 fprintf(stderr, "\tapp1node1 accepting channel request from %s at %lu\n", channel->node->name, time(NULL));
81 meshlink_set_channel_receive_cb(mesh, channel, channel_receive_cb);
87 fprintf(stderr, "\tapp1node1 rejecting channel request from %s at %lu\n", channel->node->name, time(NULL));
91 /* channel receive callback */
92 static void channel_receive_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, const void *dat, size_t len) {
94 char *message = "Channel Message";
97 fprintf(stderr, "\tapp1node1 got error from %s at %lu\n", channel->node->name, time(NULL));
98 send_event(ERR_NETWORK);
102 memcpy(data, dat, len);
104 fprintf(stderr, "\tapp1node1 got message from %s as %s\n", channel->node->name, data);
106 if(!strcmp(channel->node->name, "corenode1")) {
107 if(!memcmp(dat, "Channel Message", len)) {
108 set_sync_flag(&channel_data_recieved, true);
109 } else if(!memcmp(dat, "failure", 7)) {
112 } else if(!strcmp(channel->node->name, "app1node2")) {
113 if(!memcmp(dat, "Channel Message", len)) {
114 assert(meshlink_channel_send(mesh, channel, message, strlen(message)) >= 0);
115 } else if(!memcmp(dat, "failure", 7)) {
123 static void poll_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, size_t len) {
124 char *message = "Channel Message";
125 char *node = (char *)channel->node->name;
127 meshlink_set_channel_poll_cb(mesh, channel, NULL);
128 fprintf(stderr, "\tapp1node1's Channel request has been accepted by %s at : %lu\n", node, time(NULL));
130 if(0 == strcmp("corenode1", node)) {
131 set_sync_flag(&channel_opened, true);
134 assert(meshlink_channel_send(mesh, channel, message, strlen(message)) >= 0);
139 static void node_status_cb(meshlink_handle_t *mesh, meshlink_node_t *node,
141 if(!strcasecmp(node->name, "corenode1")) {
143 fprintf(stderr, "\tNode corenode1 became reachable\n");
144 set_sync_flag(&peer_reachable, true);
151 void mesh_start_test_handler(int a) {
152 fprintf(stderr, "Starting test in app1node1\n");
153 set_sync_flag(&start_test, true);
156 int main(int argc, char *argv[]) {
157 struct timeval main_loop_wait = { 2, 0 };
158 meshlink_channel_t *channel = NULL;
159 meshlink_node_t *core_node = NULL;
161 fprintf(stderr, "\tMesh node 'app1node1' starting up........\n");
163 // Import mesh event handler
165 if((argv[CMD_LINE_ARG_CLIENTID]) && (argv[CMD_LINE_ARG_IMPORTSTR])) {
166 client_id = atoi(argv[CMD_LINE_ARG_CLIENTID]);
167 mesh_event_sock_connect(argv[CMD_LINE_ARG_IMPORTSTR]);
170 // Setup required signals
173 signal(SIGIO, mesh_start_test_handler);
175 // Run peer node instance
177 mesh = meshlink_open("app1node1conf", argv[CMD_LINE_ARG_NODENAME],
178 "test_channel_conn", atoi(argv[CMD_LINE_ARG_DEVCLASS]));
180 meshlink_set_log_cb(mesh, MESHLINK_DEBUG, meshlink_callback_logger);
181 meshlink_set_channel_accept_cb(mesh, channel_accept);
182 meshlink_set_node_status_cb(mesh, node_status_cb);
184 if(argv[CMD_LINE_ARG_INVITEURL]) {
185 assert(meshlink_join(mesh, argv[CMD_LINE_ARG_INVITEURL]));
188 assert(meshlink_start(mesh));
190 send_event(NODE_STARTED);
192 // Wait for peer node to join
194 assert(wait_sync_flag(&peer_reachable, 15));
195 send_event(NODE_JOINED);
197 while(false == wait_sync_flag(&start_test, 10));
199 // Open a channel to peer node
200 core_node = meshlink_get_node(mesh, "corenode1");
202 fprintf(stderr, "\tapp1node1 Sending Channel request to corenode1 at : %lu\n", time(NULL));
203 channel = meshlink_channel_open(mesh, core_node, CHANNEL_PORT,
204 channel_receive_cb, NULL, 0);
205 meshlink_set_channel_poll_cb(mesh, channel, poll_cb);
206 assert(wait_sync_flag(&channel_opened, 100));
207 send_event(CHANNEL_OPENED);
209 assert(wait_sync_flag(&channel_data_recieved, 100));
210 send_event(CHANNEL_DATA_RECIEVED);
212 // All test steps executed - wait for signals to stop/start or close the mesh
214 while(test_running) {
215 select(1, NULL, NULL, NULL, &main_loop_wait);
218 meshlink_close(mesh);