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 int client_id = -1;
40 static struct sync_flag channel_opened = {.mutex = PTHREAD_MUTEX_INITIALIZER, .cond = PTHREAD_COND_INITIALIZER, .flag = false};
41 static struct sync_flag channel_data_recieved = {.mutex = PTHREAD_MUTEX_INITIALIZER, .cond = PTHREAD_COND_INITIALIZER, .flag = false};
43 static meshlink_handle_t *mesh = NULL;
45 static void mesh_send_message_handler(const char *destination);
47 static void send_event(mesh_event_t event) {
50 for(attempts = 0; attempts < 5; attempts += 1) {
51 if(mesh_event_sock_send(client_id, event, NULL, 0)) {
61 /* channel receive callback */
62 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, bool reachable) {
93 fprintf(stderr, "Node %s became reachable\n", node->name);
95 fprintf(stderr, "Node %s is unreachable\n", node->name);
101 static void poll_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, size_t len) {
102 const char *message = "Channel Message";
103 const char *node = channel->node->name;
105 meshlink_set_channel_poll_cb(mesh, channel, NULL);
106 fprintf(stderr, "corenode1's Channel request has been accepted by %s at : %lu\n", node, time(NULL));
108 if(0 == strcmp("corenode2", node)) {
109 set_sync_flag(&channel_opened, true);
112 assert(meshlink_channel_send(mesh, channel, message, strlen(message)) >= 0);
116 /* channel receive callback */
117 static bool channel_accept(meshlink_handle_t *mesh, meshlink_channel_t *channel, uint16_t port, const void *dat, size_t len) {
121 assert(port == CHANNEL_PORT);
123 fprintf(stderr, "corenode1 got channel request from %s\n", channel->node->name);
124 meshlink_set_channel_receive_cb(mesh, channel, channel_receive_cb);
129 static void mesh_send_message_handler(const char *destination) {
130 meshlink_channel_t *channel = NULL;
131 meshlink_node_t *target_node = NULL;
133 // Open a channel to destination node
134 target_node = meshlink_get_node(mesh, destination);
136 fprintf(stderr, "corenode1 Sending Channel request to %s at : %lu\n", destination, time(NULL));
137 channel = meshlink_channel_open(mesh, target_node, CHANNEL_PORT,
138 channel_receive_cb, NULL, 0);
139 meshlink_set_channel_poll_cb(mesh, channel, poll_cb);
142 int main(int argc, char *argv[]) {
145 struct timeval main_loop_wait = { 5, 0 };
147 // Import mesh event handler
149 fprintf(stderr, "Mesh node 'corenode1' starting up........\n");
151 if((argv[CMD_LINE_ARG_CLIENTID]) && (argv[CMD_LINE_ARG_IMPORTSTR])) {
152 client_id = atoi(argv[CMD_LINE_ARG_CLIENTID]);
153 mesh_event_sock_connect(argv[CMD_LINE_ARG_IMPORTSTR]);
158 // Execute test steps
160 mesh = meshlink_open("testconf", argv[CMD_LINE_ARG_NODENAME],
161 "test_channel_conn", atoi(argv[CMD_LINE_ARG_DEVCLASS]));
163 meshlink_set_log_cb(mesh, MESHLINK_DEBUG, meshlink_callback_logger);
164 meshlink_set_channel_accept_cb(mesh, channel_accept);
165 meshlink_set_node_status_cb(mesh, node_status_cb);
167 if(argv[CMD_LINE_ARG_INVITEURL]) {
168 assert(meshlink_join(mesh, argv[CMD_LINE_ARG_INVITEURL]));
171 assert(meshlink_start(mesh));
173 send_event(NODE_STARTED);
175 assert(wait_sync_flag(&channel_opened, 10));
176 send_event(CHANNEL_OPENED);
178 assert(wait_sync_flag(&channel_data_recieved, 10));
179 send_event(CHANNEL_DATA_RECIEVED);
181 // All test steps executed - wait for signals to stop/start or close the mesh
183 while(test_running) {
184 select(1, NULL, NULL, NULL, &main_loop_wait);
187 meshlink_close(mesh);