blob: aeab96f7fb1542eb808dded68a724923d4a90096 [file] [log] [blame]
Eric Newberry4164b8e2015-04-23 17:29:18 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesaventoaacc7da2019-03-15 19:42:24 -04002/*
3 * Copyright (c) 2015-2019, Arizona Board of Regents.
Eric Newberry4164b8e2015-04-23 17:29:18 -07004 *
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 Liang62884862016-03-31 18:15:36 -070021 * @author: Teng Liang <philoliang@email.arizona.edu>
Eric Newberry4164b8e2015-04-23 17:29:18 -070022 */
23
24#ifndef NDN_TOOLS_PING_CLIENT_PING_HPP
25#define NDN_TOOLS_PING_CLIENT_PING_HPP
26
27#include "core/common.hpp"
28
29namespace ndn {
30namespace ping {
31namespace client {
32
33typedef time::duration<double, time::milliseconds::period> Rtt;
34
35/**
36 * @brief options for ndnping client
37 */
38struct 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 */
54class Ping : noncopyable
55{
56public:
57 Ping(Face& face, const Options& options);
58
59 /**
Teng Liang62884862016-03-31 18:15:36 -070060 * @brief Signals on the successful return of a Data packet
Alexander Afanasyev1e7a7b22015-08-26 15:35:26 -070061 *
Eric Newberry4164b8e2015-04-23 17:29:18 -070062 * @param seq ping sequence number
63 * @param rtt round trip time
64 */
Teng Liang62884862016-03-31 18:15:36 -070065 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 Newberry4164b8e2015-04-23 17:29:18 -070075
76 /**
Alexander Afanasyev1e7a7b22015-08-26 15:35:26 -070077 * @brief Signals on timeout of a packet
78 *
Eric Newberry4164b8e2015-04-23 17:29:18 -070079 * @param seq ping sequence number
80 */
81 signal::Signal<Ping, uint64_t> afterTimeout;
82
83 /**
Alexander Afanasyev1e7a7b22015-08-26 15:35:26 -070084 * @brief Signals when finished pinging
Eric Newberry4164b8e2015-04-23 17:29:18 -070085 */
86 signal::Signal<Ping> afterFinish;
87
88 /**
Alexander Afanasyev1e7a7b22015-08-26 15:35:26 -070089 * @brief Start sending ping interests
90 *
91 * @note This method is non-blocking and caller need to call face.processEvents()
Eric Newberrya93680e2015-07-15 19:25:29 -070092 */
93 void
94 start();
95
Alexander Afanasyev1e7a7b22015-08-26 15:35:26 -070096 /**
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 Newberry4164b8e2015-04-23 17:29:18 -0700106private:
107 /**
Alexander Afanasyev1e7a7b22015-08-26 15:35:26 -0700108 * @brief Creates a ping Name from the sequence number
109 *
Eric Newberry4164b8e2015-04-23 17:29:18 -0700110 * @param seq ping sequence number
111 */
112 Name
113 makePingName(uint64_t seq) const;
114
115 /**
Alexander Afanasyev1e7a7b22015-08-26 15:35:26 -0700116 * @brief Performs individual ping
Eric Newberry4164b8e2015-04-23 17:29:18 -0700117 */
118 void
119 performPing();
120
121 /**
Teng Liang62884862016-03-31 18:15:36 -0700122 * @brief Called when a Data packet is received in response to a ping
Alexander Afanasyev1e7a7b22015-08-26 15:35:26 -0700123 *
Eric Newberry4164b8e2015-04-23 17:29:18 -0700124 * @param seq ping sequence number
125 * @param sendTime time ping sent
126 */
127 void
Davide Pesaventobf2c5172019-03-20 19:08:09 -0400128 onData(uint64_t seq, const time::steady_clock::TimePoint& sendTime);
Teng Liang62884862016-03-31 18:15:36 -0700129
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 Pesaventobf2c5172019-03-20 19:08:09 -0400139 onNack(const lp::Nack& nack, uint64_t seq,
Teng Liang62884862016-03-31 18:15:36 -0700140 const time::steady_clock::TimePoint& sendTime);
Eric Newberry4164b8e2015-04-23 17:29:18 -0700141
142 /**
Alexander Afanasyev1e7a7b22015-08-26 15:35:26 -0700143 * @brief Called when ping timed out
144 *
Eric Newberry4164b8e2015-04-23 17:29:18 -0700145 * @param interest NDN interest
146 * @param seq ping sequence number
147 */
148 void
Davide Pesaventobf2c5172019-03-20 19:08:09 -0400149 onTimeout(uint64_t seq);
Eric Newberry4164b8e2015-04-23 17:29:18 -0700150
151 /**
Alexander Afanasyev1e7a7b22015-08-26 15:35:26 -0700152 * @brief Called after ping received or timed out
Eric Newberry4164b8e2015-04-23 17:29:18 -0700153 */
154 void
155 finish();
156
157private:
158 const Options& m_options;
159 int m_nSent;
160 uint64_t m_nextSeq;
161 int m_nOutstanding;
162 Face& m_face;
Davide Pesaventoaacc7da2019-03-15 19:42:24 -0400163 Scheduler m_scheduler;
Alexander Afanasyev1e7a7b22015-08-26 15:35:26 -0700164 scheduler::ScopedEventId m_nextPingEvent;
Eric Newberry4164b8e2015-04-23 17:29:18 -0700165};
166
167} // namespace client
168} // namespace ping
169} // namespace ndn
170
171#endif // NDN_TOOLS_PING_CLIENT_PING_HPP