blob: 0a94d0f64e480e6bd275519f15109e7925cb6985 [file] [log] [blame]
Eric Newberry94996d62015-05-07 13:48:46 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesavento60f8cc12018-05-10 22:05:21 -04002/*
3 * Copyright (c) 2015-2018, Arizona Board of Regents.
Eric Newberry94996d62015-05-07 13:48:46 -07004 *
5 * This file is part of ndn-tools (Named Data Networking Essential Tools).
6 * See AUTHORS.md for complete list of ndn-tools authors and contributors.
7 *
8 * ndn-tools is free software: you can redistribute it and/or modify it under the terms
9 * of the GNU General Public License as published by the Free Software Foundation,
10 * either version 3 of the License, or (at your option) any later version.
11 *
12 * ndn-tools is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
13 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along with
17 * ndn-tools, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
18 *
19 * @author Eric Newberry <enewberry@email.arizona.edu>
20 * @author Jerald Paul Abraham <jeraldabraham@email.arizona.edu>
21 */
22
23#include "ping-server.hpp"
24
Davide Pesavento60f8cc12018-05-10 22:05:21 -040025#include <ndn-cxx/security/signing-helpers.hpp>
26
Eric Newberry94996d62015-05-07 13:48:46 -070027namespace ndn {
28namespace ping {
29namespace server {
30
Alexander Afanasyev1e7a7b22015-08-26 15:35:26 -070031PingServer::PingServer(Face& face, KeyChain& keyChain, const Options& options)
Eric Newberry94996d62015-05-07 13:48:46 -070032 : m_options(options)
Alexander Afanasyev1e7a7b22015-08-26 15:35:26 -070033 , m_keyChain(keyChain)
Eric Newberry94996d62015-05-07 13:48:46 -070034 , m_name(options.prefix)
35 , m_nPings(0)
36 , m_face(face)
Alexander Afanasyev1e7a7b22015-08-26 15:35:26 -070037 , m_registeredPrefixId(nullptr)
Eric Newberry94996d62015-05-07 13:48:46 -070038{
Eric Newberry62f7f712015-05-17 12:15:52 -070039 shared_ptr<Buffer> b = make_shared<Buffer>();
40 b->assign(m_options.payloadSize, 'a');
41 m_payload = Block(tlv::Content, b);
Eric Newberry94996d62015-05-07 13:48:46 -070042}
43
44void
Eric Newberrya93680e2015-07-15 19:25:29 -070045PingServer::start()
46{
Eric Newberry94996d62015-05-07 13:48:46 -070047 m_name.append("ping");
Alexander Afanasyev1e7a7b22015-08-26 15:35:26 -070048 m_registeredPrefixId = m_face.setInterestFilter(m_name,
49 bind(&PingServer::onInterest,
50 this, _2),
51 bind(&PingServer::onRegisterFailed,
52 this, _2));
53}
54
55void
56PingServer::stop()
57{
58 if (m_registeredPrefixId != nullptr) {
59 m_face.unsetInterestFilter(m_registeredPrefixId);
60 }
Eric Newberry94996d62015-05-07 13:48:46 -070061}
62
63int
64PingServer::getNPings() const
65{
66 return m_nPings;
67}
68
69void
70PingServer::onInterest(const Interest& interest)
71{
72 Name interestName = interest.getName();
73
74 afterReceive(interestName);
75
Eric Newberry94996d62015-05-07 13:48:46 -070076 shared_ptr<Data> data = make_shared<Data>(interestName);
77 data->setFreshnessPeriod(m_options.freshnessPeriod);
Eric Newberry62f7f712015-05-17 12:15:52 -070078 data->setContent(m_payload);
Eric Newberry38146c52015-06-22 15:11:30 -070079 m_keyChain.sign(*data, signingWithSha256());
Eric Newberry94996d62015-05-07 13:48:46 -070080 m_face.put(*data);
81
82 ++m_nPings;
83 if (m_options.shouldLimitSatisfied && m_options.nMaxPings > 0 && m_options.nMaxPings == m_nPings) {
Alexander Afanasyev1e7a7b22015-08-26 15:35:26 -070084 afterFinish();
Eric Newberry94996d62015-05-07 13:48:46 -070085 }
86}
87
88void
89PingServer::onRegisterFailed(const std::string& reason)
90{
91 throw "Failed to register prefix in local hub's daemon, REASON: " + reason;
92}
93
94} // namespace server
95} // namespace ping
Alexander Afanasyev1e7a7b22015-08-26 15:35:26 -070096} // namespace ndn