Kea  1.5.0
unix_control_socket.cc
Go to the documentation of this file.
1 // Copyright (C) 2018 Internet Systems Consortium, Inc. ("ISC")
2 //
3 // This Source Code Form is subject to the terms of the Mozilla Public
4 // License, v. 2.0. If a copy of the MPL was not distributed with this
5 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
6 
9 
10 #include <config.h>
11 
13 #include <asiolink/asio_wrapper.h>
14 #include <asiolink/io_service.h>
15 #include <cc/command_interpreter.h>
16 #include <cc/json_feed.h>
18 #include <config/timeouts.h>
19 
20 using namespace std;
21 using namespace isc::asiolink;
22 using namespace isc::config;
23 using namespace isc::data;
24 
25 namespace isc {
26 namespace netconf {
27 
29 createControlSocket<CfgControlSocket::Type::UNIX>(CfgControlSocketPtr ctrl_sock) {
30  return (UnixControlSocketPtr(new UnixControlSocket(ctrl_sock)));
31 }
32 
33 UnixControlSocket::UnixControlSocket(CfgControlSocketPtr ctrl_sock)
34  : ControlSocketBase(ctrl_sock) {
35 }
36 
38 }
39 
41 UnixControlSocket::configGet(const string& /*service*/) {
42  return (sendCommand(createCommand("config-get")));
43 }
44 
47  const string& /*service*/) {
48  return (sendCommand(createCommand("config-test", config)));
49 }
50 
53  const string& /*service*/) {
54  return (sendCommand(createCommand("config-set", config)));
55 }
56 
58 UnixControlSocket::sendCommand(ConstElementPtr command) {
59  // We are using our own IO service because this method is synchronous.
60  IOServicePtr io_service(new IOService());
61  ClientConnection conn(*io_service);
62  boost::system::error_code received_ec;
63  ConstJSONFeedPtr received_feed;
64 
66  ClientConnection::ControlCommand(command->toWire()),
67  [&io_service, &received_ec, &received_feed]
68  (const boost::system::error_code& ec, ConstJSONFeedPtr feed) {
69  // Capture error code and parsed data.
70  received_ec = ec;
71  received_feed = feed;
72  // Got the IO service so stop IO service. This causes to
73  // stop IO service when all handlers have been invoked.
74  io_service->stopWork();
75  },
77 
78  // Perform this synchronously.
79  io_service->run();
80 
81  if (received_ec) {
82  // Got an error.
83  isc_throw(ControlSocketError, "communication error: "
84  << received_ec.message());
85  }
86 
87  if (!received_feed) {
88  // Failed to get the answer.
89  isc_throw(ControlSocketError, "empty response");
90  }
91 
92  try {
93  return (received_feed->toElement());
94  } catch (const std::exception& ex) {
95  isc_throw(ControlSocketError, "unparsable response: " << ex.what());
96  }
97 }
98 
99 } // namespace netconf
100 } // namespace isc
isc::config::ClientConnection::Timeout
Encapsulates timeout value.
Definition: client_connection.h:94
isc::netconf::ControlSocketError
Exception thrown when the error during communication.
Definition: control_socket.h:20
isc::config
Definition: command_interpreter.cc:23
isc::data
Definition: cfg_to_element.h:25
io_service.h
isc
Defines the logger used by the top-level component of kea-dhcp-ddns.
Definition: agent_parser.cc:144
isc::netconf::UnixControlSocket::configTest
virtual data::ConstElementPtr configTest(data::ConstElementPtr config, const std::string &service)
Test configuration.
Definition: unix_control_socket.cc:46
isc_throw
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
Definition: exceptions/exceptions.h:192
isc::config::ClientConnection::ControlCommand
Encapsulates control command.
Definition: client_connection.h:86
isc::config::createCommand
ConstElementPtr createCommand(const std::string &command)
Creates a standard command message with no argument (of the form { "command": "my_command" })
Definition: command_interpreter.cc:137
command_interpreter.h
isc::config::TIMEOUT_AGENT_FORWARD_COMMAND
constexpr long TIMEOUT_AGENT_FORWARD_COMMAND
Timeout for the Control Agent to forward command to a Kea server, e.g.
Definition: timeouts.h:31
isc::netconf::UnixControlSocket::configSet
virtual data::ConstElementPtr configSet(data::ConstElementPtr config, const std::string &service)
Set configuration.
Definition: unix_control_socket.cc:52
json_feed.h
client_connection.h
isc::netconf::UnixControlSocket
Class for control socket communication over UNIX socket.
Definition: unix_control_socket.h:23
isc::config::ConstJSONFeedPtr
boost::shared_ptr< const JSONFeed > ConstJSONFeedPtr
Pointer to the const JSONFeed.
Definition: json_feed.h:27
isc::netconf::UnixControlSocket::configGet
virtual data::ConstElementPtr configGet(const std::string &service)
Get configuration.
Definition: unix_control_socket.cc:41
isc::netconf::ControlSocketBase::getName
const std::string getName() const
Returns the Unix socket name.
Definition: control_socket.h:60
isc::netconf::ControlSocketBasePtr
boost::shared_ptr< ControlSocketBase > ControlSocketBasePtr
Type definition for the pointer to the ControlSocketBase.
Definition: control_socket.h:107
isc::netconf::UnixControlSocketPtr
boost::shared_ptr< UnixControlSocket > UnixControlSocketPtr
Type definition for the pointer to the UnixControlSocket.
Definition: unix_control_socket.h:77
isc::netconf::CfgControlSocketPtr
boost::shared_ptr< CfgControlSocket > CfgControlSocketPtr
Defines a pointer for CfgControlSocket instances.
Definition: netconf_config.h:144
unix_control_socket.h
isc::netconf::UnixControlSocket::~UnixControlSocket
virtual ~UnixControlSocket()
Destructor (does nothing).
Definition: unix_control_socket.cc:37
isc::config::ClientConnection::SocketPath
Encapsulates socket path.
Definition: client_connection.h:78
isc::config::ClientConnection
Represents client side connection over the unix domain socket.
Definition: client_connection.h:70
isc::data::ConstElementPtr
boost::shared_ptr< const Element > ConstElementPtr
Definition: data.h:23
timeouts.h
asio_wrapper.h
isc::netconf::ControlSocketBase
Base class for control socket communication.
Definition: control_socket.h:32