]> git.meshlink.io Git - meshlink/blob - test/blackbox/test_cases_submesh04/node_sim_corenode1.c
Fix compiler warnings in the test suites.
[meshlink] / test / blackbox / test_cases_submesh04 / 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 int client_id = -1;
39
40 static struct sync_flag channel_opened = {.mutex  = PTHREAD_MUTEX_INITIALIZER, .cond = PTHREAD_COND_INITIALIZER, .flag = false};
41 static struct sync_flag channel_data_recieved = {.mutex  = PTHREAD_MUTEX_INITIALIZER, .cond = PTHREAD_COND_INITIALIZER, .flag = false};
42
43 static meshlink_handle_t *mesh = NULL;
44
45 static void mesh_send_message_handler(const char *destination);
46
47 static void send_event(mesh_event_t event) {
48         int attempts;
49
50         for(attempts = 0; attempts < 5; attempts += 1) {
51                 if(mesh_event_sock_send(client_id, event, NULL, 0)) {
52                         break;
53                 }
54         }
55
56         assert(attempts < 5);
57
58         return;
59 }
60
61 /* channel receive callback */
62 static void channel_receive_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, const void *dat, size_t len) {
63         (void)mesh;
64
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(channel->node->name);
78
79                 if(0 == strcmp("app1node2", 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, bool reachable) {
90         (void)mesh;
91
92         if(reachable) {
93                 fprintf(stderr, "Node %s became reachable\n", node->name);
94         } else {
95                 fprintf(stderr, "Node %s is unreachable\n", node->name);
96         }
97
98         return;
99 }
100
101 static void poll_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, size_t len) {
102         const char *message = "Channel Message";
103         const char *node = channel->node->name;
104         (void)len;
105         meshlink_set_channel_poll_cb(mesh, channel, NULL);
106         fprintf(stderr, "corenode1's Channel request has been accepted by %s at : %lu\n", node, time(NULL));
107
108         if(0 == strcmp("app1node2", node)) {
109                 set_sync_flag(&channel_opened, true);
110         }
111
112         assert(meshlink_channel_send(mesh, channel, message, strlen(message)) >= 0);
113         return;
114 }
115
116 /* channel receive callback */
117 static bool channel_accept(meshlink_handle_t *mesh, meshlink_channel_t *channel, uint16_t port, const void *dat, size_t len) {
118         (void)dat;
119         (void)len;
120
121         assert(port == CHANNEL_PORT);
122
123         fprintf(stderr, "corenode1 got channel request from %s\n", channel->node->name);
124         meshlink_set_channel_receive_cb(mesh, channel, channel_receive_cb);
125
126         return true;
127 }
128
129 static void mesh_send_message_handler(const char *destination) {
130         meshlink_channel_t *channel = NULL;
131         meshlink_node_t *target_node = NULL;
132
133         // Open a channel to destination node
134         target_node = meshlink_get_node(mesh, destination);
135         assert(target_node);
136         fprintf(stderr, "corenode1 Sending Channel request to %s at : %lu\n", destination, time(NULL));
137         channel = meshlink_channel_open(mesh, target_node, CHANNEL_PORT,
138                                         channel_receive_cb, NULL, 0);
139         meshlink_set_channel_poll_cb(mesh, channel, poll_cb);
140 }
141
142 int main(int argc, char *argv[]) {
143         (void)argc;
144
145         struct timeval main_loop_wait = { 5, 0 };
146
147         // Import mesh event handler
148
149         fprintf(stderr, "Mesh node 'corenode1' starting up........\n");
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_signals();
157
158         // Execute test steps
159
160         mesh = meshlink_open("testconf", argv[CMD_LINE_ARG_NODENAME],
161                              "test_channel_conn", atoi(argv[CMD_LINE_ARG_DEVCLASS]));
162         assert(mesh);
163         meshlink_set_log_cb(mesh, MESHLINK_DEBUG, meshlink_callback_logger);
164         meshlink_set_channel_accept_cb(mesh, channel_accept);
165         meshlink_set_node_status_cb(mesh, node_status_cb);
166
167         if(argv[CMD_LINE_ARG_INVITEURL]) {
168                 assert(meshlink_join(mesh, argv[CMD_LINE_ARG_INVITEURL]));
169         }
170
171         assert(meshlink_start(mesh));
172
173         send_event(NODE_STARTED);
174
175         assert(wait_sync_flag(&channel_opened, 50));
176         send_event(CHANNEL_OPENED);
177
178         assert(wait_sync_flag(&channel_data_recieved, 50));
179         send_event(CHANNEL_DATA_RECIEVED);
180
181         // All test steps executed - wait for signals to stop/start or close the mesh
182
183         while(test_running) {
184                 select(1, NULL, NULL, NULL, &main_loop_wait);
185         }
186
187         meshlink_close(mesh);
188
189         return 0;
190 }