blob: 79b7591ef13b0520233617e174af7e0e8669947e [file] [log] [blame]
Eric Newberry2f041d22018-06-03 18:02:31 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
Davide Pesavento5748e822024-01-26 18:40:22 -05003 * Copyright (c) 2014-2024, Regents of the University of California,
Eric Newberry2f041d22018-06-03 18:02:31 -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>
Davide Pesavento6a1396e2019-07-26 15:03:28 -040026 * @author Davide Pesavento <davidepesa@gmail.com>
Eric Newberry2f041d22018-06-03 18:02:31 -070027 */
28
29#ifndef NDN_TOOLS_NDNPOKE_NDNPOKE_HPP
30#define NDN_TOOLS_NDNPOKE_NDNPOKE_HPP
31
32#include "core/common.hpp"
33
Davide Pesavento5748e822024-01-26 18:40:22 -050034#include <ndn-cxx/face.hpp>
35#include <ndn-cxx/security/key-chain.hpp>
Davide Pesaventoe75861e2019-07-24 21:55:39 -040036#include <ndn-cxx/util/scheduler.hpp>
37
Davide Pesavento5748e822024-01-26 18:40:22 -050038#include <optional>
39
Davide Pesaventob3570c62022-02-19 19:19:00 -050040namespace ndn::peek {
Eric Newberry2f041d22018-06-03 18:02:31 -070041
42/**
43 * \brief options for NdnPoke
44 */
45struct PokeOptions
46{
Davide Pesaventoe75861e2019-07-24 21:55:39 -040047 // Data construction options
48 Name name;
Davide Pesavento6a1396e2019-07-26 15:03:28 -040049 time::milliseconds freshnessPeriod = DEFAULT_FRESHNESS_PERIOD;
Davide Pesaventoe75861e2019-07-24 21:55:39 -040050 bool wantFinalBlockId = false;
Eric Newberry2f041d22018-06-03 18:02:31 -070051 security::SigningInfo signingInfo;
Davide Pesaventoe75861e2019-07-24 21:55:39 -040052
53 // program behavior options
Davide Pesaventob3ae6342019-07-25 21:07:16 -040054 bool isVerbose = false;
Davide Pesavento6a1396e2019-07-26 15:03:28 -040055 bool wantUnsolicited = false;
Davide Pesavento242d5062022-03-11 16:34:23 -050056 std::optional<time::milliseconds> timeout;
Eric Newberry2f041d22018-06-03 18:02:31 -070057};
58
Davide Pesaventob3570c62022-02-19 19:19:00 -050059class NdnPoke : noncopyable
Eric Newberry2f041d22018-06-03 18:02:31 -070060{
61public:
Davide Pesaventoe75861e2019-07-24 21:55:39 -040062 NdnPoke(Face& face, KeyChain& keyChain, std::istream& input, const PokeOptions& options);
Eric Newberry2f041d22018-06-03 18:02:31 -070063
Davide Pesavento87434be2019-07-25 19:04:23 -040064 enum class Result {
65 DATA_SENT = 0,
66 UNKNOWN = 1,
67 // 2 is reserved for "malformed command line"
68 TIMEOUT = 3,
69 // 4 is reserved for "nack"
70 PREFIX_REG_FAIL = 5,
71 };
72
73 /**
74 * @return the result of NdnPoke execution
75 */
76 Result
77 getResult() const
78 {
79 return m_result;
80 }
81
Eric Newberry2f041d22018-06-03 18:02:31 -070082 void
83 start();
84
Eric Newberry2f041d22018-06-03 18:02:31 -070085private:
Davide Pesavento5748e822024-01-26 18:40:22 -050086 std::shared_ptr<Data>
Davide Pesaventoe75861e2019-07-24 21:55:39 -040087 createData() const;
Eric Newberry2f041d22018-06-03 18:02:31 -070088
Davide Pesaventob3ae6342019-07-25 21:07:16 -040089 void
90 sendData(const Data& data);
91
92 void
93 onInterest(const Interest& interest, const Data& data);
94
95 void
96 onRegSuccess();
97
98 void
Davide Pesaventob3570c62022-02-19 19:19:00 -050099 onRegFailure(std::string_view reason);
Davide Pesaventob3ae6342019-07-25 21:07:16 -0400100
Eric Newberry2f041d22018-06-03 18:02:31 -0700101private:
Davide Pesaventoe75861e2019-07-24 21:55:39 -0400102 const PokeOptions m_options;
Eric Newberry2f041d22018-06-03 18:02:31 -0700103 Face& m_face;
104 KeyChain& m_keyChain;
Davide Pesaventoe75861e2019-07-24 21:55:39 -0400105 std::istream& m_input;
106 Scheduler m_scheduler;
107 ScopedRegisteredPrefixHandle m_registeredPrefix;
108 scheduler::ScopedEventId m_timeoutEvent;
Davide Pesavento87434be2019-07-25 19:04:23 -0400109 Result m_result = Result::UNKNOWN;
Eric Newberry2f041d22018-06-03 18:02:31 -0700110};
111
Davide Pesaventob3570c62022-02-19 19:19:00 -0500112} // namespace ndn::peek
Eric Newberry2f041d22018-06-03 18:02:31 -0700113
114#endif // NDN_TOOLS_NDNPOKE_NDNPOKE_HPP