akmhoque | 3d06e79 | 2014-05-27 16:23:20 -0500 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame^] | 3 | * Copyright (c) 2014-2020, The University of Memphis, |
Nick Gordon | f8b5bcd | 2016-08-11 15:06:50 -0500 | [diff] [blame] | 4 | * Regents of the University of California |
akmhoque | 3d06e79 | 2014-05-27 16:23:20 -0500 | [diff] [blame] | 5 | * |
| 6 | * This file is part of NLSR (Named-data Link State Routing). |
| 7 | * See AUTHORS.md for complete list of NLSR authors and contributors. |
| 8 | * |
| 9 | * NLSR is free software: you can redistribute it and/or modify it under the terms |
| 10 | * of the GNU General Public License as published by the Free Software Foundation, |
| 11 | * either version 3 of the License, or (at your option) any later version. |
| 12 | * |
| 13 | * NLSR is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 14 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 15 | * PURPOSE. See the GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License along with |
| 18 | * NLSR, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
akmhoque | 3d06e79 | 2014-05-27 16:23:20 -0500 | [diff] [blame] | 19 | **/ |
Nick Gordon | 22b5c95 | 2017-08-10 17:48:15 -0500 | [diff] [blame] | 20 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 21 | #ifndef NLSR_MAP_HPP |
| 22 | #define NLSR_MAP_HPP |
| 23 | |
Nick Gordon | 22b5c95 | 2017-08-10 17:48:15 -0500 | [diff] [blame] | 24 | #include "common.hpp" |
| 25 | #include "map-entry.hpp" |
| 26 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 27 | #include <list> |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 28 | #include <boost/cstdint.hpp> |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 29 | |
Nick Gordon | e40377d | 2017-08-11 15:10:02 -0500 | [diff] [blame] | 30 | #include <boost/multi_index_container.hpp> |
| 31 | #include <boost/multi_index/hashed_index.hpp> |
| 32 | #include <boost/multi_index/mem_fun.hpp> |
| 33 | #include <boost/multi_index/tag.hpp> |
| 34 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 35 | namespace nlsr { |
| 36 | |
Nick Gordon | e40377d | 2017-08-11 15:10:02 -0500 | [diff] [blame] | 37 | namespace detail { |
| 38 | |
| 39 | using namespace boost::multi_index; |
| 40 | // Define tags so that we can search by different indices. |
| 41 | struct byRouterName {}; |
| 42 | struct byMappingNumber{}; |
| 43 | using entryContainer = multi_index_container< |
| 44 | MapEntry, |
| 45 | indexed_by< |
| 46 | hashed_unique<tag<byRouterName>, |
| 47 | const_mem_fun<MapEntry, const ndn::Name&, &MapEntry::getRouter>, |
| 48 | std::hash<ndn::Name>>, |
| 49 | hashed_unique<tag<byMappingNumber>, |
| 50 | const_mem_fun<MapEntry, int32_t, &MapEntry::getMappingNumber>> |
| 51 | > |
| 52 | >; |
| 53 | |
| 54 | } // namespace detail |
| 55 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 56 | class Map |
| 57 | { |
| 58 | public: |
| 59 | Map() |
| 60 | : m_mappingIndex(0) |
| 61 | { |
| 62 | } |
| 63 | |
Nick G | 97e3494 | 2016-07-11 14:46:27 -0500 | [diff] [blame] | 64 | /*! \brief Add a map entry to this map. |
| 65 | \param rtrName The name of the router. |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 66 | |
Nick G | 97e3494 | 2016-07-11 14:46:27 -0500 | [diff] [blame] | 67 | Adds a router to this map. Each entry is also given an arbitrary, |
| 68 | ascending mappingNo (mapping number). |
| 69 | */ |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 70 | void |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 71 | addEntry(const ndn::Name& rtrName); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 72 | |
Nick Gordon | 22b5c95 | 2017-08-10 17:48:15 -0500 | [diff] [blame] | 73 | /*! Populates the Map with AdjacencyLsas. |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 74 | |
Nick Gordon | 22b5c95 | 2017-08-10 17:48:15 -0500 | [diff] [blame] | 75 | \note IteratorType must an iterator type, and begin to end must represent a valid range. |
| 76 | */ |
| 77 | template<typename IteratorType> |
Nick Gordon | e8e03ac | 2016-07-07 14:24:38 -0500 | [diff] [blame] | 78 | void |
Nick Gordon | 22b5c95 | 2017-08-10 17:48:15 -0500 | [diff] [blame] | 79 | createFromAdjLsdb(IteratorType begin, IteratorType end) |
| 80 | { |
| 81 | BOOST_STATIC_ASSERT_MSG(is_iterator<IteratorType>::value, "IteratorType must be an iterator!"); |
| 82 | for (auto lsa = begin; lsa != end; lsa++) { |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame^] | 83 | addEntry(lsa->getOriginRouter()); |
Nick Gordon | 22b5c95 | 2017-08-10 17:48:15 -0500 | [diff] [blame] | 84 | for (const auto& adjacent : lsa->getAdl().getAdjList()) { |
| 85 | addEntry(adjacent.getName()); |
| 86 | } |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | /*! Populates the Map with CoordinateLsas. |
| 91 | |
| 92 | \note IteratorType must an iterator type, and begin to end must represent a valid range. |
| 93 | */ |
| 94 | template<typename IteratorType> |
| 95 | void |
| 96 | createFromCoordinateLsdb(IteratorType begin, IteratorType end) |
| 97 | { |
| 98 | BOOST_STATIC_ASSERT_MSG(is_iterator<IteratorType>::value, "IteratorType must be an iterator!"); |
| 99 | for (auto lsa = begin; lsa != end; lsa++) { |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame^] | 100 | addEntry(lsa->getOriginRouter()); |
Nick Gordon | 22b5c95 | 2017-08-10 17:48:15 -0500 | [diff] [blame] | 101 | } |
| 102 | } |
Nick Gordon | e8e03ac | 2016-07-07 14:24:38 -0500 | [diff] [blame] | 103 | |
Nick Gordon | e40377d | 2017-08-11 15:10:02 -0500 | [diff] [blame] | 104 | ndn::optional<ndn::Name> |
Saurab Dulal | 9da6aa7 | 2018-01-12 17:25:51 +0000 | [diff] [blame] | 105 | getRouterNameByMappingNo(int32_t mn) const; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 106 | |
Nick Gordon | e40377d | 2017-08-11 15:10:02 -0500 | [diff] [blame] | 107 | ndn::optional<int32_t> |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 108 | getMappingNoByRouterName(const ndn::Name& rName); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 109 | |
| 110 | void |
| 111 | reset(); |
| 112 | |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 113 | size_t |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 114 | getMapSize() const |
| 115 | { |
Nick Gordon | e40377d | 2017-08-11 15:10:02 -0500 | [diff] [blame] | 116 | return m_entries.size(); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 117 | } |
| 118 | |
akmhoque | 2f42335 | 2014-06-03 11:49:35 -0500 | [diff] [blame] | 119 | void |
| 120 | writeLog(); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 121 | |
| 122 | private: |
| 123 | bool |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 124 | addEntry(MapEntry& mpe); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 125 | |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 126 | int32_t m_mappingIndex; |
Nick Gordon | e40377d | 2017-08-11 15:10:02 -0500 | [diff] [blame] | 127 | detail::entryContainer m_entries; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 128 | }; |
| 129 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 130 | } // namespace nlsr |
Nick Gordon | 22b5c95 | 2017-08-10 17:48:15 -0500 | [diff] [blame] | 131 | |
| 132 | #endif // NLSR_MAP_HPP |