Eric Newberry | 4164b8e | 2015-04-23 17:29:18 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2015, Arizona Board of Regents. |
| 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> |
| 21 | */ |
| 22 | |
| 23 | #ifndef NDN_TOOLS_PING_CLIENT_PING_HPP |
| 24 | #define NDN_TOOLS_PING_CLIENT_PING_HPP |
| 25 | |
| 26 | #include "core/common.hpp" |
| 27 | |
| 28 | namespace ndn { |
| 29 | namespace ping { |
| 30 | namespace client { |
| 31 | |
| 32 | typedef time::duration<double, time::milliseconds::period> Rtt; |
| 33 | |
| 34 | /** |
| 35 | * @brief options for ndnping client |
| 36 | */ |
| 37 | struct Options |
| 38 | { |
| 39 | Name prefix; //!< prefix pinged |
| 40 | bool shouldAllowStaleData; //!< allow stale Data |
| 41 | bool shouldGenerateRandomSeq; //!< random ping sequence |
| 42 | bool shouldPrintTimestamp; //!< print timestamp |
| 43 | int nPings; //!< number of pings |
| 44 | time::milliseconds interval; //!< ping interval |
| 45 | time::milliseconds timeout; //!< timeout threshold |
| 46 | uint64_t startSeq; //!< start ping sequence number |
| 47 | name::Component clientIdentifier; //!< client identifier |
| 48 | }; |
| 49 | |
| 50 | /** |
| 51 | * @brief NDN modular ping client |
| 52 | */ |
| 53 | class Ping : noncopyable |
| 54 | { |
| 55 | public: |
| 56 | Ping(Face& face, const Options& options); |
| 57 | |
| 58 | /** |
Alexander Afanasyev | 1e7a7b2 | 2015-08-26 15:35:26 -0700 | [diff] [blame^] | 59 | * @brief Signals on the successful return of a packet |
| 60 | * |
Eric Newberry | 4164b8e | 2015-04-23 17:29:18 -0700 | [diff] [blame] | 61 | * @param seq ping sequence number |
| 62 | * @param rtt round trip time |
| 63 | */ |
| 64 | signal::Signal<Ping, uint64_t, Rtt> afterResponse; |
| 65 | |
| 66 | /** |
Alexander Afanasyev | 1e7a7b2 | 2015-08-26 15:35:26 -0700 | [diff] [blame^] | 67 | * @brief Signals on timeout of a packet |
| 68 | * |
Eric Newberry | 4164b8e | 2015-04-23 17:29:18 -0700 | [diff] [blame] | 69 | * @param seq ping sequence number |
| 70 | */ |
| 71 | signal::Signal<Ping, uint64_t> afterTimeout; |
| 72 | |
| 73 | /** |
Alexander Afanasyev | 1e7a7b2 | 2015-08-26 15:35:26 -0700 | [diff] [blame^] | 74 | * @brief Signals when finished pinging |
Eric Newberry | 4164b8e | 2015-04-23 17:29:18 -0700 | [diff] [blame] | 75 | */ |
| 76 | signal::Signal<Ping> afterFinish; |
| 77 | |
| 78 | /** |
Alexander Afanasyev | 1e7a7b2 | 2015-08-26 15:35:26 -0700 | [diff] [blame^] | 79 | * @brief Start sending ping interests |
| 80 | * |
| 81 | * @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] | 82 | */ |
| 83 | void |
| 84 | start(); |
| 85 | |
Alexander Afanasyev | 1e7a7b2 | 2015-08-26 15:35:26 -0700 | [diff] [blame^] | 86 | /** |
| 87 | * @brief Stop sending ping interests |
| 88 | * |
| 89 | * This method cancels any future ping interests and does not affect already pending interests. |
| 90 | * |
| 91 | * @todo Cancel pending ping interest |
| 92 | */ |
| 93 | void |
| 94 | stop(); |
| 95 | |
Eric Newberry | 4164b8e | 2015-04-23 17:29:18 -0700 | [diff] [blame] | 96 | private: |
| 97 | /** |
Alexander Afanasyev | 1e7a7b2 | 2015-08-26 15:35:26 -0700 | [diff] [blame^] | 98 | * @brief Creates a ping Name from the sequence number |
| 99 | * |
Eric Newberry | 4164b8e | 2015-04-23 17:29:18 -0700 | [diff] [blame] | 100 | * @param seq ping sequence number |
| 101 | */ |
| 102 | Name |
| 103 | makePingName(uint64_t seq) const; |
| 104 | |
| 105 | /** |
Alexander Afanasyev | 1e7a7b2 | 2015-08-26 15:35:26 -0700 | [diff] [blame^] | 106 | * @brief Performs individual ping |
Eric Newberry | 4164b8e | 2015-04-23 17:29:18 -0700 | [diff] [blame] | 107 | */ |
| 108 | void |
| 109 | performPing(); |
| 110 | |
| 111 | /** |
Alexander Afanasyev | 1e7a7b2 | 2015-08-26 15:35:26 -0700 | [diff] [blame^] | 112 | * @brief Called when ping returned successfully |
| 113 | * |
Eric Newberry | 4164b8e | 2015-04-23 17:29:18 -0700 | [diff] [blame] | 114 | * @param interest NDN interest |
| 115 | * @param data returned data |
| 116 | * @param seq ping sequence number |
| 117 | * @param sendTime time ping sent |
| 118 | */ |
| 119 | void |
| 120 | onData(const Interest& interest, Data& data, uint64_t seq, const time::steady_clock::TimePoint& sendTime); |
| 121 | |
| 122 | /** |
Alexander Afanasyev | 1e7a7b2 | 2015-08-26 15:35:26 -0700 | [diff] [blame^] | 123 | * @brief Called when ping timed out |
| 124 | * |
Eric Newberry | 4164b8e | 2015-04-23 17:29:18 -0700 | [diff] [blame] | 125 | * @param interest NDN interest |
| 126 | * @param seq ping sequence number |
| 127 | */ |
| 128 | void |
| 129 | onTimeout(const Interest& interest, uint64_t seq); |
| 130 | |
| 131 | /** |
Alexander Afanasyev | 1e7a7b2 | 2015-08-26 15:35:26 -0700 | [diff] [blame^] | 132 | * @brief Called after ping received or timed out |
Eric Newberry | 4164b8e | 2015-04-23 17:29:18 -0700 | [diff] [blame] | 133 | */ |
| 134 | void |
| 135 | finish(); |
| 136 | |
| 137 | private: |
| 138 | const Options& m_options; |
| 139 | int m_nSent; |
| 140 | uint64_t m_nextSeq; |
| 141 | int m_nOutstanding; |
| 142 | Face& m_face; |
| 143 | scheduler::Scheduler m_scheduler; |
Alexander Afanasyev | 1e7a7b2 | 2015-08-26 15:35:26 -0700 | [diff] [blame^] | 144 | scheduler::ScopedEventId m_nextPingEvent; |
Eric Newberry | 4164b8e | 2015-04-23 17:29:18 -0700 | [diff] [blame] | 145 | }; |
| 146 | |
| 147 | } // namespace client |
| 148 | } // namespace ping |
| 149 | } // namespace ndn |
| 150 | |
| 151 | #endif // NDN_TOOLS_PING_CLIENT_PING_HPP |