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 | 65d1155 | 2019-06-09 19:15:50 -0400 | [diff] [blame] | 3 | * Copyright (c) 2014-2019, 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> |
| 27 | */ |
| 28 | |
| 29 | #ifndef NDN_TOOLS_NDNPEEK_NDNPEEK_HPP |
| 30 | #define NDN_TOOLS_NDNPEEK_NDNPEEK_HPP |
| 31 | |
| 32 | #include "core/common.hpp" |
Davide Pesavento | 65d1155 | 2019-06-09 19:15:50 -0400 | [diff] [blame] | 33 | |
Junxiao Shi | a5b60cc | 2017-07-26 01:35:48 +0000 | [diff] [blame] | 34 | #include <ndn-cxx/link.hpp> |
Davide Pesavento | 65d1155 | 2019-06-09 19:15:50 -0400 | [diff] [blame] | 35 | #include <ndn-cxx/util/scheduler.hpp> |
Zhuo Li | b355889 | 2016-08-12 15:51:12 -0700 | [diff] [blame] | 36 | |
| 37 | namespace ndn { |
| 38 | namespace peek { |
| 39 | |
| 40 | /** |
| 41 | * @brief options for NdnPeek |
| 42 | */ |
| 43 | struct PeekOptions |
| 44 | { |
Junxiao Shi | b54aabd | 2018-04-16 19:36:24 +0000 | [diff] [blame] | 45 | // Interest construction options |
Davide Pesavento | 65d1155 | 2019-06-09 19:15:50 -0400 | [diff] [blame] | 46 | Name name; |
Junxiao Shi | b54aabd | 2018-04-16 19:36:24 +0000 | [diff] [blame] | 47 | bool canBePrefix = false; |
| 48 | bool mustBeFresh = false; |
Zhuo Li | b355889 | 2016-08-12 15:51:12 -0700 | [diff] [blame] | 49 | shared_ptr<Link> link; |
Davide Pesavento | 65d1155 | 2019-06-09 19:15:50 -0400 | [diff] [blame] | 50 | optional<time::milliseconds> interestLifetime; |
Junxiao Shi | b54aabd | 2018-04-16 19:36:24 +0000 | [diff] [blame] | 51 | |
| 52 | // output behavior options |
| 53 | bool isVerbose = false; |
Junxiao Shi | b54aabd | 2018-04-16 19:36:24 +0000 | [diff] [blame] | 54 | bool wantPayloadOnly = false; |
Davide Pesavento | 65d1155 | 2019-06-09 19:15:50 -0400 | [diff] [blame] | 55 | optional<time::milliseconds> timeout; |
Zhuo Li | b355889 | 2016-08-12 15:51:12 -0700 | [diff] [blame] | 56 | }; |
| 57 | |
| 58 | enum class ResultCode { |
Davide Pesavento | 65d1155 | 2019-06-09 19:15:50 -0400 | [diff] [blame] | 59 | UNKNOWN = 1, |
Zhuo Li | b355889 | 2016-08-12 15:51:12 -0700 | [diff] [blame] | 60 | DATA = 0, |
| 61 | NACK = 4, |
Davide Pesavento | 65d1155 | 2019-06-09 19:15:50 -0400 | [diff] [blame] | 62 | TIMEOUT = 3, |
Zhuo Li | b355889 | 2016-08-12 15:51:12 -0700 | [diff] [blame] | 63 | }; |
| 64 | |
| 65 | class NdnPeek : boost::noncopyable |
| 66 | { |
| 67 | public: |
| 68 | NdnPeek(Face& face, const PeekOptions& options); |
| 69 | |
| 70 | /** |
Davide Pesavento | 65d1155 | 2019-06-09 19:15:50 -0400 | [diff] [blame] | 71 | * @return the result of NdnPeek execution |
Zhuo Li | b355889 | 2016-08-12 15:51:12 -0700 | [diff] [blame] | 72 | */ |
| 73 | ResultCode |
Davide Pesavento | 65d1155 | 2019-06-09 19:15:50 -0400 | [diff] [blame] | 74 | getResultCode() const |
| 75 | { |
| 76 | return m_resultCode; |
| 77 | } |
Zhuo Li | b355889 | 2016-08-12 15:51:12 -0700 | [diff] [blame] | 78 | |
| 79 | /** |
| 80 | * @brief express the Interest |
| 81 | * @note The caller must invoke face.processEvents() afterwards |
| 82 | */ |
| 83 | void |
| 84 | start(); |
| 85 | |
| 86 | private: |
| 87 | Interest |
| 88 | createInterest() const; |
| 89 | |
| 90 | /** |
| 91 | * @brief called when a Data packet is received |
| 92 | */ |
| 93 | void |
| 94 | onData(const Data& data); |
| 95 | |
| 96 | /** |
| 97 | * @brief called when a Nack packet is received |
| 98 | */ |
| 99 | void |
| 100 | onNack(const lp::Nack& nack); |
| 101 | |
Davide Pesavento | 65d1155 | 2019-06-09 19:15:50 -0400 | [diff] [blame] | 102 | /** |
| 103 | * @brief called when the Interest times out |
| 104 | */ |
| 105 | void |
| 106 | onTimeout(); |
| 107 | |
Zhuo Li | b355889 | 2016-08-12 15:51:12 -0700 | [diff] [blame] | 108 | private: |
Zhuo Li | b355889 | 2016-08-12 15:51:12 -0700 | [diff] [blame] | 109 | const PeekOptions& m_options; |
Davide Pesavento | 65d1155 | 2019-06-09 19:15:50 -0400 | [diff] [blame] | 110 | Face& m_face; |
| 111 | Scheduler m_scheduler; |
| 112 | time::steady_clock::TimePoint m_sendTime; |
| 113 | ScopedPendingInterestHandle m_pendingInterest; |
| 114 | scheduler::ScopedEventId m_timeoutEvent; |
| 115 | ResultCode m_resultCode = ResultCode::UNKNOWN; |
Zhuo Li | b355889 | 2016-08-12 15:51:12 -0700 | [diff] [blame] | 116 | }; |
| 117 | |
| 118 | } // namespace peek |
| 119 | } // namespace ndn |
| 120 | |
| 121 | #endif // NDN_TOOLS_NDNPEEK_NDNPEEK_HPP |