blob: 642aa1efe46649dfcb1adac00ff9cbf47a30dc06 [file] [log] [blame]
Vince Lehman76c751c2014-11-18 17:36:38 -06001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shid47cd632018-09-11 03:10:00 +00002/*
jaczhib0657682025-01-08 23:01:45 -08003 * Copyright (c) 2014-2025, Regents of the University of California,
Vince Lehman76c751c2014-11-18 17:36:38 -06004 * 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 "route.hpp"
Davide Pesavento20d94f62022-09-26 03:30:53 -040027
Junxiao Shi3f21e582017-05-29 15:26:32 +000028#include <ndn-cxx/util/string-helper.hpp>
Vince Lehman76c751c2014-11-18 17:36:38 -060029
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040030namespace nfd::rib {
Vince Lehman76c751c2014-11-18 17:36:38 -060031
Davide Pesavento191a7a22023-05-17 22:40:43 -040032Route::Route() = default;
33
Davide Pesavento412c9822021-07-02 00:21:05 -040034static time::steady_clock::time_point
Junxiao Shid47cd632018-09-11 03:10:00 +000035computeExpiration(const ndn::PrefixAnnouncement& ann)
36{
Davide Pesavento412c9822021-07-02 00:21:05 -040037 auto validityEnd = time::steady_clock::duration::max();
Davide Pesavento21e24f92025-01-10 22:22:43 -050038 const auto& validityPeriod = ann.getValidityPeriod();
39 if (validityPeriod) {
Junxiao Shid47cd632018-09-11 03:10:00 +000040 auto now = time::system_clock::now();
Davide Pesavento21e24f92025-01-10 22:22:43 -050041 if (!validityPeriod->isValid(now)) {
Davide Pesavento412c9822021-07-02 00:21:05 -040042 validityEnd = time::steady_clock::duration::zero();
Junxiao Shid47cd632018-09-11 03:10:00 +000043 }
44 else {
Davide Pesavento21e24f92025-01-10 22:22:43 -050045 validityEnd = validityPeriod->getPeriod().second - now;
Junxiao Shid47cd632018-09-11 03:10:00 +000046 }
47 }
48 return time::steady_clock::now() +
Davide Pesavento412c9822021-07-02 00:21:05 -040049 std::min(validityEnd, time::duration_cast<time::steady_clock::duration>(ann.getExpiration()));
Junxiao Shid47cd632018-09-11 03:10:00 +000050}
51
52Route::Route(const ndn::PrefixAnnouncement& ann, uint64_t faceId)
53 : faceId(faceId)
54 , origin(ndn::nfd::ROUTE_ORIGIN_PREFIXANN)
55 , cost(PA_ROUTE_COST)
56 , flags(ndn::nfd::ROUTE_FLAG_CHILD_INHERIT)
57 , expires(computeExpiration(ann))
58 , announcement(ann)
59 , annExpires(*expires)
Vince Lehman76c751c2014-11-18 17:36:38 -060060{
Junxiao Shi3f21e582017-05-29 15:26:32 +000061}
62
Vince Lehman76c751c2014-11-18 17:36:38 -060063std::ostream&
64operator<<(std::ostream& os, const Route& route)
65{
Davide Pesavento21e24f92025-01-10 22:22:43 -050066 os << "Route{"
67 << "face: " << route.faceId
Vince Lehman76c751c2014-11-18 17:36:38 -060068 << ", origin: " << route.origin
69 << ", cost: " << route.cost
Junxiao Shi3f21e582017-05-29 15:26:32 +000070 << ", flags: " << ndn::AsHex{route.flags};
Davide Pesavento191a7a22023-05-17 22:40:43 -040071
Junxiao Shi3f21e582017-05-29 15:26:32 +000072 if (route.expires) {
73 os << ", expires in: " << time::duration_cast<time::milliseconds>(*route.expires - time::steady_clock::now());
Vince Lehman76c751c2014-11-18 17:36:38 -060074 }
75 else {
Davide Pesavento21e24f92025-01-10 22:22:43 -050076 os << ", expires: never";
Vince Lehman76c751c2014-11-18 17:36:38 -060077 }
Davide Pesavento191a7a22023-05-17 22:40:43 -040078
Junxiao Shid47cd632018-09-11 03:10:00 +000079 if (route.announcement) {
80 os << ", announcement: (" << *route.announcement << ')';
81 }
Vince Lehman76c751c2014-11-18 17:36:38 -060082
Davide Pesavento21e24f92025-01-10 22:22:43 -050083 return os << '}';
Vince Lehman76c751c2014-11-18 17:36:38 -060084}
85
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040086} // namespace nfd::rib