Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Alexander Afanasyev | 7c10b3b | 2015-01-20 12:24:27 -0800 | [diff] [blame^] | 3 | * Copyright (c) 2014-2015, Regents of the University of California, |
| 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 | |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 30 | #include <ndn-cxx/management/nfd-control-command.hpp> |
| 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 | |
| 43 | bool |
Vince Lehman | 218be0a | 2015-01-15 17:25:20 -0600 | [diff] [blame] | 44 | RibEntry::insertRoute(const Route& route) |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 45 | { |
Vince Lehman | 218be0a | 2015-01-15 17:25:20 -0600 | [diff] [blame] | 46 | iterator it = findRoute(route); |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 47 | |
| 48 | if (it == end()) |
| 49 | { |
Vince Lehman | 218be0a | 2015-01-15 17:25:20 -0600 | [diff] [blame] | 50 | if (route.flags & ndn::nfd::ROUTE_FLAG_CAPTURE) |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 51 | { |
Vince Lehman | 218be0a | 2015-01-15 17:25:20 -0600 | [diff] [blame] | 52 | m_nRoutesWithCaptureSet++; |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 53 | } |
| 54 | |
Vince Lehman | 218be0a | 2015-01-15 17:25:20 -0600 | [diff] [blame] | 55 | m_routes.push_back(route); |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 56 | return true; |
| 57 | } |
| 58 | else |
| 59 | { |
| 60 | return false; |
| 61 | } |
| 62 | } |
| 63 | |
Vince Lehman | 281ded7 | 2014-08-21 12:17:08 -0500 | [diff] [blame] | 64 | void |
Vince Lehman | 218be0a | 2015-01-15 17:25:20 -0600 | [diff] [blame] | 65 | RibEntry::eraseRoute(const Route& route) |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 66 | { |
Vince Lehman | 218be0a | 2015-01-15 17:25:20 -0600 | [diff] [blame] | 67 | RibEntry::iterator it = findRoute(route); |
| 68 | eraseRoute(it); |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | bool |
Vince Lehman | 218be0a | 2015-01-15 17:25:20 -0600 | [diff] [blame] | 72 | RibEntry::hasRoute(const Route& route) |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 73 | { |
Vince Lehman | 218be0a | 2015-01-15 17:25:20 -0600 | [diff] [blame] | 74 | RibEntry::const_iterator it = findRoute(route); |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 75 | |
Vince Lehman | 218be0a | 2015-01-15 17:25:20 -0600 | [diff] [blame] | 76 | return it != end(); |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | bool |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 80 | RibEntry::hasFaceId(const uint64_t faceId) const |
| 81 | { |
Vince Lehman | 218be0a | 2015-01-15 17:25:20 -0600 | [diff] [blame] | 82 | RibEntry::const_iterator it = std::find_if(begin(), end(), bind(&compareFaceId, _1, faceId)); |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 83 | |
Vince Lehman | 218be0a | 2015-01-15 17:25:20 -0600 | [diff] [blame] | 84 | return it != end(); |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | void |
| 88 | RibEntry::addChild(shared_ptr<RibEntry> child) |
| 89 | { |
| 90 | BOOST_ASSERT(!static_cast<bool>(child->getParent())); |
| 91 | child->setParent(this->shared_from_this()); |
| 92 | m_children.push_back(child); |
| 93 | } |
| 94 | |
| 95 | void |
| 96 | RibEntry::removeChild(shared_ptr<RibEntry> child) |
| 97 | { |
| 98 | BOOST_ASSERT(child->getParent().get() == this); |
| 99 | child->setParent(shared_ptr<RibEntry>()); |
| 100 | m_children.remove(child); |
| 101 | } |
| 102 | |
Vince Lehman | 218be0a | 2015-01-15 17:25:20 -0600 | [diff] [blame] | 103 | RibEntry::RouteList::iterator |
| 104 | RibEntry::eraseRoute(RouteList::iterator route) |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 105 | { |
Vince Lehman | 218be0a | 2015-01-15 17:25:20 -0600 | [diff] [blame] | 106 | if (route != m_routes.end()) |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 107 | { |
Vince Lehman | 218be0a | 2015-01-15 17:25:20 -0600 | [diff] [blame] | 108 | if (route->flags & ndn::nfd::ROUTE_FLAG_CAPTURE) |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 109 | { |
Vince Lehman | 218be0a | 2015-01-15 17:25:20 -0600 | [diff] [blame] | 110 | m_nRoutesWithCaptureSet--; |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 111 | } |
| 112 | |
Vince Lehman | 281ded7 | 2014-08-21 12:17:08 -0500 | [diff] [blame] | 113 | //cancel any scheduled event |
Vince Lehman | 218be0a | 2015-01-15 17:25:20 -0600 | [diff] [blame] | 114 | NFD_LOG_TRACE("Cancelling expiration eventId: " << route->getExpirationEvent()); |
| 115 | scheduler::cancel(route->getExpirationEvent()); |
Vince Lehman | 281ded7 | 2014-08-21 12:17:08 -0500 | [diff] [blame] | 116 | |
Vince Lehman | 218be0a | 2015-01-15 17:25:20 -0600 | [diff] [blame] | 117 | return m_routes.erase(route); |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 118 | } |
| 119 | |
Vince Lehman | 218be0a | 2015-01-15 17:25:20 -0600 | [diff] [blame] | 120 | return m_routes.end(); |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | void |
Vince Lehman | 218be0a | 2015-01-15 17:25:20 -0600 | [diff] [blame] | 124 | RibEntry::addInheritedRoute(const Route& route) |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 125 | { |
Vince Lehman | 218be0a | 2015-01-15 17:25:20 -0600 | [diff] [blame] | 126 | m_inheritedRoutes.push_back(route); |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | void |
Vince Lehman | 218be0a | 2015-01-15 17:25:20 -0600 | [diff] [blame] | 130 | RibEntry::removeInheritedRoute(const Route& route) |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 131 | { |
Vince Lehman | 218be0a | 2015-01-15 17:25:20 -0600 | [diff] [blame] | 132 | RouteList::iterator it = findInheritedRoute(route); |
| 133 | m_inheritedRoutes.erase(it); |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 134 | } |
| 135 | |
Vince Lehman | 218be0a | 2015-01-15 17:25:20 -0600 | [diff] [blame] | 136 | RibEntry::RouteList::iterator |
| 137 | RibEntry::findInheritedRoute(const Route& route) |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 138 | { |
Vince Lehman | 218be0a | 2015-01-15 17:25:20 -0600 | [diff] [blame] | 139 | return std::find_if(m_inheritedRoutes.begin(), m_inheritedRoutes.end(), |
| 140 | bind(&compareFaceId, _1, route.faceId)); |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | bool |
Vince Lehman | 218be0a | 2015-01-15 17:25:20 -0600 | [diff] [blame] | 144 | RibEntry::hasInheritedRoute(const Route& route) |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 145 | { |
Vince Lehman | 218be0a | 2015-01-15 17:25:20 -0600 | [diff] [blame] | 146 | RouteList::const_iterator it = findInheritedRoute(route); |
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 (it != m_inheritedRoutes.end()); |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | bool |
| 152 | RibEntry::hasCapture() const |
| 153 | { |
Vince Lehman | 218be0a | 2015-01-15 17:25:20 -0600 | [diff] [blame] | 154 | return m_nRoutesWithCaptureSet > 0; |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | bool |
| 158 | RibEntry::hasChildInheritOnFaceId(uint64_t faceId) const |
| 159 | { |
Vince Lehman | 218be0a | 2015-01-15 17:25:20 -0600 | [diff] [blame] | 160 | for (RibEntry::const_iterator it = m_routes.begin(); it != m_routes.end(); ++it) |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 161 | { |
| 162 | if (it->faceId == faceId && (it->flags & ndn::nfd::ROUTE_FLAG_CHILD_INHERIT)) |
| 163 | { |
| 164 | return true; |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | return false; |
| 169 | } |
| 170 | |
Vince Lehman | 218be0a | 2015-01-15 17:25:20 -0600 | [diff] [blame] | 171 | shared_ptr<Route> |
| 172 | RibEntry::getRouteWithLowestCostByFaceId(uint64_t faceId) |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 173 | { |
Vince Lehman | 218be0a | 2015-01-15 17:25:20 -0600 | [diff] [blame] | 174 | shared_ptr<Route> candidate; |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 175 | |
Vince Lehman | 218be0a | 2015-01-15 17:25:20 -0600 | [diff] [blame] | 176 | for (const Route& route : m_routes) |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 177 | { |
| 178 | // Correct face ID |
Vince Lehman | 218be0a | 2015-01-15 17:25:20 -0600 | [diff] [blame] | 179 | if (route.faceId == faceId) |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 180 | { |
Vince Lehman | 218be0a | 2015-01-15 17:25:20 -0600 | [diff] [blame] | 181 | // If this is the first route with this Face ID found |
| 182 | if (candidate == nullptr) |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 183 | { |
Vince Lehman | 218be0a | 2015-01-15 17:25:20 -0600 | [diff] [blame] | 184 | candidate = make_shared<Route>(route); |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 185 | } |
Vince Lehman | 218be0a | 2015-01-15 17:25:20 -0600 | [diff] [blame] | 186 | else if (route.cost < candidate->cost) // Found a route with a lower cost |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 187 | { |
Vince Lehman | 218be0a | 2015-01-15 17:25:20 -0600 | [diff] [blame] | 188 | candidate = make_shared<Route>(route); |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 189 | } |
| 190 | } |
| 191 | } |
| 192 | |
Vince Lehman | 218be0a | 2015-01-15 17:25:20 -0600 | [diff] [blame] | 193 | return candidate; |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 194 | } |
| 195 | |
Vince Lehman | 218be0a | 2015-01-15 17:25:20 -0600 | [diff] [blame] | 196 | shared_ptr<Route> |
| 197 | RibEntry::getRouteWithLowestCostAndChildInheritByFaceId(uint64_t faceId) |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 198 | { |
Vince Lehman | 218be0a | 2015-01-15 17:25:20 -0600 | [diff] [blame] | 199 | shared_ptr<Route> candidate; |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 200 | |
Vince Lehman | 218be0a | 2015-01-15 17:25:20 -0600 | [diff] [blame] | 201 | for (const Route& route : m_routes) |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 202 | { |
| 203 | // Correct face ID and Child Inherit flag set |
Vince Lehman | 218be0a | 2015-01-15 17:25:20 -0600 | [diff] [blame] | 204 | if (route.faceId == faceId && |
| 205 | (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] | 206 | { |
Vince Lehman | 218be0a | 2015-01-15 17:25:20 -0600 | [diff] [blame] | 207 | // If this is the first route with this Face ID found |
| 208 | if (candidate == nullptr) |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 209 | { |
Vince Lehman | 218be0a | 2015-01-15 17:25:20 -0600 | [diff] [blame] | 210 | candidate = make_shared<Route>(route); |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 211 | } |
Vince Lehman | 218be0a | 2015-01-15 17:25:20 -0600 | [diff] [blame] | 212 | else if (route.cost < candidate->cost) // Found a route with a lower cost |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 213 | { |
Vince Lehman | 218be0a | 2015-01-15 17:25:20 -0600 | [diff] [blame] | 214 | candidate = make_shared<Route>(route); |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 215 | } |
| 216 | } |
| 217 | } |
| 218 | |
Vince Lehman | 218be0a | 2015-01-15 17:25:20 -0600 | [diff] [blame] | 219 | return candidate; |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | std::ostream& |
Vince Lehman | 218be0a | 2015-01-15 17:25:20 -0600 | [diff] [blame] | 223 | operator<<(std::ostream& os, const Route& route) |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 224 | { |
Vince Lehman | 218be0a | 2015-01-15 17:25:20 -0600 | [diff] [blame] | 225 | os << "Route(" |
| 226 | << "faceid: " << route.faceId |
| 227 | << ", origin: " << route.origin |
| 228 | << ", cost: " << route.cost |
| 229 | << ", flags: " << route.flags; |
| 230 | if (route.expires != time::steady_clock::TimePoint::max()) { |
| 231 | os << ", expires in: " << (route.expires - time::steady_clock::now()); |
Syed Obaid | 3313a37 | 2014-07-01 01:31:33 -0500 | [diff] [blame] | 232 | } |
| 233 | else { |
| 234 | os << ", never expires"; |
| 235 | } |
| 236 | os << ")"; |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 237 | |
| 238 | return os; |
| 239 | } |
| 240 | |
| 241 | std::ostream& |
| 242 | operator<<(std::ostream& os, const RibEntry& entry) |
| 243 | { |
Syed Obaid | 3313a37 | 2014-07-01 01:31:33 -0500 | [diff] [blame] | 244 | os << "RibEntry {\n"; |
| 245 | os << "\tName: " << entry.getName() << "\n"; |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 246 | |
Vince Lehman | 218be0a | 2015-01-15 17:25:20 -0600 | [diff] [blame] | 247 | for (const Route& route : entry) |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 248 | { |
Vince Lehman | 218be0a | 2015-01-15 17:25:20 -0600 | [diff] [blame] | 249 | os << "\t" << route << "\n"; |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | os << "}"; |
| 253 | |
| 254 | return os; |
| 255 | } |
| 256 | |
| 257 | } // namespace rib |
| 258 | } // namespace nfd |