Kea  2.1.7-git
unix_control_socket.cc
Go to the documentation of this file.
1 // Copyright (C) 2018-2021 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 
28 template <> ControlSocketBasePtr
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 UnixControlSocket::configGet(const string& /*service*/) {
39  return (sendCommand(createCommand("config-get")));
40 }
41 
44  const string& /*service*/) {
45  return (sendCommand(createCommand("config-test", config)));
46 }
47 
50  const string& /*service*/) {
51  return (sendCommand(createCommand("config-set", config)));
52 }
53 
55 UnixControlSocket::sendCommand(ConstElementPtr command) {
56  // We are using our own IO service because this method is synchronous.
57  IOServicePtr io_service(new IOService());
58  ClientConnection conn(*io_service);
59  boost::system::error_code received_ec;
60  ConstJSONFeedPtr received_feed;
61 
63  ClientConnection::ControlCommand(command->toWire()),
64  [&io_service, &received_ec, &received_feed]
65  (const boost::system::error_code& ec, ConstJSONFeedPtr feed) {
66  // Capture error code and parsed data.
67  received_ec = ec;
68  received_feed = feed;
69  // Got the IO service so stop IO service. This causes to
70  // stop IO service when all handlers have been invoked.
71  io_service->stopWork();
72  },
74 
75  // Perform this synchronously.
76  io_service->run();
77 
78  if (received_ec) {
79  // Got an error.
80  isc_throw(ControlSocketError, "communication error: "
81  << received_ec.message());
82  }
83 
84  if (!received_feed) {
85  // Failed to get the answer.
86  isc_throw(ControlSocketError, "empty response");
87  }
88 
89  try {
90  return (received_feed->toElement());
91  } catch (const std::exception& ex) {
92  isc_throw(ControlSocketError, "unparsable response: " << ex.what());
93  }
94 }
95 
96 } // namespace netconf
97 } // namespace isc
Exception thrown when the error during communication.
boost::shared_ptr< ControlSocketBase > ControlSocketBasePtr
Type definition for the pointer to the ControlSocketBase.
Base class for control socket communication.
const std::string getName() const
Returns the Unix socket name.
STL namespace.
Represents client side connection over the unix domain socket.
boost::shared_ptr< UnixControlSocket > UnixControlSocketPtr
Type definition for the pointer to the UnixControlSocket.
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
virtual data::ConstElementPtr configTest(data::ConstElementPtr config, const std::string &service)
Test configuration.
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
ConstElementPtr createCommand(const std::string &command)
Creates a standard command message with no argument (of the form { "command": "my_command" }) ...
virtual data::ConstElementPtr configSet(data::ConstElementPtr config, const std::string &service)
Set configuration.
boost::shared_ptr< const Element > ConstElementPtr
Definition: data.h:27
constexpr long TIMEOUT_AGENT_FORWARD_COMMAND
Timeout for the Control Agent to forward command to a Kea server, e.g.
Definition: timeouts.h:31
Class for control socket communication over UNIX socket.
virtual data::ConstElementPtr configGet(const std::string &service)
Get configuration.
Defines the logger used by the top-level component of kea-lfc.
boost::shared_ptr< const JSONFeed > ConstJSONFeedPtr
Pointer to the const JSONFeed.
Definition: json_feed.h:27
This file contains several functions and constants that are used for handling commands and responses ...
Encapsulates timeout value.
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< CfgControlSocket > CfgControlSocketPtr
Defines a pointer for CfgControlSocket instances.
Contains declarations for UNIX control socket communication.