blob: b2e4bfde83edb0c3d37060bc1612a5b89e1cf188 [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/*
Junxiao Shi869d73e2023-08-10 22:52:26 +00003 * Copyright (c) 2015-2023, 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
Davide Pesaventoa0e6b602021-01-21 19:47:04 -050029#include <ndn-cxx/util/signal.hpp>
30
Davide Pesaventob3570c62022-02-19 19:19:00 -050031namespace ndn::ping::client {
Eric Newberry4164b8e2015-04-23 17:29:18 -070032
Davide Pesaventob3570c62022-02-19 19:19:00 -050033using Rtt = time::duration<double, time::milliseconds::period>;
Eric Newberry4164b8e2015-04-23 17:29:18 -070034
35/**
Davide Pesaventob3570c62022-02-19 19:19:00 -050036 * @brief %Options for ndnping client.
Eric Newberry4164b8e2015-04-23 17:29:18 -070037 */
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 */
Junxiao Shi869d73e2023-08-10 22:52:26 +000065 signal::Signal<Ping, uint64_t, Rtt> afterData;
Teng Liang62884862016-03-31 18:15:36 -070066
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 */
Junxiao Shi869d73e2023-08-10 22:52:26 +000074 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 */
Junxiao Shi869d73e2023-08-10 22:52:26 +000081 signal::Signal<Ping, uint64_t> afterTimeout;
Eric Newberry4164b8e2015-04-23 17:29:18 -070082
83 /**
Alexander Afanasyev1e7a7b22015-08-26 15:35:26 -070084 * @brief Signals when finished pinging
Eric Newberry4164b8e2015-04-23 17:29:18 -070085 */
Junxiao Shi869d73e2023-08-10 22:52:26 +000086 signal::Signal<Ping> afterFinish;
Eric Newberry4164b8e2015-04-23 17:29:18 -070087
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
Eric Newberry4164b8e2015-04-23 17:29:18 -0700109 */
110 Name
111 makePingName(uint64_t seq) const;
112
113 /**
Alexander Afanasyev1e7a7b22015-08-26 15:35:26 -0700114 * @brief Performs individual ping
Eric Newberry4164b8e2015-04-23 17:29:18 -0700115 */
116 void
117 performPing();
118
119 /**
Teng Liang62884862016-03-31 18:15:36 -0700120 * @brief Called when a Data packet is received in response to a ping
Eric Newberry4164b8e2015-04-23 17:29:18 -0700121 */
122 void
Davide Pesaventof8d9a532021-07-03 16:04:12 -0400123 onData(uint64_t seq, const time::steady_clock::time_point& sendTime);
Teng Liang62884862016-03-31 18:15:36 -0700124
125 /**
126 * @brief Called when a Nack is received in response to a ping
Teng Liang62884862016-03-31 18:15:36 -0700127 */
128 void
Davide Pesaventof8d9a532021-07-03 16:04:12 -0400129 onNack(uint64_t seq, const time::steady_clock::time_point& sendTime, const lp::Nack& nack);
Eric Newberry4164b8e2015-04-23 17:29:18 -0700130
131 /**
Alexander Afanasyev1e7a7b22015-08-26 15:35:26 -0700132 * @brief Called when ping timed out
Eric Newberry4164b8e2015-04-23 17:29:18 -0700133 */
134 void
Davide Pesaventobf2c5172019-03-20 19:08:09 -0400135 onTimeout(uint64_t seq);
Eric Newberry4164b8e2015-04-23 17:29:18 -0700136
137 /**
Alexander Afanasyev1e7a7b22015-08-26 15:35:26 -0700138 * @brief Called after ping received or timed out
Eric Newberry4164b8e2015-04-23 17:29:18 -0700139 */
140 void
141 finish();
142
143private:
144 const Options& m_options;
Davide Pesaventob3570c62022-02-19 19:19:00 -0500145 int m_nSent = 0;
Eric Newberry4164b8e2015-04-23 17:29:18 -0700146 uint64_t m_nextSeq;
Davide Pesaventob3570c62022-02-19 19:19:00 -0500147 int m_nOutstanding = 0;
Eric Newberry4164b8e2015-04-23 17:29:18 -0700148 Face& m_face;
Davide Pesaventoaacc7da2019-03-15 19:42:24 -0400149 Scheduler m_scheduler;
Alexander Afanasyev1e7a7b22015-08-26 15:35:26 -0700150 scheduler::ScopedEventId m_nextPingEvent;
Eric Newberry4164b8e2015-04-23 17:29:18 -0700151};
152
Davide Pesaventob3570c62022-02-19 19:19:00 -0500153} // namespace ndn::ping::client
Eric Newberry4164b8e2015-04-23 17:29:18 -0700154
155#endif // NDN_TOOLS_PING_CLIENT_PING_HPP