| Kea 3.0.2-git

cmds_impl.h
Go to the documentation of this file.
1// Copyright (C) 2017-2023 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#ifndef CMDS_IMPL_H
7#define CMDS_IMPL_H
8
9#include <config.h>
10#include <cc/command_interpreter.h>
11#include <cc/data.h>
12#include <hooks/hooks.h>
13#include <exceptions/exceptions.h>
14
15#include <string>
16
17 namespace isc {
18 namespace config {
19
21 class CmdsImpl {
22 protected:
29 void extractCommand(hooks::CalloutHandle& handle) {
30 try {
31 data::ConstElementPtr command;
32 handle.getArgument("command", command);
33 cmd_name_ = parseCommand(cmd_args_, command);
34 } catch (const std::exception& ex) {
35 isc_throw(isc::BadValue, "JSON command text is invalid: " << ex.what());
36 }
37 }
38
43 void setSuccessResponse(hooks::CalloutHandle& handle, const std::string& text) {
44 data::ConstElementPtr response = createAnswer(CONTROL_RESULT_SUCCESS, text);
45 setResponse(handle, response);
46 }
47
54 void setErrorResponse(hooks::CalloutHandle& handle, const std::string& text,
55 int status = CONTROL_RESULT_ERROR) {
56 data::ConstElementPtr response = createAnswer(status, text);
57 setResponse(handle, response);
58 }
59
64 void setResponse(hooks::CalloutHandle& handle, data::ConstElementPtr& response) {
65 handle.setArgument("response", response);
66 }
67
69 std::string cmd_name_;
70
72 data::ConstElementPtr cmd_args_;
73};
74
75}
76}
77
78#endif // CMDS_IMPL_H
isc::BadValue
A generic exception that is thrown if a parameter given to a method is considered invalid in that con...
Definition exceptions/exceptions.h:132
isc::config::CmdsImpl
Base class that command handler implementers may use for common tasks.
Definition cmds_impl.h:21
isc::config::CmdsImpl::cmd_name_
std::string cmd_name_
Stores the command name extracted by a call to extractCommand.
Definition cmds_impl.h:69
isc::config::CmdsImpl::setErrorResponse
void setErrorResponse(hooks::CalloutHandle &handle, const std::string &text, int status=CONTROL_RESULT_ERROR)
Set the callout argument "response" to indicate an error.
Definition cmds_impl.h:54
isc::config::CmdsImpl::cmd_args_
data::ConstElementPtr cmd_args_
Stores the command arguments extracted by a call to extractCommand.
Definition cmds_impl.h:72
isc::config::CmdsImpl::extractCommand
void extractCommand(hooks::CalloutHandle &handle)
Extracts the command name and arguments from a Callout handle.
Definition cmds_impl.h:29
isc::config::CmdsImpl::setSuccessResponse
void setSuccessResponse(hooks::CalloutHandle &handle, const std::string &text)
Set the callout argument "response" to indicate success.
Definition cmds_impl.h:43
isc::config::CmdsImpl::setResponse
void setResponse(hooks::CalloutHandle &handle, data::ConstElementPtr &response)
Set the callout argument "response" to the given response.
Definition cmds_impl.h:64
isc::hooks::CalloutHandle
Per-packet callout handle.
Definition callout_handle.h:78
isc::hooks::CalloutHandle::getArgument
void getArgument(const std::string &name, T &value) const
Get argument.
Definition callout_handle.h:164
isc::hooks::CalloutHandle::setArgument
void setArgument(const std::string &name, T value)
Set argument.
Definition callout_handle.h:147
command_interpreter.h
This file contains several functions and constants that are used for handling commands and responses ...
data.h
exceptions.h
isc_throw
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
Definition exceptions/exceptions.h:210
hooks.h
isc::config
Definition command_interpreter.cc:23
isc::config::CONTROL_RESULT_ERROR
const int CONTROL_RESULT_ERROR
Status code indicating a general failure.
Definition command_interpreter.h:45
isc::config::parseCommand
std::string parseCommand(ConstElementPtr &arg, ConstElementPtr command)
Parses the given command into a string containing the actual command and an ElementPtr containing the...
Definition command_interpreter.cc:201
isc::config::createAnswer
ConstElementPtr createAnswer()
Creates a standard config/command level success answer message (i.e.
Definition command_interpreter.cc:54
isc::config::CONTROL_RESULT_SUCCESS
const int CONTROL_RESULT_SUCCESS
Status code indicating a successful operation.
Definition command_interpreter.h:42
isc::data::ConstElementPtr
boost::shared_ptr< const Element > ConstElementPtr
Definition data.h:29
isc
Defines the logger used by the top-level component of kea-lfc.
Definition agent_parser.cc:148