blob: 0d04efac8491acd254c12abcba40693c5e90134f [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>
26 */
27
28#include "ndnpoke.hpp"
29
Davide Pesaventoe75861e2019-07-24 21:55:39 -040030#include <ndn-cxx/encoding/buffer-stream.hpp>
Eric Newberry2f041d22018-06-03 18:02:31 -070031
32namespace ndn {
33namespace peek {
34
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
Eric Newberry2f041d22018-06-03 18:02:31 -070049 if (m_options.wantForceData) {
Davide Pesaventoe75861e2019-07-24 21:55:39 -040050 m_face.put(*data);
Davide Pesavento87434be2019-07-25 19:04:23 -040051 m_result = Result::DATA_SENT;
Davide Pesaventoe75861e2019-07-24 21:55:39 -040052 return;
Eric Newberry2f041d22018-06-03 18:02:31 -070053 }
Davide Pesaventoe75861e2019-07-24 21:55:39 -040054
55 m_registeredPrefix = m_face.setInterestFilter(m_options.name,
56 [this, data] (auto&&...) {
57 m_timeoutEvent.cancel();
58 m_face.put(*data);
Davide Pesavento87434be2019-07-25 19:04:23 -040059 m_result = Result::DATA_SENT;
Davide Pesaventoe75861e2019-07-24 21:55:39 -040060 m_registeredPrefix.cancel();
61 },
62 [this] (auto&&) {
63 m_timeoutEvent = m_scheduler.schedule(m_options.timeout, [this] {
Davide Pesavento94dff002019-07-24 23:35:05 -040064 m_result = Result::TIMEOUT;
Davide Pesaventoe75861e2019-07-24 21:55:39 -040065 m_registeredPrefix.cancel();
66 });
67 },
Davide Pesavento87434be2019-07-25 19:04:23 -040068 [this] (auto&&, const auto& reason) {
69 m_result = Result::PREFIX_REG_FAIL;
Davide Pesaventoe75861e2019-07-24 21:55:39 -040070 std::cerr << "Prefix registration failure (" << reason << ")\n";
71 });
Eric Newberry2f041d22018-06-03 18:02:31 -070072}
73
74shared_ptr<Data>
Davide Pesaventoe75861e2019-07-24 21:55:39 -040075NdnPoke::createData() const
Eric Newberry2f041d22018-06-03 18:02:31 -070076{
Davide Pesaventoe75861e2019-07-24 21:55:39 -040077 auto data = make_shared<Data>(m_options.name);
Eric Newberry2f041d22018-06-03 18:02:31 -070078 if (m_options.freshnessPeriod) {
Davide Pesaventoe75861e2019-07-24 21:55:39 -040079 data->setFreshnessPeriod(*m_options.freshnessPeriod);
80 }
81 if (m_options.wantFinalBlockId) {
82 data->setFinalBlock(m_options.name.at(-1));
Eric Newberry2f041d22018-06-03 18:02:31 -070083 }
84
Davide Pesaventoe75861e2019-07-24 21:55:39 -040085 OBufferStream os;
86 os << m_input.rdbuf();
87 data->setContent(os.buf());
Eric Newberry2f041d22018-06-03 18:02:31 -070088
Davide Pesaventoe75861e2019-07-24 21:55:39 -040089 m_keyChain.sign(*data, m_options.signingInfo);
Eric Newberry2f041d22018-06-03 18:02:31 -070090
Davide Pesaventoe75861e2019-07-24 21:55:39 -040091 return data;
Eric Newberry2f041d22018-06-03 18:02:31 -070092}
93
94} // namespace peek
95} // namespace ndn