server_hooks.cc 4.6 KB

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