]> git.meshlink.io Git - meshlink/blob - test/blackbox/test_case_channel_conn_03/node_sim_nut.c
421542b93bfcbaa29f875cd178a4ad85877043f1
[meshlink] / test / blackbox / test_case_channel_conn_03 / node_sim_nut.c
1 /*
2     node_sim_nut.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 <assert.h>
26 #include <signal.h>
27 #include "../common/common_handlers.h"
28 #include "../common/test_step.h"
29 #include "../common/mesh_event_handler.h"
30 #include "../../utils.h"
31
32 #define CMD_LINE_ARG_NODENAME   1
33 #define CMD_LINE_ARG_DEVCLASS   2
34 #define CMD_LINE_ARG_CLIENTID   3
35 #define CMD_LINE_ARG_IMPORTSTR  4
36 #define CMD_LINE_ARG_INVITEURL  5
37 #define CHANNEL_PORT 1234
38
39 static int client_id = -1;
40
41 static struct sync_flag peer_reachable = {.mutex  = PTHREAD_MUTEX_INITIALIZER, .cond = PTHREAD_COND_INITIALIZER};
42 static struct sync_flag channel_opened = {.mutex  = PTHREAD_MUTEX_INITIALIZER, .cond = PTHREAD_COND_INITIALIZER};
43 static struct sync_flag peer_unreachable = {.mutex  = PTHREAD_MUTEX_INITIALIZER, .cond = PTHREAD_COND_INITIALIZER};
44 static struct sync_flag sigusr_received = {.mutex  = PTHREAD_MUTEX_INITIALIZER, .cond = PTHREAD_COND_INITIALIZER};
45
46 static void send_event(mesh_event_t event);
47 static void node_status_cb(meshlink_handle_t *mesh, meshlink_node_t *node, bool reachable);
48
49 static void mesh_siguser1_signal_handler(int sig_num) {
50         (void)sig_num;
51
52         set_sync_flag(&sigusr_received, true);
53         return;
54 }
55
56 static void send_event(mesh_event_t event) {
57         int attempts;
58
59         for(attempts = 0; attempts < 5; attempts += 1) {
60                 if(mesh_event_sock_send(client_id, event, NULL, 0)) {
61                         break;
62                 }
63         }
64
65         assert(attempts < 5);
66
67         return;
68 }
69
70 static void node_status_cb(meshlink_handle_t *mesh, meshlink_node_t *node, bool reachable) {
71         (void)mesh;
72
73         if(!strcasecmp(node->name, "peer")) {
74                 if(reachable) {
75                         set_sync_flag(&peer_reachable, true);
76                 } else {
77                         set_sync_flag(&peer_unreachable, true);
78                 }
79         }
80
81         return;
82 }
83
84 static void poll_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, size_t len) {
85         (void)len;
86         meshlink_set_channel_poll_cb(mesh, channel, NULL);
87         assert(meshlink_channel_send(mesh, channel, "test", 5) >= 0);
88         return;
89 }
90
91 static void channel_receive_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, const void *dat, size_t len) {
92         (void)mesh;
93
94         if(!strcmp(channel->node->name, "peer")) {
95                 if(len == 5 && !memcmp(dat, "reply", 5)) {
96                         set_sync_flag(&channel_opened, true);
97                 }
98         }
99
100         return;
101 }
102
103 int main(int argc, char *argv[]) {
104         (void)argc;
105
106         struct timeval main_loop_wait = { 2, 0 };
107
108         // Import mesh event handler
109
110         if((argv[CMD_LINE_ARG_CLIENTID]) && (argv[CMD_LINE_ARG_IMPORTSTR])) {
111                 client_id = atoi(argv[CMD_LINE_ARG_CLIENTID]);
112                 mesh_event_sock_connect(argv[CMD_LINE_ARG_IMPORTSTR]);
113         }
114
115         // Setup required signals
116
117         setup_signals();
118         signal(SIGUSR1, mesh_siguser1_signal_handler);
119
120         // Execute test steps
121
122         meshlink_handle_t *mesh = meshlink_open("testconf", argv[CMD_LINE_ARG_NODENAME],
123                                                 "test_channel_conn", atoi(argv[CMD_LINE_ARG_DEVCLASS]));
124         assert(mesh);
125         meshlink_set_log_cb(mesh, MESHLINK_DEBUG, meshlink_callback_logger);
126         meshlink_set_node_status_cb(mesh, node_status_cb);
127
128         if(argv[CMD_LINE_ARG_INVITEURL]) {
129                 assert(meshlink_join(mesh, argv[CMD_LINE_ARG_INVITEURL]));
130         }
131
132         assert(meshlink_start(mesh));
133
134         // Wait for peer node to join
135
136         assert(wait_sync_flag(&peer_reachable, 30));
137         send_event(NODE_JOINED);
138
139         // Open a channel to peer node
140
141         meshlink_node_t *peer_node = meshlink_get_node(mesh, "peer");
142         assert(peer_node);
143         meshlink_channel_t *channel = meshlink_channel_open(mesh, peer_node, CHANNEL_PORT,
144                                       channel_receive_cb, NULL, 0);
145         meshlink_set_channel_poll_cb(mesh, channel, poll_cb);
146
147         assert(wait_sync_flag(&channel_opened, 10));
148         send_event(CHANNEL_OPENED);
149
150         peer_unreachable.flag = false;
151         peer_reachable.flag = false;
152         assert(wait_sync_flag(&sigusr_received, 10));
153
154         assert(wait_sync_flag(&peer_unreachable, 100));
155         send_event(NODE_UNREACHABLE);
156
157         assert(wait_sync_flag(&peer_reachable, 100));
158         send_event(NODE_REACHABLE);
159
160         assert(meshlink_channel_send(mesh, channel, "after", 6) >= 0);
161
162         // All test steps executed - wait for signals to stop/start or close the mesh
163
164         while(test_running) {
165                 select(1, NULL, NULL, NULL, &main_loop_wait);
166         }
167
168         meshlink_close(mesh);
169 }