blob: 27feb17051f0d3edf8fc4bef13719355fa45eeaf [file] [log] [blame]
Eric Newberry2f041d22018-06-03 18:02:31 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
Davide Pesaventob3570c62022-02-19 19:19:00 -05003 * Copyright (c) 2014-2022, 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#include "ndnpoke.hpp"
30
Davide Pesaventoe75861e2019-07-24 21:55:39 -040031#include <ndn-cxx/encoding/buffer-stream.hpp>
Eric Newberry2f041d22018-06-03 18:02:31 -070032
Davide Pesaventob3570c62022-02-19 19:19:00 -050033namespace ndn::peek {
Eric Newberry2f041d22018-06-03 18:02:31 -070034
Davide Pesaventoe75861e2019-07-24 21:55:39 -040035NdnPoke::NdnPoke(Face& face, KeyChain& keyChain, std::istream& input, const PokeOptions& options)
36 : m_options(options)
37 , m_face(face)
Eric Newberry2f041d22018-06-03 18:02:31 -070038 , m_keyChain(keyChain)
Davide Pesaventoe75861e2019-07-24 21:55:39 -040039 , m_input(input)
40 , m_scheduler(m_face.getIoService())
Eric Newberry2f041d22018-06-03 18:02:31 -070041{
42}
43
44void
45NdnPoke::start()
46{
Davide Pesaventoe75861e2019-07-24 21:55:39 -040047 auto data = createData();
48
Davide Pesavento6a1396e2019-07-26 15:03:28 -040049 if (m_options.wantUnsolicited) {
Davide Pesaventob3ae6342019-07-25 21:07:16 -040050 return sendData(*data);
Eric Newberry2f041d22018-06-03 18:02:31 -070051 }
Davide Pesaventoe75861e2019-07-24 21:55:39 -040052
53 m_registeredPrefix = m_face.setInterestFilter(m_options.name,
Davide Pesaventob3ae6342019-07-25 21:07:16 -040054 [this, data] (auto&&, const auto& interest) { this->onInterest(interest, *data); },
55 [this] (auto&&) { this->onRegSuccess(); },
56 [this] (auto&&, const auto& reason) { this->onRegFailure(reason); });
Eric Newberry2f041d22018-06-03 18:02:31 -070057}
58
59shared_ptr<Data>
Davide Pesaventoe75861e2019-07-24 21:55:39 -040060NdnPoke::createData() const
Eric Newberry2f041d22018-06-03 18:02:31 -070061{
Davide Pesaventoe75861e2019-07-24 21:55:39 -040062 auto data = make_shared<Data>(m_options.name);
Davide Pesavento6a1396e2019-07-26 15:03:28 -040063 data->setFreshnessPeriod(m_options.freshnessPeriod);
Davide Pesaventoe75861e2019-07-24 21:55:39 -040064 if (m_options.wantFinalBlockId) {
65 data->setFinalBlock(m_options.name.at(-1));
Eric Newberry2f041d22018-06-03 18:02:31 -070066 }
67
Davide Pesaventoe75861e2019-07-24 21:55:39 -040068 OBufferStream os;
69 os << m_input.rdbuf();
70 data->setContent(os.buf());
Eric Newberry2f041d22018-06-03 18:02:31 -070071
Davide Pesaventoe75861e2019-07-24 21:55:39 -040072 m_keyChain.sign(*data, m_options.signingInfo);
Eric Newberry2f041d22018-06-03 18:02:31 -070073
Davide Pesaventoe75861e2019-07-24 21:55:39 -040074 return data;
Eric Newberry2f041d22018-06-03 18:02:31 -070075}
76
Davide Pesaventob3ae6342019-07-25 21:07:16 -040077void
78NdnPoke::sendData(const Data& data)
79{
80 m_face.put(data);
81 m_result = Result::DATA_SENT;
82
83 if (m_options.isVerbose) {
84 std::cerr << "DATA: " << data;
85 }
86}
87
88void
89NdnPoke::onInterest(const Interest& interest, const Data& data)
90{
91 if (m_options.isVerbose) {
92 std::cerr << "INTEREST: " << interest << std::endl;
93 }
94
Davide Pesaventoc5243b42019-07-26 13:30:16 -040095 if (interest.matchesData(data)) {
96 m_timeoutEvent.cancel();
97 m_registeredPrefix.cancel();
98 sendData(data);
99 }
100 else if (m_options.isVerbose) {
101 std::cerr << "Interest cannot be satisfied" << std::endl;
102 }
Davide Pesaventob3ae6342019-07-25 21:07:16 -0400103}
104
105void
106NdnPoke::onRegSuccess()
107{
108 if (m_options.isVerbose) {
109 std::cerr << "Prefix registration successful" << std::endl;
110 }
111
112 if (m_options.timeout) {
113 m_timeoutEvent = m_scheduler.schedule(*m_options.timeout, [this] {
114 m_result = Result::TIMEOUT;
115 m_registeredPrefix.cancel();
116
117 if (m_options.isVerbose) {
118 std::cerr << "TIMEOUT" << std::endl;
119 }
120 });
121 }
122}
123
124void
Davide Pesaventob3570c62022-02-19 19:19:00 -0500125NdnPoke::onRegFailure(std::string_view reason)
Davide Pesaventob3ae6342019-07-25 21:07:16 -0400126{
127 m_result = Result::PREFIX_REG_FAIL;
128 std::cerr << "Prefix registration failure (" << reason << ")" << std::endl;
129}
130
Davide Pesaventob3570c62022-02-19 19:19:00 -0500131} // namespace ndn::peek