Kea  2.1.6-git
callout_handle.h
Go to the documentation of this file.
1 // Copyright (C) 2013-2020 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;
52 
76 
78 public:
79 
86  NEXT_STEP_CONTINUE = 0,
87  NEXT_STEP_SKIP = 1,
88  NEXT_STEP_DROP = 2,
89  NEXT_STEP_PARK = 3
90  };
91 
92 
96  typedef std::map<std::string, boost::any> ElementCollection;
97 
108  typedef std::map<int, ElementCollection> ContextCollection;
109 
128  CalloutHandle(const boost::shared_ptr<CalloutManager>& manager,
129  const boost::shared_ptr<LibraryManagerCollection>& lmcoll =
130  boost::shared_ptr<LibraryManagerCollection>());
131 
136  ~CalloutHandle();
137 
145  template <typename T>
146  void setArgument(const std::string& name, T value) {
147  arguments_[name] = value;
148  }
149 
162  template <typename T>
163  void getArgument(const std::string& name, T& value) const {
164  ElementCollection::const_iterator element_ptr = arguments_.find(name);
165  if (element_ptr == arguments_.end()) {
166  isc_throw(NoSuchArgument, "unable to find argument with name " <<
167  name);
168  }
169 
170  value = boost::any_cast<T>(element_ptr->second);
171  }
172 
179  std::vector<std::string> getArgumentNames() const;
180 
190  void deleteArgument(const std::string& name) {
191  static_cast<void>(arguments_.erase(name));
192  }
193 
201  arguments_.clear();
202  }
203 
230  void setStatus(const CalloutNextStep next) {
231  next_step_ = next;
232  }
233 
241  return (next_step_);
242  }
243 
251  template <typename T>
252  void setContext(const std::string& name, T value) {
253  getContextForLibrary()[name] = value;
254  }
255 
269  template <typename T>
270  void getContext(const std::string& name, T& value) const {
271  const ElementCollection& lib_context = getContextForLibrary();
272 
273  ElementCollection::const_iterator element_ptr = lib_context.find(name);
274  if (element_ptr == lib_context.end()) {
275  isc_throw(NoSuchCalloutContext, "unable to find callout context "
276  "item " << name << " in the context associated with "
277  "current library");
278  }
279 
280  value = boost::any_cast<T>(element_ptr->second);
281  }
282 
290  std::vector<std::string> getContextNames() const;
291 
302  void deleteContext(const std::string& name) {
303  static_cast<void>(getContextForLibrary().erase(name));
304  }
305 
313  getContextForLibrary().clear();
314  }
315 
323  std::string getHookName() const;
324 
328  ParkingLotHandlePtr getParkingLotHandlePtr() const;
329 
333  int getCurrentLibrary() const {
334  return (current_library_);
335  }
336 
340  void setCurrentLibrary(int library_index) {
341  current_library_ = library_index;
342  }
343 
347  int getCurrentHook() const {
348  return (current_hook_);
349  }
350 
354  void setCurrentHook(int hook_index) {
355  current_hook_ = hook_index;
356  }
357 
358 private:
359 
369  int getLibraryIndex() const;
370 
382  ElementCollection& getContextForLibrary();
383 
395  const ElementCollection& getContextForLibrary() const;
396 
397  // Member variables
398 
401  boost::shared_ptr<LibraryManagerCollection> lm_collection_;
402 
404  ElementCollection arguments_;
405 
407  ContextCollection context_collection_;
408 
410  boost::shared_ptr<CalloutManager> manager_;
411 
415  ServerHooks& server_hooks_;
416 
422  int current_library_;
423 
429  int current_hook_;
430 
432  CalloutNextStep next_step_;
433 };
434 
436 typedef boost::shared_ptr<CalloutHandle> CalloutHandlePtr;
437 
477 public:
478 
486  explicit ScopedCalloutHandleState(const CalloutHandlePtr& callout_handle);
487 
492 
493 private:
494 
498  void resetState();
499 
501  CalloutHandlePtr callout_handle_;
502 };
503 
504 } // namespace hooks
505 } // namespace isc
506 
507 
508 #endif // CALLOUT_HANDLE_H
void setStatus(const CalloutNextStep next)
Sets the next processing step.
int getCurrentHook() const
Get current hook index.
CalloutNextStep getStatus() const
Returns the next processing step.
No such argument.
void getContext(const std::string &name, T &value) const
Get context.
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
Per-packet callout handle.
NoSuchCalloutContext(const char *file, size_t line, const char *what)
Wrapper class around callout handle which automatically resets handle&#39;s state.
CalloutNextStep
Specifies allowed next steps.
void setArgument(const std::string &name, T value)
Set argument.
std::map< std::string, boost::any > ElementCollection
Typedef to allow abbreviation of iterator specification in methods.
void deleteContext(const std::string &name)
Delete context element.
void setCurrentHook(int hook_index)
Set current hook index.
void deleteAllContext()
Delete all context items.
std::map< int, ElementCollection > ContextCollection
Typedef to allow abbreviations in specifications when accessing context.
int getCurrentLibrary() const
Get current library index.
This is a base class for exceptions thrown from the DNS library module.
Defines the logger used by the top-level component of kea-lfc.
void getArgument(const std::string &name, T &value) const
Get argument.
boost::shared_ptr< CalloutHandle > CalloutHandlePtr
A shared pointer to a CalloutHandle object.
void setContext(const std::string &name, T value)
Set context.
void deleteArgument(const std::string &name)
Delete argument.
void setCurrentLibrary(int library_index)
Set current library index.
No such callout context item.
boost::shared_ptr< ParkingLotHandle > ParkingLotHandlePtr
Pointer to the parking lot handle.
Definition: parking_lots.h:381
NoSuchArgument(const char *file, size_t line, const char *what)
Server hook collection.
Definition: server_hooks.h:62
void deleteAllArguments()
Delete all arguments.