blob: ce4119d58cddbfc888cba3d77c41c8343ea850db [file] [log] [blame]
Eric Newberry4164b8e2015-04-23 17:29:18 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Teng Liang62884862016-03-31 18:15:36 -07003 * Copyright (c) 2015-2016, 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 interest NDN interest
125 * @param data returned data
126 * @param seq ping sequence number
127 * @param sendTime time ping sent
128 */
129 void
Teng Liang62884862016-03-31 18:15:36 -0700130 onData(const Interest& interest,
131 const Data& data,
132 uint64_t seq,
133 const time::steady_clock::TimePoint& sendTime);
134
135 /**
136 * @brief Called when a Nack is received in response to a ping
137 *
138 * @param interest NDN interest
139 * @param nack returned nack
140 * @param seq ping sequence number
141 * @param sendTime time ping sent
142 */
143 void
144 onNack(const Interest& interest,
145 const lp::Nack& nack,
146 uint64_t seq,
147 const time::steady_clock::TimePoint& sendTime);
Eric Newberry4164b8e2015-04-23 17:29:18 -0700148
149 /**
Alexander Afanasyev1e7a7b22015-08-26 15:35:26 -0700150 * @brief Called when ping timed out
151 *
Eric Newberry4164b8e2015-04-23 17:29:18 -0700152 * @param interest NDN interest
153 * @param seq ping sequence number
154 */
155 void
156 onTimeout(const Interest& interest, uint64_t seq);
157
158 /**
Alexander Afanasyev1e7a7b22015-08-26 15:35:26 -0700159 * @brief Called after ping received or timed out
Eric Newberry4164b8e2015-04-23 17:29:18 -0700160 */
161 void
162 finish();
163
164private:
165 const Options& m_options;
166 int m_nSent;
167 uint64_t m_nextSeq;
168 int m_nOutstanding;
169 Face& m_face;
170 scheduler::Scheduler m_scheduler;
Alexander Afanasyev1e7a7b22015-08-26 15:35:26 -0700171 scheduler::ScopedEventId m_nextPingEvent;
Eric Newberry4164b8e2015-04-23 17:29:18 -0700172};
173
174} // namespace client
175} // namespace ping
176} // namespace ndn
177
178#endif // NDN_TOOLS_PING_CLIENT_PING_HPP