blob: ca11c2a2278c95c5e363c25f7b756e19037c0d03 [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/*
Davide Pesavento5748e822024-01-26 18:40:22 -05003 * Copyright (c) 2015-2024, 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>
Davide Pesavento5748e822024-01-26 18:40:22 -050026#include <ndn-cxx/util/exception.hpp>
Davide Pesavento60f8cc12018-05-10 22:05:21 -040027
Davide Pesaventob3570c62022-02-19 19:19:00 -050028namespace ndn::ping::server {
Eric Newberry94996d62015-05-07 13:48:46 -070029
Alexander Afanasyev1e7a7b22015-08-26 15:35:26 -070030PingServer::PingServer(Face& face, KeyChain& keyChain, const Options& options)
Eric Newberry94996d62015-05-07 13:48:46 -070031 : m_options(options)
Eric Newberry94996d62015-05-07 13:48:46 -070032 , m_face(face)
Davide Pesavento5923bf82018-08-09 01:55:44 -040033 , m_keyChain(keyChain)
Eric Newberry94996d62015-05-07 13:48:46 -070034{
Davide Pesavento5748e822024-01-26 18:40:22 -050035 auto b = std::make_shared<Buffer>();
Eric Newberry62f7f712015-05-17 12:15:52 -070036 b->assign(m_options.payloadSize, 'a');
Davide Pesavento5923bf82018-08-09 01:55:44 -040037 m_payload = Block(tlv::Content, std::move(b));
Eric Newberry94996d62015-05-07 13:48:46 -070038}
39
40void
Eric Newberrya93680e2015-07-15 19:25:29 -070041PingServer::start()
42{
Davide Pesaventof8d9a532021-07-03 16:04:12 -040043 m_registeredPrefix = m_face.setInterestFilter(Name(m_options.prefix).append("ping"),
44 [this] (const auto&, const auto& interest) { onInterest(interest); },
45 [] (const auto&, const auto& reason) {
46 NDN_THROW(std::runtime_error("Failed to register prefix: " + reason));
47 });
Alexander Afanasyev1e7a7b22015-08-26 15:35:26 -070048}
49
50void
51PingServer::stop()
52{
Junxiao Shi06d008c2019-02-04 08:26:59 +000053 m_registeredPrefix.cancel();
Eric Newberry94996d62015-05-07 13:48:46 -070054}
55
Davide Pesavento5923bf82018-08-09 01:55:44 -040056size_t
Eric Newberry94996d62015-05-07 13:48:46 -070057PingServer::getNPings() const
58{
59 return m_nPings;
60}
61
62void
63PingServer::onInterest(const Interest& interest)
64{
Davide Pesavento5923bf82018-08-09 01:55:44 -040065 afterReceive(interest.getName());
Eric Newberry94996d62015-05-07 13:48:46 -070066
Davide Pesavento5748e822024-01-26 18:40:22 -050067 auto data = std::make_shared<Data>(interest.getName());
Eric Newberry94996d62015-05-07 13:48:46 -070068 data->setFreshnessPeriod(m_options.freshnessPeriod);
Eric Newberry62f7f712015-05-17 12:15:52 -070069 data->setContent(m_payload);
Eric Newberry38146c52015-06-22 15:11:30 -070070 m_keyChain.sign(*data, signingWithSha256());
Eric Newberry94996d62015-05-07 13:48:46 -070071 m_face.put(*data);
72
73 ++m_nPings;
Davide Pesavento5923bf82018-08-09 01:55:44 -040074 if (m_options.nMaxPings > 0 && m_options.nMaxPings == m_nPings) {
Alexander Afanasyev1e7a7b22015-08-26 15:35:26 -070075 afterFinish();
Eric Newberry94996d62015-05-07 13:48:46 -070076 }
77}
78
Davide Pesaventob3570c62022-02-19 19:19:00 -050079} // namespace ndn::ping::server