Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Junxiao Shi | 3f21e58 | 2017-05-29 15:26:32 +0000 | [diff] [blame^] | 3 | * Copyright (c) 2014-2017, Regents of the University of California, |
Alexander Afanasyev | 7c10b3b | 2015-01-20 12:24:27 -0800 | [diff] [blame] | 4 | * Arizona Board of Regents, |
| 5 | * Colorado State University, |
| 6 | * University Pierre & Marie Curie, Sorbonne University, |
| 7 | * Washington University in St. Louis, |
| 8 | * Beijing Institute of Technology, |
| 9 | * The University of Memphis. |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 10 | * |
| 11 | * This file is part of NFD (Named Data Networking Forwarding Daemon). |
| 12 | * See AUTHORS.md for complete list of NFD authors and contributors. |
| 13 | * |
| 14 | * NFD is free software: you can redistribute it and/or modify it under the terms |
| 15 | * of the GNU General Public License as published by the Free Software Foundation, |
| 16 | * either version 3 of the License, or (at your option) any later version. |
| 17 | * |
| 18 | * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 19 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 20 | * PURPOSE. See the GNU General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU General Public License along with |
| 23 | * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 24 | */ |
| 25 | |
| 26 | #include "rib-entry.hpp" |
| 27 | |
Vince Lehman | 281ded7 | 2014-08-21 12:17:08 -0500 | [diff] [blame] | 28 | #include "core/logger.hpp" |
| 29 | |
Junxiao Shi | 25c6ce4 | 2016-09-09 13:49:59 +0000 | [diff] [blame] | 30 | #include <ndn-cxx/mgmt/nfd/control-command.hpp> |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 31 | |
Vince Lehman | 281ded7 | 2014-08-21 12:17:08 -0500 | [diff] [blame] | 32 | NFD_LOG_INIT("RibEntry"); |
| 33 | |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 34 | namespace nfd { |
| 35 | namespace rib { |
| 36 | |
Vince Lehman | 218be0a | 2015-01-15 17:25:20 -0600 | [diff] [blame] | 37 | RibEntry::RouteList::iterator |
| 38 | RibEntry::findRoute(const Route& route) |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 39 | { |
Vince Lehman | 218be0a | 2015-01-15 17:25:20 -0600 | [diff] [blame] | 40 | return std::find_if(begin(), end(), bind(&compareFaceIdAndOrigin, _1, route)); |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 41 | } |
| 42 | |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 43 | RibEntry::RouteList::const_iterator |
| 44 | RibEntry::findRoute(const Route& route) const |
| 45 | { |
| 46 | return std::find_if(begin(), end(), bind(&compareFaceIdAndOrigin, _1, route)); |
| 47 | } |
| 48 | |
Nick Gordon | 89c4cca | 2016-11-02 15:42:32 +0000 | [diff] [blame] | 49 | std::pair<RibEntry::iterator, bool> |
Vince Lehman | 218be0a | 2015-01-15 17:25:20 -0600 | [diff] [blame] | 50 | RibEntry::insertRoute(const Route& route) |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 51 | { |
Vince Lehman | 218be0a | 2015-01-15 17:25:20 -0600 | [diff] [blame] | 52 | iterator it = findRoute(route); |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 53 | |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 54 | if (it == end()) { |
| 55 | if (route.flags & ndn::nfd::ROUTE_FLAG_CAPTURE) { |
| 56 | m_nRoutesWithCaptureSet++; |
| 57 | } |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 58 | |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 59 | m_routes.push_back(route); |
| 60 | |
Nick Gordon | 89c4cca | 2016-11-02 15:42:32 +0000 | [diff] [blame] | 61 | return std::make_pair(std::prev(m_routes.end()), true); |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 62 | } |
| 63 | else { |
Nick Gordon | 89c4cca | 2016-11-02 15:42:32 +0000 | [diff] [blame] | 64 | return std::make_pair(it, false); |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 65 | } |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 66 | } |
| 67 | |
Vince Lehman | 281ded7 | 2014-08-21 12:17:08 -0500 | [diff] [blame] | 68 | void |
Vince Lehman | 218be0a | 2015-01-15 17:25:20 -0600 | [diff] [blame] | 69 | RibEntry::eraseRoute(const Route& route) |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 70 | { |
Vince Lehman | 218be0a | 2015-01-15 17:25:20 -0600 | [diff] [blame] | 71 | RibEntry::iterator it = findRoute(route); |
| 72 | eraseRoute(it); |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | bool |
Vince Lehman | 218be0a | 2015-01-15 17:25:20 -0600 | [diff] [blame] | 76 | RibEntry::hasRoute(const Route& route) |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 77 | { |
Vince Lehman | 218be0a | 2015-01-15 17:25:20 -0600 | [diff] [blame] | 78 | RibEntry::const_iterator it = findRoute(route); |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 79 | |
Vince Lehman | 218be0a | 2015-01-15 17:25:20 -0600 | [diff] [blame] | 80 | return it != end(); |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | bool |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 84 | RibEntry::hasFaceId(const uint64_t faceId) const |
| 85 | { |
Vince Lehman | 218be0a | 2015-01-15 17:25:20 -0600 | [diff] [blame] | 86 | RibEntry::const_iterator it = std::find_if(begin(), end(), bind(&compareFaceId, _1, faceId)); |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 87 | |
Vince Lehman | 218be0a | 2015-01-15 17:25:20 -0600 | [diff] [blame] | 88 | return it != end(); |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 89 | } |
| 90 | |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 91 | size_t |
| 92 | RibEntry::getNRoutes() const |
| 93 | { |
| 94 | return m_routes.size(); |
| 95 | } |
| 96 | |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 97 | void |
| 98 | RibEntry::addChild(shared_ptr<RibEntry> child) |
| 99 | { |
| 100 | BOOST_ASSERT(!static_cast<bool>(child->getParent())); |
| 101 | child->setParent(this->shared_from_this()); |
| 102 | m_children.push_back(child); |
| 103 | } |
| 104 | |
| 105 | void |
| 106 | RibEntry::removeChild(shared_ptr<RibEntry> child) |
| 107 | { |
| 108 | BOOST_ASSERT(child->getParent().get() == this); |
| 109 | child->setParent(shared_ptr<RibEntry>()); |
| 110 | m_children.remove(child); |
| 111 | } |
| 112 | |
Vince Lehman | 218be0a | 2015-01-15 17:25:20 -0600 | [diff] [blame] | 113 | RibEntry::RouteList::iterator |
| 114 | RibEntry::eraseRoute(RouteList::iterator route) |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 115 | { |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 116 | if (route != m_routes.end()) { |
| 117 | if (route->flags & ndn::nfd::ROUTE_FLAG_CAPTURE) { |
| 118 | m_nRoutesWithCaptureSet--; |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 119 | } |
| 120 | |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 121 | // Cancel any scheduled event |
| 122 | NFD_LOG_TRACE("Cancelling expiration eventId: " << route->getExpirationEvent()); |
| 123 | scheduler::cancel(route->getExpirationEvent()); |
| 124 | |
| 125 | return m_routes.erase(route); |
| 126 | } |
| 127 | |
Vince Lehman | 218be0a | 2015-01-15 17:25:20 -0600 | [diff] [blame] | 128 | return m_routes.end(); |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | void |
Vince Lehman | 218be0a | 2015-01-15 17:25:20 -0600 | [diff] [blame] | 132 | RibEntry::addInheritedRoute(const Route& route) |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 133 | { |
Vince Lehman | 218be0a | 2015-01-15 17:25:20 -0600 | [diff] [blame] | 134 | m_inheritedRoutes.push_back(route); |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | void |
Vince Lehman | 218be0a | 2015-01-15 17:25:20 -0600 | [diff] [blame] | 138 | RibEntry::removeInheritedRoute(const Route& route) |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 139 | { |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 140 | RouteList::iterator it = std::find_if(m_inheritedRoutes.begin(), m_inheritedRoutes.end(), |
| 141 | bind(&compareFaceId, _1, route.faceId)); |
Vince Lehman | 218be0a | 2015-01-15 17:25:20 -0600 | [diff] [blame] | 142 | m_inheritedRoutes.erase(it); |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 143 | } |
| 144 | |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 145 | RibEntry::RouteList::const_iterator |
| 146 | RibEntry::findInheritedRoute(const Route& route) const |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 147 | { |
Vince Lehman | 218be0a | 2015-01-15 17:25:20 -0600 | [diff] [blame] | 148 | return std::find_if(m_inheritedRoutes.begin(), m_inheritedRoutes.end(), |
| 149 | bind(&compareFaceId, _1, route.faceId)); |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | bool |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 153 | RibEntry::hasInheritedRoute(const Route& route) const |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 154 | { |
Vince Lehman | 218be0a | 2015-01-15 17:25:20 -0600 | [diff] [blame] | 155 | RouteList::const_iterator it = findInheritedRoute(route); |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 156 | |
Vince Lehman | 218be0a | 2015-01-15 17:25:20 -0600 | [diff] [blame] | 157 | return (it != m_inheritedRoutes.end()); |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | bool |
| 161 | RibEntry::hasCapture() const |
| 162 | { |
Vince Lehman | 218be0a | 2015-01-15 17:25:20 -0600 | [diff] [blame] | 163 | return m_nRoutesWithCaptureSet > 0; |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | bool |
| 167 | RibEntry::hasChildInheritOnFaceId(uint64_t faceId) const |
| 168 | { |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 169 | for (const Route& route : m_routes) { |
| 170 | if (route.faceId == faceId && (route.flags & ndn::nfd::ROUTE_FLAG_CHILD_INHERIT)) { |
| 171 | return true; |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 172 | } |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 173 | } |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 174 | |
| 175 | return false; |
| 176 | } |
| 177 | |
Vince Lehman | 9dcfc40 | 2015-03-26 03:18:54 -0500 | [diff] [blame] | 178 | const Route* |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 179 | RibEntry::getRouteWithLowestCostByFaceId(uint64_t faceId) const |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 180 | { |
Vince Lehman | 9dcfc40 | 2015-03-26 03:18:54 -0500 | [diff] [blame] | 181 | const Route* candidate = nullptr; |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 182 | |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 183 | for (const Route& route : m_routes) { |
| 184 | // Matching face ID |
| 185 | if (route.faceId == faceId) { |
| 186 | // If this is the first route with this Face ID found |
| 187 | if (candidate == nullptr) { |
Vince Lehman | 9dcfc40 | 2015-03-26 03:18:54 -0500 | [diff] [blame] | 188 | candidate = &route; |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 189 | } |
| 190 | else if (route.cost < candidate->cost) { |
| 191 | // Found a route with a lower cost |
Vince Lehman | 9dcfc40 | 2015-03-26 03:18:54 -0500 | [diff] [blame] | 192 | candidate = &route; |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 193 | } |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 194 | } |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 195 | } |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 196 | |
Vince Lehman | 218be0a | 2015-01-15 17:25:20 -0600 | [diff] [blame] | 197 | return candidate; |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 198 | } |
| 199 | |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 200 | const Route* |
| 201 | RibEntry::getRouteWithSecondLowestCostByFaceId(uint64_t faceId) const |
| 202 | { |
Vince Lehman | 4310d50 | 2016-03-04 11:58:59 -0600 | [diff] [blame] | 203 | std::vector<const Route*> matches; |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 204 | |
| 205 | // Copy routes which have faceId |
Vince Lehman | 4310d50 | 2016-03-04 11:58:59 -0600 | [diff] [blame] | 206 | for (const Route& route : m_routes) { |
| 207 | if (route.faceId == faceId) { |
| 208 | matches.push_back(&route); |
| 209 | } |
| 210 | } |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 211 | |
Vince Lehman | 4310d50 | 2016-03-04 11:58:59 -0600 | [diff] [blame] | 212 | // If there are less than 2 routes, there is no second lowest |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 213 | if (matches.size() < 2) { |
| 214 | return nullptr; |
| 215 | } |
| 216 | |
| 217 | // Get second lowest cost |
| 218 | std::nth_element(matches.begin(), matches.begin() + 1, matches.end(), |
Vince Lehman | 4310d50 | 2016-03-04 11:58:59 -0600 | [diff] [blame] | 219 | [] (const Route* lhs, const Route* rhs) { return lhs->cost < rhs->cost; }); |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 220 | |
Vince Lehman | 4310d50 | 2016-03-04 11:58:59 -0600 | [diff] [blame] | 221 | return matches.at(1); |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 222 | } |
| 223 | |
Vince Lehman | 9dcfc40 | 2015-03-26 03:18:54 -0500 | [diff] [blame] | 224 | const Route* |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 225 | RibEntry::getRouteWithLowestCostAndChildInheritByFaceId(uint64_t faceId) const |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 226 | { |
Vince Lehman | 9dcfc40 | 2015-03-26 03:18:54 -0500 | [diff] [blame] | 227 | const Route* candidate = nullptr; |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 228 | |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 229 | for (const Route& route : m_routes) { |
| 230 | // Correct face ID and Child Inherit flag set |
| 231 | if (route.faceId == faceId && |
| 232 | (route.flags & ndn::nfd::ROUTE_FLAG_CHILD_INHERIT) == ndn::nfd::ROUTE_FLAG_CHILD_INHERIT) |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 233 | { |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 234 | // If this is the first route with this Face ID found |
| 235 | if (candidate == nullptr) { |
Vince Lehman | 9dcfc40 | 2015-03-26 03:18:54 -0500 | [diff] [blame] | 236 | candidate = &route; |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 237 | } |
| 238 | else if (route.cost < candidate->cost) { |
| 239 | // Found a route with a lower cost |
Vince Lehman | 9dcfc40 | 2015-03-26 03:18:54 -0500 | [diff] [blame] | 240 | candidate = &route; |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 241 | } |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 242 | } |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 243 | } |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 244 | |
Vince Lehman | 218be0a | 2015-01-15 17:25:20 -0600 | [diff] [blame] | 245 | return candidate; |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | std::ostream& |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 249 | operator<<(std::ostream& os, const RibEntry& entry) |
| 250 | { |
Syed Obaid | 3313a37 | 2014-07-01 01:31:33 -0500 | [diff] [blame] | 251 | os << "RibEntry {\n"; |
Junxiao Shi | 3f21e58 | 2017-05-29 15:26:32 +0000 | [diff] [blame^] | 252 | os << " Name: " << entry.getName() << "\n"; |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 253 | |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 254 | for (const Route& route : entry) { |
Junxiao Shi | 3f21e58 | 2017-05-29 15:26:32 +0000 | [diff] [blame^] | 255 | os << " " << route << "\n"; |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 256 | } |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 257 | |
| 258 | os << "}"; |
| 259 | |
| 260 | return os; |
| 261 | } |
| 262 | |
| 263 | } // namespace rib |
| 264 | } // namespace nfd |