From: Stephen Morris Date: Fri, 7 Jun 2013 16:34:06 +0000 (+0100) Subject: [2974] Split out CalloutManager and LibraryHandle into separate files X-Git-Tag: bind10-1.2.0beta1-release~402^2~8 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=ab1890f4594c873aca0b7f483234de5cd3e5e01c;p=thirdparty%2Fkea.git [2974] Split out CalloutManager and LibraryHandle into separate files --- diff --git a/src/lib/util/Makefile.am b/src/lib/util/Makefile.am index 0f22a74a21..9c66fd58b4 100644 --- a/src/lib/util/Makefile.am +++ b/src/lib/util/Makefile.am @@ -39,6 +39,7 @@ libb10_util_la_SOURCES += encode/binary_from_base32hex.h libb10_util_la_SOURCES += encode/binary_from_base16.h libb10_util_la_SOURCES += hooks/callout_manager.h hooks/callout_manager.cc # libb10_util_la_SOURCES += hooks/callout_handle.h hooks/callout_handle.cc +libb10_util_la_SOURCES += hooks/library_handle.h hooks/library_handle.cc libb10_util_la_SOURCES += hooks/server_hooks.h hooks/server_hooks.cc libb10_util_la_SOURCES += random/qid_gen.h random/qid_gen.cc libb10_util_la_SOURCES += random/random_number_generator.h diff --git a/src/lib/util/hooks/callout_manager.cc b/src/lib/util/hooks/callout_manager.cc index 1cae83f0b4..400610317d 100644 --- a/src/lib/util/hooks/callout_manager.cc +++ b/src/lib/util/hooks/callout_manager.cc @@ -25,23 +25,6 @@ using namespace isc::util; namespace isc { namespace util { -// Callout manipulation - all deferred to the CalloutManager. - -void -LibraryHandle::registerCallout(const std::string& name, CalloutPtr callout) { - callout_manager_->registerCallout(library_index_, name, callout); -} - -bool -LibraryHandle::deregisterCallout(const std::string& name, CalloutPtr callout) { - return (callout_manager_->deregisterCallout(library_index_, name, callout)); -} - -bool -LibraryHandle::deregisterAllCallouts(const std::string& name) { - return (callout_manager_->deregisterAllCallouts(library_index_, name)); -} - // Register a callout for a particular library. void @@ -147,7 +130,7 @@ CalloutManager::deregisterCallout(int library_index, const std::string& name, bool CalloutManager::deregisterAllCallouts(int library_index, - const std::string& name) { + const std::string& name) { // Get the index associated with this hook (validating the name in the // process). diff --git a/src/lib/util/hooks/callout_manager.h b/src/lib/util/hooks/callout_manager.h index f1b8d8cc62..bfe88c9874 100644 --- a/src/lib/util/hooks/callout_manager.h +++ b/src/lib/util/hooks/callout_manager.h @@ -12,10 +12,11 @@ // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR // PERFORMANCE OF THIS SOFTWARE. -#ifndef LIBRARY_HANDLE_H -#define LIBRARY_HANDLE_H +#ifndef CALLOUT_MANAGER_H +#define CALLOUT_MANAGER_H #include +#include #include #include @@ -26,127 +27,6 @@ namespace isc { namespace util { -/// @brief No Such Context -/// -/// Thrown if an attempt is made to obtain context that has not been previously -/// set. - -class NoSuchLibraryContext : public Exception { -public: - NoSuchLibraryContext(const char* file, size_t line, const char* what) : - isc::Exception(file, line, what) {} -}; - -/// @brief Invalid index -/// -/// Thrown if an attempt is made to obtain a library handle but the current -/// library handle index is invalid. This will occur if the method -/// CalloutManager::getHandleVector() is called outside of a callout. - -class InvalidIndex : public Exception { -public: - InvalidIndex(const char* file, size_t line, const char* what) : - isc::Exception(file, line, what) {} -}; - -// Forward declarations -class CalloutHandle; -class CalloutManager; - -/// Typedef for a callout pointer. (Callouts must have "C" linkage.) -extern "C" { - typedef int (*CalloutPtr)(CalloutHandle&); -}; - - - - -/// @brief Library handle -/// -/// This class is used to manage a loaded library. It is used by the user -/// library to register callouts. -/// -/// The main processing is done by the CalloutManager class. By -/// presenting this object to the user-library callouts, they can manage the -/// callout list for their own library, but cannot affect the callouts registered -/// by other libraries. - -class LibraryHandle { -public: - - /// @brief Constructor - /// - /// @param hooks Library index. A number (starting at 0) that represents - /// the index of the library in the list of libraries loaded by the - /// server. - /// @param collection Back pointer to the containing CalloutManager. - /// This pointer is used to access appropriate methods in the collection - /// object. - LibraryHandle(int library_index, CalloutManager* collection) - : library_index_(library_index), callout_manager_(collection) - {} - - /// @brief Register a callout on a hook - /// - /// Registers a callout function with a given hook. The callout is added - /// to the end of the callouts for this library that are associated with - /// that hook. - /// - /// @param name Name of the hook to which the callout is added. - /// @param callout Pointer to the callout function to be registered. - /// - /// @throw NoSuchHook The hook name is unrecognised. - /// @throw Unexpected The hook name is valid but an internal data structure - /// is of the wrong size. - void registerCallout(const std::string& name, CalloutPtr callout); - - /// @brief De-Register a callout on a hook - /// - /// Searches through the functions registered by this library with the named - /// hook and removes all entries matching the callout. It does not affect - /// callouts registered by other libraries. - /// - /// @param name Name of the hook from which the callout is removed. - /// @param callout Pointer to the callout function to be removed. - /// - /// @return true if a one or more callouts were deregistered. - /// - /// @throw NoSuchHook The hook name is unrecognised. - /// @throw Unexpected The hook name is valid but an internal data structure - /// is of the wrong size. - bool deregisterCallout(const std::string& name, CalloutPtr callout); - - /// @brief Removes all callouts on a hook - /// - /// Removes all callouts associated with a given hook that were registered. - /// by this library. It does not affect callouts that were registered by - /// other libraries. - /// - /// @param name Name of the hook from which the callouts are removed. - /// - /// @return true if one or more callouts were deregistered. - /// - /// @throw NoSuchHook Thrown if the hook name is unrecognised. - bool deregisterAllCallouts(const std::string& name); - - /// @brief Return handle index - /// - /// For test purposes only, this returns the index allocated to this - /// LibraryHandle. - /// - /// @return Handle index - int getIndex() const { - return (library_index_); - } - -private: - /// Index of this handle in the library handle list - int library_index_; - - /// Back pointer to the collection object for the library - CalloutManager* callout_manager_; -}; - /// @brief Callout Manager /// @@ -284,7 +164,8 @@ private: /// @brief Compare two callout entries for library equality /// - /// This is used in callout removal code. + /// This is used in callout removal code. It just checks whether two + /// entries have the same library element. /// /// @param ent1 First callout entry to check /// @param ent2 Second callout entry to check @@ -312,4 +193,4 @@ private: } // namespace util } // namespace isc -#endif // LIBRARY_HANDLE_H +#endif // CALLOUT_MANAGER_H diff --git a/src/lib/util/hooks/library_handle.cc b/src/lib/util/hooks/library_handle.cc new file mode 100644 index 0000000000..3ea21922c7 --- /dev/null +++ b/src/lib/util/hooks/library_handle.cc @@ -0,0 +1,39 @@ +// Copyright (C) 2013 Internet Systems Consortium, Inc. ("ISC") +// +// Permission to use, copy, modify, and/or distribute this software for any +// purpose with or without fee is hereby granted, provided that the above +// copyright notice and this permission notice appear in all copies. +// +// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH +// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY +// AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, +// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM +// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE +// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR +// PERFORMANCE OF THIS SOFTWARE. + +#include +#include + +namespace isc { +namespace util { + +// Callout manipulation - all deferred to the CalloutManager. + +void +LibraryHandle::registerCallout(const std::string& name, CalloutPtr callout) { + callout_manager_->registerCallout(library_index_, name, callout); +} + +bool +LibraryHandle::deregisterCallout(const std::string& name, CalloutPtr callout) { + return (callout_manager_->deregisterCallout(library_index_, name, callout)); +} + +bool +LibraryHandle::deregisterAllCallouts(const std::string& name) { + return (callout_manager_->deregisterAllCallouts(library_index_, name)); +} + +} // namespace util +} // namespace isc diff --git a/src/lib/util/hooks/library_handle.h b/src/lib/util/hooks/library_handle.h new file mode 100644 index 0000000000..9b90245e84 --- /dev/null +++ b/src/lib/util/hooks/library_handle.h @@ -0,0 +1,122 @@ +// Copyright (C) 2013 Internet Systems Consortium, Inc. ("ISC") +// +// Permission to use, copy, modify, and/or distribute this software for any +// purpose with or without fee is hereby granted, provided that the above +// copyright notice and this permission notice appear in all copies. +// +// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH +// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY +// AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, +// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM +// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE +// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR +// PERFORMANCE OF THIS SOFTWARE. + +#ifndef LIBRARY_HANDLE_H +#define LIBRARY_HANDLE_H + +#include + +namespace isc { +namespace util { + +// Forward declarations +class CalloutHandle; +class CalloutManager; + +/// Typedef for a callout pointer. (Callouts must have "C" linkage.) +extern "C" { + typedef int (*CalloutPtr)(CalloutHandle&); +}; + +/// @brief Library handle +/// +/// This class is accessed by the user library when registering callouts, +/// either by the library's load() function, or by one of the callouts +/// themselves. +/// +/// It is really little more than a shell around the CalloutManager/ By +/// presenting this object to the user-library callouts, callouts can manage the +/// callout list for their own library, but cannot affect the callouts +/// registered by other libraries. + +class LibraryHandle { +public: + + /// @brief Constructor + /// + /// @param hooks Library index. A number (starting at 0) that represents + /// the index of the library in the list of libraries loaded by the + /// server. + /// @param collection Back pointer to the containing CalloutManager. + /// This pointer is used to access appropriate methods in that + /// object. + LibraryHandle(int library_index, CalloutManager* collection) + : library_index_(library_index), callout_manager_(collection) + {} + + /// @brief Register a callout on a hook + /// + /// Registers a callout function with a given hook. The callout is added + /// to the end of the callouts for this library that are associated with + /// that hook. + /// + /// @param name Name of the hook to which the callout is added. + /// @param callout Pointer to the callout function to be registered. + /// + /// @throw NoSuchHook The hook name is unrecognised. + /// @throw Unexpected The hook name is valid but an internal data structure + /// is of the wrong size. + void registerCallout(const std::string& name, CalloutPtr callout); + + /// @brief De-Register a callout on a hook + /// + /// Searches through the functions registered by this library with the named + /// hook and removes all entries matching the callout. It does not affect + /// callouts registered by other libraries. + /// + /// @param name Name of the hook from which the callout is removed. + /// @param callout Pointer to the callout function to be removed. + /// + /// @return true if a one or more callouts were deregistered. + /// + /// @throw NoSuchHook The hook name is unrecognised. + /// @throw Unexpected The hook name is valid but an internal data structure + /// is of the wrong size. + bool deregisterCallout(const std::string& name, CalloutPtr callout); + + /// @brief Removes all callouts on a hook + /// + /// Removes all callouts associated with a given hook that were registered. + /// by this library. It does not affect callouts that were registered by + /// other libraries. + /// + /// @param name Name of the hook from which the callouts are removed. + /// + /// @return true if one or more callouts were deregistered. + /// + /// @throw NoSuchHook Thrown if the hook name is unrecognised. + bool deregisterAllCallouts(const std::string& name); + + /// @brief Return handle index + /// + /// For test purposes only, this returns the index allocated to this + /// LibraryHandle. + /// + /// @return Handle index + int getIndex() const { + return (library_index_); + } + +private: + /// Index of this handle in the library handle list + int library_index_; + + /// Back pointer to the collection object for the library + CalloutManager* callout_manager_; +}; + +} // namespace util +} // namespace isc + +#endif // LIBRARY_HANDLE_H diff --git a/src/lib/util/tests/callout_manager_unittest.cc b/src/lib/util/tests/callout_manager_unittest.cc index 214eec2338..02943054e4 100644 --- a/src/lib/util/tests/callout_manager_unittest.cc +++ b/src/lib/util/tests/callout_manager_unittest.cc @@ -15,6 +15,7 @@ #include #include #include +#include #include #include