14#include <boost/enable_shared_from_this.hpp>
67 void doSend(
const void* buffer,
const size_t length,
86 void terminate(
const boost::system::error_code& ec,
106 std::string current_command_;
109 std::array<char, 32768> read_buf_;
119 : socket_(io_service), feed_(), current_command_(), timer_(io_service),
143 current_command_.assign(command.control_command_);
147 auto self(shared_from_this());
150 [
this, self, command, handler](
const boost::system::error_code& ec) {
155 terminate(ec, handler);
160 doSend(current_command_.c_str(), current_command_.length(),
167ClientConnectionImpl::doSend(
const void* buffer,
const size_t length,
168 ClientConnection::Handler handler) {
171 auto self(shared_from_this());
174 socket_.asyncSend(buffer, length,
175 [
this, self, buffer, length, handler]
176 (
const boost::system::error_code& ec,
size_t bytes_transferred) {
181 terminate(ec, handler);
185 scheduleTimer(handler);
191 if (bytes_transferred < length) {
192 doSend(
static_cast<const char*
>(buffer) + bytes_transferred,
193 length - bytes_transferred, handler);
204ClientConnectionImpl::doReceive(ClientConnection::Handler handler) {
207 auto self(shared_from_this());
208 socket_.asyncReceive(&read_buf_[0], read_buf_.size(),
209 [
this, self, handler]
210 (
const boost::system::error_code& ec,
size_t length) {
215 terminate(ec, handler);
219 scheduleTimer(handler);
221 std::string x(&read_buf_[0], length);
226 feed_.reset(new JSONFeed());
231 feed_->postBuffer(&read_buf_[0], length);
235 if (feed_->needData()) {
241 terminate(ec, handler);
248ClientConnectionImpl::terminate(
const boost::system::error_code& ec,
249 ClientConnection::Handler handler) {
253 current_command_.clear();
264ClientConnectionImpl::timeoutCallback(ClientConnection::Handler handler) {
268 terminate(boost::asio::error::timed_out, handler);
280 impl_->start(socket_path, command, handler, timeout);
The IntervalTimer class is a wrapper for the ASIO boost::asio::deadline_timer class.
void setup(const Callback &cbfunc, const long interval, const Mode &mode=REPEATING)
Register timer callback function and interval.
Represents unix domain socket implemented in terms of boost asio.
void asyncConnect(const std::string &path, const ConnectHandler &handler)
Asynchronously connects the socket to the specified endpoint.
Implementation of the ClientConnection.
void start(const ClientConnection::SocketPath &socket_path, const ClientConnection::ControlCommand &command, ClientConnection::Handler handler, const ClientConnection::Timeout &timeout)
Starts asynchronous transaction with a remote endpoint.
void timeoutCallback(ClientConnection::Handler handler)
Callback invoked when the timeout occurs.
void scheduleTimer(ClientConnection::Handler handler)
This method schedules timer or reschedules existing timer.
ClientConnectionImpl(const IOServicePtr &io_service)
Constructor.
void doSend(const void *buffer, const size_t length, ClientConnection::Handler handler)
Starts asynchronous send.
void stop()
Closes the socket.
void terminate(const boost::system::error_code &ec, ClientConnection::Handler handler)
Terminates the connection and invokes a user callback indicating an error.
void doReceive(ClientConnection::Handler handler)
Starts asynchronous receive from the server.
std::function< void(const boost::system::error_code &ec, const ConstJSONFeedPtr &feed) Handler)
Type of the callback invoked when the communication with the server is complete or an error has occur...
void start(const SocketPath &socket_path, const ControlCommand &command, Handler handler, const Timeout &timeout=Timeout(5000))
Starts asynchronous transaction with a remote endpoint.
boost::shared_ptr< IOService > IOServicePtr
Defines a smart pointer to an IOService instance.
boost::shared_ptr< JSONFeed > JSONFeedPtr
Pointer to the JSONFeed.
Defines the logger used by the top-level component of kea-lfc.
Encapsulates control command.
Encapsulates socket path.
Encapsulates timeout value.