123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162 |
- #include <exceptions/exceptions.h>
- #include <hooks/server_hooks.h>
- #include <utility>
- #include <vector>
- using namespace std;
- using namespace isc;
- namespace isc {
- namespace util {
- ServerHooks::ServerHooks() {
- reset();
- }
- int
- ServerHooks::registerHook(const string& name) {
-
- int index = hooks_.size();
- pair<HookCollection::iterator, bool> result =
- hooks_.insert(make_pair(name, index));
- if (!result.second) {
-
-
- isc_throw(DuplicateHook, "hook with name " << name <<
- " is already registered");
- }
-
- inverse_hooks_[index] = name;
-
- return (index);
- }
- void
- ServerHooks::reset() {
-
- hooks_.clear();
- inverse_hooks_.clear();
-
- int create = registerHook("context_create");
- int destroy = registerHook("context_destroy");
-
- if ((create != CONTEXT_CREATE) || (destroy != CONTEXT_DESTROY)) {
- isc_throw(Unexpected, "pre-defined hook indexes are not as expected. "
- "context_create: expected = " << CONTEXT_CREATE <<
- ", actual = " << create <<
- ". context_destroy: expected = " << CONTEXT_DESTROY <<
- ", actual = " << destroy);
- }
- }
- std::string
- ServerHooks::getName(int index) const {
-
- InverseHookCollection::const_iterator i = inverse_hooks_.find(index);
- if (i == inverse_hooks_.end()) {
- isc_throw(NoSuchHook, "hook index " << index << " is not recognised");
- }
- return (i->second);
- }
- int
- ServerHooks::getIndex(const string& name) const {
-
- HookCollection::const_iterator i = hooks_.find(name);
- if (i == hooks_.end()) {
- isc_throw(NoSuchHook, "hook name " << name << " is not recognised");
- }
- return (i->second);
- }
- vector<string>
- ServerHooks::getHookNames() const {
- vector<string> names;
- HookCollection::const_iterator i;
- for (i = hooks_.begin(); i != hooks_.end(); ++i) {
- names.push_back(i->first);
- }
- return (names);
- }
- std::vector<HookRegistrationFunction::RegistrationFunctionPtr>&
- HookRegistrationFunction::getFunctionVector() {
- static std::vector<RegistrationFunctionPtr> reg_functions;
- return (reg_functions);
- }
- HookRegistrationFunction::HookRegistrationFunction(
- HookRegistrationFunction::RegistrationFunctionPtr reg_func) {
- getFunctionVector().push_back(reg_func);
- }
- void
- HookRegistrationFunction::execute(ServerHooks& hooks) {
- std::vector<RegistrationFunctionPtr>& reg_functions = getFunctionVector();
- for (int i = 0; i < reg_functions.size(); ++i) {
- (*reg_functions[i])(hooks);
- }
- }
- ServerHooks&
- ServerHooks::getServerHooks() {
- static ServerHooks hooks;
- return (hooks);
- }
- }
- }
|