15#include <boost/static_assert.hpp>
29 : server_hooks_(
ServerHooks::getServerHooks()), current_library_(-1),
30 hook_vector_(
ServerHooks::getServerHooks().getCount()),
31 library_handle_(*this), pre_library_handle_(*this, 0),
32 post_library_handle_(*this, INT_MAX), num_libraries_(num_libraries) {
33 if (num_libraries < 0) {
35 "CalloutManager must be >= 0");
44CalloutManager::checkLibraryIndex(
int library_index)
const {
45 if (((library_index >= -1) && (library_index <= num_libraries_)) ||
46 (library_index == INT_MAX)) {
50 isc_throw(NoSuchLibrary,
"library index " << library_index <<
51 " is not valid for the number of loaded libraries (" <<
52 num_libraries_ <<
")");
62 .arg(library_index).arg(name);
65 checkLibraryIndex(library_index);
68 ensureHookLibsVectorSize();
72 int hook_index = server_hooks_.
getIndex(name);
77 for (CalloutVector::iterator i = hook_vector_[hook_index].begin();
78 i != hook_vector_[hook_index].end(); ++i) {
79 if (i->first > library_index) {
82 hook_vector_[hook_index].insert(i, make_pair(library_index,
91 hook_vector_[hook_index].push_back(make_pair(library_index, callout));
98 if ((hook_index < 0) || (hook_index >= hook_vector_.size())) {
100 " is not valid for the list of registered hooks");
104 return (!hook_vector_[hook_index].empty());
141 callout_handle.setCurrentHook(hook_index);
149 .arg(server_hooks_.
getName(callout_handle.getCurrentHook()));
152 for (
auto const& i : hook_vector_[hook_index]) {
157 callout_handle.setCurrentLibrary(i.first);
162 int status = (*i.second)(callout_handle);
167 .arg(callout_handle.getCurrentLibrary())
168 .arg(server_hooks_.
getName(callout_handle.getCurrentHook()))
170 .arg(stopwatch.logFormatLastDuration());
173 .arg(server_hooks_.
getName(callout_handle.getCurrentHook()))
174 .arg(callout_handle.getCurrentLibrary())
176 .arg(stopwatch.logFormatLastDuration());
178 }
catch (
const std::exception& e) {
184 .arg(server_hooks_.
getName(callout_handle.getCurrentHook()))
185 .arg(callout_handle.getCurrentLibrary())
188 .arg(stopwatch.logFormatLastDuration());
196 .arg(server_hooks_.
getName(callout_handle.getCurrentHook()))
197 .arg(callout_handle.getCurrentLibrary())
199 .arg(
"Unspecified exception")
200 .arg(stopwatch.logFormatLastDuration());
208 .arg(server_hooks_.
getName(callout_handle.getCurrentHook()))
209 .arg(stopwatch.logFormatTotalDuration());
213 callout_handle.setCurrentHook(-1);
214 callout_handle.setCurrentLibrary(-1);
235 checkLibraryIndex(library_index);
238 ensureHookLibsVectorSize();
242 int hook_index = server_hooks_.
getIndex(name);
245 if (hook_index >= hook_vector_.size()) {
251 CalloutEntry target(library_index, callout);
256 size_t initial_size = hook_vector_[hook_index].size();
267 hook_vector_[hook_index].erase(remove_if(hook_vector_[hook_index].begin(),
268 hook_vector_[hook_index].end(),
269 [&target] (CalloutEntry x) {
270 return (x == target); }),
271 hook_vector_[hook_index].end());
274 bool removed = initial_size != hook_vector_[hook_index].size();
288 ensureHookLibsVectorSize();
292 int hook_index = server_hooks_.
getIndex(name);
296 CalloutEntry target(library_index,
static_cast<CalloutPtr>(0));
301 size_t initial_size = hook_vector_[hook_index].size();
304 hook_vector_[hook_index].erase(remove_if(hook_vector_[hook_index].begin(),
305 hook_vector_[hook_index].end(),
306 [&target] (CalloutEntry x) {
307 return (x.first == target.first);
309 hook_vector_[hook_index].end());
312 bool removed = initial_size != hook_vector_[hook_index].size();
324 ensureHookLibsVectorSize();
328 if (hook_index < 0) {
336 hook_vector_.resize(server_hooks_.
getCount());
341CalloutManager::ensureHookLibsVectorSize() {
343 if (hooks.
getCount() > hook_vector_.size()) {
345 hook_vector_.resize(hooks.
getCount());
A generic exception that is thrown if a parameter given to a method is considered invalid in that con...
Per-packet callout handle.
@ NEXT_STEP_CONTINUE
continue normally
@ NEXT_STEP_DROP
drop the packet
void callCallouts(int hook_index, CalloutHandle &callout_handle)
Calls the callouts for a given hook.
bool commandHandlersPresent(const std::string &command_name) const
Checks if control command handlers are present for the specified command.
void callCommandHandlers(const std::string &command_name, CalloutHandle &callout_handle)
Calls the callouts/command handlers for a given command name.
CalloutManager(int num_libraries=0)
Constructor.
bool deregisterAllCallouts(const std::string &name, int library_index)
Removes all callouts on a hook for the current library.
bool deregisterCallout(const std::string &name, CalloutPtr callout, int library_index)
De-Register a callout on a hook for the current library.
bool calloutsPresent(int hook_index) const
Checks if callouts are present on a hook.
void registerCommandHook(const std::string &command_name)
Registers a hook point for the specified command name.
void registerCallout(const std::string &name, CalloutPtr callout, int library_index)
Register a callout on a hook for the current library.
Local class for conversion of void pointers to function pointers.
void * dlsymPtr() const
Return pointer returned by dlsym call.
int getIndex(const std::string &name) const
Get hook index.
static ServerHooks & getServerHooks()
Return ServerHooks object.
int findIndex(const std::string &name) const
Find hook index.
int getCount() const
Return number of hooks.
static std::string commandToHookName(const std::string &command_name)
Generates hook point name for the given control command name.
int registerHook(const std::string &name)
Register a hook.
std::string getName(int index) const
Get hook name.
Utility class to measure code execution times.
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
#define LOG_ERROR(LOGGER, MESSAGE)
Macro to conveniently test error output and log it.
#define LOG_DEBUG(LOGGER, LEVEL, MESSAGE)
Macro to conveniently test debug output and log it.
int(* CalloutPtr)(CalloutHandle &)
Typedef for a callout pointer. (Callouts must have "C" linkage.)
const int HOOKS_DBG_EXTENDED_CALLS
const isc::log::MessageID HOOKS_CALLOUTS_COMPLETE
const isc::log::MessageID HOOKS_CALLOUT_EXCEPTION
const isc::log::MessageID HOOKS_CALLOUTS_BEGIN
const isc::log::MessageID HOOKS_CALLOUT_REGISTRATION
const int HOOKS_DBG_CALLS
isc::log::Logger callouts_logger("callouts")
Callouts logger.
const isc::log::MessageID HOOKS_ALL_CALLOUTS_DEREGISTERED
const isc::log::MessageID HOOKS_CALLOUT_ERROR
const isc::log::MessageID HOOKS_CALLOUT_DEREGISTERED
const isc::log::MessageID HOOKS_CALLOUT_CALLED
Defines the logger used by the top-level component of kea-lfc.