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> |
| 26 | */ |
| 27 | |
| 28 | #include "ndnpoke.hpp" |
| 29 | |
Davide Pesavento | e75861e | 2019-07-24 21:55:39 -0400 | [diff] [blame] | 30 | #include <ndn-cxx/encoding/buffer-stream.hpp> |
Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 31 | |
| 32 | namespace ndn { |
| 33 | namespace peek { |
| 34 | |
Davide Pesavento | e75861e | 2019-07-24 21:55:39 -0400 | [diff] [blame] | 35 | NdnPoke::NdnPoke(Face& face, KeyChain& keyChain, std::istream& input, const PokeOptions& options) |
| 36 | : m_options(options) |
| 37 | , m_face(face) |
Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 38 | , m_keyChain(keyChain) |
Davide Pesavento | e75861e | 2019-07-24 21:55:39 -0400 | [diff] [blame] | 39 | , m_input(input) |
| 40 | , m_scheduler(m_face.getIoService()) |
Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 41 | { |
| 42 | } |
| 43 | |
| 44 | void |
| 45 | NdnPoke::start() |
| 46 | { |
Davide Pesavento | e75861e | 2019-07-24 21:55:39 -0400 | [diff] [blame] | 47 | auto data = createData(); |
| 48 | |
Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 49 | if (m_options.wantForceData) { |
Davide Pesavento | e75861e | 2019-07-24 21:55:39 -0400 | [diff] [blame] | 50 | m_face.put(*data); |
Davide Pesavento | 87434be | 2019-07-25 19:04:23 -0400 | [diff] [blame] | 51 | m_result = Result::DATA_SENT; |
Davide Pesavento | e75861e | 2019-07-24 21:55:39 -0400 | [diff] [blame] | 52 | return; |
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, |
| 56 | [this, data] (auto&&...) { |
| 57 | m_timeoutEvent.cancel(); |
| 58 | m_face.put(*data); |
Davide Pesavento | 87434be | 2019-07-25 19:04:23 -0400 | [diff] [blame] | 59 | m_result = Result::DATA_SENT; |
Davide Pesavento | e75861e | 2019-07-24 21:55:39 -0400 | [diff] [blame] | 60 | m_registeredPrefix.cancel(); |
| 61 | }, |
| 62 | [this] (auto&&) { |
Davide Pesavento | de01c81 | 2019-07-24 23:46:09 -0400 | [diff] [blame^] | 63 | if (m_options.timeout) { |
| 64 | m_timeoutEvent = m_scheduler.schedule(*m_options.timeout, [this] { |
| 65 | m_result = Result::TIMEOUT; |
| 66 | m_registeredPrefix.cancel(); |
| 67 | }); |
| 68 | } |
Davide Pesavento | e75861e | 2019-07-24 21:55:39 -0400 | [diff] [blame] | 69 | }, |
Davide Pesavento | 87434be | 2019-07-25 19:04:23 -0400 | [diff] [blame] | 70 | [this] (auto&&, const auto& reason) { |
| 71 | m_result = Result::PREFIX_REG_FAIL; |
Davide Pesavento | e75861e | 2019-07-24 21:55:39 -0400 | [diff] [blame] | 72 | std::cerr << "Prefix registration failure (" << reason << ")\n"; |
| 73 | }); |
Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | shared_ptr<Data> |
Davide Pesavento | e75861e | 2019-07-24 21:55:39 -0400 | [diff] [blame] | 77 | NdnPoke::createData() const |
Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 78 | { |
Davide Pesavento | e75861e | 2019-07-24 21:55:39 -0400 | [diff] [blame] | 79 | auto data = make_shared<Data>(m_options.name); |
Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 80 | if (m_options.freshnessPeriod) { |
Davide Pesavento | e75861e | 2019-07-24 21:55:39 -0400 | [diff] [blame] | 81 | data->setFreshnessPeriod(*m_options.freshnessPeriod); |
| 82 | } |
| 83 | if (m_options.wantFinalBlockId) { |
| 84 | data->setFinalBlock(m_options.name.at(-1)); |
Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 85 | } |
| 86 | |
Davide Pesavento | e75861e | 2019-07-24 21:55:39 -0400 | [diff] [blame] | 87 | OBufferStream os; |
| 88 | os << m_input.rdbuf(); |
| 89 | data->setContent(os.buf()); |
Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 90 | |
Davide Pesavento | e75861e | 2019-07-24 21:55:39 -0400 | [diff] [blame] | 91 | m_keyChain.sign(*data, m_options.signingInfo); |
Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 92 | |
Davide Pesavento | e75861e | 2019-07-24 21:55:39 -0400 | [diff] [blame] | 93 | return data; |
Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | } // namespace peek |
| 97 | } // namespace ndn |