blob: 3e2427508d92de48292464d56c16c06bf9879a3b [file] [log] [blame]
Vince12e49462014-06-09 13:29:32 -05001/* -*- 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 Lehman281ded72014-08-21 12:17:08 -050028#include "core/logger.hpp"
29
Vince12e49462014-06-09 13:29:32 -050030#include <ndn-cxx/management/nfd-control-command.hpp>
31
Vince Lehman281ded72014-08-21 12:17:08 -050032NFD_LOG_INIT("RibEntry");
33
Vince12e49462014-06-09 13:29:32 -050034namespace nfd {
35namespace rib {
36
37RibEntry::FaceList::iterator
38RibEntry::findFace(const FaceEntry& face)
39{
40 return std::find_if(begin(), end(), bind(&compareFaceIdAndOrigin, _1, face));
41}
42
43bool
44RibEntry::insertFace(const FaceEntry& entry)
45{
46 iterator it = findFace(entry);
47
48 if (it == end())
49 {
Vince Lehman4387e782014-06-19 16:57:45 -050050 if (entry.flags & ndn::nfd::ROUTE_FLAG_CAPTURE)
51 {
52 m_nFacesWithCaptureSet++;
53 }
54
Vince12e49462014-06-09 13:29:32 -050055 m_faces.push_back(entry);
56 return true;
57 }
58 else
59 {
60 return false;
61 }
62}
63
Vince Lehman281ded72014-08-21 12:17:08 -050064void
Vince12e49462014-06-09 13:29:32 -050065RibEntry::eraseFace(const FaceEntry& face)
66{
67 RibEntry::iterator it = std::find_if(begin(), end(), bind(&compareFaceIdAndOrigin, _1, face));
Vince Lehman281ded72014-08-21 12:17:08 -050068 eraseFace(it);
Vince12e49462014-06-09 13:29:32 -050069}
70
71bool
Vince Lehman4387e782014-06-19 16:57:45 -050072RibEntry::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
80bool
Vince12e49462014-06-09 13:29:32 -050081RibEntry::hasFaceId(const uint64_t faceId) const
82{
83 RibEntry::const_iterator it = std::find_if(cbegin(), cend(), bind(&compareFaceId, _1, faceId));
84
Vince Lehman4387e782014-06-19 16:57:45 -050085 return it != cend();
Vince12e49462014-06-09 13:29:32 -050086}
87
88void
89RibEntry::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
96void
97RibEntry::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
104RibEntry::FaceList::iterator
105RibEntry::eraseFace(FaceList::iterator face)
106{
Vince Lehman4387e782014-06-19 16:57:45 -0500107 if (face != m_faces.end())
108 {
109 if (face->flags & ndn::nfd::ROUTE_FLAG_CAPTURE)
110 {
111 m_nFacesWithCaptureSet--;
112 }
113
Vince Lehman281ded72014-08-21 12:17:08 -0500114 //cancel any scheduled event
115 NFD_LOG_TRACE("Cancelling expiration eventId: " << face->getExpirationEvent());
116 scheduler::cancel(face->getExpirationEvent());
117
Vince Lehman4387e782014-06-19 16:57:45 -0500118 return m_faces.erase(face);
119 }
120
121 return m_faces.end();
122}
123
124void
125RibEntry::addInheritedFace(const FaceEntry& face)
126{
127 m_inheritedFaces.push_back(face);
128}
129
130void
131RibEntry::removeInheritedFace(const FaceEntry& face)
132{
133 FaceList::iterator it = findInheritedFace(face);
134 m_inheritedFaces.erase(it);
135}
136
137RibEntry::FaceList::iterator
138RibEntry::findInheritedFace(const FaceEntry& face)
139{
140 return std::find_if(m_inheritedFaces.begin(), m_inheritedFaces.end(),
141 bind(&compareFaceId, _1, face.faceId));
142}
143
144bool
145RibEntry::hasInheritedFace(const FaceEntry& face)
146{
147 FaceList::const_iterator it = findInheritedFace(face);
148
149 return (it != m_inheritedFaces.end());
150}
151
152bool
153RibEntry::hasCapture() const
154{
155 return m_nFacesWithCaptureSet > 0;
156}
157
158bool
159RibEntry::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
172shared_ptr<FaceEntry>
173RibEntry::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
197shared_ptr<FaceEntry>
198RibEntry::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;
Vince12e49462014-06-09 13:29:32 -0500220}
221
222std::ostream&
223operator<<(std::ostream& os, const FaceEntry& entry)
224{
225 os << "FaceEntry("
Syed Obaid3313a372014-07-01 01:31:33 -0500226 << "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 << ")";
Vince12e49462014-06-09 13:29:32 -0500237
238 return os;
239}
240
241std::ostream&
242operator<<(std::ostream& os, const RibEntry& entry)
243{
Syed Obaid3313a372014-07-01 01:31:33 -0500244 os << "RibEntry {\n";
245 os << "\tName: " << entry.getName() << "\n";
Vince12e49462014-06-09 13:29:32 -0500246
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