blob: 98c3b9089e25e1a598c7fbe4383e40ae8b70ae4a [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
Vince12e49462014-06-09 13:29:32 -050072 m_faces.erase(it);
73 return true;
74 }
75 else
76 {
77 return false;
78 }
79}
80
81bool
Vince Lehman4387e782014-06-19 16:57:45 -050082RibEntry::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
90bool
Vince12e49462014-06-09 13:29:32 -050091RibEntry::hasFaceId(const uint64_t faceId) const
92{
93 RibEntry::const_iterator it = std::find_if(cbegin(), cend(), bind(&compareFaceId, _1, faceId));
94
Vince Lehman4387e782014-06-19 16:57:45 -050095 return it != cend();
Vince12e49462014-06-09 13:29:32 -050096}
97
98void
99RibEntry::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
106void
107RibEntry::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
114RibEntry::FaceList::iterator
115RibEntry::eraseFace(FaceList::iterator face)
116{
Vince Lehman4387e782014-06-19 16:57:45 -0500117 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
130void
131RibEntry::addInheritedFace(const FaceEntry& face)
132{
133 m_inheritedFaces.push_back(face);
134}
135
136void
137RibEntry::removeInheritedFace(const FaceEntry& face)
138{
139 FaceList::iterator it = findInheritedFace(face);
140 m_inheritedFaces.erase(it);
141}
142
143RibEntry::FaceList::iterator
144RibEntry::findInheritedFace(const FaceEntry& face)
145{
146 return std::find_if(m_inheritedFaces.begin(), m_inheritedFaces.end(),
147 bind(&compareFaceId, _1, face.faceId));
148}
149
150bool
151RibEntry::hasInheritedFace(const FaceEntry& face)
152{
153 FaceList::const_iterator it = findInheritedFace(face);
154
155 return (it != m_inheritedFaces.end());
156}
157
158bool
159RibEntry::hasCapture() const
160{
161 return m_nFacesWithCaptureSet > 0;
162}
163
164bool
165RibEntry::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
178shared_ptr<FaceEntry>
179RibEntry::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
203shared_ptr<FaceEntry>
204RibEntry::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;
Vince12e49462014-06-09 13:29:32 -0500226}
227
228std::ostream&
229operator<<(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
242std::ostream&
243operator<<(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