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.
25 #include "../common/common_handlers.h"
26 #include "../common/test_step.h"
27 #include "../common/mesh_event_handler.h"
29 #define CMD_LINE_ARG_NODENAME 1
30 #define CMD_LINE_ARG_DEVCLASS 2
31 #define CMD_LINE_ARG_CLIENTID 3
32 #define CMD_LINE_ARG_IMPORTSTR 4
33 #define CMD_LINE_ARG_INVITEURL 5
35 static bool conn_status = false;
37 void callback_logger(meshlink_handle_t *mesh, meshlink_log_level_t level, const char *text) {
41 char connection_match_msg[100];
43 fprintf(stderr, "meshlink>> %s\n", text);
45 if(strstr(text, "Connection") || strstr(text, "connection")) {
46 assert(snprintf(connection_match_msg, sizeof(connection_match_msg),
47 "Connection with peer") >= 0);
49 if(strstr(text, connection_match_msg) && strstr(text, "activated")) {
54 assert(snprintf(connection_match_msg, sizeof(connection_match_msg),
55 "Already connected to peer") >= 0);
57 if(strstr(text, connection_match_msg)) {
62 assert(snprintf(connection_match_msg, sizeof(connection_match_msg),
63 "Connection closed by peer") >= 0);
65 if(strstr(text, connection_match_msg)) {
70 assert(snprintf(connection_match_msg, sizeof(connection_match_msg),
71 "Closing connection with peer") >= 0);
73 if(strstr(text, connection_match_msg)) {
82 int main(int argc, char *argv[]) {
88 if((argv[3]) && (argv[4])) {
89 clientId = atoi(argv[3]);
90 mesh_event_sock_connect(argv[4]);
93 execute_open(argv[1], argv[2]);
94 meshlink_set_log_cb(mesh_handle, MESHLINK_INFO, callback_logger);
96 if(argv[CMD_LINE_ARG_INVITEURL]) {
97 execute_join(argv[CMD_LINE_ARG_INVITEURL]);
102 if(!mesh_event_sock_send(clientId, NODE_STARTED, NULL, 0)) {
103 fprintf(stderr, "Trying to resend mesh event\n");
107 fprintf(stderr, "Generating Invitation to PEER\n");
108 invite_peer = execute_invite("peer", NULL);
109 assert(invite_peer != NULL);
111 if(!mesh_event_sock_send(clientId, NODE_INVITATION, invite_peer, strlen(invite_peer) + 1)) {
112 fprintf(stderr, "Trying to resend mesh event\n");
116 fprintf(stderr, "Waiting for PEER to be connected\n");
118 /* Connectivity of peer is checked */
119 while(!conn_status) {
123 fprintf(stderr, "Connected with Peer\n");
125 if(!mesh_event_sock_send(clientId, META_CONN_SUCCESSFUL, NULL, 0)) {
126 fprintf(stderr, "Trying to resend mesh event\n");
131 fprintf(stderr, "Waiting for PEER to be re-connected\n");
133 /* Connectivity of peer */
134 while(!conn_status) {
138 fprintf(stderr, "Re-connected with Peer\n");
140 if(!mesh_event_sock_send(clientId, META_CONN_SUCCESSFUL, NULL, 0)) {
141 fprintf(stderr, "Trying to resend mesh event\n");
146 assert(meshlink_destroy(argv[1]));