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 | /* |
| 3 | * Copyright (c) 2015-2019, 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 | |
| 29 | namespace ndn { |
| 30 | namespace ping { |
| 31 | namespace client { |
| 32 | |
| 33 | typedef time::duration<double, time::milliseconds::period> Rtt; |
| 34 | |
| 35 | /** |
| 36 | * @brief options for ndnping client |
| 37 | */ |
| 38 | struct Options |
| 39 | { |
| 40 | Name prefix; //!< prefix pinged |
| 41 | bool shouldAllowStaleData; //!< allow stale Data |
| 42 | bool shouldGenerateRandomSeq; //!< random ping sequence |
| 43 | bool shouldPrintTimestamp; //!< print timestamp |
| 44 | int nPings; //!< number of pings |
| 45 | time::milliseconds interval; //!< ping interval |
| 46 | time::milliseconds timeout; //!< timeout threshold |
| 47 | uint64_t startSeq; //!< start ping sequence number |
| 48 | name::Component clientIdentifier; //!< client identifier |
| 49 | }; |
| 50 | |
| 51 | /** |
| 52 | * @brief NDN modular ping client |
| 53 | */ |
| 54 | class Ping : noncopyable |
| 55 | { |
| 56 | public: |
| 57 | Ping(Face& face, const Options& options); |
| 58 | |
| 59 | /** |
Teng Liang | 6288486 | 2016-03-31 18:15:36 -0700 | [diff] [blame] | 60 | * @brief Signals on the successful return of a Data packet |
Alexander Afanasyev | 1e7a7b2 | 2015-08-26 15:35:26 -0700 | [diff] [blame] | 61 | * |
Eric Newberry | 4164b8e | 2015-04-23 17:29:18 -0700 | [diff] [blame] | 62 | * @param seq ping sequence number |
| 63 | * @param rtt round trip time |
| 64 | */ |
Teng Liang | 6288486 | 2016-03-31 18:15:36 -0700 | [diff] [blame] | 65 | signal::Signal<Ping, uint64_t, Rtt> afterData; |
| 66 | |
| 67 | /** |
| 68 | * @brief Signals on the return of a Nack |
| 69 | * |
| 70 | * @param seq ping sequence number |
| 71 | * @param rtt round trip time |
| 72 | * @param header the received Network NACK header |
| 73 | */ |
| 74 | signal::Signal<Ping, uint64_t, Rtt, lp::NackHeader> afterNack; |
Eric Newberry | 4164b8e | 2015-04-23 17:29:18 -0700 | [diff] [blame] | 75 | |
| 76 | /** |
Alexander Afanasyev | 1e7a7b2 | 2015-08-26 15:35:26 -0700 | [diff] [blame] | 77 | * @brief Signals on timeout of a packet |
| 78 | * |
Eric Newberry | 4164b8e | 2015-04-23 17:29:18 -0700 | [diff] [blame] | 79 | * @param seq ping sequence number |
| 80 | */ |
| 81 | signal::Signal<Ping, uint64_t> afterTimeout; |
| 82 | |
| 83 | /** |
Alexander Afanasyev | 1e7a7b2 | 2015-08-26 15:35:26 -0700 | [diff] [blame] | 84 | * @brief Signals when finished pinging |
Eric Newberry | 4164b8e | 2015-04-23 17:29:18 -0700 | [diff] [blame] | 85 | */ |
| 86 | signal::Signal<Ping> afterFinish; |
| 87 | |
| 88 | /** |
Alexander Afanasyev | 1e7a7b2 | 2015-08-26 15:35:26 -0700 | [diff] [blame] | 89 | * @brief Start sending ping interests |
| 90 | * |
| 91 | * @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] | 92 | */ |
| 93 | void |
| 94 | start(); |
| 95 | |
Alexander Afanasyev | 1e7a7b2 | 2015-08-26 15:35:26 -0700 | [diff] [blame] | 96 | /** |
| 97 | * @brief Stop sending ping interests |
| 98 | * |
| 99 | * This method cancels any future ping interests and does not affect already pending interests. |
| 100 | * |
| 101 | * @todo Cancel pending ping interest |
| 102 | */ |
| 103 | void |
| 104 | stop(); |
| 105 | |
Eric Newberry | 4164b8e | 2015-04-23 17:29:18 -0700 | [diff] [blame] | 106 | private: |
| 107 | /** |
Alexander Afanasyev | 1e7a7b2 | 2015-08-26 15:35:26 -0700 | [diff] [blame] | 108 | * @brief Creates a ping Name from the sequence number |
| 109 | * |
Eric Newberry | 4164b8e | 2015-04-23 17:29:18 -0700 | [diff] [blame] | 110 | * @param seq ping sequence number |
| 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 |
Alexander Afanasyev | 1e7a7b2 | 2015-08-26 15:35:26 -0700 | [diff] [blame] | 123 | * |
Eric Newberry | 4164b8e | 2015-04-23 17:29:18 -0700 | [diff] [blame] | 124 | * @param seq ping sequence number |
| 125 | * @param sendTime time ping sent |
| 126 | */ |
| 127 | void |
Davide Pesavento | bf2c517 | 2019-03-20 19:08:09 -0400 | [diff] [blame] | 128 | onData(uint64_t seq, const time::steady_clock::TimePoint& sendTime); |
Teng Liang | 6288486 | 2016-03-31 18:15:36 -0700 | [diff] [blame] | 129 | |
| 130 | /** |
| 131 | * @brief Called when a Nack is received in response to a ping |
| 132 | * |
| 133 | * @param interest NDN interest |
| 134 | * @param nack returned nack |
| 135 | * @param seq ping sequence number |
| 136 | * @param sendTime time ping sent |
| 137 | */ |
| 138 | void |
Davide Pesavento | bf2c517 | 2019-03-20 19:08:09 -0400 | [diff] [blame] | 139 | onNack(const lp::Nack& nack, uint64_t seq, |
Teng Liang | 6288486 | 2016-03-31 18:15:36 -0700 | [diff] [blame] | 140 | const time::steady_clock::TimePoint& sendTime); |
Eric Newberry | 4164b8e | 2015-04-23 17:29:18 -0700 | [diff] [blame] | 141 | |
| 142 | /** |
Alexander Afanasyev | 1e7a7b2 | 2015-08-26 15:35:26 -0700 | [diff] [blame] | 143 | * @brief Called when ping timed out |
| 144 | * |
Eric Newberry | 4164b8e | 2015-04-23 17:29:18 -0700 | [diff] [blame] | 145 | * @param interest NDN interest |
| 146 | * @param seq ping sequence number |
| 147 | */ |
| 148 | void |
Davide Pesavento | bf2c517 | 2019-03-20 19:08:09 -0400 | [diff] [blame] | 149 | onTimeout(uint64_t seq); |
Eric Newberry | 4164b8e | 2015-04-23 17:29:18 -0700 | [diff] [blame] | 150 | |
| 151 | /** |
Alexander Afanasyev | 1e7a7b2 | 2015-08-26 15:35:26 -0700 | [diff] [blame] | 152 | * @brief Called after ping received or timed out |
Eric Newberry | 4164b8e | 2015-04-23 17:29:18 -0700 | [diff] [blame] | 153 | */ |
| 154 | void |
| 155 | finish(); |
| 156 | |
| 157 | private: |
| 158 | const Options& m_options; |
| 159 | int m_nSent; |
| 160 | uint64_t m_nextSeq; |
| 161 | int m_nOutstanding; |
| 162 | Face& m_face; |
Davide Pesavento | aacc7da | 2019-03-15 19:42:24 -0400 | [diff] [blame] | 163 | Scheduler m_scheduler; |
Alexander Afanasyev | 1e7a7b2 | 2015-08-26 15:35:26 -0700 | [diff] [blame] | 164 | scheduler::ScopedEventId m_nextPingEvent; |
Eric Newberry | 4164b8e | 2015-04-23 17:29:18 -0700 | [diff] [blame] | 165 | }; |
| 166 | |
| 167 | } // namespace client |
| 168 | } // namespace ping |
| 169 | } // namespace ndn |
| 170 | |
| 171 | #endif // NDN_TOOLS_PING_CLIENT_PING_HPP |