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