blob: aa55c9e0d8b407ce5e186e72b7ae79dce37ca318 [file] [log] [blame]
akmhoque3d06e792014-05-27 16:23:20 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Ashlesh Gawande304bebf2018-02-07 17:10:35 -06003 * Copyright (c) 2014-2018, The University of Memphis,
Vince Lehmancae33b62015-06-05 09:21:30 -05004 * Regents of the University of California,
5 * Arizona Board of Regents.
akmhoque3d06e792014-05-27 16:23:20 -05006 *
7 * This file is part of NLSR (Named-data Link State Routing).
8 * See AUTHORS.md for complete list of NLSR authors and contributors.
9 *
10 * NLSR is free software: you can redistribute it and/or modify it under the terms
11 * of the GNU General Public License as published by the Free Software Foundation,
12 * either version 3 of the License, or (at your option) any later version.
13 *
14 * NLSR is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
15 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
16 * PURPOSE. See the GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along with
19 * NLSR, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
akmhoque3d06e792014-05-27 16:23:20 -050020 **/
Vince Lehmancae33b62015-06-05 09:21:30 -050021
akmhoquefdbddb12014-05-02 18:35:19 -050022#ifndef NLSR_NAME_PREFIX_TABLE_HPP
23#define NLSR_NAME_PREFIX_TABLE_HPP
akmhoque53353462014-04-22 08:43:45 -050024
akmhoquec8a10f72014-04-25 18:42:55 -050025#include "name-prefix-table-entry.hpp"
Nick Gordonb50e51b2016-07-22 16:05:57 -050026#include "routing-table-pool-entry.hpp"
Nick Gordonb7b58392017-08-17 16:29:21 -050027#include "signals.hpp"
Nick Gordonb50e51b2016-07-22 16:05:57 -050028#include "test-access-control.hpp"
akmhoque53353462014-04-22 08:43:45 -050029
Vince Lehmancae33b62015-06-05 09:21:30 -050030#include <list>
Nick Gordonb50e51b2016-07-22 16:05:57 -050031#include <unordered_map>
Vince Lehmancae33b62015-06-05 09:21:30 -050032
akmhoque53353462014-04-22 08:43:45 -050033namespace nlsr {
34class Nlsr;
35
akmhoquec8a10f72014-04-25 18:42:55 -050036class NamePrefixTable
akmhoque53353462014-04-22 08:43:45 -050037{
38public:
Nick Gordonc0c6bcf2017-08-15 18:11:21 -050039 using RoutingTableEntryPool =
40 std::unordered_map<ndn::Name, std::shared_ptr<RoutingTablePoolEntry>>;
41 using NptEntryList = std::list<std::shared_ptr<NamePrefixTableEntry>>;
42 using const_iterator = NptEntryList::const_iterator;
Nick Gordonb50e51b2016-07-22 16:05:57 -050043
Nick Gordond40a5882017-09-05 15:34:58 -050044 NamePrefixTable(Nlsr& nlsr, std::unique_ptr<AfterRoutingChange>& afterRoutingChangeSignal);
Nick Gordonb7b58392017-08-17 16:29:21 -050045
46 ~NamePrefixTable();
akmhoque53353462014-04-22 08:43:45 -050047
Nick Gordonb50e51b2016-07-22 16:05:57 -050048 /*! \brief Adds a destination to the specified name prefix.
49 \param name The name prefix
50 \param destRouter The destination router prefix
Nick G97e34942016-07-11 14:46:27 -050051
Nick Gordonb50e51b2016-07-22 16:05:57 -050052 This method adds a router to a name prefix table entry. If the
53 name prefix table entry does not exist, it is created. The method
54 will first look through its local pool of cached entries to find
55 the routing information for destRouter. If it is not found there,
56 it will construct one and fill it with information from an
57 appropriate RoutingTableEntry in the routing table. If there isn't
58 a match, it will instantiate it with no next hops. The FIB will be
59 notified of the change to the NPT entry, too.
60 */
akmhoque53353462014-04-22 08:43:45 -050061 void
akmhoque31d1d4b2014-05-05 22:08:14 -050062 addEntry(const ndn::Name& name, const ndn::Name& destRouter);
akmhoquefdbddb12014-05-02 18:35:19 -050063
Nick Gordonb50e51b2016-07-22 16:05:57 -050064 /*! \brief Removes a destination from a name prefix table entry.
65 \param name The name prefix
66 \param destRouter The destination.
67
68 This method removes a destination from an entry. It will not fail
69 if an invalid name/destination pair are passed. After removal, if
70 the RoutingTablePoolEntry has a use count of 0, it is deleted from
71 the table. Additionally, if the name prefix has no routing table
72 entries associated with it, it is deleted from the NPT. In any
73 case, the FIB is informed of the changes.
74 */
akmhoquefdbddb12014-05-02 18:35:19 -050075 void
akmhoque31d1d4b2014-05-05 22:08:14 -050076 removeEntry(const ndn::Name& name, const ndn::Name& destRouter);
akmhoque53353462014-04-22 08:43:45 -050077
Nick Gordonb50e51b2016-07-22 16:05:57 -050078 /*! \brief Updates all routing information in the NPT.
79
Nick Gordonb7b58392017-08-17 16:29:21 -050080 Takes in a list of entries that are assumed to be exhaustive, and
81 updates each pool entry with the next hop information contained in
82 the corresponding entry in entries. If no entry is found, it is
83 assumed that the destination for that pool entry is inaccessible,
84 and its next hop information is deleted.
Nick Gordonb50e51b2016-07-22 16:05:57 -050085 */
akmhoque53353462014-04-22 08:43:45 -050086 void
Nick Gordonb7b58392017-08-17 16:29:21 -050087 updateWithNewRoute(const std::list<RoutingTableEntry>& entries);
akmhoque53353462014-04-22 08:43:45 -050088
Nick Gordonb50e51b2016-07-22 16:05:57 -050089 /*! \brief Adds a pool entry to the pool.
90 \param rtpe The entry.
91
92 \return A shared_ptr to the entry, now in the pool.
93
94 Adds a RoutingTablePoolEntry to the NPT's local pool. Shared
95 pointers are used because it eliminates complicated hacks to deal
96 with lifetime issues, and to simplify memory management.
97 */
98 std::shared_ptr<RoutingTablePoolEntry>
99 addRtpeToPool(RoutingTablePoolEntry& rtpe);
100
101 /*! \brief Removes a pool entry from the pool.
102 \param rtpePtr The shared_ptr to the entry.
103
104 Removes a pool entry from the pool. Comparing these shared_ptrs
105 should not be a problem, because the same pointer is moved around,
106 all sourced from this central location. A more robust solution is
107 certainly possible, though.
108 */
109 void
110 deleteRtpeFromPool(std::shared_ptr<RoutingTablePoolEntry> rtpePtr);
111
akmhoque53353462014-04-22 08:43:45 -0500112 void
akmhoque674b0b12014-05-20 14:33:28 -0500113 writeLog();
114
Vince Lehmancae33b62015-06-05 09:21:30 -0500115 const_iterator
116 begin() const;
117
118 const_iterator
119 end() const;
120
Nick Gordonb50e51b2016-07-22 16:05:57 -0500121PUBLIC_WITH_TESTS_ELSE_PRIVATE:
Nick Gordonc0c6bcf2017-08-15 18:11:21 -0500122 RoutingTableEntryPool m_rtpool;
123
Nick Gordonb50e51b2016-07-22 16:05:57 -0500124 NptEntryList m_table;
akmhoque53353462014-04-22 08:43:45 -0500125
126private:
akmhoque31d1d4b2014-05-05 22:08:14 -0500127 Nlsr& m_nlsr;
Nick Gordonb7b58392017-08-17 16:29:21 -0500128 ndn::util::signal::Connection m_afterRoutingChangeConnection;
akmhoque53353462014-04-22 08:43:45 -0500129};
130
Vince Lehmancae33b62015-06-05 09:21:30 -0500131inline NamePrefixTable::const_iterator
132NamePrefixTable::begin() const
133{
134 return m_table.begin();
135}
akmhoque53353462014-04-22 08:43:45 -0500136
Vince Lehmancae33b62015-06-05 09:21:30 -0500137inline NamePrefixTable::const_iterator
138NamePrefixTable::end() const
139{
140 return m_table.end();
141}
142
143std::ostream&
144operator<<(std::ostream& os, const NamePrefixTable& table);
145
146} // namespace nlsr
147
148#endif // NLSR_NAME_PREFIX_TABLE_HPP