command_mgr.h 3.46 KB
Newer Older
1
// Copyright (C) 2015-2017 Internet Systems Consortium, Inc. ("ISC")
2
//
3 4 5
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
6 7 8 9 10

#ifndef COMMAND_MGR_H
#define COMMAND_MGR_H

#include <cc/data.h>
11
#include <config/hooked_command_mgr.h>
Tomek Mrugalski's avatar
Tomek Mrugalski committed
12
#include <config/command_socket.h>
13
#include <boost/noncopyable.hpp>
14
#include <list>
15 16 17 18

namespace isc {
namespace config {

19
/// @brief Commands Manager implementation for the Kea servers.
20
///
21 22
/// This class extends @ref BaseCommandMgr with the ability to receive and
/// respond to commands over unix domain sockets.
23
class CommandMgr : public HookedCommandMgr, public boost::noncopyable {
24 25 26 27 28 29 30 31
public:

    /// @brief CommandMgr is a singleton class. This method returns reference
    /// to its sole instance.
    ///
    /// @return the only existing instance of the manager
    static CommandMgr& instance();

32
    /// @brief Opens control socket with parameters specified in socket_info
33 34 35 36
    ///
    /// Currently supported types are:
    /// - unix (required parameters: socket-type: unix, socket-name:/unix/path)
    ///
37 38 39 40
    /// This method will close previously open command socket (if exists).
    ///
    /// @throw CommandSocketError if socket creation fails.
    /// @throw SocketError if command socket is already open.
41 42
    ///
    /// @param socket_info describes control socket parameters
Tomek Mrugalski's avatar
Tomek Mrugalski committed
43 44 45
    /// @return object representing a socket
    CommandSocketPtr
    openCommandSocket(const isc::data::ConstElementPtr& socket_info);
46 47

    /// @brief Shuts down any open control sockets
48
    void closeCommandSocket();
49

50 51 52
    /// @brief Reads data from a socket, parses as JSON command and processes it
    ///
    /// This method is used to handle traffic on connected socket. This callback
53 54
    /// is installed by the @c isc::config::UnixCommandSocket::receiveHandler
    /// (located in the src/lib/config/command_socket_factory.cc)
Tomek Mrugalski's avatar
Tomek Mrugalski committed
55 56 57
    /// once the incoming connection is accepted. If end-of-file is detected, this
    /// method will close the socket and will uninstall itself from
    /// @ref isc::dhcp::IfaceMgr.
58 59 60 61
    ///
    /// @param sockfd socket descriptor of a connected socket
    static void commandReader(int sockfd);

Tomek Mrugalski's avatar
Tomek Mrugalski committed
62 63 64 65 66 67 68 69 70 71
    /// @brief Adds an information about opened connection socket
    ///
    /// @param conn Connection socket to be stored
    void addConnection(const CommandSocketPtr& conn);

    /// @brief Closes connection with a specific socket descriptor
    ///
    /// @param fd socket descriptor
    /// @return true if closed successfully, false if not found
    bool closeConnection(int fd);
72 73 74 75 76

    /// @brief Returns control socket descriptor
    ///
    /// This method should be used only in tests.
    int getControlSocketFD() const {
Tomek Mrugalski's avatar
Tomek Mrugalski committed
77
        return (socket_->getFD());
78 79
    }

80 81 82 83 84 85 86
private:

    /// @brief Private constructor
    ///
    /// Registers internal 'list-commands' command.
    CommandMgr();

Tomek Mrugalski's avatar
Tomek Mrugalski committed
87 88 89 90 91
    /// @brief Control socket structure
    ///
    /// This is the socket that accepts incoming connections. There can be at
    /// most one (if command channel is configured).
    CommandSocketPtr socket_;
92

Tomek Mrugalski's avatar
Tomek Mrugalski committed
93 94 95 96 97
    /// @brief Sockets for open connections
    ///
    /// These are the sockets that are dedicated to handle a specific connection.
    /// Their number is equal to number of current control connections.
    std::list<CommandSocketPtr> connections_;
98 99 100 101 102 103
};

}; // end of isc::config namespace
}; // end of isc namespace

#endif