blob: d31a93975dabde40e2f6fb0e30e4743ff2d96431 [file] [log] [blame]
Eric Newberry2f041d22018-06-03 18:02:31 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
Junxiao Shi06d008c2019-02-04 08:26:59 +00003 * Copyright (c) 2014-2019, 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
33namespace ndn {
34namespace peek {
35
Davide Pesaventoe75861e2019-07-24 21:55:39 -040036NdnPoke::NdnPoke(Face& face, KeyChain& keyChain, std::istream& input, const PokeOptions& options)
37 : m_options(options)
38 , m_face(face)
Eric Newberry2f041d22018-06-03 18:02:31 -070039 , m_keyChain(keyChain)
Davide Pesaventoe75861e2019-07-24 21:55:39 -040040 , m_input(input)
41 , m_scheduler(m_face.getIoService())
Eric Newberry2f041d22018-06-03 18:02:31 -070042{
43}
44
45void
46NdnPoke::start()
47{
Davide Pesaventoe75861e2019-07-24 21:55:39 -040048 auto data = createData();
49
Davide Pesavento6a1396e2019-07-26 15:03:28 -040050 if (m_options.wantUnsolicited) {
Davide Pesaventob3ae6342019-07-25 21:07:16 -040051 return sendData(*data);
Eric Newberry2f041d22018-06-03 18:02:31 -070052 }
Davide Pesaventoe75861e2019-07-24 21:55:39 -040053
54 m_registeredPrefix = m_face.setInterestFilter(m_options.name,
Davide Pesaventob3ae6342019-07-25 21:07:16 -040055 [this, data] (auto&&, const auto& interest) { this->onInterest(interest, *data); },
56 [this] (auto&&) { this->onRegSuccess(); },
57 [this] (auto&&, const auto& reason) { this->onRegFailure(reason); });
Eric Newberry2f041d22018-06-03 18:02:31 -070058}
59
60shared_ptr<Data>
Davide Pesaventoe75861e2019-07-24 21:55:39 -040061NdnPoke::createData() const
Eric Newberry2f041d22018-06-03 18:02:31 -070062{
Davide Pesaventoe75861e2019-07-24 21:55:39 -040063 auto data = make_shared<Data>(m_options.name);
Davide Pesavento6a1396e2019-07-26 15:03:28 -040064 data->setFreshnessPeriod(m_options.freshnessPeriod);
Davide Pesaventoe75861e2019-07-24 21:55:39 -040065 if (m_options.wantFinalBlockId) {
66 data->setFinalBlock(m_options.name.at(-1));
Eric Newberry2f041d22018-06-03 18:02:31 -070067 }
68
Davide Pesaventoe75861e2019-07-24 21:55:39 -040069 OBufferStream os;
70 os << m_input.rdbuf();
71 data->setContent(os.buf());
Eric Newberry2f041d22018-06-03 18:02:31 -070072
Davide Pesaventoe75861e2019-07-24 21:55:39 -040073 m_keyChain.sign(*data, m_options.signingInfo);
Eric Newberry2f041d22018-06-03 18:02:31 -070074
Davide Pesaventoe75861e2019-07-24 21:55:39 -040075 return data;
Eric Newberry2f041d22018-06-03 18:02:31 -070076}
77
Davide Pesaventob3ae6342019-07-25 21:07:16 -040078void
79NdnPoke::sendData(const Data& data)
80{
81 m_face.put(data);
82 m_result = Result::DATA_SENT;
83
84 if (m_options.isVerbose) {
85 std::cerr << "DATA: " << data;
86 }
87}
88
89void
90NdnPoke::onInterest(const Interest& interest, const Data& data)
91{
92 if (m_options.isVerbose) {
93 std::cerr << "INTEREST: " << interest << std::endl;
94 }
95
Davide Pesaventoc5243b42019-07-26 13:30:16 -040096 if (interest.matchesData(data)) {
97 m_timeoutEvent.cancel();
98 m_registeredPrefix.cancel();
99 sendData(data);
100 }
101 else if (m_options.isVerbose) {
102 std::cerr << "Interest cannot be satisfied" << std::endl;
103 }
Davide Pesaventob3ae6342019-07-25 21:07:16 -0400104}
105
106void
107NdnPoke::onRegSuccess()
108{
109 if (m_options.isVerbose) {
110 std::cerr << "Prefix registration successful" << std::endl;
111 }
112
113 if (m_options.timeout) {
114 m_timeoutEvent = m_scheduler.schedule(*m_options.timeout, [this] {
115 m_result = Result::TIMEOUT;
116 m_registeredPrefix.cancel();
117
118 if (m_options.isVerbose) {
119 std::cerr << "TIMEOUT" << std::endl;
120 }
121 });
122 }
123}
124
125void
126NdnPoke::onRegFailure(const std::string& reason)
127{
128 m_result = Result::PREFIX_REG_FAIL;
129 std::cerr << "Prefix registration failure (" << reason << ")" << std::endl;
130}
131
Eric Newberry2f041d22018-06-03 18:02:31 -0700132} // namespace peek
133} // namespace ndn