map.hpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2014-2022, The University of Memphis,
4  * Regents of the University of California
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/>.
19  */
20 
21 #ifndef NLSR_MAP_HPP
22 #define NLSR_MAP_HPP
23 
24 #include "common.hpp"
25 #include "lsa/adj-lsa.hpp"
26 
27 #include <boost/multi_index_container.hpp>
28 #include <boost/multi_index/hashed_index.hpp>
29 #include <boost/multi_index/member.hpp>
30 #include <boost/multi_index/tag.hpp>
31 
32 #include <optional>
33 
34 namespace nlsr {
35 
36 struct MapEntry
37 {
38  ndn::Name router;
39  int32_t mappingNumber = -1;
40 };
41 
42 namespace detail {
43 
44  using namespace boost::multi_index;
45  // Define tags so that we can search by different indices.
46  struct byRouterName {};
47  struct byMappingNumber{};
48  using entryContainer = multi_index_container<
49  MapEntry,
50  indexed_by<
51  hashed_unique<tag<byRouterName>,
52  member<MapEntry, ndn::Name, &MapEntry::router>,
53  std::hash<ndn::Name>>,
54  hashed_unique<tag<byMappingNumber>,
55  member<MapEntry, int32_t, &MapEntry::mappingNumber>>
56  >
57  >;
58 
59 } // namespace detail
60 
61 class Map
62 {
63 public:
64  Map()
65  : m_mappingIndex(0)
66  {
67  }
68 
75  void
76  addEntry(const ndn::Name& rtrName);
77 
82  template<typename IteratorType>
83  void
84  createFromAdjLsdb(IteratorType begin, IteratorType end)
85  {
86  BOOST_STATIC_ASSERT_MSG(is_iterator<IteratorType>::value, "IteratorType must be an iterator!");
87  for (auto lsa = begin; lsa != end; lsa++) {
88  auto adjLsa = std::static_pointer_cast<AdjLsa>(*lsa);
89  addEntry(adjLsa->getOriginRouter());
90  for (const auto& adjacent : adjLsa->getAdl().getAdjList()) {
91  addEntry(adjacent.getName());
92  }
93  }
94  }
95 
100  template<typename IteratorType>
101  void
102  createFromCoordinateLsdb(IteratorType begin, IteratorType end)
103  {
104  BOOST_STATIC_ASSERT_MSG(is_iterator<IteratorType>::value, "IteratorType must be an iterator!");
105  for (auto lsa = begin; lsa != end; lsa++) {
106  addEntry((*lsa)->getOriginRouter());
107  }
108  }
109 
110  std::optional<ndn::Name>
111  getRouterNameByMappingNo(int32_t mn) const;
112 
113  std::optional<int32_t>
114  getMappingNoByRouterName(const ndn::Name& rName);
115 
116  size_t
117  getMapSize() const
118  {
119  return m_entries.size();
120  }
121 
122  void
123  writeLog();
124 
125 private:
126  bool
127  addEntry(MapEntry& mpe);
128 
129  int32_t m_mappingIndex;
130  detail::entryContainer m_entries;
131 };
132 
133 } // namespace nlsr
134 
135 #endif // NLSR_MAP_HPP
void createFromAdjLsdb(IteratorType begin, IteratorType end)
Definition: map.hpp:84
size_t getMapSize() const
Definition: map.hpp:117
void createFromCoordinateLsdb(IteratorType begin, IteratorType end)
Definition: map.hpp:102
Map()
Definition: map.hpp:64
multi_index_container< MapEntry, indexed_by< hashed_unique< tag< byRouterName >, member< MapEntry, ndn::Name, &MapEntry::router >, std::hash< ndn::Name > >, hashed_unique< tag< byMappingNumber >, member< MapEntry, int32_t, &MapEntry::mappingNumber > > > > entryContainer
Definition: map.hpp:57
Copyright (c) 2014-2020, The University of Memphis, Regents of the University of California.
Definition: map.hpp:37
int32_t mappingNumber
Definition: map.hpp:39
ndn::Name router
Definition: map.hpp:38