Kea  2.3.7
Go to the documentation of this file.
1 // Copyright (C) 2017-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
6 #ifndef CMDS_IMPL_H
7 #define CMDS_IMPL_H
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>
15 #include <string>
17 namespace isc {
18 namespace config {
21 class CmdsImpl {
22 protected:
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  }
43  void setSuccessResponse(hooks::CalloutHandle& handle, const std::string& text) {
45  setResponse (handle, response);
46  }
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  }
65  handle.setArgument ("response", response);
66  }
69  std::string cmd_name_;
73 };
75 }
76 }
78 #endif // CMDS_IMPL_H
A generic exception that is thrown if a parameter given to a method is considered invalid in that con...
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
Base class that command handler implementers may use for common tasks.
Definition: cmds_impl.h:21
std::string cmd_name_
Stores the command name extracted by a call to extractCommand.
Definition: cmds_impl.h:69
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
data::ConstElementPtr cmd_args_
Stores the command arguments extracted by a call to extractCommand.
Definition: cmds_impl.h:72
void extractCommand(hooks::CalloutHandle &handle)
Extracts the command name and arguments from a Callout handle.
Definition: cmds_impl.h:29
void setSuccessResponse(hooks::CalloutHandle &handle, const std::string &text)
Set the callout argument "response" to indicate success.
Definition: cmds_impl.h:43
void setResponse(hooks::CalloutHandle &handle, data::ConstElementPtr &response)
Set the callout argument "response" to the given response.
Definition: cmds_impl.h:64
Per-packet callout handle.
void getArgument(const std::string &name, T &value) const
Get argument.
void setArgument(const std::string &name, T value)
Set argument.
This file contains several functions and constants that are used for handling commands and responses ...
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
Status code indicating a general failure.
std::string parseCommand(ConstElementPtr &arg, ConstElementPtr command)
ConstElementPtr createAnswer(const int status_code, const std::string &text, const ConstElementPtr &arg)
Status code indicating a successful operation.
boost::shared_ptr< const Element > ConstElementPtr
Definition: data.h:27
Defines the logger used by the top-level component of kea-lfc.