Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /* |
Junxiao Shi | 06d008c | 2019-02-04 08:26:59 +0000 | [diff] [blame] | 3 | * Copyright (c) 2014-2019, Regents of the University of California, |
Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 4 | * 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 Pesavento | 6a1396e | 2019-07-26 15:03:28 -0400 | [diff] [blame] | 26 | * @author Davide Pesavento <davidepesa@gmail.com> |
Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 27 | */ |
| 28 | |
| 29 | #include "ndnpoke.hpp" |
| 30 | |
Davide Pesavento | e75861e | 2019-07-24 21:55:39 -0400 | [diff] [blame] | 31 | #include <ndn-cxx/encoding/buffer-stream.hpp> |
Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 32 | |
| 33 | namespace ndn { |
| 34 | namespace peek { |
| 35 | |
Davide Pesavento | e75861e | 2019-07-24 21:55:39 -0400 | [diff] [blame] | 36 | NdnPoke::NdnPoke(Face& face, KeyChain& keyChain, std::istream& input, const PokeOptions& options) |
| 37 | : m_options(options) |
| 38 | , m_face(face) |
Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 39 | , m_keyChain(keyChain) |
Davide Pesavento | e75861e | 2019-07-24 21:55:39 -0400 | [diff] [blame] | 40 | , m_input(input) |
| 41 | , m_scheduler(m_face.getIoService()) |
Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 42 | { |
| 43 | } |
| 44 | |
| 45 | void |
| 46 | NdnPoke::start() |
| 47 | { |
Davide Pesavento | e75861e | 2019-07-24 21:55:39 -0400 | [diff] [blame] | 48 | auto data = createData(); |
| 49 | |
Davide Pesavento | 6a1396e | 2019-07-26 15:03:28 -0400 | [diff] [blame] | 50 | if (m_options.wantUnsolicited) { |
Davide Pesavento | b3ae634 | 2019-07-25 21:07:16 -0400 | [diff] [blame] | 51 | return sendData(*data); |
Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 52 | } |
Davide Pesavento | e75861e | 2019-07-24 21:55:39 -0400 | [diff] [blame] | 53 | |
| 54 | m_registeredPrefix = m_face.setInterestFilter(m_options.name, |
Davide Pesavento | b3ae634 | 2019-07-25 21:07:16 -0400 | [diff] [blame] | 55 | [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 Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | shared_ptr<Data> |
Davide Pesavento | e75861e | 2019-07-24 21:55:39 -0400 | [diff] [blame] | 61 | NdnPoke::createData() const |
Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 62 | { |
Davide Pesavento | e75861e | 2019-07-24 21:55:39 -0400 | [diff] [blame] | 63 | auto data = make_shared<Data>(m_options.name); |
Davide Pesavento | 6a1396e | 2019-07-26 15:03:28 -0400 | [diff] [blame] | 64 | data->setFreshnessPeriod(m_options.freshnessPeriod); |
Davide Pesavento | e75861e | 2019-07-24 21:55:39 -0400 | [diff] [blame] | 65 | if (m_options.wantFinalBlockId) { |
| 66 | data->setFinalBlock(m_options.name.at(-1)); |
Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 67 | } |
| 68 | |
Davide Pesavento | e75861e | 2019-07-24 21:55:39 -0400 | [diff] [blame] | 69 | OBufferStream os; |
| 70 | os << m_input.rdbuf(); |
| 71 | data->setContent(os.buf()); |
Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 72 | |
Davide Pesavento | e75861e | 2019-07-24 21:55:39 -0400 | [diff] [blame] | 73 | m_keyChain.sign(*data, m_options.signingInfo); |
Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 74 | |
Davide Pesavento | e75861e | 2019-07-24 21:55:39 -0400 | [diff] [blame] | 75 | return data; |
Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 76 | } |
| 77 | |
Davide Pesavento | b3ae634 | 2019-07-25 21:07:16 -0400 | [diff] [blame] | 78 | void |
| 79 | NdnPoke::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 | |
| 89 | void |
| 90 | NdnPoke::onInterest(const Interest& interest, const Data& data) |
| 91 | { |
| 92 | if (m_options.isVerbose) { |
| 93 | std::cerr << "INTEREST: " << interest << std::endl; |
| 94 | } |
| 95 | |
Davide Pesavento | c5243b4 | 2019-07-26 13:30:16 -0400 | [diff] [blame] | 96 | 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 Pesavento | b3ae634 | 2019-07-25 21:07:16 -0400 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | void |
| 107 | NdnPoke::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 | |
| 125 | void |
| 126 | NdnPoke::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 Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 132 | } // namespace peek |
| 133 | } // namespace ndn |