blob: d02155fb0c691918f1fa39da0067fe183a0dfd81 [file] [log] [blame]
Zhuo Lib3558892016-08-12 15:51:12 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shib54aabd2018-04-16 19:36:24 +00002/*
Davide Pesavento65d11552019-06-09 19:15:50 -04003 * Copyright (c) 2014-2019, Regents of the University of California,
Zhuo Lib3558892016-08-12 15:51:12 -07004 * 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 Pesavento65d11552019-06-09 19:15:50 -040033
Junxiao Shia5b60cc2017-07-26 01:35:48 +000034#include <ndn-cxx/link.hpp>
Davide Pesavento65d11552019-06-09 19:15:50 -040035#include <ndn-cxx/util/scheduler.hpp>
Zhuo Lib3558892016-08-12 15:51:12 -070036
37namespace ndn {
38namespace peek {
39
40/**
41 * @brief options for NdnPeek
42 */
43struct PeekOptions
44{
Junxiao Shib54aabd2018-04-16 19:36:24 +000045 // Interest construction options
Davide Pesavento65d11552019-06-09 19:15:50 -040046 Name name;
Junxiao Shib54aabd2018-04-16 19:36:24 +000047 bool canBePrefix = false;
48 bool mustBeFresh = false;
Zhuo Lib3558892016-08-12 15:51:12 -070049 shared_ptr<Link> link;
Davide Pesavento0da1f442019-07-26 17:38:13 -040050 time::milliseconds interestLifetime = DEFAULT_INTEREST_LIFETIME;
Junxiao Shib54aabd2018-04-16 19:36:24 +000051
Davide Pesavento0da1f442019-07-26 17:38:13 -040052 // program behavior options
Junxiao Shib54aabd2018-04-16 19:36:24 +000053 bool isVerbose = false;
Junxiao Shib54aabd2018-04-16 19:36:24 +000054 bool wantPayloadOnly = false;
Davide Pesavento65d11552019-06-09 19:15:50 -040055 optional<time::milliseconds> timeout;
Zhuo Lib3558892016-08-12 15:51:12 -070056};
57
Zhuo Lib3558892016-08-12 15:51:12 -070058class NdnPeek : boost::noncopyable
59{
60public:
61 NdnPeek(Face& face, const PeekOptions& options);
62
Davide Pesavento87434be2019-07-25 19:04:23 -040063 enum class Result {
64 DATA = 0,
65 UNKNOWN = 1,
66 // 2 is reserved for "malformed command line"
67 TIMEOUT = 3,
68 NACK = 4,
69 };
70
Zhuo Lib3558892016-08-12 15:51:12 -070071 /**
Davide Pesavento65d11552019-06-09 19:15:50 -040072 * @return the result of NdnPeek execution
Zhuo Lib3558892016-08-12 15:51:12 -070073 */
Davide Pesavento87434be2019-07-25 19:04:23 -040074 Result
75 getResult() const
Davide Pesavento65d11552019-06-09 19:15:50 -040076 {
Davide Pesavento87434be2019-07-25 19:04:23 -040077 return m_result;
Davide Pesavento65d11552019-06-09 19:15:50 -040078 }
Zhuo Lib3558892016-08-12 15:51:12 -070079
80 /**
81 * @brief express the Interest
82 * @note The caller must invoke face.processEvents() afterwards
83 */
84 void
85 start();
86
87private:
88 Interest
89 createInterest() const;
90
91 /**
92 * @brief called when a Data packet is received
93 */
94 void
95 onData(const Data& data);
96
97 /**
98 * @brief called when a Nack packet is received
99 */
100 void
101 onNack(const lp::Nack& nack);
102
Davide Pesavento65d11552019-06-09 19:15:50 -0400103 /**
104 * @brief called when the Interest times out
105 */
106 void
107 onTimeout();
108
Zhuo Lib3558892016-08-12 15:51:12 -0700109private:
Zhuo Lib3558892016-08-12 15:51:12 -0700110 const PeekOptions& m_options;
Davide Pesavento65d11552019-06-09 19:15:50 -0400111 Face& m_face;
112 Scheduler m_scheduler;
113 time::steady_clock::TimePoint m_sendTime;
114 ScopedPendingInterestHandle m_pendingInterest;
115 scheduler::ScopedEventId m_timeoutEvent;
Davide Pesavento87434be2019-07-25 19:04:23 -0400116 Result m_result = Result::UNKNOWN;
Zhuo Lib3558892016-08-12 15:51:12 -0700117};
118
119} // namespace peek
120} // namespace ndn
121
122#endif // NDN_TOOLS_NDNPEEK_NDNPEEK_HPP