-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
224 lines (167 loc) · 4.89 KB
/
main.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
//
// async_tcp_echo_server.cpp -> main.cpp
// ~~~~~~~~~~~~~~~~~~~~~~~~~
//
#include <cstdlib>
#include <iostream>
#include <memory>
#include <utility>
#include <set>
#include "IPSME_MsgEnv.h"
#include "msg_cache-dedup.h"
duplicate g_duplicate;
#include <asio/ts/buffer.hpp>
#include <asio/ts/internet.hpp>
using asio::ip::tcp;
using namespace std::chrono_literals;
typedef std::shared_ptr<tcp::socket> t_ptr_socket;
typedef std::set<t_ptr_socket> t_set_sockets;
t_set_sockets g_set_sockets;
enum { max_length = 0xffff };
char msg_[max_length];
//----------------------------------------------------------------------------------------------------------------
// pragma mark asio -> sdbus
void do_read(t_ptr_socket ptr_socket)
{
// std::cerr << "do_read(): \n";
ptr_socket->async_read_some(asio::buffer(msg_, max_length), [ptr_socket](std::error_code ec, std::size_t length)
{
if (ec) {
std::cerr << "async_read_some(): ERROR! " << ec << "\n";
if (ptr_socket->is_open()) {
ptr_socket->close();
g_set_sockets.erase(ptr_socket);
}
return;
}
// std::cerr << "async_read_some(): " << msg_ << "\n";
std::string str_msg= std::string(msg_, length);
if (true == g_duplicate.exists(str_msg))
std::cerr << "async_read_some(): asio ->| *DUP -- [" << str_msg << "]\n";
else
{
std::cerr << "handler_msg(kpks_recvd): asio -> sdbus -- [" << str_msg << "]\n";
IPSME_MsgEnv::publish("s", str_msg.c_str());
}
do_read(ptr_socket);
});
}
void do_write(t_ptr_socket ptr_socket, std::string str_msg)
{
asio::async_write(*ptr_socket, asio::buffer(str_msg), [ptr_socket](std::error_code ec, std::size_t /*length*/)
{
if (ec) {
std::cerr << "async_write(): ERROR! " << ec << "\n";
if (ptr_socket->is_open()) {
ptr_socket->close();
g_set_sockets.erase(ptr_socket);
}
return;
}
});
}
//----------------------------------------------------------------------------------------------------------------
// pragma mark asio <- sdbus
bool handler_str_(sd_bus_message* p_msg, std::string str_msg)
{
std::cerr << __func__ << ": asio[" << g_set_sockets.size() << "] <- sdbus -- [" << str_msg.c_str() << "]\n";
g_duplicate.cache(str_msg, t_entry_context(30s));
for (auto it : g_set_sockets)
do_write(it, str_msg);
return true;
}
std::string bus_message_str(sd_bus_message* p_msg) {
const char* psz= nullptr;
int i_r = sd_bus_message_read_basic(p_msg, SD_BUS_TYPE_STRING, &psz);
if (i_r < 0)
throw i_r;
return psz;
}
void handler_(sd_bus_message* p_msg)
{
// printf("%s: \n", __func__);
try {
char ch_type;
if (sd_bus_message_peek_type(p_msg, &ch_type, nullptr) && (ch_type=='s') && handler_str_(p_msg, bus_message_str(p_msg) ))
return;
}
catch (int &i_r) {
printf("ERR: error is message execution: %s \n", strerror(-i_r));
}
catch (...) {
printf("ERR: error is message execution \n");
return;
}
printf("%s: DROP! \n", __func__);
}
//----------------------------------------------------------------------------------------------------------------
// pragma mark server
class server
{
tcp::acceptor acceptor_;
tcp::socket socket_;
void do_accept()
{
acceptor_.async_accept(socket_, [this](std::error_code ec)
{
if (!ec)
{
t_ptr_socket ptr_socket= std::make_shared<tcp::socket>(std::move(socket_));
g_set_sockets.insert(ptr_socket);
tcp::endpoint r_ep= ptr_socket->remote_endpoint();
tcp::endpoint l_ep= ptr_socket->local_endpoint();
std::cerr << "do_accept(): client [" << r_ep.address() << ":" << r_ep.port() << "] connected on port [" << l_ep.port() << "]\n";
do_read(ptr_socket);
}
do_accept();
});
}
public:
server(asio::io_context& io_context, short port)
: acceptor_(io_context, tcp::endpoint(tcp::v4(), port)), socket_(io_context)
{
do_accept();
}
};
#ifdef _WIN32
const char k_sep_ = '\\';
#else
const char k_sep_ = '/';
#endif
int main(int argc, char* argv[])
{
std::string str_exec= argv[0];
const size_t i = str_exec.rfind(k_sep_, str_exec.length());
if (i != std::string::npos)
str_exec= str_exec.substr(i+1, str_exec.length());
if (argc != 2) {
std::cerr << "Usage: " << str_exec << " <port>\n";
return 1;
}
try
{
// -----
std::cerr << __func__ << ": " << "Connecting ..." << "\n";
IPSME_MsgEnv::subscribe(&handler_);
// -----
asio::io_context ioctx;
int i_port= std::atoi(argv[1]);
server s(ioctx, i_port);
std::cout << __func__ << ": " << str_exec << " listening on [" << i_port << "]\n";
std::cerr << __func__ << ": " << "Running ..." << "\n";
// ioctx.run();
while (true)
{
ioctx.poll();
IPSME_MsgEnv::process_requests();
// if (ioctx.stopped() && ioctx.poll_one() == 0)
// break;
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
}
catch (std::exception& e)
{
std::cerr << "Exception: " << e.what() << "\n";
}
return 0;
}