2 node_sim.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.
26 #include "../common/common_handlers.h"
27 #include "../common/test_step.h"
28 #include "../common/mesh_event_handler.h"
29 #include "../../utils.h"
31 #define CMD_LINE_ARG_NODENAME 1
32 #define CMD_LINE_ARG_DEVCLASS 2
33 #define CMD_LINE_ARG_CLIENTID 3
34 #define CMD_LINE_ARG_IMPORTSTR 4
35 #define CMD_LINE_ARG_INVITEURL 5
36 #define CHANNEL_PORT 1234
38 static bool conn_status = false;
39 static int client_id = -1;
41 static struct sync_flag peer_reachable = {.mutex = PTHREAD_MUTEX_INITIALIZER, .cond = PTHREAD_COND_INITIALIZER, .flag = false};
42 static struct sync_flag channel_opened = {.mutex = PTHREAD_MUTEX_INITIALIZER, .cond = PTHREAD_COND_INITIALIZER, .flag = false};
43 static struct sync_flag channel_data_recieved = {.mutex = PTHREAD_MUTEX_INITIALIZER, .cond = PTHREAD_COND_INITIALIZER, .flag = false};
45 static meshlink_handle_t *mesh = NULL;
47 static void mesh_send_message_handler(char *destination);
49 static void send_event(mesh_event_t event) {
52 for(attempts = 0; attempts < 5; attempts += 1) {
53 if(mesh_event_sock_send(client_id, event, NULL, 0)) {
63 /* channel receive callback */
64 static void channel_receive_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, const void *dat, size_t len) {
68 send_event(ERR_NETWORK);
72 memcpy(data, dat, len);
74 fprintf(stderr, "corenode1 got message from %s as %s\n", channel->node->name, data);
76 if(!memcmp(dat, "Channel Message", len)) {
77 mesh_send_message_handler(channel->node->name);
79 if(0 == strcmp("corenode2", channel->node->name)) {
80 set_sync_flag(&channel_data_recieved, true);
82 } else if(!memcmp(dat, "failure", 7)) {
89 static void node_status_cb(meshlink_handle_t *mesh, meshlink_node_t *node,
92 fprintf(stderr, "Node %s became reachable\n", node->name);
94 fprintf(stderr, "Node %s is unreachable\n", node->name);
100 static void poll_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, size_t len) {
101 char *message = "Channel Message";
102 char *node = channel->node->name;
104 meshlink_set_channel_poll_cb(mesh, channel, NULL);
105 fprintf(stderr, "corenode1's Channel request has been accepted by %s at : %lu\n", node, time(NULL));
107 if(0 == strcmp("corenode2", node)) {
108 set_sync_flag(&channel_opened, true);
111 assert(meshlink_channel_send(mesh, channel, message, strlen(message)) >= 0);
115 /* channel receive callback */
116 static bool channel_accept(meshlink_handle_t *mesh, meshlink_channel_t *channel, uint16_t port, const void *dat, size_t len) {
120 assert(port == CHANNEL_PORT);
122 fprintf(stderr, "corenode1 got channel request from %s\n", channel->node->name);
123 meshlink_set_channel_receive_cb(mesh, channel, channel_receive_cb);
128 void mesh_send_message_handler(char *destination) {
129 meshlink_channel_t *channel = NULL;
130 meshlink_node_t *target_node = NULL;
132 // Open a channel to destination node
133 target_node = meshlink_get_node(mesh, destination);
135 fprintf(stderr, "corenode1 Sending Channel request to %s at : %lu\n", destination, time(NULL));
136 channel = meshlink_channel_open(mesh, target_node, CHANNEL_PORT,
137 channel_receive_cb, NULL, 0);
138 meshlink_set_channel_poll_cb(mesh, channel, poll_cb);
141 int main(int argc, char *argv[]) {
142 struct timeval main_loop_wait = { 5, 0 };
145 // Import mesh event handler
147 fprintf(stderr, "Mesh node 'corenode1' starting up........\n");
149 if((argv[CMD_LINE_ARG_CLIENTID]) && (argv[CMD_LINE_ARG_IMPORTSTR])) {
150 client_id = atoi(argv[CMD_LINE_ARG_CLIENTID]);
151 mesh_event_sock_connect(argv[CMD_LINE_ARG_IMPORTSTR]);
156 // Execute test steps
158 mesh = meshlink_open("testconf", argv[CMD_LINE_ARG_NODENAME],
159 "test_channel_conn", atoi(argv[CMD_LINE_ARG_DEVCLASS]));
161 meshlink_set_log_cb(mesh, MESHLINK_DEBUG, meshlink_callback_logger);
162 meshlink_set_channel_accept_cb(mesh, channel_accept);
163 meshlink_set_node_status_cb(mesh, node_status_cb);
165 if(argv[CMD_LINE_ARG_INVITEURL]) {
166 assert(meshlink_join(mesh, argv[CMD_LINE_ARG_INVITEURL]));
169 assert(meshlink_start(mesh));
171 send_event(NODE_STARTED);
173 assert(wait_sync_flag(&channel_opened, 10));
174 send_event(CHANNEL_OPENED);
176 assert(wait_sync_flag(&channel_data_recieved, 10));
177 send_event(CHANNEL_DATA_RECIEVED);
179 // All test steps executed - wait for signals to stop/start or close the mesh
181 while(test_running) {
182 select(1, NULL, NULL, NULL, &main_loop_wait);
185 meshlink_close(mesh);