blob: b4aa3965ebacc9a28720d2890e7d150d1e17f324 [file] [log] [blame]
akmhoque3d06e792014-05-27 16:23:20 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Ashlesh Gawande85998a12017-12-07 22:22:13 -06003 * Copyright (c) 2014-2019, 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/>.
akmhoque3d06e792014-05-27 16:23:20 -050019 **/
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"
25#include "map-entry.hpp"
26
akmhoque53353462014-04-22 08:43:45 -050027#include <iostream>
28#include <list>
akmhoquefdbddb12014-05-02 18:35:19 -050029#include <boost/cstdint.hpp>
akmhoque53353462014-04-22 08:43:45 -050030
Nick Gordone40377d2017-08-11 15:10:02 -050031#include <boost/multi_index_container.hpp>
32#include <boost/multi_index/hashed_index.hpp>
33#include <boost/multi_index/mem_fun.hpp>
34#include <boost/multi_index/tag.hpp>
35
akmhoque53353462014-04-22 08:43:45 -050036namespace nlsr {
37
Nick Gordone40377d2017-08-11 15:10:02 -050038namespace detail {
39
40 using namespace boost::multi_index;
41 // Define tags so that we can search by different indices.
42 struct byRouterName {};
43 struct byMappingNumber{};
44 using entryContainer = multi_index_container<
45 MapEntry,
46 indexed_by<
47 hashed_unique<tag<byRouterName>,
48 const_mem_fun<MapEntry, const ndn::Name&, &MapEntry::getRouter>,
49 std::hash<ndn::Name>>,
50 hashed_unique<tag<byMappingNumber>,
51 const_mem_fun<MapEntry, int32_t, &MapEntry::getMappingNumber>>
52 >
53 >;
54
55} // namespace detail
56
akmhoque53353462014-04-22 08:43:45 -050057class Map
58{
59public:
60 Map()
61 : m_mappingIndex(0)
62 {
63 }
64
Nick G97e34942016-07-11 14:46:27 -050065 /*! \brief Add a map entry to this map.
66 \param rtrName The name of the router.
akmhoque53353462014-04-22 08:43:45 -050067
Nick G97e34942016-07-11 14:46:27 -050068 Adds a router to this map. Each entry is also given an arbitrary,
69 ascending mappingNo (mapping number).
70 */
akmhoque53353462014-04-22 08:43:45 -050071 void
akmhoque31d1d4b2014-05-05 22:08:14 -050072 addEntry(const ndn::Name& rtrName);
akmhoque53353462014-04-22 08:43:45 -050073
Nick Gordon22b5c952017-08-10 17:48:15 -050074 /*! Populates the Map with AdjacencyLsas.
akmhoque53353462014-04-22 08:43:45 -050075
Nick Gordon22b5c952017-08-10 17:48:15 -050076 \note IteratorType must an iterator type, and begin to end must represent a valid range.
77 */
78 template<typename IteratorType>
Nick Gordone8e03ac2016-07-07 14:24:38 -050079 void
Nick Gordon22b5c952017-08-10 17:48:15 -050080 createFromAdjLsdb(IteratorType begin, IteratorType end)
81 {
82 BOOST_STATIC_ASSERT_MSG(is_iterator<IteratorType>::value, "IteratorType must be an iterator!");
83 for (auto lsa = begin; lsa != end; lsa++) {
84 addEntry(lsa->getOrigRouter());
85 for (const auto& adjacent : lsa->getAdl().getAdjList()) {
86 addEntry(adjacent.getName());
87 }
88 }
89 }
90
91 /*! Populates the Map with CoordinateLsas.
92
93 \note IteratorType must an iterator type, and begin to end must represent a valid range.
94 */
95 template<typename IteratorType>
96 void
97 createFromCoordinateLsdb(IteratorType begin, IteratorType end)
98 {
99 BOOST_STATIC_ASSERT_MSG(is_iterator<IteratorType>::value, "IteratorType must be an iterator!");
100 for (auto lsa = begin; lsa != end; lsa++) {
101 addEntry(lsa->getOrigRouter());
102 }
103 }
Nick Gordone8e03ac2016-07-07 14:24:38 -0500104
Nick Gordone40377d2017-08-11 15:10:02 -0500105 ndn::optional<ndn::Name>
Saurab Dulal9da6aa72018-01-12 17:25:51 +0000106 getRouterNameByMappingNo(int32_t mn) const;
akmhoque53353462014-04-22 08:43:45 -0500107
Nick Gordone40377d2017-08-11 15:10:02 -0500108 ndn::optional<int32_t>
akmhoque31d1d4b2014-05-05 22:08:14 -0500109 getMappingNoByRouterName(const ndn::Name& rName);
akmhoque53353462014-04-22 08:43:45 -0500110
111 void
112 reset();
113
akmhoque31d1d4b2014-05-05 22:08:14 -0500114 size_t
akmhoque53353462014-04-22 08:43:45 -0500115 getMapSize() const
116 {
Nick Gordone40377d2017-08-11 15:10:02 -0500117 return m_entries.size();
akmhoque53353462014-04-22 08:43:45 -0500118 }
119
akmhoque2f423352014-06-03 11:49:35 -0500120 void
121 writeLog();
akmhoque53353462014-04-22 08:43:45 -0500122
123private:
124 bool
akmhoquefdbddb12014-05-02 18:35:19 -0500125 addEntry(MapEntry& mpe);
akmhoque53353462014-04-22 08:43:45 -0500126
akmhoquefdbddb12014-05-02 18:35:19 -0500127 int32_t m_mappingIndex;
Nick Gordone40377d2017-08-11 15:10:02 -0500128 detail::entryContainer m_entries;
akmhoque53353462014-04-22 08:43:45 -0500129};
130
akmhoque53353462014-04-22 08:43:45 -0500131} // namespace nlsr
Nick Gordon22b5c952017-08-10 17:48:15 -0500132
133#endif // NLSR_MAP_HPP