lease_mgr_factory.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. // Copyright (C) 2012-2016 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. #ifndef LEASE_MGR_FACTORY_H
  7. #define LEASE_MGR_FACTORY_H
  8. #include <dhcpsrv/lease_mgr.h>
  9. #include <dhcpsrv/database_connection.h>
  10. #include <exceptions/exceptions.h>
  11. #include <boost/scoped_ptr.hpp>
  12. #include <string>
  13. namespace isc {
  14. namespace dhcp {
  15. /// @brief No lease manager exception
  16. ///
  17. /// Thrown if an attempt is made to get a reference to the current lease
  18. /// manager and none is currently available.
  19. class NoLeaseManager : public Exception {
  20. public:
  21. NoLeaseManager(const char* file, size_t line, const char* what) :
  22. isc::Exception(file, line, what) {}
  23. };
  24. /// @brief Lease Manager Factory
  25. ///
  26. /// This class comprises nothing but static methods used to create a lease
  27. /// manager. It analyzes the database information passed to the creation
  28. /// function and instantiates an appropriate lease manager based on the type
  29. /// requested.
  30. ///
  31. /// Strictly speaking these functions could be stand-alone functions. However,
  32. /// it is convenient to encapsulate them in a class for naming purposes.
  33. ///
  34. /// @todo: Will need to develop some form of registration mechanism for
  35. /// user-supplied backends (so that there is no need to modify the code).
  36. class LeaseMgrFactory {
  37. public:
  38. /// @brief Create an instance of a lease manager.
  39. ///
  40. /// Each database backend has its own lease manager type. This static
  41. /// method sets the "current" lease manager to be a manager of the
  42. /// appropriate type. The actual lease manager is returned by the
  43. /// "instance" method.
  44. ///
  45. /// @note When called, the current lease manager is <b>always</b> destroyed
  46. /// and a new one created - even if the parameters are the same.
  47. ///
  48. /// dbaccess is a generic way of passing parameters. Parameters are passed
  49. /// in the "name=value" format, separated by spaces. The data MUST include
  50. /// a keyword/value pair of the form "type=dbtype" giving the database
  51. /// type, e.q. "mysql" or "sqlite3".
  52. ///
  53. /// @param dbaccess Database access parameters. These are in the form of
  54. /// "keyword=value" pairs, separated by spaces. They are backend-
  55. /// -end specific, although must include the "type" keyword which
  56. /// gives the backend in use.
  57. ///
  58. /// @throw isc::InvalidParameter dbaccess string does not contain the "type"
  59. /// keyword.
  60. /// @throw isc::dhcp::InvalidType The "type" keyword in dbaccess does not
  61. /// identify a supported backend.
  62. static void create(const std::string& dbaccess);
  63. /// @brief Destroy lease manager
  64. ///
  65. /// Destroys the current lease manager object. This should have the effect
  66. /// of closing the database connection. The method is a no-op if no
  67. /// lease manager is available.
  68. static void destroy();
  69. /// @brief Return current lease manager
  70. ///
  71. /// Returns an instance of the "current" lease manager. An exception
  72. /// will be thrown if none is available.
  73. ///
  74. /// @throw isc::dhcp::NoLeaseManager No lease manager is available: use
  75. /// create() to create one before calling this method.
  76. static LeaseMgr& instance();
  77. private:
  78. /// @brief Hold pointer to lease manager
  79. ///
  80. /// Holds a pointer to the singleton lease manager. The singleton
  81. /// is encapsulated in this method to avoid a "static initialization
  82. /// fiasco" if defined in an external static variable.
  83. static boost::scoped_ptr<LeaseMgr>& getLeaseMgrPtr();
  84. };
  85. }; // end of isc::dhcp namespace
  86. }; // end of isc namespace
  87. #endif // LEASE_MGR_FACTORY_H