Eric Newberry | 94996d6 | 2015-05-07 13:48:46 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | 60f8cc1 | 2018-05-10 22:05:21 -0400 | [diff] [blame] | 2 | /* |
Davide Pesavento | 5748e82 | 2024-01-26 18:40:22 -0500 | [diff] [blame] | 3 | * Copyright (c) 2015-2024, Arizona Board of Regents. |
Eric Newberry | 94996d6 | 2015-05-07 13:48:46 -0700 | [diff] [blame] | 4 | * |
| 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 Pesavento | 60f8cc1 | 2018-05-10 22:05:21 -0400 | [diff] [blame] | 25 | #include <ndn-cxx/security/signing-helpers.hpp> |
Davide Pesavento | 5748e82 | 2024-01-26 18:40:22 -0500 | [diff] [blame] | 26 | #include <ndn-cxx/util/exception.hpp> |
Davide Pesavento | 60f8cc1 | 2018-05-10 22:05:21 -0400 | [diff] [blame] | 27 | |
Davide Pesavento | b3570c6 | 2022-02-19 19:19:00 -0500 | [diff] [blame] | 28 | namespace ndn::ping::server { |
Eric Newberry | 94996d6 | 2015-05-07 13:48:46 -0700 | [diff] [blame] | 29 | |
Alexander Afanasyev | 1e7a7b2 | 2015-08-26 15:35:26 -0700 | [diff] [blame] | 30 | PingServer::PingServer(Face& face, KeyChain& keyChain, const Options& options) |
Eric Newberry | 94996d6 | 2015-05-07 13:48:46 -0700 | [diff] [blame] | 31 | : m_options(options) |
Eric Newberry | 94996d6 | 2015-05-07 13:48:46 -0700 | [diff] [blame] | 32 | , m_face(face) |
Davide Pesavento | 5923bf8 | 2018-08-09 01:55:44 -0400 | [diff] [blame] | 33 | , m_keyChain(keyChain) |
Eric Newberry | 94996d6 | 2015-05-07 13:48:46 -0700 | [diff] [blame] | 34 | { |
Davide Pesavento | 5748e82 | 2024-01-26 18:40:22 -0500 | [diff] [blame] | 35 | auto b = std::make_shared<Buffer>(); |
Eric Newberry | 62f7f71 | 2015-05-17 12:15:52 -0700 | [diff] [blame] | 36 | b->assign(m_options.payloadSize, 'a'); |
Davide Pesavento | 5923bf8 | 2018-08-09 01:55:44 -0400 | [diff] [blame] | 37 | m_payload = Block(tlv::Content, std::move(b)); |
Eric Newberry | 94996d6 | 2015-05-07 13:48:46 -0700 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | void |
Eric Newberry | a93680e | 2015-07-15 19:25:29 -0700 | [diff] [blame] | 41 | PingServer::start() |
| 42 | { |
Davide Pesavento | f8d9a53 | 2021-07-03 16:04:12 -0400 | [diff] [blame] | 43 | 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 Afanasyev | 1e7a7b2 | 2015-08-26 15:35:26 -0700 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | void |
| 51 | PingServer::stop() |
| 52 | { |
Junxiao Shi | 06d008c | 2019-02-04 08:26:59 +0000 | [diff] [blame] | 53 | m_registeredPrefix.cancel(); |
Eric Newberry | 94996d6 | 2015-05-07 13:48:46 -0700 | [diff] [blame] | 54 | } |
| 55 | |
Davide Pesavento | 5923bf8 | 2018-08-09 01:55:44 -0400 | [diff] [blame] | 56 | size_t |
Eric Newberry | 94996d6 | 2015-05-07 13:48:46 -0700 | [diff] [blame] | 57 | PingServer::getNPings() const |
| 58 | { |
| 59 | return m_nPings; |
| 60 | } |
| 61 | |
| 62 | void |
| 63 | PingServer::onInterest(const Interest& interest) |
| 64 | { |
Davide Pesavento | 5923bf8 | 2018-08-09 01:55:44 -0400 | [diff] [blame] | 65 | afterReceive(interest.getName()); |
Eric Newberry | 94996d6 | 2015-05-07 13:48:46 -0700 | [diff] [blame] | 66 | |
Davide Pesavento | 5748e82 | 2024-01-26 18:40:22 -0500 | [diff] [blame] | 67 | auto data = std::make_shared<Data>(interest.getName()); |
Eric Newberry | 94996d6 | 2015-05-07 13:48:46 -0700 | [diff] [blame] | 68 | data->setFreshnessPeriod(m_options.freshnessPeriod); |
Eric Newberry | 62f7f71 | 2015-05-17 12:15:52 -0700 | [diff] [blame] | 69 | data->setContent(m_payload); |
Eric Newberry | 38146c5 | 2015-06-22 15:11:30 -0700 | [diff] [blame] | 70 | m_keyChain.sign(*data, signingWithSha256()); |
Eric Newberry | 94996d6 | 2015-05-07 13:48:46 -0700 | [diff] [blame] | 71 | m_face.put(*data); |
| 72 | |
| 73 | ++m_nPings; |
Davide Pesavento | 5923bf8 | 2018-08-09 01:55:44 -0400 | [diff] [blame] | 74 | if (m_options.nMaxPings > 0 && m_options.nMaxPings == m_nPings) { |
Alexander Afanasyev | 1e7a7b2 | 2015-08-26 15:35:26 -0700 | [diff] [blame] | 75 | afterFinish(); |
Eric Newberry | 94996d6 | 2015-05-07 13:48:46 -0700 | [diff] [blame] | 76 | } |
| 77 | } |
| 78 | |
Davide Pesavento | b3570c6 | 2022-02-19 19:19:00 -0500 | [diff] [blame] | 79 | } // namespace ndn::ping::server |