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 | |
| 30 | #include <ndn-cxx/security/signing-helpers.hpp> |
| 31 | |
| 32 | #include <sstream> |
| 33 | |
| 34 | namespace ndn { |
| 35 | namespace peek { |
| 36 | |
| 37 | NdnPoke::NdnPoke(Face& face, KeyChain& keyChain, std::istream& inStream, const PokeOptions& options) |
| 38 | : m_face(face) |
| 39 | , m_keyChain(keyChain) |
| 40 | , m_inStream(inStream) |
| 41 | , m_options(options) |
| 42 | , m_wasDataSent(false) |
| 43 | { |
| 44 | } |
| 45 | |
| 46 | void |
| 47 | NdnPoke::start() |
| 48 | { |
| 49 | shared_ptr<Data> dataPacket = createDataPacket(); |
| 50 | if (m_options.wantForceData) { |
| 51 | m_face.put(*dataPacket); |
| 52 | m_wasDataSent = true; |
| 53 | } |
| 54 | else { |
Eric Newberry | 468dbdf | 2018-06-20 23:58:55 -0700 | [diff] [blame] | 55 | m_registeredPrefix = m_face.setInterestFilter(m_options.prefixName, |
| 56 | bind(&NdnPoke::onInterest, this, _1, _2, dataPacket), |
| 57 | nullptr, |
| 58 | bind(&NdnPoke::onRegisterFailed, this, _1, _2)); |
Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 59 | } |
| 60 | } |
| 61 | |
| 62 | shared_ptr<Data> |
| 63 | NdnPoke::createDataPacket() |
| 64 | { |
| 65 | auto dataPacket = make_shared<Data>(m_options.prefixName); |
| 66 | |
| 67 | std::stringstream payloadStream; |
| 68 | payloadStream << m_inStream.rdbuf(); |
| 69 | std::string payload = payloadStream.str(); |
| 70 | dataPacket->setContent(reinterpret_cast<const uint8_t*>(payload.c_str()), payload.length()); |
| 71 | |
| 72 | if (m_options.freshnessPeriod) { |
| 73 | dataPacket->setFreshnessPeriod(*m_options.freshnessPeriod); |
| 74 | } |
| 75 | |
| 76 | if (m_options.wantLastAsFinalBlockId) { |
| 77 | dataPacket->setFinalBlock(m_options.prefixName.get(-1)); |
| 78 | } |
| 79 | |
| 80 | m_keyChain.sign(*dataPacket, m_options.signingInfo); |
| 81 | |
| 82 | return dataPacket; |
| 83 | } |
| 84 | |
| 85 | void |
| 86 | NdnPoke::onInterest(const Name& name, const Interest& interest, const shared_ptr<Data>& data) |
| 87 | { |
| 88 | try { |
| 89 | m_face.put(*data); |
| 90 | m_wasDataSent = true; |
| 91 | } |
| 92 | catch (const Face::OversizedPacketError& e) { |
| 93 | std::cerr << "Data exceeded maximum packet size" << std::endl; |
| 94 | } |
Eric Newberry | 468dbdf | 2018-06-20 23:58:55 -0700 | [diff] [blame] | 95 | |
Junxiao Shi | 06d008c | 2019-02-04 08:26:59 +0000 | [diff] [blame] | 96 | m_registeredPrefix.cancel(); |
Eric Newberry | 2f041d2 | 2018-06-03 18:02:31 -0700 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | void |
| 100 | NdnPoke::onRegisterFailed(const Name& prefix, const std::string& reason) |
| 101 | { |
| 102 | std::cerr << "Prefix Registration Failure. Reason = " << reason << std::endl; |
| 103 | } |
| 104 | |
| 105 | } // namespace peek |
| 106 | } // namespace ndn |