]> git.meshlink.io Git - meshlink/blob - test/blackbox/test_cases_submesh01/node_sim_app1node2.c
Fix compiler warnings in the test suites.
[meshlink] / test / blackbox / test_cases_submesh01 / node_sim_app1node2.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 #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 <time.h>
28 #include "../common/common_handlers.h"
29 #include "../common/test_step.h"
30 #include "../common/mesh_event_handler.h"
31 #include "../../utils.h"
32
33 #define CMD_LINE_ARG_NODENAME   1
34 #define CMD_LINE_ARG_DEVCLASS   2
35 #define CMD_LINE_ARG_CLIENTID   3
36 #define CMD_LINE_ARG_IMPORTSTR  4
37 #define CMD_LINE_ARG_INVITEURL  5
38 #define CHANNEL_PORT 1234
39
40 static void channel_receive_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, const void *dat, size_t len);
41 static bool channel_accept(meshlink_handle_t *mesh, meshlink_channel_t *channel, uint16_t port, const void *dat, size_t len);
42
43 static int client_id = -1;
44 static meshlink_handle_t *mesh = NULL;
45
46 static struct sync_flag peer_reachable = {.mutex  = PTHREAD_MUTEX_INITIALIZER, .cond = PTHREAD_COND_INITIALIZER, .flag = false};
47 static struct sync_flag start_test = {.mutex  = PTHREAD_MUTEX_INITIALIZER, .cond = PTHREAD_COND_INITIALIZER, .flag = false};
48 static struct sync_flag app_reachable = {.mutex  = PTHREAD_MUTEX_INITIALIZER, .cond = PTHREAD_COND_INITIALIZER, .flag = false};
49 static struct sync_flag channel_opened = {.mutex  = PTHREAD_MUTEX_INITIALIZER, .cond = PTHREAD_COND_INITIALIZER, .flag = false};
50 static struct sync_flag channel_data_recieved = {.mutex  = PTHREAD_MUTEX_INITIALIZER, .cond = PTHREAD_COND_INITIALIZER, .flag = false};
51
52 static void send_event(mesh_event_t event) {
53         int attempts;
54
55         for(attempts = 0; attempts < 5; attempts += 1) {
56                 if(mesh_event_sock_send(client_id, event, NULL, 0)) {
57                         break;
58                 }
59         }
60
61         assert(attempts < 5);
62
63         return;
64 }
65
66 static bool channel_accept(meshlink_handle_t *mesh, meshlink_channel_t *channel, uint16_t port, const void *dat, size_t len) {
67         (void)dat;
68         (void)len;
69
70         assert(port == CHANNEL_PORT);
71
72         fprintf(stderr, "\tapp1node2 got channel request from %s\n", channel->node->name);
73
74         if(!strcmp(channel->node->name, "corenode1")) {
75                 meshlink_set_channel_receive_cb(mesh, channel, channel_receive_cb);
76                 mesh->priv = channel;
77
78                 return true;
79         }
80
81         return false;
82 }
83
84 /* channel receive callback */
85 static void channel_receive_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, const void *dat, size_t len) {
86         (void)mesh;
87
88         char data[100] = {0};
89
90         if(len == 0) {
91                 fprintf(stderr, "\tapp1node2 got error from %s at %lu\n", channel->node->name, time(NULL));
92                 send_event(ERR_NETWORK);
93                 return;
94         }
95
96         memcpy(data, dat, len);
97
98         fprintf(stderr, "\tapp1node2 got message from %s as %s\n", channel->node->name, data);
99
100         if(!strcmp(channel->node->name, "corenode1")) {
101                 if(!memcmp(dat, "Channel Message", len)) {
102                         set_sync_flag(&channel_data_recieved, true);
103                 } else if(!memcmp(dat, "failure", 7)) {
104                         assert(false);
105                 }
106         } else if(!strcmp(channel->node->name, "app1node1")) {
107                 if(!memcmp(dat, "Channel Message", len)) {
108                         set_sync_flag(&channel_data_recieved, true);
109                 } else if(!memcmp(dat, "failure", 7)) {
110                         assert(false);
111                 }
112         } else {
113                 assert(false);
114         }
115
116         return;
117 }
118
119 static void poll_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, size_t len) {
120         char *message = "Channel Message";
121         char *node = (char *)channel->node->name;
122         (void)len;
123         meshlink_set_channel_poll_cb(mesh, channel, NULL);
124         fprintf(stderr, "\tapp1node2's Channel request has been accepted by %s at : %lu\n", node, time(NULL));
125         set_sync_flag(&channel_opened, true);
126         assert(meshlink_channel_send(mesh, channel, message, strlen(message)) >= 0);
127         return;
128 }
129
130
131 static void node_status_cb(meshlink_handle_t *mesh, meshlink_node_t *node, bool reachable) {
132         (void)mesh;
133
134         if(!strcasecmp(node->name, "corenode1")) {
135                 if(reachable) {
136                         fprintf(stderr, "\tNode corenode1 became reachable\n");
137                         set_sync_flag(&peer_reachable, true);
138                 }
139         } else if(!strcasecmp(node->name, "app1node1")) {
140                 if(reachable) {
141                         fprintf(stderr, "\tNode app1node1 became reachable\n");
142                         set_sync_flag(&app_reachable, true);
143                 }
144         }
145
146         return;
147 }
148
149 void mesh_start_test_handler(int signum) {
150         (void)signum;
151
152         fprintf(stderr, "Starting test in app1node2\n");
153         set_sync_flag(&start_test, true);
154 }
155
156 int main(int argc, char *argv[]) {
157         (void)argc;
158
159         struct timeval main_loop_wait = { 2, 0 };
160         meshlink_channel_t *channel = NULL;
161         meshlink_node_t *core_node = NULL;
162
163         fprintf(stderr, "\tMesh node 'app1node2' starting up........\n");
164
165         // Import mesh event handler
166
167         if((argv[CMD_LINE_ARG_CLIENTID]) && (argv[CMD_LINE_ARG_IMPORTSTR])) {
168                 client_id = atoi(argv[CMD_LINE_ARG_CLIENTID]);
169                 mesh_event_sock_connect(argv[CMD_LINE_ARG_IMPORTSTR]);
170         }
171
172         // Setup required signals
173
174         setup_signals();
175         signal(SIGIO, mesh_start_test_handler);
176
177         // Run peer node instance
178
179         mesh = meshlink_open("app1node2conf", argv[CMD_LINE_ARG_NODENAME],
180                              "test_channel_conn", atoi(argv[CMD_LINE_ARG_DEVCLASS]));
181         assert(mesh);
182         meshlink_set_log_cb(mesh, MESHLINK_DEBUG, meshlink_callback_logger);
183         meshlink_set_channel_accept_cb(mesh, channel_accept);
184         meshlink_set_node_status_cb(mesh, node_status_cb);
185
186         if(argv[CMD_LINE_ARG_INVITEURL]) {
187                 assert(meshlink_join(mesh, argv[CMD_LINE_ARG_INVITEURL]));
188         }
189
190         assert(meshlink_start(mesh));
191
192         send_event(NODE_STARTED);
193
194         // Wait for peer node to join
195
196         assert(wait_sync_flag(&peer_reachable, 15));
197         send_event(NODE_JOINED);
198
199         while(false == wait_sync_flag(&start_test, 10));
200
201         // Open a channel to peer node
202         core_node = meshlink_get_node(mesh, "corenode1");
203         assert(core_node);
204         fprintf(stderr, "\tapp1node2 Sending Channel request to corenode1 at : %lu\n", time(NULL));
205         channel = meshlink_channel_open(mesh, core_node, CHANNEL_PORT,
206                                         channel_receive_cb, NULL, 0);
207         meshlink_set_channel_poll_cb(mesh, channel, poll_cb);
208         assert(wait_sync_flag(&channel_opened, 15));
209         send_event(CHANNEL_OPENED);
210
211         assert(wait_sync_flag(&channel_data_recieved, 30));
212         send_event(CHANNEL_DATA_RECIEVED);
213
214         // Open a channel to peer node
215         channel_opened.flag = false;
216         channel_data_recieved.flag = false;
217
218         assert(wait_sync_flag(&app_reachable, 60));
219
220         core_node = meshlink_get_node(mesh, "app1node1");
221         assert(core_node);
222         fprintf(stderr, "\tapp1node2 Sending Channel request to app1node1 at : %lu\n", time(NULL));
223         channel = meshlink_channel_open(mesh, core_node, CHANNEL_PORT,
224                                         channel_receive_cb, NULL, 0);
225         meshlink_set_channel_poll_cb(mesh, channel, poll_cb);
226         assert(wait_sync_flag(&channel_opened, 100));
227         send_event(CHANNEL_OPENED);
228
229         assert(wait_sync_flag(&channel_data_recieved, 100));
230         send_event(CHANNEL_DATA_RECIEVED);
231
232         core_node = meshlink_get_node(mesh, "app2node1");
233
234         if(NULL != core_node) {
235                 send_event(SIG_ABORT);
236                 assert(false);
237         }
238
239         send_event(MESH_EVENT_COMPLETED);
240
241         // All test steps executed - wait for signals to stop/start or close the mesh
242
243         while(test_running) {
244                 select(1, NULL, NULL, NULL, &main_loop_wait);
245         }
246
247         meshlink_close(mesh);
248
249         return EXIT_SUCCESS;
250 }