blob: c43a777de3c0512042c2b88d79848f852c73822f [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/*
Junxiao Shid6922b52024-01-14 19:50:34 +00003 * Copyright (c) 2014-2024, 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
Junxiao Shid6922b52024-01-14 19:50:34 +000027#include <boost/bimap.hpp>
28#include <boost/bimap/unordered_set_of.hpp>
Nick Gordone40377d2017-08-11 15:10:02 -050029
Davide Pesaventoc1d0e8e2022-06-15 14:26:02 -040030#include <optional>
31
akmhoque53353462014-04-22 08:43:45 -050032namespace nlsr {
33
akmhoque53353462014-04-22 08:43:45 -050034class Map
35{
36public:
Nick G97e34942016-07-11 14:46:27 -050037 /*! \brief Add a map entry to this map.
38 \param rtrName The name of the router.
akmhoque53353462014-04-22 08:43:45 -050039
Nick G97e34942016-07-11 14:46:27 -050040 Adds a router to this map. Each entry is also given an arbitrary,
41 ascending mappingNo (mapping number).
42 */
akmhoque53353462014-04-22 08:43:45 -050043 void
akmhoque31d1d4b2014-05-05 22:08:14 -050044 addEntry(const ndn::Name& rtrName);
akmhoque53353462014-04-22 08:43:45 -050045
Nick Gordon22b5c952017-08-10 17:48:15 -050046 /*! Populates the Map with AdjacencyLsas.
akmhoque53353462014-04-22 08:43:45 -050047
Nick Gordon22b5c952017-08-10 17:48:15 -050048 \note IteratorType must an iterator type, and begin to end must represent a valid range.
49 */
50 template<typename IteratorType>
Nick Gordone8e03ac2016-07-07 14:24:38 -050051 void
Nick Gordon22b5c952017-08-10 17:48:15 -050052 createFromAdjLsdb(IteratorType begin, IteratorType end)
53 {
54 BOOST_STATIC_ASSERT_MSG(is_iterator<IteratorType>::value, "IteratorType must be an iterator!");
55 for (auto lsa = begin; lsa != end; lsa++) {
Ashlesh Gawande57a87172020-05-09 19:47:06 -070056 auto adjLsa = std::static_pointer_cast<AdjLsa>(*lsa);
57 addEntry(adjLsa->getOriginRouter());
58 for (const auto& adjacent : adjLsa->getAdl().getAdjList()) {
Nick Gordon22b5c952017-08-10 17:48:15 -050059 addEntry(adjacent.getName());
60 }
61 }
62 }
63
64 /*! Populates the Map with CoordinateLsas.
65
66 \note IteratorType must an iterator type, and begin to end must represent a valid range.
67 */
68 template<typename IteratorType>
69 void
70 createFromCoordinateLsdb(IteratorType begin, IteratorType end)
71 {
72 BOOST_STATIC_ASSERT_MSG(is_iterator<IteratorType>::value, "IteratorType must be an iterator!");
73 for (auto lsa = begin; lsa != end; lsa++) {
Ashlesh Gawande57a87172020-05-09 19:47:06 -070074 addEntry((*lsa)->getOriginRouter());
Nick Gordon22b5c952017-08-10 17:48:15 -050075 }
76 }
Nick Gordone8e03ac2016-07-07 14:24:38 -050077
Davide Pesaventoc1d0e8e2022-06-15 14:26:02 -040078 std::optional<ndn::Name>
Saurab Dulal9da6aa72018-01-12 17:25:51 +000079 getRouterNameByMappingNo(int32_t mn) const;
akmhoque53353462014-04-22 08:43:45 -050080
Davide Pesaventoc1d0e8e2022-06-15 14:26:02 -040081 std::optional<int32_t>
akmhoque31d1d4b2014-05-05 22:08:14 -050082 getMappingNoByRouterName(const ndn::Name& rName);
akmhoque53353462014-04-22 08:43:45 -050083
akmhoque31d1d4b2014-05-05 22:08:14 -050084 size_t
Junxiao Shid6922b52024-01-14 19:50:34 +000085 size() const
akmhoque53353462014-04-22 08:43:45 -050086 {
Junxiao Shid6922b52024-01-14 19:50:34 +000087 return m_bimap.size();
akmhoque53353462014-04-22 08:43:45 -050088 }
89
akmhoque53353462014-04-22 08:43:45 -050090private:
Junxiao Shid6922b52024-01-14 19:50:34 +000091 struct MappingNo;
92 boost::bimap<
93 boost::bimaps::unordered_set_of<
94 boost::bimaps::tagged<ndn::Name, ndn::Name>,
95 std::hash<ndn::Name>
96 >,
97 boost::bimaps::unordered_set_of<
98 boost::bimaps::tagged<int32_t, MappingNo>
99 >
100 > m_bimap;
akmhoque53353462014-04-22 08:43:45 -0500101
Junxiao Shid6922b52024-01-14 19:50:34 +0000102 friend std::ostream&
103 operator<<(std::ostream& os, const Map& map);
akmhoque53353462014-04-22 08:43:45 -0500104};
105
Junxiao Shid6922b52024-01-14 19:50:34 +0000106std::ostream&
107operator<<(std::ostream& os, const Map& map);
108
akmhoque53353462014-04-22 08:43:45 -0500109} // namespace nlsr
Nick Gordon22b5c952017-08-10 17:48:15 -0500110
111#endif // NLSR_MAP_HPP