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 | /** |
| 59 | * Signals on the successful return of a packet |
| 60 | * @param seq ping sequence number |
| 61 | * @param rtt round trip time |
| 62 | */ |
| 63 | signal::Signal<Ping, uint64_t, Rtt> afterResponse; |
| 64 | |
| 65 | /** |
| 66 | * Signals on timeout of a packet |
| 67 | * @param seq ping sequence number |
| 68 | */ |
| 69 | signal::Signal<Ping, uint64_t> afterTimeout; |
| 70 | |
| 71 | /** |
| 72 | * Signals when finished pinging |
| 73 | */ |
| 74 | signal::Signal<Ping> afterFinish; |
| 75 | |
| 76 | /** |
Eric Newberry | a93680e | 2015-07-15 19:25:29 -0700 | [diff] [blame^] | 77 | * Runs the ping client (blocking) |
Eric Newberry | 4164b8e | 2015-04-23 17:29:18 -0700 | [diff] [blame] | 78 | */ |
| 79 | void |
| 80 | run(); |
| 81 | |
Eric Newberry | a93680e | 2015-07-15 19:25:29 -0700 | [diff] [blame^] | 82 | /** |
| 83 | * Runs the ping client (non-blocking) |
| 84 | */ |
| 85 | void |
| 86 | start(); |
| 87 | |
Eric Newberry | 4164b8e | 2015-04-23 17:29:18 -0700 | [diff] [blame] | 88 | private: |
| 89 | /** |
| 90 | * Creates a ping Name from the sequence number |
| 91 | * @param seq ping sequence number |
| 92 | */ |
| 93 | Name |
| 94 | makePingName(uint64_t seq) const; |
| 95 | |
| 96 | /** |
| 97 | * Performs individual ping |
| 98 | */ |
| 99 | void |
| 100 | performPing(); |
| 101 | |
| 102 | /** |
| 103 | * Called when ping returned successfully |
| 104 | * @param interest NDN interest |
| 105 | * @param data returned data |
| 106 | * @param seq ping sequence number |
| 107 | * @param sendTime time ping sent |
| 108 | */ |
| 109 | void |
| 110 | onData(const Interest& interest, Data& data, uint64_t seq, const time::steady_clock::TimePoint& sendTime); |
| 111 | |
| 112 | /** |
| 113 | * Called when ping timed out |
| 114 | * @param interest NDN interest |
| 115 | * @param seq ping sequence number |
| 116 | */ |
| 117 | void |
| 118 | onTimeout(const Interest& interest, uint64_t seq); |
| 119 | |
| 120 | /** |
| 121 | * Called after ping received or timed out |
| 122 | */ |
| 123 | void |
| 124 | finish(); |
| 125 | |
| 126 | private: |
| 127 | const Options& m_options; |
| 128 | int m_nSent; |
| 129 | uint64_t m_nextSeq; |
| 130 | int m_nOutstanding; |
| 131 | Face& m_face; |
| 132 | scheduler::Scheduler m_scheduler; |
| 133 | }; |
| 134 | |
| 135 | } // namespace client |
| 136 | } // namespace ping |
| 137 | } // namespace ndn |
| 138 | |
| 139 | #endif // NDN_TOOLS_PING_CLIENT_PING_HPP |