Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /* |
Davide Pesavento | 5748e82 | 2024-01-26 18:40:22 -0500 | [diff] [blame] | 3 | * Copyright (c) 2014-2024, 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 | |
Davide Pesavento | 5748e82 | 2024-01-26 18:40:22 -0500 | [diff] [blame] | 33 | #include <iostream> |
| 34 | |
Davide Pesavento | b3570c6 | 2022-02-19 19:19:00 -0500 | [diff] [blame] | 35 | namespace ndn::peek { |
Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 36 | |
Davide Pesavento | e75861e | 2019-07-24 21:55:39 -0400 | [diff] [blame] | 37 | NdnPoke::NdnPoke(Face& face, KeyChain& keyChain, std::istream& input, const PokeOptions& options) |
| 38 | : m_options(options) |
| 39 | , m_face(face) |
Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 40 | , m_keyChain(keyChain) |
Davide Pesavento | e75861e | 2019-07-24 21:55:39 -0400 | [diff] [blame] | 41 | , m_input(input) |
Davide Pesavento | 7e9d7e4 | 2023-11-11 15:00:03 -0500 | [diff] [blame] | 42 | , m_scheduler(m_face.getIoContext()) |
Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 43 | { |
| 44 | } |
| 45 | |
| 46 | void |
| 47 | NdnPoke::start() |
| 48 | { |
Davide Pesavento | e75861e | 2019-07-24 21:55:39 -0400 | [diff] [blame] | 49 | auto data = createData(); |
| 50 | |
Davide Pesavento | 6a1396e | 2019-07-26 15:03:28 -0400 | [diff] [blame] | 51 | if (m_options.wantUnsolicited) { |
Davide Pesavento | b3ae634 | 2019-07-25 21:07:16 -0400 | [diff] [blame] | 52 | return sendData(*data); |
Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 53 | } |
Davide Pesavento | e75861e | 2019-07-24 21:55:39 -0400 | [diff] [blame] | 54 | |
| 55 | m_registeredPrefix = m_face.setInterestFilter(m_options.name, |
Davide Pesavento | b3ae634 | 2019-07-25 21:07:16 -0400 | [diff] [blame] | 56 | [this, data] (auto&&, const auto& interest) { this->onInterest(interest, *data); }, |
| 57 | [this] (auto&&) { this->onRegSuccess(); }, |
| 58 | [this] (auto&&, const auto& reason) { this->onRegFailure(reason); }); |
Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 59 | } |
| 60 | |
Davide Pesavento | 5748e82 | 2024-01-26 18:40:22 -0500 | [diff] [blame] | 61 | std::shared_ptr<Data> |
Davide Pesavento | e75861e | 2019-07-24 21:55:39 -0400 | [diff] [blame] | 62 | NdnPoke::createData() const |
Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 63 | { |
Davide Pesavento | 5748e82 | 2024-01-26 18:40:22 -0500 | [diff] [blame] | 64 | auto data = std::make_shared<Data>(m_options.name); |
Davide Pesavento | 6a1396e | 2019-07-26 15:03:28 -0400 | [diff] [blame] | 65 | data->setFreshnessPeriod(m_options.freshnessPeriod); |
Davide Pesavento | e75861e | 2019-07-24 21:55:39 -0400 | [diff] [blame] | 66 | if (m_options.wantFinalBlockId) { |
| 67 | data->setFinalBlock(m_options.name.at(-1)); |
Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 68 | } |
| 69 | |
Davide Pesavento | e75861e | 2019-07-24 21:55:39 -0400 | [diff] [blame] | 70 | OBufferStream os; |
| 71 | os << m_input.rdbuf(); |
| 72 | data->setContent(os.buf()); |
Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 73 | |
Davide Pesavento | e75861e | 2019-07-24 21:55:39 -0400 | [diff] [blame] | 74 | m_keyChain.sign(*data, m_options.signingInfo); |
Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 75 | |
Davide Pesavento | e75861e | 2019-07-24 21:55:39 -0400 | [diff] [blame] | 76 | return data; |
Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 77 | } |
| 78 | |
Davide Pesavento | b3ae634 | 2019-07-25 21:07:16 -0400 | [diff] [blame] | 79 | void |
| 80 | NdnPoke::sendData(const Data& data) |
| 81 | { |
| 82 | m_face.put(data); |
| 83 | m_result = Result::DATA_SENT; |
| 84 | |
| 85 | if (m_options.isVerbose) { |
| 86 | std::cerr << "DATA: " << data; |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | void |
| 91 | NdnPoke::onInterest(const Interest& interest, const Data& data) |
| 92 | { |
| 93 | if (m_options.isVerbose) { |
| 94 | std::cerr << "INTEREST: " << interest << std::endl; |
| 95 | } |
| 96 | |
Davide Pesavento | c5243b4 | 2019-07-26 13:30:16 -0400 | [diff] [blame] | 97 | if (interest.matchesData(data)) { |
| 98 | m_timeoutEvent.cancel(); |
| 99 | m_registeredPrefix.cancel(); |
| 100 | sendData(data); |
| 101 | } |
| 102 | else if (m_options.isVerbose) { |
| 103 | std::cerr << "Interest cannot be satisfied" << std::endl; |
| 104 | } |
Davide Pesavento | b3ae634 | 2019-07-25 21:07:16 -0400 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | void |
| 108 | NdnPoke::onRegSuccess() |
| 109 | { |
| 110 | if (m_options.isVerbose) { |
| 111 | std::cerr << "Prefix registration successful" << std::endl; |
| 112 | } |
| 113 | |
| 114 | if (m_options.timeout) { |
| 115 | m_timeoutEvent = m_scheduler.schedule(*m_options.timeout, [this] { |
| 116 | m_result = Result::TIMEOUT; |
| 117 | m_registeredPrefix.cancel(); |
| 118 | |
| 119 | if (m_options.isVerbose) { |
| 120 | std::cerr << "TIMEOUT" << std::endl; |
| 121 | } |
| 122 | }); |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | void |
Davide Pesavento | b3570c6 | 2022-02-19 19:19:00 -0500 | [diff] [blame] | 127 | NdnPoke::onRegFailure(std::string_view reason) |
Davide Pesavento | b3ae634 | 2019-07-25 21:07:16 -0400 | [diff] [blame] | 128 | { |
| 129 | m_result = Result::PREFIX_REG_FAIL; |
| 130 | std::cerr << "Prefix registration failure (" << reason << ")" << std::endl; |
| 131 | } |
| 132 | |
Davide Pesavento | b3570c6 | 2022-02-19 19:19:00 -0500 | [diff] [blame] | 133 | } // namespace ndn::peek |