Repository navigation
Expand file tree
/
Copy pathserver.cpp
More file actions
209 lines (171 loc) · 4.32 KB
/
Copy pathserver.cpp
File metadata and controls
209 lines (171 loc) · 4.32 KB
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
/**
*
*
**/
#include "server.hpp"
#include <cassert>
#include <sstream>
#include <algorithm>
#include <log4cplus/loggingmacros.h>
#include <boost/date_time/posix_time/posix_time.hpp>
#include "client.hpp"
#include "settings.hpp"
#include "backends/base.hpp"
#include "backends/factory.hpp"
using std::string;
using std::distance;
using std::remove_if;
using std::ostringstream;
using boost::asio::ip::tcp;
using boost::system::error_code;
using boost::posix_time::seconds;
/**
*
*
**/
communicator::server_t::server_t(const options_t & options, create_func_type create_func) :
log_(log4cplus::Logger::getInstance("main")),
interval_(5),
num_workers_(options.workers),
create_client_func_(create_func),
signals_(io_service_),
acceptor_(io_service_),
timer_(io_service_, seconds(interval_)),
store_backend_(backend::factory::create(options.storage)) {
LOG4CPLUS_TRACE(log_, "Initializing server");
LOG4CPLUS_TRACE(log_, "Starting starage backend");
store_backend_->start();
bind_signals();
ostringstream port_str;
port_str << options.listen.tcp_port;
start_listen(options.listen.ip_addr, port_str.str());
LOG4CPLUS_TRACE(log_, "Starting timer");
timer_.async_wait(
[this](const error_code & error) {
this->handle_timer(error);
}
);
}
/**
*
*
**/
void communicator::server_t::bind_signals() {
LOG4CPLUS_TRACE(log_, "Binding interruption signal handler");
signals_.add(SIGINT);
signals_.add(SIGTERM);
# if defined(SIGQUIT)
signals_.add(SIGQUIT);
# endif
LOG4CPLUS_TRACE(log_, "Start waiting for interruption signal");
signals_.async_wait(
[this](const error_code &, const int &) {
this->handle_stop();
}
);
}
/**
*
*
**/
void communicator::server_t::start_listen(const string & ip_addr, const string & port) {
LOG4CPLUS_TRACE(log_, "Initializing listening socket");
tcp::resolver resolver(io_service_);
tcp::resolver::query query(ip_addr, port);
tcp::endpoint endpoint = *resolver.resolve(query);
acceptor_.open(endpoint.protocol());
acceptor_.set_option(tcp::acceptor::reuse_address(true));
acceptor_.bind(endpoint);
LOG4CPLUS_TRACE(log_, "Start listening");
acceptor_.listen();
start_accept();
}
/**
*
*
**/
communicator::server_t::~server_t() {
store_backend_->stop();
}
/**
*
*
**/
void communicator::server_t::run() {
io_service_.run();
}
/**
*
*
**/
void communicator::server_t::start_accept() {
LOG4CPLUS_TRACE(log_, "Starting async accept operation");
assert(create_client_func_);
client_pointer_type client(client_t::create(io_service_, create_client_func_(), *store_backend_));
acceptor_.async_accept(
client->get_socket(),
[this, client](const error_code & error) {
this->handle_accept(error, client);
}
);
}
/**
*
*
**/
void communicator::server_t::handle_stop() {
io_service_.stop();
LOG4CPLUS_INFO(log_, "Bye!");
}
/**
*
*
**/
void communicator::server_t::handle_timer(const error_code & error) {
LOG4CPLUS_TRACE(log_, "Server timer event");
if (!error) {
timer_.expires_at(timer_.expires_at() + seconds(interval_));
timer_.async_wait(
[this](const error_code & error) {
this->handle_timer(error);
}
);
drop_unused_clients();
}
}
/**
*
*
**/
void communicator::server_t::handle_accept(const error_code & error, client_pointer_type client) {
LOG4CPLUS_DEBUG(log_, "Connection accepted");
if (!acceptor_.is_open()) {
return;
}
if (!error) {
client->start();
{
lock_guard_type lock(mutex_);
clients_.push_back(client);
}
}
start_accept();
}
/**
*
*
**/
void communicator::server_t::drop_unused_clients() {
lock_guard_type lock(mutex_);
auto pos = remove_if(
clients_.begin(),
clients_.end(),
[](const client_pointer_type & client) {
return !client->get_socket().is_open();
}
);
if (pos != clients_.end()) {
LOG4CPLUS_TRACE(log_, "Where are " << distance(pos, clients_.end()) << " disconnected client(s). Kill them all!!!");
clients_.erase(pos, clients_.end());
}
}