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 
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
LibraryHandle & getLibraryHandle() const
Access current library handle.
void setStatus(const CalloutNextStep next)
Sets the next processing step.
ParkingLotHandlePtr getParkingLotHandlePtr() const
Returns pointer to the parking lot handle for this hook point.
std::vector< std::string > getArgumentNames() const
Get argument names.
CalloutNextStep getStatus() const
Returns the next processing step.
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.
ScopedCalloutHandleState(const CalloutHandlePtr &callout_handle)
Constructor.
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
std::string getHookName() const
Get hook name.
Per-packet callout handle.
NoSuchCalloutContext(const char *file, size_t line, const char *what)
Wrapper class around callout handle which automatically resets handle'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 deleteAllContext()
Delete all context items.
std::map< int, ElementCollection > ContextCollection
Typedef to allow abbreviations in specifications when accessing context.
This is a base class for exceptions thrown from the DNS library module.
Defines the logger used by the top-level component of kea-dhcp-ddns.
CalloutHandle(const boost::shared_ptr< CalloutManager > &manager, const boost::shared_ptr< LibraryManagerCollection > &lmcoll=boost::shared_ptr< LibraryManagerCollection >())
Constructor.
void getArgument(const std::string &name, T &value) const
Get argument.
boost::shared_ptr< CalloutHandle > CalloutHandlePtr
A shared pointer to a CalloutHandle object.
std::vector< std::string > getContextNames() const
Get context names.
void setContext(const std::string &name, T value)
Set context.
void deleteArgument(const std::string &name)
Delete argument.
No such callout context item.
boost::shared_ptr< ParkingLotHandle > ParkingLotHandlePtr
Pointer to the parking lot handle.
Definition: parking_lots.h:292
NoSuchArgument(const char *file, size_t line, const char *what)
skip the next processing step
Server hook collection.
Definition: server_hooks.h:62
void deleteAllArguments()
Delete all arguments.