15 #include <boost/static_assert.hpp>
28 CalloutManager::CalloutManager(
int num_libraries)
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");
45 CalloutManager::checkLibraryIndex(
int library_index)
const {
46 if (((library_index >= -1) && (library_index <= num_libraries_)) ||
47 (library_index == INT_MAX)) {
51 isc_throw(NoSuchLibrary,
"library index " << library_index <<
52 " is not valid for the number of loaded libraries (" <<
53 num_libraries_ <<
")");
64 .arg(library_index).arg(name);
67 checkLibraryIndex(library_index);
70 ensureHookLibsVectorSize();
74 int hook_index = server_hooks_.
getIndex(name);
79 for (CalloutVector::iterator i = hook_vector_[hook_index].begin();
80 i != hook_vector_[hook_index].end(); ++i) {
81 if (i->first > library_index) {
84 hook_vector_[hook_index].insert(i, make_pair(library_index,
93 hook_vector_[hook_index].push_back(make_pair(library_index, callout));
101 if ((hook_index < 0) || (hook_index >= hook_vector_.size())) {
103 " is not valid for the list of registered hooks");
107 return (!hook_vector_[hook_index].empty());
157 for (CalloutVector::const_iterator i = hook_vector_[hook_index].begin();
158 i != hook_vector_[hook_index].end(); ++i) {
168 int status = (*i->second)(callout_handle);
184 }
catch (
const std::exception& e) {
232 checkLibraryIndex(library_index);
235 ensureHookLibsVectorSize();
239 int hook_index = server_hooks_.
getIndex(name);
242 if (hook_index >= hook_vector_.size()) {
248 CalloutEntry target(library_index, callout);
253 size_t initial_size = hook_vector_[hook_index].size();
264 hook_vector_[hook_index].erase(remove_if(hook_vector_[hook_index].begin(),
265 hook_vector_[hook_index].end(),
266 [&target] (CalloutEntry x) {
267 return (x == target); }),
268 hook_vector_[hook_index].end());
271 bool removed = initial_size != hook_vector_[hook_index].size();
286 ensureHookLibsVectorSize();
290 int hook_index = server_hooks_.
getIndex(name);
294 CalloutEntry target(library_index, static_cast<CalloutPtr>(0));
299 size_t initial_size = hook_vector_[hook_index].size();
302 hook_vector_[hook_index].erase(remove_if(hook_vector_[hook_index].begin(),
303 hook_vector_[hook_index].end(),
304 [&target] (CalloutEntry x) {
305 return (x.first == target.first);
307 hook_vector_[hook_index].end());
310 bool removed = initial_size != hook_vector_[hook_index].size();
322 ensureHookLibsVectorSize();
326 if (hook_index < 0) {
334 hook_vector_.resize(server_hooks_.
getCount());
339 CalloutManager::ensureHookLibsVectorSize() {
341 if (hooks.
getCount() > hook_vector_.size()) {
343 hook_vector_.resize(hooks.
getCount());
void setStatus(const CalloutNextStep next)
Sets the next processing step.
const int HOOKS_DBG_EXTENDED_CALLS
int(* CalloutPtr)(CalloutHandle &)
Typedef for a callout pointer. (Callouts must have "C" linkage.)
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.
void callCallouts(int hook_index, CalloutHandle &callout_handle)
Calls the callouts for a given hook.
static ServerHooks & getServerHooks()
Return ServerHooks object.
int getCurrentHook() const
Get current hook index.
int getCount() const
Return number of hooks.
int getCurrentLibrary() const
Get current library index.
#define LOG_ERROR(LOGGER, MESSAGE)
Macro to conveniently test error output and log it.
int getIndex(const std::string &name) const
Get hook index.
const isc::log::MessageID HOOKS_CALLOUTS_COMPLETE
Local class for conversion of void pointers to function pointers.
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
A generic exception that is thrown if a parameter given to a method is considered invalid in that con...
void stop()
Stops the stopwatch.
const isc::log::MessageID HOOKS_CALLOUT_REGISTRATION
isc::log::Logger callouts_logger("callouts")
Callouts logger.
std::string logFormatLastDuration() const
Returns the last measured duration in the format directly usable in log messages. ...
Per-packet callout handle.
std::string getName(int index) const
Get hook name.
Utility class to measure code execution times.
const isc::log::MessageID HOOKS_CALLOUT_ERROR
const isc::log::MessageID HOOKS_CALLOUT_CALLED
void setCurrentHook(int hook_index)
Set current hook index.
void * dlsymPtr() const
Return pointer returned by dlsym call.
const int HOOKS_DBG_CALLS
Defines the logger used by the top-level component of kea-dhcp-ddns.
bool deregisterAllCallouts(const std::string &name, int library_index)
Removes all callouts on a hook for the current library.
static std::string commandToHookName(const std::string &command_name)
Generates hook point name for the given control command name.
std::string logFormatTotalDuration() const
Returns the total measured duration in the format directly usable in the log messages.
const isc::log::MessageID HOOKS_CALLOUT_EXCEPTION
#define LOG_DEBUG(LOGGER, LEVEL, MESSAGE)
Macro to conveniently test debug output and log it.
void setCurrentLibrary(int library_index)
Set current library index.
const isc::log::MessageID HOOKS_CALLOUT_DEREGISTERED
int findIndex(const std::string &name) const
Find hook index.
const isc::log::MessageID HOOKS_CALLOUTS_BEGIN
bool calloutsPresent(int hook_index) const
Checks if callouts are present on a hook.
const isc::log::MessageID HOOKS_ALL_CALLOUTS_DEREGISTERED
bool commandHandlersPresent(const std::string &command_name) const
Checks if control command handlers are present for the specified command.
bool deregisterCallout(const std::string &name, CalloutPtr callout, int library_index)
De-Register a callout on a hook for the current library.
void callCommandHandlers(const std::string &command_name, CalloutHandle &callout_handle)
Calls the callouts/command handlers for a given command name.
int registerHook(const std::string &name)
Register a hook.
void start()
Starts the stopwatch.