Commit 5d68c24b authored by Marcin Siodelski's avatar Marcin Siodelski
Browse files

[5318] Cleanup comments and log messages in the CommandMgr.

parent ab11e239
......@@ -27,6 +27,9 @@ using namespace isc::data;
namespace {
/// @brief Maximum size of the data chunk sent/received over the socket.
const size_t BUF_SIZE = 8192;
class ConnectionPool;
/// @brief Represents a single connection over control socket.
......@@ -41,10 +44,19 @@ public:
/// This constructor registers a socket of this connection in the Interface
/// Manager to cause the blocking call to @c select() to return as soon as
/// a transmission over the control socket is received.
/// @param socket Pointer to the object representing a socket which is used
/// for data transmission.
/// @param connection_pool Reference to the connection pool to which this
/// connection belongs.
Connection(const boost::shared_ptr<UnixDomainSocket>& socket,
ConnectionPool& connection_pool)
: socket_(socket), buf_(), response_(), connection_pool_(connection_pool),
feed_(), response_in_progress_(false) {
// Callback value of 0 is used to indicate that callback function is
// not installed.
isc::dhcp::IfaceMgr::instance().addExternalSocket(socket_->getNative(), 0);
......@@ -86,7 +98,7 @@ public:
/// This method doesn't block. Once the send operation is completed, the
/// @c Connection::sendHandler cllback is invoked.
void doSend() {
size_t chunk_size = response_.size() < 8192 ? response_.size() : 8192;
size_t chunk_size = (response_.size() < BUF_SIZE) ? response_.size() : BUF_SIZE;
socket_->asyncSend(&response_[0], chunk_size,
boost::bind(&Connection::sendHandler, shared_from_this(), _1, _2));
......@@ -112,7 +124,7 @@ private:
boost::shared_ptr<UnixDomainSocket> socket_;
/// @brief Buffer used for received data.
std::array<char, 65535> buf_;
std::array<char, BUF_SIZE> buf_;
/// @brief Response created by the server.
std::string response_;
......@@ -149,8 +161,13 @@ public:
/// @param connection Pointer to the new connection object.
void stop(const ConnectionPtr& connection) {
try {
} catch (const std::exception& ex) {
/// @brief Stops all connections which are allowed to stop.
......@@ -162,10 +179,6 @@ public:
size_t getConnectionsNum() const {
return (connections_.size());
/// @brief Pool of connections.
......@@ -183,15 +196,13 @@ Connection::receiveHandler(const boost::system::error_code& ec,
// connection pool.
} else if (ec.value() != boost::asio::error::operation_aborted) {
/// @todo: Should we close the connection, similar to what is already
/// being done for bytes_transferred == 0.
} else if (bytes_transferred == 0) {
......@@ -203,19 +214,22 @@ Connection::receiveHandler(const boost::system::error_code& ec,
ConstElementPtr cmd, rsp;
ConstElementPtr rsp;
try {
// Received some data over the socket. Append them to the JSON feed
// to see if we have reached the end of command.
feed_.postBuffer(&buf_[0], bytes_transferred);
// If we haven't yet received the full command, continue receiving.
if (feed_.needData()) {
// Received entire command. Parse the command into JSON.
if (feed_.feedOk()) {
cmd = feed_.toElement();
ConstElementPtr cmd = feed_.toElement();
response_in_progress_ = true;
// If successful, then process it as a command.
......@@ -224,6 +238,9 @@ Connection::receiveHandler(const boost::system::error_code& ec,
response_in_progress_ = false;
} else {
// Failed to parse command as JSON or process the received command.
// This exception will be caught below and the error response will
// be sent.
isc_throw(BadValue, feed_.getErrorMessage());
......@@ -246,46 +263,39 @@ Connection::receiveHandler(const boost::system::error_code& ec,
/* size_t len = response_.length();
if (len > 65535) {
// Hmm, our response is too large. Let's send the first
// 64KB and hope for the best.
len = 65535;
try {
// Send the data back over socket.
socket_->write(response_.c_str(), len);
} catch (const std::exception& ex) {
// Response transmission failed. Since the response failed, it doesn't
// make sense to send any status codes. Let's log it and be done with
// it.
} */
// Close the connection if we have sent the entire response.
Connection::sendHandler(const boost::system::error_code& ec,
size_t bytes_transferred) {
if (ec && ec.value() != boost::asio::error::operation_aborted) {
if (ec) {
// If an error occurred, log this error and stop the connection.
if (ec.value() != boost::asio::error::operation_aborted) {
} else {
// No error. We are in a process of sending a response. Need to
// remove the chunk that we have managed to sent with the previous
// attempt.
response_.erase(0, bytes_transferred);
// Check if there is any data left to be sent and sent it.
if (!response_.empty()) {
// All data sent or an error has occurred. Close the connection.
......@@ -392,6 +402,10 @@ CommandMgrImpl::doAccept() {
// New connection is arriving. Start asynchronous transmission.
ConnectionPtr connection(new Connection(socket_, connection_pool_));
} else if (ec.value() != boost::asio::error::operation_aborted) {
// Unless we're stopping the service, start accepting connections again.
......@@ -64,22 +64,15 @@ This is an informational message that the socket created for handling
client's connection is closed. This usually means that the client disconnected,
but may also mean a timeout.
% COMMAND_SOCKET_CONNECTION_OPENED Opened socket %1 for incoming command connection on socket %2
% COMMAND_SOCKET_CONNECTION_CLOSE_FAIL Failed to close command connection: %1
This error message is issued when an error occurred when closing a
command connection and/or removing it from the connections pool. The
detailed error is provided as an argument.
% COMMAND_SOCKET_CONNECTION_OPENED Opened socket %1 for incoming command connection
This is an informational message that a new incoming command connection was
detected and a dedicated socket was opened for that connection.
% COMMAND_SOCKET_DUP_WARN Failed to duplicate socket for response: %1
This debug message indicates that the commandReader was unable to duplicate
the connection socket prior to executing the command. This is most likely a
system resource issue. The command should still be processed and the response
sent, unless the command caused the command channel to be closed (e.g. a
reconfiguration command).
% COMMAND_SOCKET_FAIL_NONBLOCK Failed to set non-blocking mode for socket %1 created for incoming connection on socket %2: %3
This error message indicates that the server failed to set non-blocking mode
on just created socket. That socket was created for accepting specific
incoming connection. Additional information may be provided as third parameter.
% COMMAND_SOCKET_READ Received %1 bytes over command socket %2
This debug message indicates that specified number of bytes was received
over command socket identified by specified file descriptor.
......@@ -88,16 +81,6 @@ over command socket identified by specified file descriptor.
This error message indicates that an error was encountered while
reading from command socket.
% COMMAND_SOCKET_UNIX_CLOSE Command socket closed: UNIX, fd=%1, path=%2
This informational message indicates that the daemon closed a command
processing socket. This was a UNIX socket. It was opened with the file
descriptor and path specified.
% COMMAND_SOCKET_UNIX_OPEN Command socket opened: UNIX, fd=%1, path=%2
This informational message indicates that the daemon opened a command
processing socket. This is a UNIX socket. It was opened with the file
descriptor and path specified.
% COMMAND_SOCKET_WRITE Sent response of %1 bytes over command socket %2
This debug message indicates that the specified number of bytes was sent
over command socket identifier by the specified file descriptor.
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment