library_handle.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. // Copyright (C) 2013 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  8. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. // PERFORMANCE OF THIS SOFTWARE.
  14. #ifndef LIBRARY_HANDLE_H
  15. #define LIBRARY_HANDLE_H
  16. #include <map>
  17. #include <string>
  18. #include <boost/any.hpp>
  19. #include <boost/shared_ptr.hpp>
  20. #include <exceptions/exceptions.h>
  21. #include <util/hooks/server_hooks.h>
  22. namespace isc {
  23. namespace util {
  24. /// @brief No such hook
  25. ///
  26. /// Thrown if an attempt is made to use an invalid hook name or hook index.
  27. class NoSuchHook : public Exception {
  28. public:
  29. NoSuchHook(const char* file, size_t line, const char* what) :
  30. isc::Exception(file, line, what) {}
  31. };
  32. /// @brief No Such Context
  33. ///
  34. /// Thrown if an attempt is made to obtain context that has not been previously
  35. /// set.
  36. class NoSuchContext : public Exception {
  37. public:
  38. NoSuchContext(const char* file, size_t line, const char* what) :
  39. isc::Exception(file, line, what) {}
  40. };
  41. // Forward declaration for CalloutHandle
  42. class CalloutHandle;
  43. /// Typedef for a callout pointer
  44. extern "C" {
  45. typedef int (*CalloutPtr)(CalloutHandle&);
  46. };
  47. /// @brief Library handle
  48. ///
  49. /// This class is used to manage a loaded library. It is used by the user
  50. /// library to register callouts and by the HookManager to call them. The
  51. /// class also contains storage for library-specific context.
  52. ///
  53. /// Although there is a persuasive argument for the class to load unload the
  54. /// user library, that is handled by the HookManager to prevent the user library
  55. /// from accessing those functions.
  56. class LibraryHandle {
  57. private:
  58. /// Typedef to allow abbreviation of iterator specification in methods
  59. typedef std::map<std::string, boost::any> ContextCollection;
  60. public:
  61. /// @brief Constructor
  62. ///
  63. /// This is passed the ServerHooks object and an index number: the former
  64. /// allows for the sizing of the internal hook vector, and the latter
  65. /// is used by the CalloutHandle object to access appropriate context
  66. ///
  67. /// @param hooks Pointer to the hooks registered by the server.
  68. /// @param index Index of this library in the list of loaded libraries.
  69. LibraryHandle(boost::shared_ptr<ServerHooks> hooks, int index)
  70. : context_(), hooks_(hooks), hook_vector_(hooks->getCount()),
  71. index_(index)
  72. {}
  73. /// @brief Set context
  74. ///
  75. /// Sets an element in the library context. If an element of the name
  76. /// is already present, it is replaced.
  77. ///
  78. /// @param name Name of the element in the context to set
  79. /// @param value Value to set
  80. template <typename T>
  81. void setContext(const std::string& name, T value) {
  82. context_[name] = value;
  83. }
  84. /// @brief Get context
  85. ///
  86. /// Sets an element in the library context. If the name does not exist,
  87. /// a "NoSuchContext" exception is thrown.
  88. ///
  89. /// @param name Name of the element in the context to set.
  90. /// @param value [out] Value to set. The type of "value" is important:
  91. /// it must match the type of the value set.
  92. ///
  93. /// @throw NoSuchContext Thrown if no context element with the name
  94. /// "name" is present.
  95. /// @throw boost::bad_any_cast Thrown if the context element is present,
  96. /// but the type of the element is not that expected
  97. template <typename T>
  98. void getContext(const std::string& name, T& value) const {
  99. ContextCollection::const_iterator element_ptr = context_.find(name);
  100. if (element_ptr == context_.end()) {
  101. isc_throw(NoSuchContext, "unable to find library context datum " <<
  102. name << " in library at index " << index_);
  103. }
  104. value = boost::any_cast<T>(element_ptr->second);
  105. }
  106. /// @brief Register a callout
  107. ///
  108. /// Registers a callout function with a given hook. The callout is added
  109. /// to the end of the callouts associated with the hook.
  110. ///
  111. /// @param name Name of the hook to which the callout is added.
  112. /// @param callout Pointer to the callout function to be registered.
  113. ///
  114. /// @throw NoSuchHook Thrown if the hook name is unrecognised.
  115. /// @throw Unexpected Hooks name is valid but internal data structure is
  116. /// of the wrong size.
  117. void registerCallout(const std::string& name, CalloutPtr callout);
  118. /// @brief De-Register a callout
  119. ///
  120. /// Searches through the functions associated with the named hook and
  121. /// removes all entries matching the callout. If there are no matching
  122. /// callouts, the result is a no-op.
  123. ///
  124. /// @param name Name of the hook from which the callout is removed.
  125. /// @param callout Pointer to the callout function to be removed.
  126. ///
  127. /// @throw NoSuchHook Thrown if the hook name is unrecognised.
  128. /// @throw Unexpected Hooks name is valid but internal data structure is
  129. /// of the wrong size.
  130. void deregisterCallout(const std::string& name, CalloutPtr callout);
  131. /// @brief Removes all callouts
  132. ///
  133. /// Removes all callouts associated with a given hook. This is a no-op
  134. /// if there are no callouts associated with the hook.
  135. ///
  136. /// @param name Name of the hook from which the callouts are removed.
  137. ///
  138. /// @throw NoSuchHook Thrown if the hook name is unrecognised.
  139. void deregisterAll(const std::string& name);
  140. /// @brief Checks if callouts are present
  141. ///
  142. /// @param index Hook index for which callouts are checked.
  143. ///
  144. /// @return true if callouts are present, false if not.
  145. ///
  146. /// @throw NoSuchHook Thrown if the index is not valid.
  147. bool calloutsPresent(int index) const;
  148. /// @brief Calls the callouts for a given hook
  149. ///
  150. /// Calls the callouts associated with the given hook index.
  151. ///
  152. /// @param index Index of the hook to call.
  153. /// @param handle Reference to the CalloutHandle object for the current
  154. /// object being processed.
  155. ///
  156. /// @return Status return.
  157. ///
  158. int callCallouts(int index, CalloutHandle& handle);
  159. private:
  160. /// @brief Check hook index
  161. ///
  162. /// Checks that the hook index is valid for the hook vector. If not,
  163. /// an exception is thrown.
  164. ///
  165. /// @param index Hooks index to check.
  166. ///
  167. /// @throw NoSuchHook Thrown if the index is not valid for the hook vector.
  168. void checkHookIndex(int index) const;
  169. /// @brief Get hook index
  170. ///
  171. /// Utility function to return the index associated with a hook name. It
  172. /// also checks for validity of the index: if the name is valid, the
  173. /// index should be valid. However, as the class only keeps a pointer to
  174. /// a shared ServerHooks object, it is possible that the object was modified
  175. /// after the hook_vector_ was sized. This function performs some checks
  176. /// on the name and throws an exception if the checks fail.
  177. ///
  178. /// @param name Name of the hook to check
  179. ///
  180. /// @return Index of the hook in the hook_vector_
  181. ///
  182. /// @throw NoSuchHook Thrown if the hook name is unrecognised.
  183. /// @throw Unexpected Index not valid for the hook vector.
  184. int getHookIndex(const std::string& name) const;
  185. /// @brief Callout pointers equal
  186. ///
  187. /// Unary predicate to
  188. // Member variables
  189. /// Context - mapping of names variables that can be of different types.
  190. ContextCollection context_;
  191. /// Pointer to the list of hooks registered by the server
  192. boost::shared_ptr<ServerHooks> hooks_; ///< Pointer to hooks
  193. /// Each element in the following vector corresponds to a single hook and
  194. /// is an ordered list of callouts for that hook.
  195. std::vector<std::vector<CalloutPtr> > hook_vector_;
  196. /// Index of this library in the list of libraries
  197. int index_;
  198. };
  199. } // namespace util
  200. } // namespace isc
  201. #endif // LIBRARY_HANDLE_H