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-2020, 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 namespace nlsr {
33 
34 struct MapEntry {
35  ndn::Name router;
36  int32_t mappingNumber = -1;
37 };
38 
39 namespace detail {
40 
41  using namespace boost::multi_index;
42  // Define tags so that we can search by different indices.
43  struct byRouterName {};
44  struct byMappingNumber{};
45  using entryContainer = multi_index_container<
46  MapEntry,
47  indexed_by<
48  hashed_unique<tag<byRouterName>,
49  member<MapEntry, ndn::Name, &MapEntry::router>,
50  std::hash<ndn::Name>>,
51  hashed_unique<tag<byMappingNumber>,
52  member<MapEntry, int32_t, &MapEntry::mappingNumber>>
53  >
54  >;
55 
56 } // namespace detail
57 
58 class Map
59 {
60 public:
61  Map()
62  : m_mappingIndex(0)
63  {
64  }
65 
72  void
73  addEntry(const ndn::Name& rtrName);
74 
79  template<typename IteratorType>
80  void
81  createFromAdjLsdb(IteratorType begin, IteratorType end)
82  {
83  BOOST_STATIC_ASSERT_MSG(is_iterator<IteratorType>::value, "IteratorType must be an iterator!");
84  for (auto lsa = begin; lsa != end; lsa++) {
85  auto adjLsa = std::static_pointer_cast<AdjLsa>(*lsa);
86  addEntry(adjLsa->getOriginRouter());
87  for (const auto& adjacent : adjLsa->getAdl().getAdjList()) {
88  addEntry(adjacent.getName());
89  }
90  }
91  }
92 
97  template<typename IteratorType>
98  void
99  createFromCoordinateLsdb(IteratorType begin, IteratorType end)
100  {
101  BOOST_STATIC_ASSERT_MSG(is_iterator<IteratorType>::value, "IteratorType must be an iterator!");
102  for (auto lsa = begin; lsa != end; lsa++) {
103  addEntry((*lsa)->getOriginRouter());
104  }
105  }
106 
107  ndn::optional<ndn::Name>
108  getRouterNameByMappingNo(int32_t mn) const;
109 
110  ndn::optional<int32_t>
111  getMappingNoByRouterName(const ndn::Name& rName);
112 
113  size_t
114  getMapSize() const
115  {
116  return m_entries.size();
117  }
118 
119  void
120  writeLog();
121 
122 private:
123  bool
124  addEntry(MapEntry& mpe);
125 
126  int32_t m_mappingIndex;
127  detail::entryContainer m_entries;
128 };
129 
130 } // namespace nlsr
131 
132 #endif // NLSR_MAP_HPP
void createFromCoordinateLsdb(IteratorType begin, IteratorType end)
Definition: map.hpp:99
size_t getMapSize() const
Definition: map.hpp:114
Copyright (c) 2014-2018, The University of Memphis, Regents of the University of California.
Data abstraction for AdjLsa AdjacencyLsa := ADJACENCY-LSA-TYPE TLV-LENGTH Lsa Adjacency*.
Definition: adj-lsa.hpp:37
int32_t mappingNumber
Definition: map.hpp:36
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:54
void createFromAdjLsdb(IteratorType begin, IteratorType end)
Definition: map.hpp:81
ndn::Name router
Definition: map.hpp:35
Copyright (c) 2014-2020, The University of Memphis, Regents of the University of California, Arizona Board of Regents.
Map()
Definition: map.hpp:61