]> git.meshlink.io Git - meshlink/blob - test/blackbox/test_cases_submesh03/node_sim_corenode1.c
Add blackbox test cases for submesh
[meshlink] / test / blackbox / test_cases_submesh03 / node_sim_corenode1.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 <signal.h>
26 #include "../common/common_handlers.h"
27 #include "../common/test_step.h"
28 #include "../common/mesh_event_handler.h"
29 #include "../../utils.h"
30
31 #define CMD_LINE_ARG_NODENAME   1
32 #define CMD_LINE_ARG_DEVCLASS   2
33 #define CMD_LINE_ARG_CLIENTID   3
34 #define CMD_LINE_ARG_IMPORTSTR  4
35 #define CMD_LINE_ARG_INVITEURL  5
36 #define CHANNEL_PORT 1234
37
38 static bool conn_status = false;
39 static int client_id = -1;
40
41 static struct sync_flag peer_reachable = {.mutex  = PTHREAD_MUTEX_INITIALIZER, .cond = PTHREAD_COND_INITIALIZER, .flag = false};
42 static struct sync_flag channel_opened = {.mutex  = PTHREAD_MUTEX_INITIALIZER, .cond = PTHREAD_COND_INITIALIZER, .flag = false};
43 static struct sync_flag channel_data_recieved = {.mutex  = PTHREAD_MUTEX_INITIALIZER, .cond = PTHREAD_COND_INITIALIZER, .flag = false};
44
45 static meshlink_handle_t *mesh = NULL;
46
47 static void mesh_send_message_handler(char *destination);
48
49 static void send_event(mesh_event_t event) {
50         int attempts;
51
52         for(attempts = 0; attempts < 5; attempts += 1) {
53                 if(mesh_event_sock_send(client_id, event, NULL, 0)) {
54                         break;
55                 }
56         }
57
58         assert(attempts < 5);
59
60         return;
61 }
62
63 /* channel receive callback */
64 static void channel_receive_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, const void *dat, size_t len) {
65         char data[100] = {0};
66
67         if(len == 0) {
68                 send_event(ERR_NETWORK);
69                 return;
70         }
71
72         memcpy(data, dat, len);
73
74         fprintf(stderr, "corenode1 got message from %s as %s\n", channel->node->name, data);
75
76         if(!memcmp(dat, "Channel Message", len)) {
77                 mesh_send_message_handler((char *)channel->node->name);
78
79                 if(0 == strcmp("app1node1", channel->node->name)) {
80                         set_sync_flag(&channel_data_recieved, true);
81                 }
82         } else if(!memcmp(dat, "failure", 7)) {
83                 assert(false);
84         }
85
86         return;
87 }
88
89 static void node_status_cb(meshlink_handle_t *mesh, meshlink_node_t *node,
90                            bool reachable) {
91         if(reachable) {
92                 fprintf(stderr, "Node %s became reachable\n", node->name);
93         } else {
94                 fprintf(stderr, "Node %s is unreachable\n", node->name);
95         }
96
97         return;
98 }
99
100 static void poll_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, size_t len) {
101         char *message = "Channel Message";
102         char *node = (char *)channel->node->name;
103         (void)len;
104         meshlink_set_channel_poll_cb(mesh, channel, NULL);
105         fprintf(stderr, "corenode1's Channel request has been accepted by %s at : %lu\n", node, time(NULL));
106
107         if(0 == strcmp("app1node1", node)) {
108                 set_sync_flag(&channel_opened, true);
109         }
110
111         assert(meshlink_channel_send(mesh, channel, message, strlen(message)) >= 0);
112         return;
113 }
114
115 /* channel receive callback */
116 static bool channel_accept(meshlink_handle_t *mesh, meshlink_channel_t *channel, uint16_t port, const void *dat, size_t len) {
117         (void)dat;
118         (void)len;
119
120         assert(port == CHANNEL_PORT);
121
122         fprintf(stderr, "corenode1 got channel request from %s\n", channel->node->name);
123         meshlink_set_channel_receive_cb(mesh, channel, channel_receive_cb);
124
125         return true;
126 }
127
128 void mesh_send_message_handler(char *destination) {
129         meshlink_channel_t *channel = NULL;
130         meshlink_node_t *target_node = NULL;
131
132         // Open a channel to destination node
133         target_node = meshlink_get_node(mesh, destination);
134         assert(target_node);
135         fprintf(stderr, "corenode1 Sending Channel request to %s at : %lu\n", destination, time(NULL));
136         channel = meshlink_channel_open(mesh, target_node, CHANNEL_PORT,
137                                         channel_receive_cb, NULL, 0);
138         meshlink_set_channel_poll_cb(mesh, channel, poll_cb);
139 }
140
141 int main(int argc, char *argv[]) {
142         struct timeval main_loop_wait = { 5, 0 };
143         int i;
144
145         // Import mesh event handler
146
147         fprintf(stderr, "Mesh node 'corenode1' starting up........\n");
148
149         if((argv[CMD_LINE_ARG_CLIENTID]) && (argv[CMD_LINE_ARG_IMPORTSTR])) {
150                 client_id = atoi(argv[CMD_LINE_ARG_CLIENTID]);
151                 mesh_event_sock_connect(argv[CMD_LINE_ARG_IMPORTSTR]);
152         }
153
154         setup_signals();
155
156         // Execute test steps
157
158         mesh = meshlink_open("testconf", argv[CMD_LINE_ARG_NODENAME],
159                              "test_channel_conn", atoi(argv[CMD_LINE_ARG_DEVCLASS]));
160         assert(mesh);
161         meshlink_set_log_cb(mesh, MESHLINK_DEBUG, meshlink_callback_logger);
162         meshlink_set_channel_accept_cb(mesh, channel_accept);
163         meshlink_set_node_status_cb(mesh, node_status_cb);
164
165         if(argv[CMD_LINE_ARG_INVITEURL]) {
166                 assert(meshlink_join(mesh, argv[CMD_LINE_ARG_INVITEURL]));
167         }
168
169         assert(meshlink_start(mesh));
170
171         send_event(NODE_STARTED);
172
173         assert(wait_sync_flag(&channel_opened, 10));
174         send_event(CHANNEL_OPENED);
175
176         assert(wait_sync_flag(&channel_data_recieved, 10));
177         send_event(CHANNEL_DATA_RECIEVED);
178
179         // All test steps executed - wait for signals to stop/start or close the mesh
180
181         while(test_running) {
182                 select(1, NULL, NULL, NULL, &main_loop_wait);
183         }
184
185         meshlink_close(mesh);
186
187         return 0;
188 }