blob: 40ec2470bd7cc6d1d87bbbf9a60f83085db0a0f9 [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
30#include <ndn-cxx/security/signing-helpers.hpp>
31
32#include <sstream>
33
34namespace ndn {
35namespace peek {
36
37NdnPoke::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
46void
47NdnPoke::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 Newberry468dbdf2018-06-20 23:58:55 -070055 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 Newberry2f041d22018-06-03 18:02:31 -070059 }
60}
61
62shared_ptr<Data>
63NdnPoke::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
85void
86NdnPoke::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 Newberry468dbdf2018-06-20 23:58:55 -070095
Junxiao Shi06d008c2019-02-04 08:26:59 +000096 m_registeredPrefix.cancel();
Eric Newberry2f041d22018-06-03 18:02:31 -070097}
98
99void
100NdnPoke::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