blob: 3bc5693207ea1abf45a210bb003ed3f2fc675534 [file] [log] [blame]
Eric Newberry4164b8e2015-04-23 17:29:18 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shi20d5a0b2018-08-14 09:26:11 -06002/*
Davide Pesavento11fc3eb2024-01-26 01:46:56 -05003 * Copyright (c) 2014-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#include "ping.hpp"
Davide Pesaventof8d9a532021-07-03 16:04:12 -040025
Junxiao Shi20b22972017-05-24 21:04:16 +000026#include <ndn-cxx/util/random.hpp>
Eric Newberry4164b8e2015-04-23 17:29:18 -070027
Davide Pesaventob3570c62022-02-19 19:19:00 -050028namespace ndn::ping::client {
Eric Newberry4164b8e2015-04-23 17:29:18 -070029
30Ping::Ping(Face& face, const Options& options)
31 : m_options(options)
Eric Newberry4164b8e2015-04-23 17:29:18 -070032 , m_nextSeq(options.startSeq)
Eric Newberry4164b8e2015-04-23 17:29:18 -070033 , m_face(face)
Davide Pesavento7e9d7e42023-11-11 15:00:03 -050034 , m_scheduler(m_face.getIoContext())
Eric Newberry4164b8e2015-04-23 17:29:18 -070035{
36 if (m_options.shouldGenerateRandomSeq) {
37 m_nextSeq = random::generateWord64();
38 }
39}
40
41void
Eric Newberrya93680e2015-07-15 19:25:29 -070042Ping::start()
43{
44 performPing();
Eric Newberry4164b8e2015-04-23 17:29:18 -070045}
46
47void
Alexander Afanasyev1e7a7b22015-08-26 15:35:26 -070048Ping::stop()
49{
50 m_nextPingEvent.cancel();
51}
52
53void
Eric Newberry4164b8e2015-04-23 17:29:18 -070054Ping::performPing()
55{
56 BOOST_ASSERT((m_options.nPings < 0) || (m_nSent < m_options.nPings));
57
Davide Pesaventobf2c5172019-03-20 19:08:09 -040058 Interest interest(makePingName(m_nextSeq));
Eric Newberry4164b8e2015-04-23 17:29:18 -070059 interest.setMustBeFresh(!m_options.shouldAllowStaleData);
60 interest.setInterestLifetime(m_options.timeout);
Eric Newberry4164b8e2015-04-23 17:29:18 -070061
Teng Liang62884862016-03-31 18:15:36 -070062 auto now = time::steady_clock::now();
Eric Newberry4164b8e2015-04-23 17:29:18 -070063 m_face.expressInterest(interest,
Davide Pesavento5748e822024-01-26 18:40:22 -050064 [this, seq = m_nextSeq, now] (auto&&...) { onData(seq, now); },
65 [this, seq = m_nextSeq, now] (auto&&, const auto& nack) { onNack(seq, now, nack); },
66 [this, seq = m_nextSeq] (auto&&...) { onTimeout(seq); });
Eric Newberry4164b8e2015-04-23 17:29:18 -070067
68 ++m_nSent;
69 ++m_nextSeq;
70 ++m_nOutstanding;
71
72 if ((m_options.nPings < 0) || (m_nSent < m_options.nPings)) {
Davide Pesaventobf2c5172019-03-20 19:08:09 -040073 m_nextPingEvent = m_scheduler.schedule(m_options.interval, [this] { performPing(); });
Eric Newberry4164b8e2015-04-23 17:29:18 -070074 }
75 else {
76 finish();
77 }
78}
79
80void
Davide Pesaventof8d9a532021-07-03 16:04:12 -040081Ping::onData(uint64_t seq, const time::steady_clock::time_point& sendTime)
Eric Newberry4164b8e2015-04-23 17:29:18 -070082{
83 time::nanoseconds rtt = time::steady_clock::now() - sendTime;
Teng Liang62884862016-03-31 18:15:36 -070084 afterData(seq, rtt);
Teng Liang62884862016-03-31 18:15:36 -070085 finish();
86}
87
88void
Davide Pesaventof8d9a532021-07-03 16:04:12 -040089Ping::onNack(uint64_t seq, const time::steady_clock::time_point& sendTime, const lp::Nack& nack)
Teng Liang62884862016-03-31 18:15:36 -070090{
91 time::nanoseconds rtt = time::steady_clock::now() - sendTime;
Teng Liang62884862016-03-31 18:15:36 -070092 afterNack(seq, rtt, nack.getHeader());
Eric Newberry4164b8e2015-04-23 17:29:18 -070093 finish();
94}
95
96void
Davide Pesaventobf2c5172019-03-20 19:08:09 -040097Ping::onTimeout(uint64_t seq)
Eric Newberry4164b8e2015-04-23 17:29:18 -070098{
99 afterTimeout(seq);
Eric Newberry4164b8e2015-04-23 17:29:18 -0700100 finish();
101}
102
103void
104Ping::finish()
105{
106 if (--m_nOutstanding >= 0) {
107 return;
108 }
Eric Newberry4164b8e2015-04-23 17:29:18 -0700109 afterFinish();
Eric Newberry4164b8e2015-04-23 17:29:18 -0700110}
111
112Name
113Ping::makePingName(uint64_t seq) const
114{
115 Name name(m_options.prefix);
116 name.append("ping");
117 if (!m_options.clientIdentifier.empty()) {
118 name.append(m_options.clientIdentifier);
119 }
Davide Pesavento11fc3eb2024-01-26 01:46:56 -0500120 name.append(std::to_string(seq));
Eric Newberry4164b8e2015-04-23 17:29:18 -0700121 return name;
122}
123
Davide Pesaventob3570c62022-02-19 19:19:00 -0500124} // namespace ndn::ping::client