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 
10 #include <exceptions/exceptions.h>
11 #include <hooks/library_handle.h>
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 
21 namespace isc {
22 namespace hooks {
23 
24 class ServerHooks;
25 
29 
30 class NoSuchArgument : public Exception {
31 public:
32  NoSuchArgument(const char* file, size_t line, const char* what) :
33  isc::Exception(file, line, what) {}
34 };
35 
41 
43 public:
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 
50 class CalloutManager;
51 class LibraryHandle;
52 class LibraryManagerCollection;
53 
83 
85 public:
86 
96  NEXT_STEP_PARK = 3
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 
143  ~CalloutHandle();
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 
352 private:
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 
416 typedef boost::shared_ptr<CalloutHandle> CalloutHandlePtr;
417 
457 public:
458 
466  explicit ScopedCalloutHandleState(const CalloutHandlePtr& callout_handle);
467 
472 
473 private:
474 
478  void resetState();
479 
481  CalloutHandlePtr callout_handle_;
482 };
483 
484 } // namespace hooks
485 } // namespace isc
486 
487 
488 #endif // CALLOUT_HANDLE_H
isc::hooks::CalloutHandle::deleteAllContext
void deleteAllContext()
Delete all context items.
Definition: callout_handle.h:334
isc::hooks::ScopedCalloutHandleState
Wrapper class around callout handle which automatically resets handle's state.
Definition: callout_handle.h:456
isc::hooks::CalloutHandle::NEXT_STEP_SKIP
@ NEXT_STEP_SKIP
skip the next processing step
Definition: callout_handle.h:94
isc::hooks::CalloutHandle::setArgument
void setArgument(const std::string &name, T value)
Set argument.
Definition: callout_handle.h:153
isc::hooks::ScopedCalloutHandleState::ScopedCalloutHandleState
ScopedCalloutHandleState(const CalloutHandlePtr &callout_handle)
Constructor.
Definition: callout_handle.cc:162
isc::hooks::CalloutHandle::CalloutHandle
CalloutHandle(const boost::shared_ptr< CalloutManager > &manager, const boost::shared_ptr< LibraryManagerCollection > &lmcoll=boost::shared_ptr< LibraryManagerCollection >())
Constructor.
Definition: callout_handle.cc:24
isc::hooks::CalloutHandle::getContext
void getContext(const std::string &name, T &value) const
Get context.
Definition: callout_handle.h:292
isc::hooks::CalloutHandle
Per-packet callout handle.
Definition: callout_handle.h:84
parking_lots.h
isc::hooks::NoSuchArgument::NoSuchArgument
NoSuchArgument(const char *file, size_t line, const char *what)
Definition: callout_handle.h:32
isc::hooks::CalloutHandlePtr
boost::shared_ptr< CalloutHandle > CalloutHandlePtr
A shared pointer to a CalloutHandle object.
Definition: callout_handle.h:416
isc::hooks::CalloutHandle::deleteArgument
void deleteArgument(const std::string &name)
Delete argument.
Definition: callout_handle.h:197
library_handle.h
isc::hooks::CalloutHandle::ElementCollection
std::map< std::string, boost::any > ElementCollection
Typedef to allow abbreviation of iterator specification in methods.
Definition: callout_handle.h:103
isc::hooks::NoSuchArgument
No such argument.
Definition: callout_handle.h:30
isc::Exception
This is a base class for exceptions thrown from the DNS library module.
Definition: exceptions/exceptions.h:23
isc::hooks::CalloutHandle::setContext
void setContext(const std::string &name, T value)
Set context.
Definition: callout_handle.h:274
isc::hooks::LibraryHandle
Library handle.
Definition: library_handle.h:60
isc::hooks::ParkingLotHandlePtr
boost::shared_ptr< ParkingLotHandle > ParkingLotHandlePtr
Pointer to the parking lot handle.
Definition: parking_lots.h:292
isc
Defines the logger used by the top-level component of kea-dhcp-ddns.
Definition: agent_parser.cc:144
isc::Exception::what
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
Definition: exceptions/exceptions.cc:32
isc::hooks::CalloutHandle::getArgument
void getArgument(const std::string &name, T &value) const
Get argument.
Definition: callout_handle.h:170
isc_throw
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
Definition: exceptions/exceptions.h:192
isc::hooks::NoSuchCalloutContext
No such callout context item.
Definition: callout_handle.h:42
isc::hooks::CalloutHandle::getStatus
CalloutNextStep getStatus() const
Returns the next processing step.
Definition: callout_handle.h:247
isc::hooks::NoSuchCalloutContext::NoSuchCalloutContext
NoSuchCalloutContext(const char *file, size_t line, const char *what)
Definition: callout_handle.h:44
isc::hooks::CalloutHandle::CalloutNextStep
CalloutNextStep
Specifies allowed next steps.
Definition: callout_handle.h:92
isc::hooks::CalloutHandle::getHookName
std::string getHookName() const
Get hook name.
Definition: callout_handle.cc:144
isc::hooks::ScopedCalloutHandleState::~ScopedCalloutHandleState
~ScopedCalloutHandleState()
Destructor.
Definition: callout_handle.cc:171
isc::hooks::CalloutHandle::deleteAllArguments
void deleteAllArguments()
Delete all arguments.
Definition: callout_handle.h:207
isc::hooks::CalloutHandle::getContextNames
std::vector< std::string > getContextNames() const
Get context names.
Definition: callout_handle.cc:126
isc::hooks::CalloutHandle::getParkingLotHandlePtr
ParkingLotHandlePtr getParkingLotHandlePtr() const
Returns pointer to the parking lot handle for this hook point.
Definition: callout_handle.cc:78
isc::hooks::CalloutHandle::getLibraryHandle
LibraryHandle & getLibraryHandle() const
Access current library handle.
Definition: callout_handle.cc:86
exceptions.h
isc::hooks::CalloutHandle::setStatus
void setStatus(const CalloutNextStep next)
Sets the next processing step.
Definition: callout_handle.h:237
isc::hooks::CalloutHandle::ContextCollection
std::map< int, ElementCollection > ContextCollection
Typedef to allow abbreviations in specifications when accessing context.
Definition: callout_handle.h:115
isc::hooks::CalloutHandle::deleteContext
void deleteContext(const std::string &name)
Delete context element.
Definition: callout_handle.h:324
isc::hooks::CalloutHandle::NEXT_STEP_CONTINUE
@ NEXT_STEP_CONTINUE
continue normally
Definition: callout_handle.h:93
isc::hooks::CalloutHandle::~CalloutHandle
~CalloutHandle()
Destructor.
Definition: callout_handle.cc:37
isc::hooks::CalloutHandle::NEXT_STEP_PARK
@ NEXT_STEP_PARK
park the packet
Definition: callout_handle.h:96
isc::hooks::ServerHooks
Server hook collection.
Definition: server_hooks.h:62
isc::hooks::CalloutHandle::getArgumentNames
std::vector< std::string > getArgumentNames() const
Get argument names.
Definition: callout_handle.cc:66
isc::hooks::CalloutHandle::NEXT_STEP_DROP
@ NEXT_STEP_DROP
drop the packet
Definition: callout_handle.h:95