Zhuo Li | b355889 | 2016-08-12 15:51:12 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Junxiao Shi | b54aabd | 2018-04-16 19:36:24 +0000 | [diff] [blame] | 2 | /* |
Davide Pesavento | 5748e82 | 2024-01-26 18:40:22 -0500 | [diff] [blame] | 3 | * Copyright (c) 2014-2024, Regents of the University of California, |
Zhuo Li | b355889 | 2016-08-12 15:51:12 -0700 | [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 ndn-tools (Named Data Networking Essential Tools). |
| 12 | * See AUTHORS.md for complete list of ndn-tools authors and contributors. |
| 13 | * |
| 14 | * ndn-tools 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 | * ndn-tools 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 | * ndn-tools, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 24 | * |
| 25 | * @author Jerald Paul Abraham <jeraldabraham@email.arizona.edu> |
| 26 | * @author Zhuo Li <zhuoli@email.arizona.edu> |
Davide Pesavento | 9602823 | 2019-08-17 01:26:13 -0400 | [diff] [blame] | 27 | * @author Davide Pesavento <davidepesa@gmail.com> |
Zhuo Li | b355889 | 2016-08-12 15:51:12 -0700 | [diff] [blame] | 28 | */ |
| 29 | |
| 30 | #ifndef NDN_TOOLS_NDNPEEK_NDNPEEK_HPP |
| 31 | #define NDN_TOOLS_NDNPEEK_NDNPEEK_HPP |
| 32 | |
| 33 | #include "core/common.hpp" |
Davide Pesavento | 65d1155 | 2019-06-09 19:15:50 -0400 | [diff] [blame] | 34 | |
Davide Pesavento | 5748e82 | 2024-01-26 18:40:22 -0500 | [diff] [blame] | 35 | #include <ndn-cxx/face.hpp> |
Junxiao Shi | a5b60cc | 2017-07-26 01:35:48 +0000 | [diff] [blame] | 36 | #include <ndn-cxx/link.hpp> |
Davide Pesavento | 65d1155 | 2019-06-09 19:15:50 -0400 | [diff] [blame] | 37 | #include <ndn-cxx/util/scheduler.hpp> |
Zhuo Li | b355889 | 2016-08-12 15:51:12 -0700 | [diff] [blame] | 38 | |
Davide Pesavento | 5748e82 | 2024-01-26 18:40:22 -0500 | [diff] [blame] | 39 | #include <optional> |
| 40 | #include <vector> |
| 41 | |
Davide Pesavento | b3570c6 | 2022-02-19 19:19:00 -0500 | [diff] [blame] | 42 | namespace ndn::peek { |
Zhuo Li | b355889 | 2016-08-12 15:51:12 -0700 | [diff] [blame] | 43 | |
| 44 | /** |
| 45 | * @brief options for NdnPeek |
| 46 | */ |
| 47 | struct PeekOptions |
| 48 | { |
Junxiao Shi | b54aabd | 2018-04-16 19:36:24 +0000 | [diff] [blame] | 49 | // Interest construction options |
Davide Pesavento | 65d1155 | 2019-06-09 19:15:50 -0400 | [diff] [blame] | 50 | Name name; |
Junxiao Shi | b54aabd | 2018-04-16 19:36:24 +0000 | [diff] [blame] | 51 | bool canBePrefix = false; |
| 52 | bool mustBeFresh = false; |
Junxiao Shi | fd2d101 | 2022-01-11 18:20:38 +0000 | [diff] [blame] | 53 | std::vector<Name> forwardingHint; |
Davide Pesavento | 0da1f44 | 2019-07-26 17:38:13 -0400 | [diff] [blame] | 54 | time::milliseconds interestLifetime = DEFAULT_INTEREST_LIFETIME; |
Davide Pesavento | 242d506 | 2022-03-11 16:34:23 -0500 | [diff] [blame] | 55 | std::optional<uint8_t> hopLimit; |
Davide Pesavento | 5748e82 | 2024-01-26 18:40:22 -0500 | [diff] [blame] | 56 | std::shared_ptr<Buffer> applicationParameters; |
Junxiao Shi | b54aabd | 2018-04-16 19:36:24 +0000 | [diff] [blame] | 57 | |
Davide Pesavento | 0da1f44 | 2019-07-26 17:38:13 -0400 | [diff] [blame] | 58 | // program behavior options |
Junxiao Shi | b54aabd | 2018-04-16 19:36:24 +0000 | [diff] [blame] | 59 | bool isVerbose = false; |
Junxiao Shi | b54aabd | 2018-04-16 19:36:24 +0000 | [diff] [blame] | 60 | bool wantPayloadOnly = false; |
Davide Pesavento | 242d506 | 2022-03-11 16:34:23 -0500 | [diff] [blame] | 61 | std::optional<time::milliseconds> timeout; |
Zhuo Li | b355889 | 2016-08-12 15:51:12 -0700 | [diff] [blame] | 62 | }; |
| 63 | |
Davide Pesavento | b3570c6 | 2022-02-19 19:19:00 -0500 | [diff] [blame] | 64 | class NdnPeek : noncopyable |
Zhuo Li | b355889 | 2016-08-12 15:51:12 -0700 | [diff] [blame] | 65 | { |
| 66 | public: |
| 67 | NdnPeek(Face& face, const PeekOptions& options); |
| 68 | |
Davide Pesavento | 87434be | 2019-07-25 19:04:23 -0400 | [diff] [blame] | 69 | enum class Result { |
| 70 | DATA = 0, |
| 71 | UNKNOWN = 1, |
| 72 | // 2 is reserved for "malformed command line" |
| 73 | TIMEOUT = 3, |
| 74 | NACK = 4, |
| 75 | }; |
| 76 | |
Zhuo Li | b355889 | 2016-08-12 15:51:12 -0700 | [diff] [blame] | 77 | /** |
Davide Pesavento | 65d1155 | 2019-06-09 19:15:50 -0400 | [diff] [blame] | 78 | * @return the result of NdnPeek execution |
Zhuo Li | b355889 | 2016-08-12 15:51:12 -0700 | [diff] [blame] | 79 | */ |
Davide Pesavento | 87434be | 2019-07-25 19:04:23 -0400 | [diff] [blame] | 80 | Result |
| 81 | getResult() const |
Davide Pesavento | 65d1155 | 2019-06-09 19:15:50 -0400 | [diff] [blame] | 82 | { |
Davide Pesavento | 87434be | 2019-07-25 19:04:23 -0400 | [diff] [blame] | 83 | return m_result; |
Davide Pesavento | 65d1155 | 2019-06-09 19:15:50 -0400 | [diff] [blame] | 84 | } |
Zhuo Li | b355889 | 2016-08-12 15:51:12 -0700 | [diff] [blame] | 85 | |
| 86 | /** |
| 87 | * @brief express the Interest |
| 88 | * @note The caller must invoke face.processEvents() afterwards |
| 89 | */ |
| 90 | void |
| 91 | start(); |
| 92 | |
| 93 | private: |
| 94 | Interest |
| 95 | createInterest() const; |
| 96 | |
| 97 | /** |
| 98 | * @brief called when a Data packet is received |
| 99 | */ |
| 100 | void |
| 101 | onData(const Data& data); |
| 102 | |
| 103 | /** |
| 104 | * @brief called when a Nack packet is received |
| 105 | */ |
| 106 | void |
| 107 | onNack(const lp::Nack& nack); |
| 108 | |
Davide Pesavento | 65d1155 | 2019-06-09 19:15:50 -0400 | [diff] [blame] | 109 | /** |
| 110 | * @brief called when the Interest times out |
| 111 | */ |
| 112 | void |
| 113 | onTimeout(); |
| 114 | |
Zhuo Li | b355889 | 2016-08-12 15:51:12 -0700 | [diff] [blame] | 115 | private: |
Davide Pesavento | 9602823 | 2019-08-17 01:26:13 -0400 | [diff] [blame] | 116 | const PeekOptions m_options; |
Davide Pesavento | 65d1155 | 2019-06-09 19:15:50 -0400 | [diff] [blame] | 117 | Face& m_face; |
| 118 | Scheduler m_scheduler; |
Davide Pesavento | f8d9a53 | 2021-07-03 16:04:12 -0400 | [diff] [blame] | 119 | time::steady_clock::time_point m_sendTime; |
Davide Pesavento | 65d1155 | 2019-06-09 19:15:50 -0400 | [diff] [blame] | 120 | ScopedPendingInterestHandle m_pendingInterest; |
| 121 | scheduler::ScopedEventId m_timeoutEvent; |
Davide Pesavento | 87434be | 2019-07-25 19:04:23 -0400 | [diff] [blame] | 122 | Result m_result = Result::UNKNOWN; |
Zhuo Li | b355889 | 2016-08-12 15:51:12 -0700 | [diff] [blame] | 123 | }; |
| 124 | |
Davide Pesavento | b3570c6 | 2022-02-19 19:19:00 -0500 | [diff] [blame] | 125 | } // namespace ndn::peek |
Zhuo Li | b355889 | 2016-08-12 15:51:12 -0700 | [diff] [blame] | 126 | |
| 127 | #endif // NDN_TOOLS_NDNPEEK_NDNPEEK_HPP |