blob: 504323337ff5f42b5d90e5bfc1dc71932c63d0cc [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
28#include <ndn-cxx/management/nfd-control-command.hpp>
29
30namespace nfd {
31namespace rib {
32
33RibEntry::FaceList::iterator
34RibEntry::findFace(const FaceEntry& face)
35{
36 return std::find_if(begin(), end(), bind(&compareFaceIdAndOrigin, _1, face));
37}
38
39bool
40RibEntry::insertFace(const FaceEntry& entry)
41{
42 iterator it = findFace(entry);
43
44 if (it == end())
45 {
Vince Lehman4387e782014-06-19 16:57:45 -050046 if (entry.flags & ndn::nfd::ROUTE_FLAG_CAPTURE)
47 {
48 m_nFacesWithCaptureSet++;
49 }
50
Vince12e49462014-06-09 13:29:32 -050051 m_faces.push_back(entry);
52 return true;
53 }
54 else
55 {
56 return false;
57 }
58}
59
60bool
61RibEntry::eraseFace(const FaceEntry& face)
62{
63 RibEntry::iterator it = std::find_if(begin(), end(), bind(&compareFaceIdAndOrigin, _1, face));
Vince Lehman4387e782014-06-19 16:57:45 -050064
Vince12e49462014-06-09 13:29:32 -050065 if (it != m_faces.end())
66 {
Vince Lehman4387e782014-06-19 16:57:45 -050067 if (it->flags & ndn::nfd::ROUTE_FLAG_CAPTURE)
68 {
69 m_nFacesWithCaptureSet--;
70 }
71
Syed Obaid3313a372014-07-01 01:31:33 -050072 //cancel any scheduled event
73 scheduler::cancel(it->getExpirationEvent());
74
Vince12e49462014-06-09 13:29:32 -050075 m_faces.erase(it);
76 return true;
77 }
78 else
79 {
80 return false;
81 }
82}
83
84bool
Vince Lehman4387e782014-06-19 16:57:45 -050085RibEntry::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
93bool
Vince12e49462014-06-09 13:29:32 -050094RibEntry::hasFaceId(const uint64_t faceId) const
95{
96 RibEntry::const_iterator it = std::find_if(cbegin(), cend(), bind(&compareFaceId, _1, faceId));
97
Vince Lehman4387e782014-06-19 16:57:45 -050098 return it != cend();
Vince12e49462014-06-09 13:29:32 -050099}
100
101void
102RibEntry::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
109void
110RibEntry::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
117RibEntry::FaceList::iterator
118RibEntry::eraseFace(FaceList::iterator face)
119{
Vince Lehman4387e782014-06-19 16:57:45 -0500120 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
133void
134RibEntry::addInheritedFace(const FaceEntry& face)
135{
136 m_inheritedFaces.push_back(face);
137}
138
139void
140RibEntry::removeInheritedFace(const FaceEntry& face)
141{
142 FaceList::iterator it = findInheritedFace(face);
143 m_inheritedFaces.erase(it);
144}
145
146RibEntry::FaceList::iterator
147RibEntry::findInheritedFace(const FaceEntry& face)
148{
149 return std::find_if(m_inheritedFaces.begin(), m_inheritedFaces.end(),
150 bind(&compareFaceId, _1, face.faceId));
151}
152
153bool
154RibEntry::hasInheritedFace(const FaceEntry& face)
155{
156 FaceList::const_iterator it = findInheritedFace(face);
157
158 return (it != m_inheritedFaces.end());
159}
160
161bool
162RibEntry::hasCapture() const
163{
164 return m_nFacesWithCaptureSet > 0;
165}
166
167bool
168RibEntry::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
181shared_ptr<FaceEntry>
182RibEntry::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
206shared_ptr<FaceEntry>
207RibEntry::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;
Vince12e49462014-06-09 13:29:32 -0500229}
230
231std::ostream&
232operator<<(std::ostream& os, const FaceEntry& entry)
233{
234 os << "FaceEntry("
Syed Obaid3313a372014-07-01 01:31:33 -0500235 << "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 << ")";
Vince12e49462014-06-09 13:29:32 -0500246
247 return os;
248}
249
250std::ostream&
251operator<<(std::ostream& os, const RibEntry& entry)
252{
Syed Obaid3313a372014-07-01 01:31:33 -0500253 os << "RibEntry {\n";
254 os << "\tName: " << entry.getName() << "\n";
Vince12e49462014-06-09 13:29:32 -0500255
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