blob: 78ccdad049a73febd2c14cf47ebaf638db18015d [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/*
Davide Pesaventoe422f9e2022-06-03 01:30:23 -04003 * Copyright (c) 2014-2022, 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"
Junxiao Shi3f21e582017-05-29 15:26:32 +000027#include <ndn-cxx/util/string-helper.hpp>
Vince Lehman76c751c2014-11-18 17:36:38 -060028
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040029namespace nfd::rib {
Vince Lehman76c751c2014-11-18 17:36:38 -060030
Junxiao Shid47cd632018-09-11 03:10:00 +000031const uint64_t PA_ROUTE_COST = 2048; ///< cost of route created by prefix announcement
32
Davide Pesavento412c9822021-07-02 00:21:05 -040033static time::steady_clock::time_point
Junxiao Shid47cd632018-09-11 03:10:00 +000034computeExpiration(const ndn::PrefixAnnouncement& ann)
35{
Davide Pesavento412c9822021-07-02 00:21:05 -040036 auto validityEnd = time::steady_clock::duration::max();
Junxiao Shid47cd632018-09-11 03:10:00 +000037 if (ann.getValidityPeriod()) {
38 auto now = time::system_clock::now();
39 if (!ann.getValidityPeriod()->isValid(now)) {
Davide Pesavento412c9822021-07-02 00:21:05 -040040 validityEnd = time::steady_clock::duration::zero();
Junxiao Shid47cd632018-09-11 03:10:00 +000041 }
42 else {
43 validityEnd = ann.getValidityPeriod()->getPeriod().second - now;
44 }
45 }
46 return time::steady_clock::now() +
Davide Pesavento412c9822021-07-02 00:21:05 -040047 std::min(validityEnd, time::duration_cast<time::steady_clock::duration>(ann.getExpiration()));
Junxiao Shid47cd632018-09-11 03:10:00 +000048}
49
50Route::Route(const ndn::PrefixAnnouncement& ann, uint64_t faceId)
51 : faceId(faceId)
52 , origin(ndn::nfd::ROUTE_ORIGIN_PREFIXANN)
53 , cost(PA_ROUTE_COST)
54 , flags(ndn::nfd::ROUTE_FLAG_CHILD_INHERIT)
55 , expires(computeExpiration(ann))
56 , announcement(ann)
57 , annExpires(*expires)
Vince Lehman76c751c2014-11-18 17:36:38 -060058{
Junxiao Shi3f21e582017-05-29 15:26:32 +000059}
60
61bool
62operator==(const Route& lhs, const Route& rhs)
63{
64 return lhs.faceId == rhs.faceId &&
65 lhs.origin == rhs.origin &&
66 lhs.flags == rhs.flags &&
67 lhs.cost == rhs.cost &&
Junxiao Shid47cd632018-09-11 03:10:00 +000068 lhs.expires == rhs.expires &&
69 lhs.announcement == rhs.announcement;
Vince Lehman76c751c2014-11-18 17:36:38 -060070}
71
72std::ostream&
73operator<<(std::ostream& os, const Route& route)
74{
75 os << "Route("
76 << "faceid: " << route.faceId
77 << ", origin: " << route.origin
78 << ", cost: " << route.cost
Junxiao Shi3f21e582017-05-29 15:26:32 +000079 << ", flags: " << ndn::AsHex{route.flags};
80 if (route.expires) {
81 os << ", expires in: " << time::duration_cast<time::milliseconds>(*route.expires - time::steady_clock::now());
Vince Lehman76c751c2014-11-18 17:36:38 -060082 }
83 else {
84 os << ", never expires";
85 }
Junxiao Shid47cd632018-09-11 03:10:00 +000086 if (route.announcement) {
87 os << ", announcement: (" << *route.announcement << ')';
88 }
89 os << ')';
Vince Lehman76c751c2014-11-18 17:36:38 -060090
91 return os;
92}
93
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040094} // namespace nfd::rib