akmhoque | 3d06e79 | 2014-05-27 16:23:20 -0500 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Nick Gordon | c6a8522 | 2017-01-03 16:54:34 -0600 | [diff] [blame] | 3 | * Copyright (c) 2014-2017, The University of Memphis, |
Vince Lehman | cae33b6 | 2015-06-05 09:21:30 -0500 | [diff] [blame] | 4 | * Regents of the University of California, |
| 5 | * Arizona Board of Regents. |
akmhoque | 3d06e79 | 2014-05-27 16:23:20 -0500 | [diff] [blame] | 6 | * |
| 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/>. |
akmhoque | 3d06e79 | 2014-05-27 16:23:20 -0500 | [diff] [blame] | 20 | **/ |
Vince Lehman | cae33b6 | 2015-06-05 09:21:30 -0500 | [diff] [blame] | 21 | |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 22 | #ifndef NLSR_NAME_PREFIX_TABLE_HPP |
| 23 | #define NLSR_NAME_PREFIX_TABLE_HPP |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 24 | |
akmhoque | c8a10f7 | 2014-04-25 18:42:55 -0500 | [diff] [blame] | 25 | #include "name-prefix-table-entry.hpp" |
Nick Gordon | b50e51b | 2016-07-22 16:05:57 -0500 | [diff] [blame] | 26 | #include "routing-table-pool-entry.hpp" |
| 27 | |
| 28 | #include "test-access-control.hpp" |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 29 | |
Vince Lehman | cae33b6 | 2015-06-05 09:21:30 -0500 | [diff] [blame] | 30 | #include <list> |
Nick Gordon | b50e51b | 2016-07-22 16:05:57 -0500 | [diff] [blame] | 31 | #include <unordered_map> |
Vince Lehman | cae33b6 | 2015-06-05 09:21:30 -0500 | [diff] [blame] | 32 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 33 | namespace nlsr { |
| 34 | class Nlsr; |
| 35 | |
akmhoque | c8a10f7 | 2014-04-25 18:42:55 -0500 | [diff] [blame] | 36 | class NamePrefixTable |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 37 | { |
| 38 | public: |
Nick Gordon | c0c6bcf | 2017-08-15 18:11:21 -0500 | [diff] [blame^] | 39 | 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 Gordon | b50e51b | 2016-07-22 16:05:57 -0500 | [diff] [blame] | 43 | |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 44 | NamePrefixTable(Nlsr& nlsr) |
| 45 | : m_nlsr(nlsr) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 46 | { |
| 47 | } |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 48 | |
Nick Gordon | b50e51b | 2016-07-22 16:05:57 -0500 | [diff] [blame] | 49 | /*! \brief Adds a destination to the specified name prefix. |
| 50 | \param name The name prefix |
| 51 | \param destRouter The destination router prefix |
Nick G | 97e3494 | 2016-07-11 14:46:27 -0500 | [diff] [blame] | 52 | |
Nick Gordon | b50e51b | 2016-07-22 16:05:57 -0500 | [diff] [blame] | 53 | This method adds a router to a name prefix table entry. If the |
| 54 | name prefix table entry does not exist, it is created. The method |
| 55 | will first look through its local pool of cached entries to find |
| 56 | the routing information for destRouter. If it is not found there, |
| 57 | it will construct one and fill it with information from an |
| 58 | appropriate RoutingTableEntry in the routing table. If there isn't |
| 59 | a match, it will instantiate it with no next hops. The FIB will be |
| 60 | notified of the change to the NPT entry, too. |
| 61 | */ |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 62 | void |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 63 | addEntry(const ndn::Name& name, const ndn::Name& destRouter); |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 64 | |
Nick Gordon | b50e51b | 2016-07-22 16:05:57 -0500 | [diff] [blame] | 65 | /*! \brief Removes a destination from a name prefix table entry. |
| 66 | \param name The name prefix |
| 67 | \param destRouter The destination. |
| 68 | |
| 69 | This method removes a destination from an entry. It will not fail |
| 70 | if an invalid name/destination pair are passed. After removal, if |
| 71 | the RoutingTablePoolEntry has a use count of 0, it is deleted from |
| 72 | the table. Additionally, if the name prefix has no routing table |
| 73 | entries associated with it, it is deleted from the NPT. In any |
| 74 | case, the FIB is informed of the changes. |
| 75 | */ |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 76 | void |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 77 | removeEntry(const ndn::Name& name, const ndn::Name& destRouter); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 78 | |
Nick Gordon | b50e51b | 2016-07-22 16:05:57 -0500 | [diff] [blame] | 79 | /*! \brief Updates all routing information in the NPT. |
| 80 | |
| 81 | Naively iterates over all the NPT entries, then over each of their |
| 82 | RoutingTablePoolEntries, passing the name/destination pair back to |
| 83 | addEntry after updating the pool entry with new information. This |
| 84 | ensures that the FIB is appropriately apprised of any changes to a |
| 85 | prefix's preferred next hops. |
| 86 | */ |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 87 | void |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 88 | updateWithNewRoute(); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 89 | |
Nick Gordon | b50e51b | 2016-07-22 16:05:57 -0500 | [diff] [blame] | 90 | /*! \brief Adds a pool entry to the pool. |
| 91 | \param rtpe The entry. |
| 92 | |
| 93 | \return A shared_ptr to the entry, now in the pool. |
| 94 | |
| 95 | Adds a RoutingTablePoolEntry to the NPT's local pool. Shared |
| 96 | pointers are used because it eliminates complicated hacks to deal |
| 97 | with lifetime issues, and to simplify memory management. |
| 98 | */ |
| 99 | std::shared_ptr<RoutingTablePoolEntry> |
| 100 | addRtpeToPool(RoutingTablePoolEntry& rtpe); |
| 101 | |
| 102 | /*! \brief Removes a pool entry from the pool. |
| 103 | \param rtpePtr The shared_ptr to the entry. |
| 104 | |
| 105 | Removes a pool entry from the pool. Comparing these shared_ptrs |
| 106 | should not be a problem, because the same pointer is moved around, |
| 107 | all sourced from this central location. A more robust solution is |
| 108 | certainly possible, though. |
| 109 | */ |
| 110 | void |
| 111 | deleteRtpeFromPool(std::shared_ptr<RoutingTablePoolEntry> rtpePtr); |
| 112 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 113 | void |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 114 | writeLog(); |
| 115 | |
Vince Lehman | cae33b6 | 2015-06-05 09:21:30 -0500 | [diff] [blame] | 116 | const_iterator |
| 117 | begin() const; |
| 118 | |
| 119 | const_iterator |
| 120 | end() const; |
| 121 | |
Nick Gordon | b50e51b | 2016-07-22 16:05:57 -0500 | [diff] [blame] | 122 | PUBLIC_WITH_TESTS_ELSE_PRIVATE: |
Nick Gordon | c0c6bcf | 2017-08-15 18:11:21 -0500 | [diff] [blame^] | 123 | RoutingTableEntryPool m_rtpool; |
| 124 | |
Nick Gordon | b50e51b | 2016-07-22 16:05:57 -0500 | [diff] [blame] | 125 | NptEntryList m_table; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 126 | |
| 127 | private: |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 128 | Nlsr& m_nlsr; |
Nick Gordon | b50e51b | 2016-07-22 16:05:57 -0500 | [diff] [blame] | 129 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 130 | }; |
| 131 | |
Vince Lehman | cae33b6 | 2015-06-05 09:21:30 -0500 | [diff] [blame] | 132 | inline NamePrefixTable::const_iterator |
| 133 | NamePrefixTable::begin() const |
| 134 | { |
| 135 | return m_table.begin(); |
| 136 | } |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 137 | |
Vince Lehman | cae33b6 | 2015-06-05 09:21:30 -0500 | [diff] [blame] | 138 | inline NamePrefixTable::const_iterator |
| 139 | NamePrefixTable::end() const |
| 140 | { |
| 141 | return m_table.end(); |
| 142 | } |
| 143 | |
Nick Gordon | b50e51b | 2016-07-22 16:05:57 -0500 | [diff] [blame] | 144 | bool |
| 145 | npteCompare(NamePrefixTableEntry& npte, const ndn::Name& name); |
| 146 | |
Vince Lehman | cae33b6 | 2015-06-05 09:21:30 -0500 | [diff] [blame] | 147 | std::ostream& |
| 148 | operator<<(std::ostream& os, const NamePrefixTable& table); |
| 149 | |
| 150 | } // namespace nlsr |
| 151 | |
| 152 | #endif // NLSR_NAME_PREFIX_TABLE_HPP |