blob: 4ded68906eb9a895d719015c8473453e2adde542 [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
Vince Lehman218be0a2015-01-15 17:25:20 -060037RibEntry::RouteList::iterator
38RibEntry::findRoute(const Route& route)
Vince12e49462014-06-09 13:29:32 -050039{
Vince Lehman218be0a2015-01-15 17:25:20 -060040 return std::find_if(begin(), end(), bind(&compareFaceIdAndOrigin, _1, route));
Vince12e49462014-06-09 13:29:32 -050041}
42
43bool
Vince Lehman218be0a2015-01-15 17:25:20 -060044RibEntry::insertRoute(const Route& route)
Vince12e49462014-06-09 13:29:32 -050045{
Vince Lehman218be0a2015-01-15 17:25:20 -060046 iterator it = findRoute(route);
Vince12e49462014-06-09 13:29:32 -050047
48 if (it == end())
49 {
Vince Lehman218be0a2015-01-15 17:25:20 -060050 if (route.flags & ndn::nfd::ROUTE_FLAG_CAPTURE)
Vince Lehman4387e782014-06-19 16:57:45 -050051 {
Vince Lehman218be0a2015-01-15 17:25:20 -060052 m_nRoutesWithCaptureSet++;
Vince Lehman4387e782014-06-19 16:57:45 -050053 }
54
Vince Lehman218be0a2015-01-15 17:25:20 -060055 m_routes.push_back(route);
Vince12e49462014-06-09 13:29:32 -050056 return true;
57 }
58 else
59 {
60 return false;
61 }
62}
63
Vince Lehman281ded72014-08-21 12:17:08 -050064void
Vince Lehman218be0a2015-01-15 17:25:20 -060065RibEntry::eraseRoute(const Route& route)
Vince12e49462014-06-09 13:29:32 -050066{
Vince Lehman218be0a2015-01-15 17:25:20 -060067 RibEntry::iterator it = findRoute(route);
68 eraseRoute(it);
Vince12e49462014-06-09 13:29:32 -050069}
70
71bool
Vince Lehman218be0a2015-01-15 17:25:20 -060072RibEntry::hasRoute(const Route& route)
Vince Lehman4387e782014-06-19 16:57:45 -050073{
Vince Lehman218be0a2015-01-15 17:25:20 -060074 RibEntry::const_iterator it = findRoute(route);
Vince Lehman4387e782014-06-19 16:57:45 -050075
Vince Lehman218be0a2015-01-15 17:25:20 -060076 return it != end();
Vince Lehman4387e782014-06-19 16:57:45 -050077}
78
79bool
Vince12e49462014-06-09 13:29:32 -050080RibEntry::hasFaceId(const uint64_t faceId) const
81{
Vince Lehman218be0a2015-01-15 17:25:20 -060082 RibEntry::const_iterator it = std::find_if(begin(), end(), bind(&compareFaceId, _1, faceId));
Vince12e49462014-06-09 13:29:32 -050083
Vince Lehman218be0a2015-01-15 17:25:20 -060084 return it != end();
Vince12e49462014-06-09 13:29:32 -050085}
86
87void
88RibEntry::addChild(shared_ptr<RibEntry> child)
89{
90 BOOST_ASSERT(!static_cast<bool>(child->getParent()));
91 child->setParent(this->shared_from_this());
92 m_children.push_back(child);
93}
94
95void
96RibEntry::removeChild(shared_ptr<RibEntry> child)
97{
98 BOOST_ASSERT(child->getParent().get() == this);
99 child->setParent(shared_ptr<RibEntry>());
100 m_children.remove(child);
101}
102
Vince Lehman218be0a2015-01-15 17:25:20 -0600103RibEntry::RouteList::iterator
104RibEntry::eraseRoute(RouteList::iterator route)
Vince12e49462014-06-09 13:29:32 -0500105{
Vince Lehman218be0a2015-01-15 17:25:20 -0600106 if (route != m_routes.end())
Vince Lehman4387e782014-06-19 16:57:45 -0500107 {
Vince Lehman218be0a2015-01-15 17:25:20 -0600108 if (route->flags & ndn::nfd::ROUTE_FLAG_CAPTURE)
Vince Lehman4387e782014-06-19 16:57:45 -0500109 {
Vince Lehman218be0a2015-01-15 17:25:20 -0600110 m_nRoutesWithCaptureSet--;
Vince Lehman4387e782014-06-19 16:57:45 -0500111 }
112
Vince Lehman281ded72014-08-21 12:17:08 -0500113 //cancel any scheduled event
Vince Lehman218be0a2015-01-15 17:25:20 -0600114 NFD_LOG_TRACE("Cancelling expiration eventId: " << route->getExpirationEvent());
115 scheduler::cancel(route->getExpirationEvent());
Vince Lehman281ded72014-08-21 12:17:08 -0500116
Vince Lehman218be0a2015-01-15 17:25:20 -0600117 return m_routes.erase(route);
Vince Lehman4387e782014-06-19 16:57:45 -0500118 }
119
Vince Lehman218be0a2015-01-15 17:25:20 -0600120 return m_routes.end();
Vince Lehman4387e782014-06-19 16:57:45 -0500121}
122
123void
Vince Lehman218be0a2015-01-15 17:25:20 -0600124RibEntry::addInheritedRoute(const Route& route)
Vince Lehman4387e782014-06-19 16:57:45 -0500125{
Vince Lehman218be0a2015-01-15 17:25:20 -0600126 m_inheritedRoutes.push_back(route);
Vince Lehman4387e782014-06-19 16:57:45 -0500127}
128
129void
Vince Lehman218be0a2015-01-15 17:25:20 -0600130RibEntry::removeInheritedRoute(const Route& route)
Vince Lehman4387e782014-06-19 16:57:45 -0500131{
Vince Lehman218be0a2015-01-15 17:25:20 -0600132 RouteList::iterator it = findInheritedRoute(route);
133 m_inheritedRoutes.erase(it);
Vince Lehman4387e782014-06-19 16:57:45 -0500134}
135
Vince Lehman218be0a2015-01-15 17:25:20 -0600136RibEntry::RouteList::iterator
137RibEntry::findInheritedRoute(const Route& route)
Vince Lehman4387e782014-06-19 16:57:45 -0500138{
Vince Lehman218be0a2015-01-15 17:25:20 -0600139 return std::find_if(m_inheritedRoutes.begin(), m_inheritedRoutes.end(),
140 bind(&compareFaceId, _1, route.faceId));
Vince Lehman4387e782014-06-19 16:57:45 -0500141}
142
143bool
Vince Lehman218be0a2015-01-15 17:25:20 -0600144RibEntry::hasInheritedRoute(const Route& route)
Vince Lehman4387e782014-06-19 16:57:45 -0500145{
Vince Lehman218be0a2015-01-15 17:25:20 -0600146 RouteList::const_iterator it = findInheritedRoute(route);
Vince Lehman4387e782014-06-19 16:57:45 -0500147
Vince Lehman218be0a2015-01-15 17:25:20 -0600148 return (it != m_inheritedRoutes.end());
Vince Lehman4387e782014-06-19 16:57:45 -0500149}
150
151bool
152RibEntry::hasCapture() const
153{
Vince Lehman218be0a2015-01-15 17:25:20 -0600154 return m_nRoutesWithCaptureSet > 0;
Vince Lehman4387e782014-06-19 16:57:45 -0500155}
156
157bool
158RibEntry::hasChildInheritOnFaceId(uint64_t faceId) const
159{
Vince Lehman218be0a2015-01-15 17:25:20 -0600160 for (RibEntry::const_iterator it = m_routes.begin(); it != m_routes.end(); ++it)
Vince Lehman4387e782014-06-19 16:57:45 -0500161 {
162 if (it->faceId == faceId && (it->flags & ndn::nfd::ROUTE_FLAG_CHILD_INHERIT))
163 {
164 return true;
165 }
166 }
167
168 return false;
169}
170
Vince Lehman218be0a2015-01-15 17:25:20 -0600171shared_ptr<Route>
172RibEntry::getRouteWithLowestCostByFaceId(uint64_t faceId)
Vince Lehman4387e782014-06-19 16:57:45 -0500173{
Vince Lehman218be0a2015-01-15 17:25:20 -0600174 shared_ptr<Route> candidate;
Vince Lehman4387e782014-06-19 16:57:45 -0500175
Vince Lehman218be0a2015-01-15 17:25:20 -0600176 for (const Route& route : m_routes)
Vince Lehman4387e782014-06-19 16:57:45 -0500177 {
178 // Correct face ID
Vince Lehman218be0a2015-01-15 17:25:20 -0600179 if (route.faceId == faceId)
Vince Lehman4387e782014-06-19 16:57:45 -0500180 {
Vince Lehman218be0a2015-01-15 17:25:20 -0600181 // If this is the first route with this Face ID found
182 if (candidate == nullptr)
Vince Lehman4387e782014-06-19 16:57:45 -0500183 {
Vince Lehman218be0a2015-01-15 17:25:20 -0600184 candidate = make_shared<Route>(route);
Vince Lehman4387e782014-06-19 16:57:45 -0500185 }
Vince Lehman218be0a2015-01-15 17:25:20 -0600186 else if (route.cost < candidate->cost) // Found a route with a lower cost
Vince Lehman4387e782014-06-19 16:57:45 -0500187 {
Vince Lehman218be0a2015-01-15 17:25:20 -0600188 candidate = make_shared<Route>(route);
Vince Lehman4387e782014-06-19 16:57:45 -0500189 }
190 }
191 }
192
Vince Lehman218be0a2015-01-15 17:25:20 -0600193 return candidate;
Vince Lehman4387e782014-06-19 16:57:45 -0500194}
195
Vince Lehman218be0a2015-01-15 17:25:20 -0600196shared_ptr<Route>
197RibEntry::getRouteWithLowestCostAndChildInheritByFaceId(uint64_t faceId)
Vince Lehman4387e782014-06-19 16:57:45 -0500198{
Vince Lehman218be0a2015-01-15 17:25:20 -0600199 shared_ptr<Route> candidate;
Vince Lehman4387e782014-06-19 16:57:45 -0500200
Vince Lehman218be0a2015-01-15 17:25:20 -0600201 for (const Route& route : m_routes)
Vince Lehman4387e782014-06-19 16:57:45 -0500202 {
203 // Correct face ID and Child Inherit flag set
Vince Lehman218be0a2015-01-15 17:25:20 -0600204 if (route.faceId == faceId &&
205 (route.flags & ndn::nfd::ROUTE_FLAG_CHILD_INHERIT) == ndn::nfd::ROUTE_FLAG_CHILD_INHERIT)
Vince Lehman4387e782014-06-19 16:57:45 -0500206 {
Vince Lehman218be0a2015-01-15 17:25:20 -0600207 // If this is the first route with this Face ID found
208 if (candidate == nullptr)
Vince Lehman4387e782014-06-19 16:57:45 -0500209 {
Vince Lehman218be0a2015-01-15 17:25:20 -0600210 candidate = make_shared<Route>(route);
Vince Lehman4387e782014-06-19 16:57:45 -0500211 }
Vince Lehman218be0a2015-01-15 17:25:20 -0600212 else if (route.cost < candidate->cost) // Found a route with a lower cost
Vince Lehman4387e782014-06-19 16:57:45 -0500213 {
Vince Lehman218be0a2015-01-15 17:25:20 -0600214 candidate = make_shared<Route>(route);
Vince Lehman4387e782014-06-19 16:57:45 -0500215 }
216 }
217 }
218
Vince Lehman218be0a2015-01-15 17:25:20 -0600219 return candidate;
Vince12e49462014-06-09 13:29:32 -0500220}
221
222std::ostream&
Vince Lehman218be0a2015-01-15 17:25:20 -0600223operator<<(std::ostream& os, const Route& route)
Vince12e49462014-06-09 13:29:32 -0500224{
Vince Lehman218be0a2015-01-15 17:25:20 -0600225 os << "Route("
226 << "faceid: " << route.faceId
227 << ", origin: " << route.origin
228 << ", cost: " << route.cost
229 << ", flags: " << route.flags;
230 if (route.expires != time::steady_clock::TimePoint::max()) {
231 os << ", expires in: " << (route.expires - time::steady_clock::now());
Syed Obaid3313a372014-07-01 01:31:33 -0500232 }
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
Vince Lehman218be0a2015-01-15 17:25:20 -0600247 for (const Route& route : entry)
Vince12e49462014-06-09 13:29:32 -0500248 {
Vince Lehman218be0a2015-01-15 17:25:20 -0600249 os << "\t" << route << "\n";
Vince12e49462014-06-09 13:29:32 -0500250 }
251
252 os << "}";
253
254 return os;
255}
256
257} // namespace rib
258} // namespace nfd