Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Junxiao Shi | d47cd63 | 2018-09-11 03:10:00 +0000 | [diff] [blame] | 2 | /* |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 3 | * Copyright (c) 2014-2022, Regents of the University of California, |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 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 "route.hpp" |
Davide Pesavento | 20d94f6 | 2022-09-26 03:30:53 -0400 | [diff] [blame] | 27 | |
Junxiao Shi | 3f21e58 | 2017-05-29 15:26:32 +0000 | [diff] [blame] | 28 | #include <ndn-cxx/util/string-helper.hpp> |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 29 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 30 | namespace nfd::rib { |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 31 | |
Davide Pesavento | 20d94f6 | 2022-09-26 03:30:53 -0400 | [diff] [blame] | 32 | constexpr uint64_t PA_ROUTE_COST = 2048; // cost of route created by prefix announcement |
Junxiao Shi | d47cd63 | 2018-09-11 03:10:00 +0000 | [diff] [blame] | 33 | |
Davide Pesavento | 412c982 | 2021-07-02 00:21:05 -0400 | [diff] [blame] | 34 | static time::steady_clock::time_point |
Junxiao Shi | d47cd63 | 2018-09-11 03:10:00 +0000 | [diff] [blame] | 35 | computeExpiration(const ndn::PrefixAnnouncement& ann) |
| 36 | { |
Davide Pesavento | 412c982 | 2021-07-02 00:21:05 -0400 | [diff] [blame] | 37 | auto validityEnd = time::steady_clock::duration::max(); |
Junxiao Shi | d47cd63 | 2018-09-11 03:10:00 +0000 | [diff] [blame] | 38 | if (ann.getValidityPeriod()) { |
| 39 | auto now = time::system_clock::now(); |
| 40 | if (!ann.getValidityPeriod()->isValid(now)) { |
Davide Pesavento | 412c982 | 2021-07-02 00:21:05 -0400 | [diff] [blame] | 41 | validityEnd = time::steady_clock::duration::zero(); |
Junxiao Shi | d47cd63 | 2018-09-11 03:10:00 +0000 | [diff] [blame] | 42 | } |
| 43 | else { |
| 44 | validityEnd = ann.getValidityPeriod()->getPeriod().second - now; |
| 45 | } |
| 46 | } |
| 47 | return time::steady_clock::now() + |
Davide Pesavento | 412c982 | 2021-07-02 00:21:05 -0400 | [diff] [blame] | 48 | std::min(validityEnd, time::duration_cast<time::steady_clock::duration>(ann.getExpiration())); |
Junxiao Shi | d47cd63 | 2018-09-11 03:10:00 +0000 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | Route::Route(const ndn::PrefixAnnouncement& ann, uint64_t faceId) |
| 52 | : faceId(faceId) |
| 53 | , origin(ndn::nfd::ROUTE_ORIGIN_PREFIXANN) |
| 54 | , cost(PA_ROUTE_COST) |
| 55 | , flags(ndn::nfd::ROUTE_FLAG_CHILD_INHERIT) |
| 56 | , expires(computeExpiration(ann)) |
| 57 | , announcement(ann) |
| 58 | , annExpires(*expires) |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 59 | { |
Junxiao Shi | 3f21e58 | 2017-05-29 15:26:32 +0000 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | bool |
| 63 | operator==(const Route& lhs, const Route& rhs) |
| 64 | { |
| 65 | return lhs.faceId == rhs.faceId && |
| 66 | lhs.origin == rhs.origin && |
| 67 | lhs.flags == rhs.flags && |
| 68 | lhs.cost == rhs.cost && |
Junxiao Shi | d47cd63 | 2018-09-11 03:10:00 +0000 | [diff] [blame] | 69 | lhs.expires == rhs.expires && |
| 70 | lhs.announcement == rhs.announcement; |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | std::ostream& |
| 74 | operator<<(std::ostream& os, const Route& route) |
| 75 | { |
| 76 | os << "Route(" |
| 77 | << "faceid: " << route.faceId |
| 78 | << ", origin: " << route.origin |
| 79 | << ", cost: " << route.cost |
Junxiao Shi | 3f21e58 | 2017-05-29 15:26:32 +0000 | [diff] [blame] | 80 | << ", flags: " << ndn::AsHex{route.flags}; |
| 81 | if (route.expires) { |
| 82 | os << ", expires in: " << time::duration_cast<time::milliseconds>(*route.expires - time::steady_clock::now()); |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 83 | } |
| 84 | else { |
| 85 | os << ", never expires"; |
| 86 | } |
Junxiao Shi | d47cd63 | 2018-09-11 03:10:00 +0000 | [diff] [blame] | 87 | if (route.announcement) { |
| 88 | os << ", announcement: (" << *route.announcement << ')'; |
| 89 | } |
| 90 | os << ')'; |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 91 | |
| 92 | return os; |
| 93 | } |
| 94 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 95 | } // namespace nfd::rib |