server_hooks.cc 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. #include <exceptions/exceptions.h>
  15. #include <hooks/hooks_log.h>
  16. #include <hooks/server_hooks.h>
  17. #include <utility>
  18. #include <vector>
  19. using namespace std;
  20. using namespace isc;
  21. namespace isc {
  22. namespace hooks {
  23. // Constructor - register the pre-defined hooks and check that the indexes
  24. // assigned to them are as expected.
  25. ServerHooks::ServerHooks() {
  26. reset();
  27. }
  28. // Register a hook. The index assigned to the hook is the current number
  29. // of entries in the collection, so ensuring that hook indexes are unique
  30. // and non-negative.
  31. int
  32. ServerHooks::registerHook(const string& name) {
  33. // Determine index for the new element and insert.
  34. int index = hooks_.size();
  35. pair<HookCollection::iterator, bool> result =
  36. hooks_.insert(make_pair(name, index));
  37. if (!result.second) {
  38. // New element was not inserted because an element with the same name
  39. // already existed.
  40. isc_throw(DuplicateHook, "hook with name " << name <<
  41. " is already registered");
  42. }
  43. // Element was inserted, so add to the inverse hooks collection.
  44. inverse_hooks_[index] = name;
  45. // Log it if debug is enabled
  46. LOG_DEBUG(hooks_logger, HOOKS_DBG_TRACE, HOOKS_REGISTER_HOOK).arg(name);
  47. // ... and return numeric index.
  48. return (index);
  49. }
  50. // Reset ServerHooks object to initial state.
  51. void
  52. ServerHooks::reset() {
  53. // Log a warning - although this is done during testing, it should never be
  54. // seen in a production system.
  55. LOG_WARN(hooks_logger, HOOKS_RESET_HOOK_LIST);
  56. // Clear out the name->index and index->name maps.
  57. hooks_.clear();
  58. inverse_hooks_.clear();
  59. // Register the pre-defined hooks.
  60. int create = registerHook("context_create");
  61. int destroy = registerHook("context_destroy");
  62. // Check registration went as expected.
  63. if ((create != CONTEXT_CREATE) || (destroy != CONTEXT_DESTROY)) {
  64. isc_throw(Unexpected, "pre-defined hook indexes are not as expected. "
  65. "context_create: expected = " << CONTEXT_CREATE <<
  66. ", actual = " << create <<
  67. ". context_destroy: expected = " << CONTEXT_DESTROY <<
  68. ", actual = " << destroy);
  69. }
  70. }
  71. // Find the name associated with a hook index.
  72. std::string
  73. ServerHooks::getName(int index) const {
  74. // Get iterator to matching element.
  75. InverseHookCollection::const_iterator i = inverse_hooks_.find(index);
  76. if (i == inverse_hooks_.end()) {
  77. isc_throw(NoSuchHook, "hook index " << index << " is not recognised");
  78. }
  79. return (i->second);
  80. }
  81. // Find the index associated with a hook name.
  82. int
  83. ServerHooks::getIndex(const string& name) const {
  84. // Get iterator to matching element.
  85. HookCollection::const_iterator i = hooks_.find(name);
  86. if (i == hooks_.end()) {
  87. isc_throw(NoSuchHook, "hook name " << name << " is not recognised");
  88. }
  89. return (i->second);
  90. }
  91. // Return vector of hook names. The names are not sorted - it is up to the
  92. // caller to perform sorting if required.
  93. vector<string>
  94. ServerHooks::getHookNames() const {
  95. vector<string> names;
  96. HookCollection::const_iterator i;
  97. for (i = hooks_.begin(); i != hooks_.end(); ++i) {
  98. names.push_back(i->first);
  99. }
  100. return (names);
  101. }
  102. // Return global ServerHooks object
  103. ServerHooks&
  104. ServerHooks::getServerHooks() {
  105. static ServerHooks hooks;
  106. return (hooks);
  107. }
  108. } // namespace util
  109. } // namespace isc