blob: f2e8eaef69031b676c47f3736675a1a7678e6772 [file] [log] [blame]
akmhoque3d06e792014-05-27 16:23:20 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Ashlesh Gawande0421bc62020-05-08 20:42:19 -07002/*
Davide Pesaventoc1d0e8e2022-06-15 14:26:02 -04003 * Copyright (c) 2014-2022, The University of Memphis,
Nick Gordonf8b5bcd2016-08-11 15:06:50 -05004 * Regents of the University of California
akmhoque3d06e792014-05-27 16:23:20 -05005 *
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/>.
Ashlesh Gawande0421bc62020-05-08 20:42:19 -070019 */
Nick Gordon22b5c952017-08-10 17:48:15 -050020
akmhoque53353462014-04-22 08:43:45 -050021#ifndef NLSR_MAP_HPP
22#define NLSR_MAP_HPP
23
Nick Gordon22b5c952017-08-10 17:48:15 -050024#include "common.hpp"
Ashlesh Gawande57a87172020-05-09 19:47:06 -070025#include "lsa/adj-lsa.hpp"
akmhoque53353462014-04-22 08:43:45 -050026
Nick Gordone40377d2017-08-11 15:10:02 -050027#include <boost/multi_index_container.hpp>
28#include <boost/multi_index/hashed_index.hpp>
Ashlesh Gawande0421bc62020-05-08 20:42:19 -070029#include <boost/multi_index/member.hpp>
Nick Gordone40377d2017-08-11 15:10:02 -050030#include <boost/multi_index/tag.hpp>
31
Davide Pesaventoc1d0e8e2022-06-15 14:26:02 -040032#include <optional>
33
akmhoque53353462014-04-22 08:43:45 -050034namespace nlsr {
35
Davide Pesaventoc1d0e8e2022-06-15 14:26:02 -040036struct MapEntry
37{
Ashlesh Gawande0421bc62020-05-08 20:42:19 -070038 ndn::Name router;
39 int32_t mappingNumber = -1;
40};
41
Nick Gordone40377d2017-08-11 15:10:02 -050042namespace 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>,
Ashlesh Gawande0421bc62020-05-08 20:42:19 -070052 member<MapEntry, ndn::Name, &MapEntry::router>,
Nick Gordone40377d2017-08-11 15:10:02 -050053 std::hash<ndn::Name>>,
54 hashed_unique<tag<byMappingNumber>,
Ashlesh Gawande0421bc62020-05-08 20:42:19 -070055 member<MapEntry, int32_t, &MapEntry::mappingNumber>>
Nick Gordone40377d2017-08-11 15:10:02 -050056 >
57 >;
58
59} // namespace detail
60
akmhoque53353462014-04-22 08:43:45 -050061class Map
62{
63public:
64 Map()
65 : m_mappingIndex(0)
66 {
67 }
68
Nick G97e34942016-07-11 14:46:27 -050069 /*! \brief Add a map entry to this map.
70 \param rtrName The name of the router.
akmhoque53353462014-04-22 08:43:45 -050071
Nick G97e34942016-07-11 14:46:27 -050072 Adds a router to this map. Each entry is also given an arbitrary,
73 ascending mappingNo (mapping number).
74 */
akmhoque53353462014-04-22 08:43:45 -050075 void
akmhoque31d1d4b2014-05-05 22:08:14 -050076 addEntry(const ndn::Name& rtrName);
akmhoque53353462014-04-22 08:43:45 -050077
Nick Gordon22b5c952017-08-10 17:48:15 -050078 /*! Populates the Map with AdjacencyLsas.
akmhoque53353462014-04-22 08:43:45 -050079
Nick Gordon22b5c952017-08-10 17:48:15 -050080 \note IteratorType must an iterator type, and begin to end must represent a valid range.
81 */
82 template<typename IteratorType>
Nick Gordone8e03ac2016-07-07 14:24:38 -050083 void
Nick Gordon22b5c952017-08-10 17:48:15 -050084 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++) {
Ashlesh Gawande57a87172020-05-09 19:47:06 -070088 auto adjLsa = std::static_pointer_cast<AdjLsa>(*lsa);
89 addEntry(adjLsa->getOriginRouter());
90 for (const auto& adjacent : adjLsa->getAdl().getAdjList()) {
Nick Gordon22b5c952017-08-10 17:48:15 -050091 addEntry(adjacent.getName());
92 }
93 }
94 }
95
96 /*! Populates the Map with CoordinateLsas.
97
98 \note IteratorType must an iterator type, and begin to end must represent a valid range.
99 */
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++) {
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700106 addEntry((*lsa)->getOriginRouter());
Nick Gordon22b5c952017-08-10 17:48:15 -0500107 }
108 }
Nick Gordone8e03ac2016-07-07 14:24:38 -0500109
Davide Pesaventoc1d0e8e2022-06-15 14:26:02 -0400110 std::optional<ndn::Name>
Saurab Dulal9da6aa72018-01-12 17:25:51 +0000111 getRouterNameByMappingNo(int32_t mn) const;
akmhoque53353462014-04-22 08:43:45 -0500112
Davide Pesaventoc1d0e8e2022-06-15 14:26:02 -0400113 std::optional<int32_t>
akmhoque31d1d4b2014-05-05 22:08:14 -0500114 getMappingNoByRouterName(const ndn::Name& rName);
akmhoque53353462014-04-22 08:43:45 -0500115
akmhoque31d1d4b2014-05-05 22:08:14 -0500116 size_t
akmhoque53353462014-04-22 08:43:45 -0500117 getMapSize() const
118 {
Nick Gordone40377d2017-08-11 15:10:02 -0500119 return m_entries.size();
akmhoque53353462014-04-22 08:43:45 -0500120 }
121
akmhoque2f423352014-06-03 11:49:35 -0500122 void
123 writeLog();
akmhoque53353462014-04-22 08:43:45 -0500124
125private:
126 bool
akmhoquefdbddb12014-05-02 18:35:19 -0500127 addEntry(MapEntry& mpe);
akmhoque53353462014-04-22 08:43:45 -0500128
akmhoquefdbddb12014-05-02 18:35:19 -0500129 int32_t m_mappingIndex;
Nick Gordone40377d2017-08-11 15:10:02 -0500130 detail::entryContainer m_entries;
akmhoque53353462014-04-22 08:43:45 -0500131};
132
akmhoque53353462014-04-22 08:43:45 -0500133} // namespace nlsr
Nick Gordon22b5c952017-08-10 17:48:15 -0500134
135#endif // NLSR_MAP_HPP