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