]> git.meshlink.io Git - meshlink/blob - test/blackbox/test_cases_submesh01/node_sim_corenode2.c
Ensure NDEBUG is not set in the test suite.
[meshlink] / test / blackbox / test_cases_submesh01 / node_sim_corenode2.c
1 /*
2     node_sim_peer.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
22 #ifdef NDEBUG
23 #undef NDEBUG
24 #endif
25
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <pthread.h>
30 #include <assert.h>
31 #include <signal.h>
32 #include <time.h>
33 #include "../common/common_handlers.h"
34 #include "../common/test_step.h"
35 #include "../common/mesh_event_handler.h"
36 #include "../../utils.h"
37
38 #define CMD_LINE_ARG_NODENAME   1
39 #define CMD_LINE_ARG_DEVCLASS   2
40 #define CMD_LINE_ARG_CLIENTID   3
41 #define CMD_LINE_ARG_IMPORTSTR  4
42 #define CMD_LINE_ARG_INVITEURL  5
43 #define CHANNEL_PORT 1234
44
45 static void channel_receive_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, const void *dat, size_t len);
46 static bool channel_accept(meshlink_handle_t *mesh, meshlink_channel_t *channel, uint16_t port, const void *dat, size_t len);
47
48 static int client_id = -1;
49 static meshlink_handle_t *mesh = NULL;
50
51 static struct sync_flag peer_reachable = {.mutex  = PTHREAD_MUTEX_INITIALIZER, .cond = PTHREAD_COND_INITIALIZER, .flag = false};
52 static struct sync_flag start_test = {.mutex  = PTHREAD_MUTEX_INITIALIZER, .cond = PTHREAD_COND_INITIALIZER, .flag = false};
53 static struct sync_flag channel_opened = {.mutex  = PTHREAD_MUTEX_INITIALIZER, .cond = PTHREAD_COND_INITIALIZER, .flag = false};
54 static struct sync_flag channel_data_recieved = {.mutex  = PTHREAD_MUTEX_INITIALIZER, .cond = PTHREAD_COND_INITIALIZER, .flag = false};
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 bool channel_accept(meshlink_handle_t *mesh, meshlink_channel_t *channel, uint16_t port, const void *dat, size_t len) {
71         (void)dat;
72         (void)len;
73
74         assert(port == CHANNEL_PORT);
75
76         fprintf(stderr, "corenode2 got channel request from %s", channel->node->name);
77
78         if(!strcmp(channel->node->name, "corenode1")) {
79                 meshlink_set_channel_receive_cb(mesh, channel, channel_receive_cb);
80                 mesh->priv = channel;
81
82                 return true;
83         }
84
85         return false;
86 }
87
88 /* channel receive callback */
89 static void channel_receive_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, const void *dat, size_t len) {
90         (void)mesh;
91
92         char data[100] = {0};
93
94         if(len == 0) {
95                 send_event(ERR_NETWORK);
96                 return;
97         }
98
99         memcpy(data, dat, len);
100
101         fprintf(stderr, "corenode2 got message from %s as %s", channel->node->name, data);
102
103         if(!strcmp(channel->node->name, "corenode1")) {
104                 if(!memcmp(dat, "Channel Message", len)) {
105                         set_sync_flag(&channel_data_recieved, true);
106                 } else if(!memcmp(dat, "failure", 7)) {
107                         assert(false);
108                 }
109         }
110
111         return;
112 }
113
114 static void poll_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, size_t len) {
115         char *message = "Channel Message";
116         (void)len;
117         meshlink_set_channel_poll_cb(mesh, channel, NULL);
118         fprintf(stderr, "corenode2's Channel request has been accepted by corenode1 at : %lu", time(NULL));
119         set_sync_flag(&channel_opened, true);
120         assert(meshlink_channel_send(mesh, channel, message, strlen(message)) >= 0);
121         return;
122 }
123
124
125 static void node_status_cb(meshlink_handle_t *mesh, meshlink_node_t *node, bool reachable) {
126         (void)mesh;
127
128         if(!strcasecmp(node->name, "corenode1")) {
129                 if(reachable) {
130                         fprintf(stderr, "Node corenode2 became reachable");
131                         set_sync_flag(&peer_reachable, true);
132                 }
133         }
134
135         return;
136 }
137
138 void mesh_start_test_handler(int signum) {
139         (void)signum;
140
141         fprintf(stderr, "Starting test in corenode2\n");
142         set_sync_flag(&start_test, true);
143 }
144
145 int main(int argc, char *argv[]) {
146         (void)argc;
147
148         struct timeval main_loop_wait = { 2, 0 };
149         meshlink_channel_t *channel = NULL;
150         meshlink_node_t *core_node = NULL;
151
152         fprintf(stderr, "Mesh node 'corenode2' starting up........\n");
153
154         // Import mesh event handler
155
156         if((argv[CMD_LINE_ARG_CLIENTID]) && (argv[CMD_LINE_ARG_IMPORTSTR])) {
157                 client_id = atoi(argv[CMD_LINE_ARG_CLIENTID]);
158                 mesh_event_sock_connect(argv[CMD_LINE_ARG_IMPORTSTR]);
159         }
160
161         // Setup required signals
162
163         setup_signals();
164         signal(SIGIO, mesh_start_test_handler);
165
166         // Run peer node instance
167
168         mesh = meshlink_open("corenode1conf", argv[CMD_LINE_ARG_NODENAME],
169                              "test_channel_conn", atoi(argv[CMD_LINE_ARG_DEVCLASS]));
170         assert(mesh);
171         meshlink_set_log_cb(mesh, MESHLINK_DEBUG, meshlink_callback_logger);
172         meshlink_set_channel_accept_cb(mesh, channel_accept);
173         meshlink_set_node_status_cb(mesh, node_status_cb);
174
175         if(argv[CMD_LINE_ARG_INVITEURL]) {
176                 assert(meshlink_join(mesh, argv[CMD_LINE_ARG_INVITEURL]));
177         }
178
179         assert(meshlink_start(mesh));
180
181         send_event(NODE_STARTED);
182
183         // Wait for peer node to join
184
185         assert(wait_sync_flag(&peer_reachable, 15));
186         send_event(NODE_JOINED);
187
188         while(false == wait_sync_flag(&start_test, 10));
189
190         // Open a channel to peer node
191         core_node = meshlink_get_node(mesh, "corenode1");
192         assert(core_node);
193         fprintf(stderr, "corenode2 Sending Channel request to corenode1 at : %lu", time(NULL));
194         channel = meshlink_channel_open(mesh, core_node, CHANNEL_PORT,
195                                         channel_receive_cb, NULL, 0);
196         meshlink_set_channel_poll_cb(mesh, channel, poll_cb);
197         assert(wait_sync_flag(&channel_opened, 15));
198         send_event(CHANNEL_OPENED);
199
200         assert(wait_sync_flag(&channel_data_recieved, 10));
201         send_event(CHANNEL_DATA_RECIEVED);
202
203         // All test steps executed - wait for signals to stop/start or close the mesh
204
205         while(test_running) {
206                 select(1, NULL, NULL, NULL, &main_loop_wait);
207         }
208
209         meshlink_close(mesh);
210
211         return EXIT_SUCCESS;
212 }