lease_mgr_factory.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. // Copyright (C) 2012-2013,2015 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  8. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. // PERFORMANCE OF THIS SOFTWARE.
  14. #ifndef LEASE_MGR_FACTORY_H
  15. #define LEASE_MGR_FACTORY_H
  16. #include <dhcpsrv/lease_mgr.h>
  17. #include <dhcpsrv/database_connection.h>
  18. #include <exceptions/exceptions.h>
  19. #include <boost/scoped_ptr.hpp>
  20. #include <string>
  21. namespace isc {
  22. namespace dhcp {
  23. /// @brief Invalid type exception
  24. ///
  25. /// Thrown when the factory doesn't recognize the type of the backend.
  26. class InvalidType : public Exception {
  27. public:
  28. InvalidType(const char* file, size_t line, const char* what) :
  29. isc::Exception(file, line, what) {}
  30. };
  31. /// @brief No lease manager exception
  32. ///
  33. /// Thrown if an attempt is made to get a reference to the current lease
  34. /// manager and none is currently available.
  35. class NoLeaseManager : public Exception {
  36. public:
  37. NoLeaseManager(const char* file, size_t line, const char* what) :
  38. isc::Exception(file, line, what) {}
  39. };
  40. /// @brief Lease Manager Factory
  41. ///
  42. /// This class comprises nothing but static methods used to create a lease
  43. /// manager. It analyzes the database information passed to the creation
  44. /// function and instantiates an appropriate lease manager based on the type
  45. /// requested.
  46. ///
  47. /// Strictly speaking these functions could be stand-alone functions. However,
  48. /// it is convenient to encapsulate them in a class for naming purposes.
  49. ///
  50. /// @todo: Will need to develop some form of registration mechanism for
  51. /// user-supplied backends (so that there is no need to modify the code).
  52. class LeaseMgrFactory {
  53. public:
  54. /// @brief Create an instance of a lease manager.
  55. ///
  56. /// Each database backend has its own lease manager type. This static
  57. /// method sets the "current" lease manager to be a manager of the
  58. /// appropriate type. The actual lease manager is returned by the
  59. /// "instance" method.
  60. ///
  61. /// @note When called, the current lease manager is <b>always</b> destroyed
  62. /// and a new one created - even if the parameters are the same.
  63. ///
  64. /// dbaccess is a generic way of passing parameters. Parameters are passed
  65. /// in the "name=value" format, separated by spaces. The data MUST include
  66. /// a keyword/value pair of the form "type=dbtype" giving the database
  67. /// type, e.q. "mysql" or "sqlite3".
  68. ///
  69. /// @param dbaccess Database access parameters. These are in the form of
  70. /// "keyword=value" pairs, separated by spaces. They are backend-
  71. /// -end specific, although must include the "type" keyword which
  72. /// gives the backend in use.
  73. ///
  74. /// @throw isc::InvalidParameter dbaccess string does not contain the "type"
  75. /// keyword.
  76. /// @throw isc::dhcp::InvalidType The "type" keyword in dbaccess does not
  77. /// identify a supported backend.
  78. static void create(const std::string& dbaccess);
  79. /// @brief Destroy lease manager
  80. ///
  81. /// Destroys the current lease manager object. This should have the effect
  82. /// of closing the database connection. The method is a no-op if no
  83. /// lease manager is available.
  84. static void destroy();
  85. /// @brief Return current lease manager
  86. ///
  87. /// Returns an instance of the "current" lease manager. An exception
  88. /// will be thrown if none is available.
  89. ///
  90. /// @throw isc::dhcp::NoLeaseManager No lease manager is available: use
  91. /// create() to create one before calling this method.
  92. static LeaseMgr& instance();
  93. private:
  94. /// @brief Hold pointer to lease manager
  95. ///
  96. /// Holds a pointer to the singleton lease manager. The singleton
  97. /// is encapsulated in this method to avoid a "static initialization
  98. /// fiasco" if defined in an external static variable.
  99. static boost::scoped_ptr<LeaseMgr>& getLeaseMgrPtr();
  100. };
  101. }; // end of isc::dhcp namespace
  102. }; // end of isc namespace
  103. #endif // LEASE_MGR_FACTORY_H