Eric Newberry | 4164b8e | 2015-04-23 17:29:18 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | aacc7da | 2019-03-15 19:42:24 -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 | 4164b8e | 2015-04-23 17:29:18 -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: Jerald Paul Abraham <jeraldabraham@email.arizona.edu> |
| 20 | * @author: Eric Newberry <enewberry@email.arizona.edu> |
Teng Liang | 6288486 | 2016-03-31 18:15:36 -0700 | [diff] [blame] | 21 | * @author: Teng Liang <philoliang@email.arizona.edu> |
Eric Newberry | 4164b8e | 2015-04-23 17:29:18 -0700 | [diff] [blame] | 22 | */ |
| 23 | |
| 24 | #ifndef NDN_TOOLS_PING_CLIENT_PING_HPP |
| 25 | #define NDN_TOOLS_PING_CLIENT_PING_HPP |
| 26 | |
| 27 | #include "core/common.hpp" |
| 28 | |
Davide Pesavento | 5748e82 | 2024-01-26 18:40:22 -0500 | [diff] [blame] | 29 | #include <ndn-cxx/face.hpp> |
| 30 | #include <ndn-cxx/util/scheduler.hpp> |
Davide Pesavento | a0e6b60 | 2021-01-21 19:47:04 -0500 | [diff] [blame] | 31 | #include <ndn-cxx/util/signal.hpp> |
| 32 | |
Davide Pesavento | b3570c6 | 2022-02-19 19:19:00 -0500 | [diff] [blame] | 33 | namespace ndn::ping::client { |
Eric Newberry | 4164b8e | 2015-04-23 17:29:18 -0700 | [diff] [blame] | 34 | |
Davide Pesavento | b3570c6 | 2022-02-19 19:19:00 -0500 | [diff] [blame] | 35 | using Rtt = time::duration<double, time::milliseconds::period>; |
Eric Newberry | 4164b8e | 2015-04-23 17:29:18 -0700 | [diff] [blame] | 36 | |
| 37 | /** |
Davide Pesavento | b3570c6 | 2022-02-19 19:19:00 -0500 | [diff] [blame] | 38 | * @brief %Options for ndnping client. |
Eric Newberry | 4164b8e | 2015-04-23 17:29:18 -0700 | [diff] [blame] | 39 | */ |
| 40 | struct Options |
| 41 | { |
| 42 | Name prefix; //!< prefix pinged |
| 43 | bool shouldAllowStaleData; //!< allow stale Data |
| 44 | bool shouldGenerateRandomSeq; //!< random ping sequence |
| 45 | bool shouldPrintTimestamp; //!< print timestamp |
| 46 | int nPings; //!< number of pings |
| 47 | time::milliseconds interval; //!< ping interval |
| 48 | time::milliseconds timeout; //!< timeout threshold |
| 49 | uint64_t startSeq; //!< start ping sequence number |
| 50 | name::Component clientIdentifier; //!< client identifier |
| 51 | }; |
| 52 | |
| 53 | /** |
| 54 | * @brief NDN modular ping client |
| 55 | */ |
| 56 | class Ping : noncopyable |
| 57 | { |
| 58 | public: |
| 59 | Ping(Face& face, const Options& options); |
| 60 | |
| 61 | /** |
Teng Liang | 6288486 | 2016-03-31 18:15:36 -0700 | [diff] [blame] | 62 | * @brief Signals on the successful return of a Data packet |
Alexander Afanasyev | 1e7a7b2 | 2015-08-26 15:35:26 -0700 | [diff] [blame] | 63 | * |
Eric Newberry | 4164b8e | 2015-04-23 17:29:18 -0700 | [diff] [blame] | 64 | * @param seq ping sequence number |
| 65 | * @param rtt round trip time |
| 66 | */ |
Junxiao Shi | 869d73e | 2023-08-10 22:52:26 +0000 | [diff] [blame] | 67 | signal::Signal<Ping, uint64_t, Rtt> afterData; |
Teng Liang | 6288486 | 2016-03-31 18:15:36 -0700 | [diff] [blame] | 68 | |
| 69 | /** |
| 70 | * @brief Signals on the return of a Nack |
| 71 | * |
| 72 | * @param seq ping sequence number |
| 73 | * @param rtt round trip time |
| 74 | * @param header the received Network NACK header |
| 75 | */ |
Junxiao Shi | 869d73e | 2023-08-10 22:52:26 +0000 | [diff] [blame] | 76 | signal::Signal<Ping, uint64_t, Rtt, lp::NackHeader> afterNack; |
Eric Newberry | 4164b8e | 2015-04-23 17:29:18 -0700 | [diff] [blame] | 77 | |
| 78 | /** |
Alexander Afanasyev | 1e7a7b2 | 2015-08-26 15:35:26 -0700 | [diff] [blame] | 79 | * @brief Signals on timeout of a packet |
| 80 | * |
Eric Newberry | 4164b8e | 2015-04-23 17:29:18 -0700 | [diff] [blame] | 81 | * @param seq ping sequence number |
| 82 | */ |
Junxiao Shi | 869d73e | 2023-08-10 22:52:26 +0000 | [diff] [blame] | 83 | signal::Signal<Ping, uint64_t> afterTimeout; |
Eric Newberry | 4164b8e | 2015-04-23 17:29:18 -0700 | [diff] [blame] | 84 | |
| 85 | /** |
Alexander Afanasyev | 1e7a7b2 | 2015-08-26 15:35:26 -0700 | [diff] [blame] | 86 | * @brief Signals when finished pinging |
Eric Newberry | 4164b8e | 2015-04-23 17:29:18 -0700 | [diff] [blame] | 87 | */ |
Junxiao Shi | 869d73e | 2023-08-10 22:52:26 +0000 | [diff] [blame] | 88 | signal::Signal<Ping> afterFinish; |
Eric Newberry | 4164b8e | 2015-04-23 17:29:18 -0700 | [diff] [blame] | 89 | |
| 90 | /** |
Alexander Afanasyev | 1e7a7b2 | 2015-08-26 15:35:26 -0700 | [diff] [blame] | 91 | * @brief Start sending ping interests |
| 92 | * |
| 93 | * @note This method is non-blocking and caller need to call face.processEvents() |
Eric Newberry | a93680e | 2015-07-15 19:25:29 -0700 | [diff] [blame] | 94 | */ |
| 95 | void |
| 96 | start(); |
| 97 | |
Alexander Afanasyev | 1e7a7b2 | 2015-08-26 15:35:26 -0700 | [diff] [blame] | 98 | /** |
| 99 | * @brief Stop sending ping interests |
| 100 | * |
| 101 | * This method cancels any future ping interests and does not affect already pending interests. |
| 102 | * |
| 103 | * @todo Cancel pending ping interest |
| 104 | */ |
| 105 | void |
| 106 | stop(); |
| 107 | |
Eric Newberry | 4164b8e | 2015-04-23 17:29:18 -0700 | [diff] [blame] | 108 | private: |
| 109 | /** |
Alexander Afanasyev | 1e7a7b2 | 2015-08-26 15:35:26 -0700 | [diff] [blame] | 110 | * @brief Creates a ping Name from the sequence number |
Eric Newberry | 4164b8e | 2015-04-23 17:29:18 -0700 | [diff] [blame] | 111 | */ |
| 112 | Name |
| 113 | makePingName(uint64_t seq) const; |
| 114 | |
| 115 | /** |
Alexander Afanasyev | 1e7a7b2 | 2015-08-26 15:35:26 -0700 | [diff] [blame] | 116 | * @brief Performs individual ping |
Eric Newberry | 4164b8e | 2015-04-23 17:29:18 -0700 | [diff] [blame] | 117 | */ |
| 118 | void |
| 119 | performPing(); |
| 120 | |
| 121 | /** |
Teng Liang | 6288486 | 2016-03-31 18:15:36 -0700 | [diff] [blame] | 122 | * @brief Called when a Data packet is received in response to a ping |
Eric Newberry | 4164b8e | 2015-04-23 17:29:18 -0700 | [diff] [blame] | 123 | */ |
| 124 | void |
Davide Pesavento | f8d9a53 | 2021-07-03 16:04:12 -0400 | [diff] [blame] | 125 | onData(uint64_t seq, const time::steady_clock::time_point& sendTime); |
Teng Liang | 6288486 | 2016-03-31 18:15:36 -0700 | [diff] [blame] | 126 | |
| 127 | /** |
| 128 | * @brief Called when a Nack is received in response to a ping |
Teng Liang | 6288486 | 2016-03-31 18:15:36 -0700 | [diff] [blame] | 129 | */ |
| 130 | void |
Davide Pesavento | f8d9a53 | 2021-07-03 16:04:12 -0400 | [diff] [blame] | 131 | onNack(uint64_t seq, const time::steady_clock::time_point& sendTime, const lp::Nack& nack); |
Eric Newberry | 4164b8e | 2015-04-23 17:29:18 -0700 | [diff] [blame] | 132 | |
| 133 | /** |
Alexander Afanasyev | 1e7a7b2 | 2015-08-26 15:35:26 -0700 | [diff] [blame] | 134 | * @brief Called when ping timed out |
Eric Newberry | 4164b8e | 2015-04-23 17:29:18 -0700 | [diff] [blame] | 135 | */ |
| 136 | void |
Davide Pesavento | bf2c517 | 2019-03-20 19:08:09 -0400 | [diff] [blame] | 137 | onTimeout(uint64_t seq); |
Eric Newberry | 4164b8e | 2015-04-23 17:29:18 -0700 | [diff] [blame] | 138 | |
| 139 | /** |
Alexander Afanasyev | 1e7a7b2 | 2015-08-26 15:35:26 -0700 | [diff] [blame] | 140 | * @brief Called after ping received or timed out |
Eric Newberry | 4164b8e | 2015-04-23 17:29:18 -0700 | [diff] [blame] | 141 | */ |
| 142 | void |
| 143 | finish(); |
| 144 | |
| 145 | private: |
| 146 | const Options& m_options; |
Davide Pesavento | b3570c6 | 2022-02-19 19:19:00 -0500 | [diff] [blame] | 147 | int m_nSent = 0; |
Eric Newberry | 4164b8e | 2015-04-23 17:29:18 -0700 | [diff] [blame] | 148 | uint64_t m_nextSeq; |
Davide Pesavento | b3570c6 | 2022-02-19 19:19:00 -0500 | [diff] [blame] | 149 | int m_nOutstanding = 0; |
Eric Newberry | 4164b8e | 2015-04-23 17:29:18 -0700 | [diff] [blame] | 150 | Face& m_face; |
Davide Pesavento | aacc7da | 2019-03-15 19:42:24 -0400 | [diff] [blame] | 151 | Scheduler m_scheduler; |
Alexander Afanasyev | 1e7a7b2 | 2015-08-26 15:35:26 -0700 | [diff] [blame] | 152 | scheduler::ScopedEventId m_nextPingEvent; |
Eric Newberry | 4164b8e | 2015-04-23 17:29:18 -0700 | [diff] [blame] | 153 | }; |
| 154 | |
Davide Pesavento | b3570c6 | 2022-02-19 19:19:00 -0500 | [diff] [blame] | 155 | } // namespace ndn::ping::client |
Eric Newberry | 4164b8e | 2015-04-23 17:29:18 -0700 | [diff] [blame] | 156 | |
| 157 | #endif // NDN_TOOLS_PING_CLIENT_PING_HPP |