From: Marcin Siodelski Date: Wed, 9 Jan 2013 12:18:15 +0000 (+0100) Subject: [2317] Added generic container holding option spaces. X-Git-Tag: bind10-1.0.0-rc-release~100^2~2^2~16 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=db45e761f5f8c140d80d242033642e2d929a2a2d;p=thirdparty%2Fkea.git [2317] Added generic container holding option spaces. --- diff --git a/src/lib/dhcpsrv/Makefile.am b/src/lib/dhcpsrv/Makefile.am index db7ea6a484..76c67a2463 100644 --- a/src/lib/dhcpsrv/Makefile.am +++ b/src/lib/dhcpsrv/Makefile.am @@ -43,6 +43,7 @@ libb10_dhcpsrv_la_SOURCES += memfile_lease_mgr.cc memfile_lease_mgr.h if HAVE_MYSQL libb10_dhcpsrv_la_SOURCES += mysql_lease_mgr.cc mysql_lease_mgr.h endif +libb10_dhcpsrv_la_SOURCES += option_space_container.h libb10_dhcpsrv_la_SOURCES += pool.cc pool.h libb10_dhcpsrv_la_SOURCES += subnet.cc subnet.h libb10_dhcpsrv_la_SOURCES += triplet.h diff --git a/src/lib/dhcpsrv/cfgmgr.cc b/src/lib/dhcpsrv/cfgmgr.cc index ce28575537..72b32549d5 100644 --- a/src/lib/dhcpsrv/cfgmgr.cc +++ b/src/lib/dhcpsrv/cfgmgr.cc @@ -56,30 +56,15 @@ CfgMgr::addOptionDef(const OptionDefinitionPtr& def, << option_space << "'."); } - // Get existing option definitions for the option space. - OptionDefContainerPtr defs = getOptionDefs(option_space); - // getOptionDefs always returns a valid pointer to - // the container. Let's make an assert to make sure. - assert(defs); - // Actually add the new definition. - defs->push_back(def); - option_def_spaces_[option_space] = defs; + // Actually add a new item. + option_def_spaces_.addItem(def, option_space); } OptionDefContainerPtr CfgMgr::getOptionDefs(const std::string& option_space) const { // @todo Validate the option space once the #2313 is implemented. - // Get all option definitions for the particular option space. - const OptionDefsMap::const_iterator& defs = - option_def_spaces_.find(option_space); - // If there are no option definitions for the particular option space - // then return empty container. - if (defs == option_def_spaces_.end()) { - return (OptionDefContainerPtr(new OptionDefContainer())); - } - // If option definitions found, return them. - return (defs->second); + return (option_def_spaces_.getItems(option_space)); } OptionDefinitionPtr @@ -199,7 +184,7 @@ void CfgMgr::addSubnet4(const Subnet4Ptr& subnet) { } void CfgMgr::deleteOptionDefs() { - option_def_spaces_.clear(); + option_def_spaces_.clearItems(); } void CfgMgr::deleteSubnets4() { diff --git a/src/lib/dhcpsrv/cfgmgr.h b/src/lib/dhcpsrv/cfgmgr.h index 9628f24225..368536c4cb 100644 --- a/src/lib/dhcpsrv/cfgmgr.h +++ b/src/lib/dhcpsrv/cfgmgr.h @@ -18,6 +18,7 @@ #include #include #include +#include #include #include #include @@ -220,15 +221,12 @@ protected: private: - /// A map containing option definitions for various option spaces. - /// They key of this map is the name of the option space. The - /// value is the the option container holding option definitions - /// for the particular option space. - typedef std::map OptionDefsMap; + /// A collection of option definitions accessible with option + /// space name. + typedef OptionSpaceContainer OptionSpaceCollection; - /// A map containing option definitions for different option spaces. - /// The map key holds an option space name. - OptionDefsMap option_def_spaces_; + OptionSpaceCollection option_def_spaces_; }; diff --git a/src/lib/dhcpsrv/option_space_container.h b/src/lib/dhcpsrv/option_space_container.h new file mode 100644 index 0000000000..d5dcc62c56 --- /dev/null +++ b/src/lib/dhcpsrv/option_space_container.h @@ -0,0 +1,78 @@ +// Copyright (C) 2013 Internet Systems Consortium, Inc. ("ISC") +// +// Permission to use, copy, modify, and/or distribute this software for any +// purpose with or without fee is hereby granted, provided that the above +// copyright notice and this permission notice appear in all copies. +// +// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH +// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY +// AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, +// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM +// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE +// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR +// PERFORMANCE OF THIS SOFTWARE. + +#ifndef OPTION_SPACE_CONTAINER_H +#define OPTION_SPACE_CONTAINER_H + +namespace isc { +namespace dhcp { + +/// @brief Simple container for option spaces holding various items. +/// +/// This helper class is used to store items of various types in +/// that are grouped by option space names. Each option space is +/// mapped to a container that holds items which specifically can +/// be OptionDefinition objects or Subnet::OptionDescriptor structures. +/// +/// @tparam ContainerType of the container holding items within +/// option space. +/// @tparam ItemType type of the item being held by the container. +template +class OptionSpaceContainer { +public: + + /// Pointer to the container. + typedef boost::shared_ptr ItemsContainerPtr; + + /// @brief Adds a new item to the option_space. + /// + /// @param item reference to the item being added. + /// @param name of the option space. + void addItem(const ItemType& item, const std::string& option_space) { + ItemsContainerPtr items = getItems(option_space); + items->push_back(item); + option_space_map_[option_space] = items; + } + + /// @brief Get all items for the particular option space. + /// + /// @param option_space name of the option space. + /// + /// @return pointer to the container holding items. + ItemsContainerPtr getItems(const std::string& option_space) const { + const typename OptionSpaceMap::const_iterator& items = + option_space_map_.find(option_space); + if (items == option_space_map_.end()) { + return (ItemsContainerPtr(new ContainerType())); + } + return (items->second); + } + + /// @brief Remove all items from the container. + void clearItems() { + option_space_map_.clear(); + } + +private: + + /// A map holding container (option space name is the key). + typedef std::map OptionSpaceMap; + OptionSpaceMap option_space_map_; +}; + + +} // end of isc::dhcp namespace +} // end of isc namespace + +#endif // OPTION_SPACE_CONTAINER_H diff --git a/src/lib/dhcpsrv/subnet.cc b/src/lib/dhcpsrv/subnet.cc index 3f31130f7d..98ef8c8fff 100644 --- a/src/lib/dhcpsrv/subnet.cc +++ b/src/lib/dhcpsrv/subnet.cc @@ -55,36 +55,18 @@ Subnet::addOption(OptionPtr& option, bool persistent, } validateOption(option); - OptionContainerPtr container = getOptionDescriptors(option_space); - // getOptionDescriptors is expected to return the pointer to the - // valid container. Let's make sure it does by performing an assert. - assert(container); - // Actually add the new descriptor. - container->push_back(OptionDescriptor(option, persistent)); - option_spaces_[option_space] = container; + // Actually add new option descriptor. + option_spaces_.addItem(OptionDescriptor(option, persistent), option_space); } void Subnet::delOptions() { - option_spaces_.clear(); + option_spaces_.clearItems(); } Subnet::OptionContainerPtr Subnet::getOptionDescriptors(const std::string& option_space) const { - // Search the map to get the options container for the particular - // option space. - const OptionSpacesPtr::const_iterator& options = - option_spaces_.find(option_space); - // If the option space has not been found it means that no option - // has been configured for this option space yet. Thus we have to - // return an empty container to the caller. - if (options == option_spaces_.end()) { - // The default constructor creates an empty container. - return (OptionContainerPtr(new OptionContainer())); - } - // We found some option container for the option space specified. - // Let's return a const reference to it. - return (options->second); + return (option_spaces_.getItems(option_space)); } Subnet::OptionDescriptor diff --git a/src/lib/dhcpsrv/subnet.h b/src/lib/dhcpsrv/subnet.h index 442369f2dd..806d86ec32 100644 --- a/src/lib/dhcpsrv/subnet.h +++ b/src/lib/dhcpsrv/subnet.h @@ -24,6 +24,7 @@ #include #include +#include #include #include @@ -383,12 +384,11 @@ protected: private: - /// Container holding options grouped by option space names. - typedef std::map OptionSpacesPtr; + /// A collection of option spaces grouping option descriptors. + typedef OptionSpaceContainer OptionSpaceCollection; + OptionSpaceCollection option_spaces_; - /// @brief a collection of DHCP option spaces holding options - /// configured for a subnet. - OptionSpacesPtr option_spaces_; }; /// @brief A configuration holder for IPv4 subnet.