-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.cpp
100 lines (81 loc) · 3.07 KB
/
example.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include "bindy-static.h"
void fail(const char *message) {
std::cout << message << std::endl;
exit(1);
}
void handler_function(bindy::conn_id_t, std::vector<uint8_t> data) {
std::string text(data.begin(), data.end());
std::cout << text << std::endl;
std::cout.flush();
}
void run_client(char *argv[]) {
bindy::Bindy *bindy_ptr;
try {
bindy_ptr = bindy::bindy_create_new(argv[1], false, false);
} catch (...) {
fail("Error initializing bindy. Please check if configuration file exists.");
}
std::cout << "CLIENT started.\n";
bindy::conn_id_t conn_id;
try {
conn_id = bindy::bindy_connect_client(bindy_ptr, argv[2]);
} catch (...) {
fail("Error establishing connection to remote address.");
}
try {
// Send user message
bindy::bindy_send_data(bindy_ptr, conn_id, (uint8_t *)argv[3], strlen(argv[3]));
bindy::sleep_ms(1000); // let the server process the data
} catch (...) {
fail("Error sending data.");
}
}
void run_server(char *argv[]) {
bindy::Bindy *bindy_ptr;
try {
bindy_ptr = bindy::bindy_create_new(argv[1], true, true, 49151);
} catch (...) {
fail("Error initializing bindy. Please check if configuration file exists.");
}
try {
bindy::bindy_connect_server(bindy_ptr);
bindy_ptr->set_handler(&handler_function);
} catch (...) {
fail("Error establishing listening connection.");
}
std::cout << "SERVER started.\n";
while (true) {
bindy::conn_id_t *connections = nullptr;
size_t connections_number = bindy::bindy_list_connections(bindy_ptr, &connections);
const int buffer_size = 1024;
uint8_t buffer[buffer_size + 1];
for (size_t i = 0; i < connections_number; i++) {
bindy::conn_id_t conn_id = connections[i];
int read_data_size = bindy::bindy_read_data(bindy_ptr, conn_id, buffer, buffer_size);
if (read_data_size > 0) {
buffer[read_data_size] = 0;
std::cout << "Client from host " << bindy::bindy_get_ip_address(bindy_ptr, conn_id) << " says: " << (const char *)buffer << std::endl;
std::cout.flush();
}
}
if (connections) {
delete[] connections;
connections = nullptr;
}
bindy::sleep_ms(10);
}
}
int main(int argc, char *argv[]) {
bindy::bindy_initialize_network();
if (argc == 4) {
run_client(argv);
} else if (argc == 2) {
run_server(argv);
} else {
std::cout << "Call '" << argv[0] << " KEYFILE IP TEXT' to become a client node and send a message \"TEXT\" to node with \"IP\" address." << std::endl;
std::cout << "Call '" << argv[0] << " KEYFILE' to become a server node and listen to/receive messages from client and server nodes." << std::endl;
std::cout << "KEYFILE is a file containing authorization data. Server node must be able to find a key of client node in its keyfile." << std::endl;
}
bindy::bindy_shutdown_network();
return 0;
}