Kea 1.5.0
hooks_manager.cc
Go to the documentation of this file.
1// Copyright (C) 2013-2018 Internet Systems Consortium, Inc. ("ISC")
2//
3// This Source Code Form is subject to the terms of the Mozilla Public
4// License, v. 2.0. If a copy of the MPL was not distributed with this
5// file, You can obtain one at http://mozilla.org/MPL/2.0/.
6
7#include <config.h>
8
14#include <hooks/hooks_manager.h>
15#include <hooks/server_hooks.h>
16
17#include <boost/shared_ptr.hpp>
18
19#include <string>
20#include <vector>
21
22using namespace std;
23
24namespace isc {
25namespace hooks {
26
27// Constructor
28
29HooksManager::HooksManager() {
30}
31
32// Return reference to singleton hooks manager.
33
34HooksManager&
36 static HooksManager manager;
37 return (manager);
38}
39
40// Are callouts present?
41
42bool
43HooksManager::calloutsPresentInternal(int index) {
44 conditionallyInitialize();
45 return (callout_manager_->calloutsPresent(index));
46}
47
48bool
50 return (getHooksManager().calloutsPresentInternal(index));
51}
52
53bool
54HooksManager::commandHandlersPresentInternal(const std::string& command_name) {
55 conditionallyInitialize();
56 return (callout_manager_->commandHandlersPresent(command_name));
57}
58
59bool
60HooksManager::commandHandlersPresent(const std::string& command_name) {
61 return (getHooksManager().commandHandlersPresentInternal(command_name));
62}
63
64// Call the callouts
65
66void
67HooksManager::callCalloutsInternal(int index, CalloutHandle& handle) {
68 conditionallyInitialize();
69 callout_manager_->callCallouts(index, handle);
70}
71
72void
74 getHooksManager().callCalloutsInternal(index, handle);
75}
76
77void
78HooksManager::callCommandHandlersInternal(const std::string& command_name,
79 CalloutHandle& handle) {
80 conditionallyInitialize();
81 callout_manager_->callCommandHandlers(command_name, handle);
82}
83
84void
85HooksManager::callCommandHandlers(const std::string& command_name,
86 CalloutHandle& handle) {
87 getHooksManager().callCommandHandlersInternal(command_name, handle);
88}
89
90
91// Load the libraries. This will delete the previously-loaded libraries
92// (if present) and load new ones.
93
94bool
95HooksManager::loadLibrariesInternal(const HookLibsCollection& libraries) {
96 // Unload current set of libraries (if any are loaded).
97 unloadLibrariesInternal();
98
99 // Create the library manager and load the libraries.
100 lm_collection_.reset(new LibraryManagerCollection(libraries));
101 bool status = lm_collection_->loadLibraries();
102
103 if (status) {
104 // ... and obtain the callout manager for them if successful.
105 callout_manager_ = lm_collection_->getCalloutManager();
106 } else {
107 // Unable to load libraries, reset to state before this function was
108 // called.
109 lm_collection_.reset();
110 callout_manager_.reset();
111 }
112
113 return (status);
114}
115
116bool
118 return (getHooksManager().loadLibrariesInternal(libraries));
119}
120
121// Unload the libraries. This just deletes all internal objects which will
122// cause the libraries to be unloaded.
123
124void
125HooksManager::unloadLibrariesInternal() {
126 // The order of deletion does not matter here, as each library manager
127 // holds its own pointer to the callout manager. However, we may as
128 // well delete the library managers first: if there are no other references
129 // to the callout manager, the second statement will delete it, which may
130 // ease debugging.
131 lm_collection_.reset();
132 callout_manager_.reset();
134}
135
137 getHooksManager().unloadLibrariesInternal();
138}
139
140// Create a callout handle
141
142boost::shared_ptr<CalloutHandle>
143HooksManager::createCalloutHandleInternal() {
144 conditionallyInitialize();
145 return (boost::shared_ptr<CalloutHandle>(
146 new CalloutHandle(callout_manager_, lm_collection_)));
147}
148
149boost::shared_ptr<CalloutHandle>
151 return (getHooksManager().createCalloutHandleInternal());
152}
153
154// Get the list of the names of loaded libraries.
155
156std::vector<std::string>
157HooksManager::getLibraryNamesInternal() const {
158 return (lm_collection_ ? lm_collection_->getLibraryNames()
159 : std::vector<std::string>());
160}
161
163HooksManager::getLibraryInfoInternal() const {
164 return (lm_collection_ ? lm_collection_->getLibraryInfo()
166}
167
168std::vector<std::string>
170 return (getHooksManager().getLibraryNamesInternal());
171}
172
175 return (getHooksManager().getLibraryInfoInternal());
176}
177
178// Perform conditional initialization if nothing is loaded.
179
180void
181HooksManager::performConditionalInitialization() {
182
183 // Nothing present, so create the collection with any empty set of
184 // libraries, and get the CalloutManager.
185 HookLibsCollection libraries;
186 lm_collection_.reset(new LibraryManagerCollection(libraries));
187 lm_collection_->loadLibraries();
188
189 callout_manager_ = lm_collection_->getCalloutManager();
190}
191
192// Shell around ServerHooks::registerHook()
193
194int
195HooksManager::registerHook(const std::string& name) {
197}
198
199// Return pre- and post- library handles.
200
202HooksManager::preCalloutsLibraryHandleInternal() {
203 conditionallyInitialize();
204 return (callout_manager_->getPreLibraryHandle());
205}
206
209 return (getHooksManager().preCalloutsLibraryHandleInternal());
210}
211
213HooksManager::postCalloutsLibraryHandleInternal() {
214 conditionallyInitialize();
215 return (callout_manager_->getPostLibraryHandle());
216}
217
220 return (getHooksManager().postCalloutsLibraryHandleInternal());
221}
222
223// Validate libraries
224
225std::vector<std::string>
226HooksManager::validateLibraries(const std::vector<std::string>& libraries) {
228}
229
230// Shared callout manager
231boost::shared_ptr<CalloutManager>&
233 return (getHooksManager().shared_callout_manager_);
234}
235
236} // namespace util
237} // namespace isc
Per-packet callout handle.
static void unloadLibraries()
Unload libraries.
static int registerHook(const std::string &name)
Register Hook.
static LibraryHandle & postCalloutsLibraryHandle()
Return post-callouts library handle.
static bool calloutsPresent(int index)
Are callouts present?
static std::vector< std::string > getLibraryNames()
Return list of loaded libraries.
static void callCommandHandlers(const std::string &command_name, CalloutHandle &handle)
Calls the callouts/command handlers for a given command name.
static boost::shared_ptr< CalloutManager > & getSharedCalloutManager()
Return the shared callout manager.
static bool loadLibraries(const HookLibsCollection &libraries)
Load and reload libraries.
static LibraryHandle & preCalloutsLibraryHandle()
Return pre-callouts library handle.
static bool commandHandlersPresent(const std::string &command_name)
Checks if control command handlers are present for the specified command.
static boost::shared_ptr< CalloutHandle > createCalloutHandle()
Return callout handle.
static void callCallouts(int index, CalloutHandle &handle)
Calls the callouts for a given hook.
static HookLibsCollection getLibraryInfo()
Return list of loaded libraries with its parameters.
static HooksManager & getHooksManager()
Get singleton hooks manager.
static std::vector< std::string > validateLibraries(const std::vector< std::string > &libraries)
Validate library list.
static std::vector< std::string > validateLibraries(const std::vector< std::string > &libraries)
Validate libraries.
static ServerHooks & getServerHooks()
Return ServerHooks object.
ParkingLotsPtr getParkingLotsPtr() const
Returns pointer to all parking lots.
std::vector< HookLibInfo > HookLibsCollection
A storage for information about hook libraries.
Definition: libinfo.h:31
Defines the logger used by the top-level component of kea-dhcp-ddns.