blob: 3509ac9842f3cc219ce831239a1572616deacd7c [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/*
Davide Pesavento5748e822024-01-26 18:40:22 -05003 * Copyright (c) 2015-2024, 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 Pesavento5748e822024-01-26 18:40:22 -050029#include <ndn-cxx/face.hpp>
30#include <ndn-cxx/util/scheduler.hpp>
Davide Pesaventoa0e6b602021-01-21 19:47:04 -050031#include <ndn-cxx/util/signal.hpp>
32
Davide Pesaventob3570c62022-02-19 19:19:00 -050033namespace ndn::ping::client {
Eric Newberry4164b8e2015-04-23 17:29:18 -070034
Davide Pesaventob3570c62022-02-19 19:19:00 -050035using Rtt = time::duration<double, time::milliseconds::period>;
Eric Newberry4164b8e2015-04-23 17:29:18 -070036
37/**
Davide Pesaventob3570c62022-02-19 19:19:00 -050038 * @brief %Options for ndnping client.
Eric Newberry4164b8e2015-04-23 17:29:18 -070039 */
40struct Options
41{
42 Name prefix; //!< prefix pinged
43 bool shouldAllowStaleData; //!< allow stale Data
44 bool shouldGenerateRandomSeq; //!< random ping sequence
45 bool shouldPrintTimestamp; //!< print timestamp
46 int nPings; //!< number of pings
47 time::milliseconds interval; //!< ping interval
48 time::milliseconds timeout; //!< timeout threshold
49 uint64_t startSeq; //!< start ping sequence number
50 name::Component clientIdentifier; //!< client identifier
51};
52
53/**
54 * @brief NDN modular ping client
55 */
56class Ping : noncopyable
57{
58public:
59 Ping(Face& face, const Options& options);
60
61 /**
Teng Liang62884862016-03-31 18:15:36 -070062 * @brief Signals on the successful return of a Data packet
Alexander Afanasyev1e7a7b22015-08-26 15:35:26 -070063 *
Eric Newberry4164b8e2015-04-23 17:29:18 -070064 * @param seq ping sequence number
65 * @param rtt round trip time
66 */
Junxiao Shi869d73e2023-08-10 22:52:26 +000067 signal::Signal<Ping, uint64_t, Rtt> afterData;
Teng Liang62884862016-03-31 18:15:36 -070068
69 /**
70 * @brief Signals on the return of a Nack
71 *
72 * @param seq ping sequence number
73 * @param rtt round trip time
74 * @param header the received Network NACK header
75 */
Junxiao Shi869d73e2023-08-10 22:52:26 +000076 signal::Signal<Ping, uint64_t, Rtt, lp::NackHeader> afterNack;
Eric Newberry4164b8e2015-04-23 17:29:18 -070077
78 /**
Alexander Afanasyev1e7a7b22015-08-26 15:35:26 -070079 * @brief Signals on timeout of a packet
80 *
Eric Newberry4164b8e2015-04-23 17:29:18 -070081 * @param seq ping sequence number
82 */
Junxiao Shi869d73e2023-08-10 22:52:26 +000083 signal::Signal<Ping, uint64_t> afterTimeout;
Eric Newberry4164b8e2015-04-23 17:29:18 -070084
85 /**
Alexander Afanasyev1e7a7b22015-08-26 15:35:26 -070086 * @brief Signals when finished pinging
Eric Newberry4164b8e2015-04-23 17:29:18 -070087 */
Junxiao Shi869d73e2023-08-10 22:52:26 +000088 signal::Signal<Ping> afterFinish;
Eric Newberry4164b8e2015-04-23 17:29:18 -070089
90 /**
Alexander Afanasyev1e7a7b22015-08-26 15:35:26 -070091 * @brief Start sending ping interests
92 *
93 * @note This method is non-blocking and caller need to call face.processEvents()
Eric Newberrya93680e2015-07-15 19:25:29 -070094 */
95 void
96 start();
97
Alexander Afanasyev1e7a7b22015-08-26 15:35:26 -070098 /**
99 * @brief Stop sending ping interests
100 *
101 * This method cancels any future ping interests and does not affect already pending interests.
102 *
103 * @todo Cancel pending ping interest
104 */
105 void
106 stop();
107
Eric Newberry4164b8e2015-04-23 17:29:18 -0700108private:
109 /**
Alexander Afanasyev1e7a7b22015-08-26 15:35:26 -0700110 * @brief Creates a ping Name from the sequence number
Eric Newberry4164b8e2015-04-23 17:29:18 -0700111 */
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
Eric Newberry4164b8e2015-04-23 17:29:18 -0700123 */
124 void
Davide Pesaventof8d9a532021-07-03 16:04:12 -0400125 onData(uint64_t seq, const time::steady_clock::time_point& sendTime);
Teng Liang62884862016-03-31 18:15:36 -0700126
127 /**
128 * @brief Called when a Nack is received in response to a ping
Teng Liang62884862016-03-31 18:15:36 -0700129 */
130 void
Davide Pesaventof8d9a532021-07-03 16:04:12 -0400131 onNack(uint64_t seq, const time::steady_clock::time_point& sendTime, const lp::Nack& nack);
Eric Newberry4164b8e2015-04-23 17:29:18 -0700132
133 /**
Alexander Afanasyev1e7a7b22015-08-26 15:35:26 -0700134 * @brief Called when ping timed out
Eric Newberry4164b8e2015-04-23 17:29:18 -0700135 */
136 void
Davide Pesaventobf2c5172019-03-20 19:08:09 -0400137 onTimeout(uint64_t seq);
Eric Newberry4164b8e2015-04-23 17:29:18 -0700138
139 /**
Alexander Afanasyev1e7a7b22015-08-26 15:35:26 -0700140 * @brief Called after ping received or timed out
Eric Newberry4164b8e2015-04-23 17:29:18 -0700141 */
142 void
143 finish();
144
145private:
146 const Options& m_options;
Davide Pesaventob3570c62022-02-19 19:19:00 -0500147 int m_nSent = 0;
Eric Newberry4164b8e2015-04-23 17:29:18 -0700148 uint64_t m_nextSeq;
Davide Pesaventob3570c62022-02-19 19:19:00 -0500149 int m_nOutstanding = 0;
Eric Newberry4164b8e2015-04-23 17:29:18 -0700150 Face& m_face;
Davide Pesaventoaacc7da2019-03-15 19:42:24 -0400151 Scheduler m_scheduler;
Alexander Afanasyev1e7a7b22015-08-26 15:35:26 -0700152 scheduler::ScopedEventId m_nextPingEvent;
Eric Newberry4164b8e2015-04-23 17:29:18 -0700153};
154
Davide Pesaventob3570c62022-02-19 19:19:00 -0500155} // namespace ndn::ping::client
Eric Newberry4164b8e2015-04-23 17:29:18 -0700156
157#endif // NDN_TOOLS_PING_CLIENT_PING_HPP