]> git.meshlink.io Git - meshlink/blob - test/blackbox/test_case_meta_conn_05/node_sim_nut.c
Fix spelling errors.
[meshlink] / test / blackbox / test_case_meta_conn_05 / node_sim_nut.c
1 /*
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>
6
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.
11
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.
16
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.
20 */
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <pthread.h>
25 #include "../common/common_handlers.h"
26 #include "../common/test_step.h"
27 #include "../common/mesh_event_handler.h"
28
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
34
35 static bool conn_status = false;
36
37 void callback_logger(meshlink_handle_t *mesh, meshlink_log_level_t level,
38                      const char *text) {
39         char connection_match_msg[100];
40
41         fprintf(stderr, "meshlink>> %s\n", text);
42
43         if(strstr(text, "Connection") || strstr(text, "connection")) {
44                 assert(snprintf(connection_match_msg, sizeof(connection_match_msg),
45                                 "Connection with peer") >= 0);
46
47                 if(strstr(text, connection_match_msg) && strstr(text, "activated")) {
48                         conn_status = true;
49                         return;
50                 }
51
52                 assert(snprintf(connection_match_msg, sizeof(connection_match_msg),
53                                 "Already connected to peer") >= 0);
54
55                 if(strstr(text, connection_match_msg)) {
56                         conn_status = true;
57                         return;
58                 }
59
60                 assert(snprintf(connection_match_msg, sizeof(connection_match_msg),
61                                 "Connection closed by peer") >= 0);
62
63                 if(strstr(text, connection_match_msg)) {
64                         conn_status = false;
65                         return;
66                 }
67
68                 assert(snprintf(connection_match_msg, sizeof(connection_match_msg),
69                                 "Closing connection with peer") >= 0);
70
71                 if(strstr(text, connection_match_msg)) {
72                         conn_status = false;
73                         return;
74                 }
75         }
76
77         return;
78 }
79
80 int main(int argc, char *argv[]) {
81         struct timeval main_loop_wait = { 5, 0 };
82         int i, clientId = -1;
83         bool result;
84         char *invite_peer;
85
86         if((argv[3]) && (argv[4])) {
87                 clientId = atoi(argv[3]);
88                 mesh_event_sock_connect(argv[4]);
89         }
90
91         execute_open(argv[1], argv[2]);
92         meshlink_set_log_cb(mesh_handle, MESHLINK_INFO, callback_logger);
93
94         if(argv[CMD_LINE_ARG_INVITEURL]) {
95                 execute_join(argv[CMD_LINE_ARG_INVITEURL]);
96         }
97
98         execute_start();
99
100         if(!mesh_event_sock_send(clientId, NODE_STARTED, NULL, 0)) {
101                 fprintf(stderr, "Trying to resend mesh event\n");
102                 sleep(1);
103         }
104
105         fprintf(stderr, "Generating Invitation to PEER\n");
106         invite_peer = execute_invite("peer", NULL);
107         assert(invite_peer != NULL);
108
109         if(!mesh_event_sock_send(clientId, NODE_INVITATION, invite_peer, strlen(invite_peer) + 1)) {
110                 fprintf(stderr, "Trying to resend mesh event\n");
111                 sleep(1);
112         }
113
114         fprintf(stderr, "Waiting for PEER to be connected\n");
115
116         /* Connectivity of peer is checked */
117         while(!conn_status) {
118                 sleep(1);
119         }
120
121         fprintf(stderr, "Connected with Peer\n");
122
123         if(!mesh_event_sock_send(clientId, META_CONN_SUCCESSFUL, NULL, 0)) {
124                 fprintf(stderr, "Trying to resend mesh event\n");
125                 sleep(1);
126         }
127
128         conn_status = false;
129         fprintf(stderr, "Waiting for PEER to be re-connected\n");
130
131         /* Connectivity of peer */
132         while(!conn_status) {
133                 sleep(1);
134         }
135
136         fprintf(stderr, "Re-connected with Peer\n");
137
138         if(!mesh_event_sock_send(clientId, META_CONN_SUCCESSFUL, NULL, 0)) {
139                 fprintf(stderr, "Trying to resend mesh event\n");
140                 sleep(1);
141         }
142
143         execute_close();
144         meshlink_destroy(argv[1]);
145 }