callout_handles.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 CALLOUT_HANDLE_H
  15. #define CALLOUT_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. class CalloutHandle; // Forward declaration for CalloutHandle
  25. /// Typedef for a callout pointer
  26. extern "C" {
  27. typedef int (*CalloutPtr)(const CalloutHandle&);
  28. };
  29. /// @brief No Such Context
  30. ///
  31. /// Thrown if an attempt is made to obtain context that has not been previously
  32. /// set.
  33. class NoSuchContext : public Exception {
  34. public:
  35. NoSuchContext(const char* file, size_t line, const char* what) :
  36. isc::Exception(file, line, what) {}
  37. };
  38. /// @brief Global Callout Handle
  39. /// @todo Change name to Library Handle?
  40. ///
  41. /// This class is used to manage a loaded library. It is used by the user
  42. /// library to register callouts and by the HookManager to call them. The
  43. /// class also contains storage for library-specific context.
  44. ///
  45. /// Although there is an argument for the class to load unload the user
  46. /// library, that is handled by the HookManager to prevent the user library
  47. /// from accessing those functions.
  48. class LibraryHandle {
  49. private:
  50. /// Typedef to allow abbreviation of iterator specification in methods
  51. typedef std::map<std::string, boost::any> ContextCollection;
  52. public:
  53. /// @brief Constructor
  54. ///
  55. /// This is passed the ServerHooks object and an index number: the former
  56. /// allows for the sizing of the internal hook vector, and the latter
  57. /// is used by the CalloutHandle object to access appropriate context
  58. ///
  59. /// @param hooks Pointer to the hooks registered by the server.
  60. /// @param index Index of this library in the list of loaded libraries.
  61. LibraryHandle(boost::shared_ptr<ServerHooks> hooks, int index)
  62. : context_(), hooks_(hooks), index_(index)
  63. {}
  64. /// @brief Set Context
  65. ///
  66. /// Sets an element in the library context. If an element of the name
  67. /// is already present, it is replaced.
  68. ///
  69. /// @param name Name of the element in the context to set
  70. /// @param value Value to set
  71. template <typename T>
  72. void setContext(const std::string& name, T value) {
  73. context_[name] = value;
  74. }
  75. /// @brief Get Context
  76. ///
  77. /// Sets an element in the library context. If the name does not exist,
  78. /// a "NoSuchContext" exception is thrown.
  79. ///
  80. /// @param name Name of the element in the context to set.
  81. /// @param value [out] Value to set. The type of "value" is important:
  82. /// it must match the type of the value set.
  83. ///
  84. /// @throw NoSuchContext Thrown if no context element with the name
  85. /// "name" is present.
  86. /// @throw boost::bad_any_cast Thrown if the context element is present,
  87. /// but the type of the element is not that expected
  88. template <typename T>
  89. void getContext(const std::string& name, T& value) const {
  90. ContextCollection::const_iterator element_ptr = context_.find(name);
  91. if (element_ptr == context_.end()) {
  92. isc_throw(NoSuchContext, "unable to find library context datum " <<
  93. name << " in library at index " << index_);
  94. }
  95. value = boost::any_cast<T>(element_ptr->second);
  96. }
  97. private:
  98. /// Context - mapping of names variables that can be of different types.
  99. ContextCollection context_;
  100. /// Pointer to the list of hooks registered by the server
  101. boost::shared_ptr<ServerHooks> hooks_; ///< Pointer to hooks
  102. /// Index of this library in the list of libraries
  103. int index_;
  104. };
  105. } // namespace util
  106. } // namespace isc
  107. #endif // CALLOUT_HANDLE_H