Kea 1.5.0
callout_handle.h
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#ifndef CALLOUT_HANDLE_H
8#define CALLOUT_HANDLE_H
9
12#include <hooks/parking_lots.h>
13
14#include <boost/any.hpp>
15#include <boost/shared_ptr.hpp>
16
17#include <map>
18#include <string>
19#include <vector>
20
21namespace isc {
22namespace hooks {
23
24class ServerHooks;
25
29
30class NoSuchArgument : public Exception {
31public:
32 NoSuchArgument(const char* file, size_t line, const char* what) :
33 isc::Exception(file, line, what) {}
34};
35
41
43public:
44 NoSuchCalloutContext(const char* file, size_t line, const char* what) :
45 isc::Exception(file, line, what) {}
46};
47
48// Forward declaration of the library handle and related collection classes.
49
50class CalloutManager;
51class LibraryHandle;
52class LibraryManagerCollection;
53
83
85public:
86
97 };
98
99
103 typedef std::map<std::string, boost::any> ElementCollection;
104
115 typedef std::map<int, ElementCollection> ContextCollection;
116
135 CalloutHandle(const boost::shared_ptr<CalloutManager>& manager,
136 const boost::shared_ptr<LibraryManagerCollection>& lmcoll =
137 boost::shared_ptr<LibraryManagerCollection>());
138
144
152 template <typename T>
153 void setArgument(const std::string& name, T value) {
154 arguments_[name] = value;
155 }
156
169 template <typename T>
170 void getArgument(const std::string& name, T& value) const {
171 ElementCollection::const_iterator element_ptr = arguments_.find(name);
172 if (element_ptr == arguments_.end()) {
173 isc_throw(NoSuchArgument, "unable to find argument with name " <<
174 name);
175 }
176
177 value = boost::any_cast<T>(element_ptr->second);
178 }
179
186 std::vector<std::string> getArgumentNames() const;
187
197 void deleteArgument(const std::string& name) {
198 static_cast<void>(arguments_.erase(name));
199 }
200
208 arguments_.clear();
209 }
210
237 void setStatus(const CalloutNextStep next) {
238 next_step_ = next;
239 }
240
248 return (next_step_);
249 }
250
265
273 template <typename T>
274 void setContext(const std::string& name, T value) {
275 getContextForLibrary()[name] = value;
276 }
277
291 template <typename T>
292 void getContext(const std::string& name, T& value) const {
293 const ElementCollection& lib_context = getContextForLibrary();
294
295 ElementCollection::const_iterator element_ptr = lib_context.find(name);
296 if (element_ptr == lib_context.end()) {
297 isc_throw(NoSuchCalloutContext, "unable to find callout context "
298 "item " << name << " in the context associated with "
299 "current library");
300 }
301
302 value = boost::any_cast<T>(element_ptr->second);
303 }
304
312 std::vector<std::string> getContextNames() const;
313
324 void deleteContext(const std::string& name) {
325 static_cast<void>(getContextForLibrary().erase(name));
326 }
327
335 getContextForLibrary().clear();
336 }
337
345 std::string getHookName() const;
346
351
352private:
353
363 int getLibraryIndex() const;
364
376 ElementCollection& getContextForLibrary();
377
389 const ElementCollection& getContextForLibrary() const;
390
391 // Member variables
392
395 boost::shared_ptr<LibraryManagerCollection> lm_collection_;
396
398 ElementCollection arguments_;
399
401 ContextCollection context_collection_;
402
404 boost::shared_ptr<CalloutManager> manager_;
405
409 ServerHooks& server_hooks_;
410
412 CalloutNextStep next_step_;
413};
414
416typedef boost::shared_ptr<CalloutHandle> CalloutHandlePtr;
417
457public:
458
466 explicit ScopedCalloutHandleState(const CalloutHandlePtr& callout_handle);
467
472
473private:
474
478 void resetState();
479
481 CalloutHandlePtr callout_handle_;
482};
483
484} // namespace hooks
485} // namespace isc
486
487
488#endif // CALLOUT_HANDLE_H
This is a base class for exceptions thrown from the DNS library module.
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
Per-packet callout handle.
void deleteContext(const std::string &name)
Delete context element.
LibraryHandle & getLibraryHandle() const
Access current library handle.
void getContext(const std::string &name, T &value) const
Get context.
CalloutNextStep
Specifies allowed next steps.
@ NEXT_STEP_PARK
park the packet
@ NEXT_STEP_CONTINUE
continue normally
@ NEXT_STEP_DROP
drop the packet
@ NEXT_STEP_SKIP
skip the next processing step
ParkingLotHandlePtr getParkingLotHandlePtr() const
Returns pointer to the parking lot handle for this hook point.
void setContext(const std::string &name, T value)
Set context.
CalloutNextStep getStatus() const
Returns the next processing step.
std::string getHookName() const
Get hook name.
void deleteAllArguments()
Delete all arguments.
std::map< std::string, boost::any > ElementCollection
Typedef to allow abbreviation of iterator specification in methods.
void setStatus(const CalloutNextStep next)
Sets the next processing step.
void deleteArgument(const std::string &name)
Delete argument.
void getArgument(const std::string &name, T &value) const
Get argument.
void deleteAllContext()
Delete all context items.
std::vector< std::string > getContextNames() const
Get context names.
void setArgument(const std::string &name, T value)
Set argument.
std::vector< std::string > getArgumentNames() const
Get argument names.
std::map< int, ElementCollection > ContextCollection
Typedef to allow abbreviations in specifications when accessing context.
NoSuchArgument(const char *file, size_t line, const char *what)
No such callout context item.
NoSuchCalloutContext(const char *file, size_t line, const char *what)
Wrapper class around callout handle which automatically resets handle's state.
Server hook collection.
Definition: server_hooks.h:62
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
boost::shared_ptr< ParkingLotHandle > ParkingLotHandlePtr
Pointer to the parking lot handle.
Definition: parking_lots.h:292
boost::shared_ptr< CalloutHandle > CalloutHandlePtr
A shared pointer to a CalloutHandle object.
Defines the logger used by the top-level component of kea-dhcp-ddns.