blob: 998d049668f175f3ed9b37683dd0180d43f2f7b5 [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>
Davide Pesavento96028232019-08-17 01:26:13 -040027 * @author Davide Pesavento <davidepesa@gmail.com>
Zhuo Lib3558892016-08-12 15:51:12 -070028 */
29
30#ifndef NDN_TOOLS_NDNPEEK_NDNPEEK_HPP
31#define NDN_TOOLS_NDNPEEK_NDNPEEK_HPP
32
33#include "core/common.hpp"
Davide Pesavento65d11552019-06-09 19:15:50 -040034
Junxiao Shia5b60cc2017-07-26 01:35:48 +000035#include <ndn-cxx/link.hpp>
Davide Pesavento65d11552019-06-09 19:15:50 -040036#include <ndn-cxx/util/scheduler.hpp>
Zhuo Lib3558892016-08-12 15:51:12 -070037
38namespace ndn {
39namespace peek {
40
41/**
42 * @brief options for NdnPeek
43 */
44struct PeekOptions
45{
Junxiao Shib54aabd2018-04-16 19:36:24 +000046 // Interest construction options
Davide Pesavento65d11552019-06-09 19:15:50 -040047 Name name;
Junxiao Shib54aabd2018-04-16 19:36:24 +000048 bool canBePrefix = false;
49 bool mustBeFresh = false;
Zhuo Lib3558892016-08-12 15:51:12 -070050 shared_ptr<Link> link;
Davide Pesavento0da1f442019-07-26 17:38:13 -040051 time::milliseconds interestLifetime = DEFAULT_INTEREST_LIFETIME;
Davide Pesaventoc214e072019-08-17 01:33:28 -040052 optional<uint8_t> hopLimit;
Davide Pesavento96028232019-08-17 01:26:13 -040053 shared_ptr<Buffer> applicationParameters;
Junxiao Shib54aabd2018-04-16 19:36:24 +000054
Davide Pesavento0da1f442019-07-26 17:38:13 -040055 // program behavior options
Junxiao Shib54aabd2018-04-16 19:36:24 +000056 bool isVerbose = false;
Junxiao Shib54aabd2018-04-16 19:36:24 +000057 bool wantPayloadOnly = false;
Davide Pesavento65d11552019-06-09 19:15:50 -040058 optional<time::milliseconds> timeout;
Zhuo Lib3558892016-08-12 15:51:12 -070059};
60
Zhuo Lib3558892016-08-12 15:51:12 -070061class NdnPeek : boost::noncopyable
62{
63public:
64 NdnPeek(Face& face, const PeekOptions& options);
65
Davide Pesavento87434be2019-07-25 19:04:23 -040066 enum class Result {
67 DATA = 0,
68 UNKNOWN = 1,
69 // 2 is reserved for "malformed command line"
70 TIMEOUT = 3,
71 NACK = 4,
72 };
73
Zhuo Lib3558892016-08-12 15:51:12 -070074 /**
Davide Pesavento65d11552019-06-09 19:15:50 -040075 * @return the result of NdnPeek execution
Zhuo Lib3558892016-08-12 15:51:12 -070076 */
Davide Pesavento87434be2019-07-25 19:04:23 -040077 Result
78 getResult() const
Davide Pesavento65d11552019-06-09 19:15:50 -040079 {
Davide Pesavento87434be2019-07-25 19:04:23 -040080 return m_result;
Davide Pesavento65d11552019-06-09 19:15:50 -040081 }
Zhuo Lib3558892016-08-12 15:51:12 -070082
83 /**
84 * @brief express the Interest
85 * @note The caller must invoke face.processEvents() afterwards
86 */
87 void
88 start();
89
90private:
91 Interest
92 createInterest() const;
93
94 /**
95 * @brief called when a Data packet is received
96 */
97 void
98 onData(const Data& data);
99
100 /**
101 * @brief called when a Nack packet is received
102 */
103 void
104 onNack(const lp::Nack& nack);
105
Davide Pesavento65d11552019-06-09 19:15:50 -0400106 /**
107 * @brief called when the Interest times out
108 */
109 void
110 onTimeout();
111
Zhuo Lib3558892016-08-12 15:51:12 -0700112private:
Davide Pesavento96028232019-08-17 01:26:13 -0400113 const PeekOptions m_options;
Davide Pesavento65d11552019-06-09 19:15:50 -0400114 Face& m_face;
115 Scheduler m_scheduler;
116 time::steady_clock::TimePoint m_sendTime;
117 ScopedPendingInterestHandle m_pendingInterest;
118 scheduler::ScopedEventId m_timeoutEvent;
Davide Pesavento87434be2019-07-25 19:04:23 -0400119 Result m_result = Result::UNKNOWN;
Zhuo Lib3558892016-08-12 15:51:12 -0700120};
121
122} // namespace peek
123} // namespace ndn
124
125#endif // NDN_TOOLS_NDNPEEK_NDNPEEK_HPP