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