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 | |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 72 | m_faces.erase(it); |
| 73 | return true; |
| 74 | } |
| 75 | else |
| 76 | { |
| 77 | return false; |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | bool |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 82 | RibEntry::hasFace(const FaceEntry& face) |
| 83 | { |
| 84 | RibEntry::const_iterator it = std::find_if(cbegin(), cend(), |
| 85 | bind(&compareFaceIdAndOrigin, _1, face)); |
| 86 | |
| 87 | return it != cend(); |
| 88 | } |
| 89 | |
| 90 | bool |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 91 | RibEntry::hasFaceId(const uint64_t faceId) const |
| 92 | { |
| 93 | RibEntry::const_iterator it = std::find_if(cbegin(), cend(), bind(&compareFaceId, _1, faceId)); |
| 94 | |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 95 | return it != cend(); |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | void |
| 99 | RibEntry::addChild(shared_ptr<RibEntry> child) |
| 100 | { |
| 101 | BOOST_ASSERT(!static_cast<bool>(child->getParent())); |
| 102 | child->setParent(this->shared_from_this()); |
| 103 | m_children.push_back(child); |
| 104 | } |
| 105 | |
| 106 | void |
| 107 | RibEntry::removeChild(shared_ptr<RibEntry> child) |
| 108 | { |
| 109 | BOOST_ASSERT(child->getParent().get() == this); |
| 110 | child->setParent(shared_ptr<RibEntry>()); |
| 111 | m_children.remove(child); |
| 112 | } |
| 113 | |
| 114 | RibEntry::FaceList::iterator |
| 115 | RibEntry::eraseFace(FaceList::iterator face) |
| 116 | { |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 117 | if (face != m_faces.end()) |
| 118 | { |
| 119 | if (face->flags & ndn::nfd::ROUTE_FLAG_CAPTURE) |
| 120 | { |
| 121 | m_nFacesWithCaptureSet--; |
| 122 | } |
| 123 | |
| 124 | return m_faces.erase(face); |
| 125 | } |
| 126 | |
| 127 | return m_faces.end(); |
| 128 | } |
| 129 | |
| 130 | void |
| 131 | RibEntry::addInheritedFace(const FaceEntry& face) |
| 132 | { |
| 133 | m_inheritedFaces.push_back(face); |
| 134 | } |
| 135 | |
| 136 | void |
| 137 | RibEntry::removeInheritedFace(const FaceEntry& face) |
| 138 | { |
| 139 | FaceList::iterator it = findInheritedFace(face); |
| 140 | m_inheritedFaces.erase(it); |
| 141 | } |
| 142 | |
| 143 | RibEntry::FaceList::iterator |
| 144 | RibEntry::findInheritedFace(const FaceEntry& face) |
| 145 | { |
| 146 | return std::find_if(m_inheritedFaces.begin(), m_inheritedFaces.end(), |
| 147 | bind(&compareFaceId, _1, face.faceId)); |
| 148 | } |
| 149 | |
| 150 | bool |
| 151 | RibEntry::hasInheritedFace(const FaceEntry& face) |
| 152 | { |
| 153 | FaceList::const_iterator it = findInheritedFace(face); |
| 154 | |
| 155 | return (it != m_inheritedFaces.end()); |
| 156 | } |
| 157 | |
| 158 | bool |
| 159 | RibEntry::hasCapture() const |
| 160 | { |
| 161 | return m_nFacesWithCaptureSet > 0; |
| 162 | } |
| 163 | |
| 164 | bool |
| 165 | RibEntry::hasChildInheritOnFaceId(uint64_t faceId) const |
| 166 | { |
| 167 | for (RibEntry::const_iterator it = m_faces.begin(); it != m_faces.end(); ++it) |
| 168 | { |
| 169 | if (it->faceId == faceId && (it->flags & ndn::nfd::ROUTE_FLAG_CHILD_INHERIT)) |
| 170 | { |
| 171 | return true; |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | return false; |
| 176 | } |
| 177 | |
| 178 | shared_ptr<FaceEntry> |
| 179 | RibEntry::getFaceWithLowestCostByFaceId(uint64_t faceId) |
| 180 | { |
| 181 | shared_ptr<FaceEntry> face; |
| 182 | |
| 183 | for (FaceList::iterator it = begin(); it != end(); ++it) |
| 184 | { |
| 185 | // Correct face ID |
| 186 | if (it->faceId == faceId) |
| 187 | { |
| 188 | // If this is the first face with this ID found |
| 189 | if (!static_cast<bool>(face)) |
| 190 | { |
| 191 | face = make_shared<FaceEntry>(*it); |
| 192 | } |
| 193 | else if (it->cost < face->cost) // Found a face with a lower cost |
| 194 | { |
| 195 | face = make_shared<FaceEntry>(*it); |
| 196 | } |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | return face; |
| 201 | } |
| 202 | |
| 203 | shared_ptr<FaceEntry> |
| 204 | RibEntry::getFaceWithLowestCostAndChildInheritByFaceId(uint64_t faceId) |
| 205 | { |
| 206 | shared_ptr<FaceEntry> face; |
| 207 | |
| 208 | for (FaceList::iterator it = begin(); it != end(); ++it) |
| 209 | { |
| 210 | // Correct face ID and Child Inherit flag set |
| 211 | if (it->faceId == faceId && it->flags & ndn::nfd::ROUTE_FLAG_CHILD_INHERIT) |
| 212 | { |
| 213 | // If this is the first face with this ID found |
| 214 | if (!static_cast<bool>(face)) |
| 215 | { |
| 216 | face = make_shared<FaceEntry>(*it); |
| 217 | } |
| 218 | else if (it->cost < face->cost) // Found a face with a lower cost |
| 219 | { |
| 220 | face = make_shared<FaceEntry>(*it); |
| 221 | } |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | return face; |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | std::ostream& |
| 229 | operator<<(std::ostream& os, const FaceEntry& entry) |
| 230 | { |
| 231 | os << "FaceEntry(" |
| 232 | << " faceid: " << entry.faceId |
| 233 | << " origin: " << entry.origin |
| 234 | << " cost: " << entry.cost |
| 235 | << " flags: " << entry.flags |
| 236 | << " expires in: " << (entry.expires - time::steady_clock::now()) |
| 237 | << ")"; |
| 238 | |
| 239 | return os; |
| 240 | } |
| 241 | |
| 242 | std::ostream& |
| 243 | operator<<(std::ostream& os, const RibEntry& entry) |
| 244 | { |
| 245 | os << "RibEntry{\n"; |
| 246 | os << "\tName: " << entry.getName() << "\n"; |
| 247 | |
| 248 | for (RibEntry::FaceList::const_iterator faceIt = entry.cbegin(); faceIt != entry.cend(); ++faceIt) |
| 249 | { |
| 250 | os << "\t" << (*faceIt) << "\n"; |
| 251 | } |
| 252 | |
| 253 | os << "}"; |
| 254 | |
| 255 | return os; |
| 256 | } |
| 257 | |
| 258 | } // namespace rib |
| 259 | } // namespace nfd |