Eric Newberry | 4164b8e | 2015-04-23 17:29:18 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Junxiao Shi | 20d5a0b | 2018-08-14 09:26:11 -0600 | [diff] [blame] | 2 | /* |
Junxiao Shi | 7664b12 | 2019-01-23 16:45:17 +0000 | [diff] [blame] | 3 | * Copyright (c) 2014-2019, Arizona Board of Regents. |
Eric Newberry | 4164b8e | 2015-04-23 17:29:18 -0700 | [diff] [blame] | 4 | * |
| 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 Liang | 6288486 | 2016-03-31 18:15:36 -0700 | [diff] [blame] | 21 | * @author: Teng Liang <philoliang@email.arizona.edu> |
Eric Newberry | 4164b8e | 2015-04-23 17:29:18 -0700 | [diff] [blame] | 22 | */ |
| 23 | |
| 24 | #include "ping.hpp" |
Junxiao Shi | 20b2297 | 2017-05-24 21:04:16 +0000 | [diff] [blame] | 25 | #include <ndn-cxx/util/random.hpp> |
Eric Newberry | 4164b8e | 2015-04-23 17:29:18 -0700 | [diff] [blame] | 26 | |
| 27 | namespace ndn { |
| 28 | namespace ping { |
| 29 | namespace client { |
| 30 | |
| 31 | Ping::Ping(Face& face, const Options& options) |
| 32 | : m_options(options) |
| 33 | , m_nSent(0) |
| 34 | , m_nextSeq(options.startSeq) |
| 35 | , m_nOutstanding(0) |
| 36 | , m_face(face) |
| 37 | , m_scheduler(m_face.getIoService()) |
| 38 | { |
| 39 | if (m_options.shouldGenerateRandomSeq) { |
| 40 | m_nextSeq = random::generateWord64(); |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | void |
Eric Newberry | a93680e | 2015-07-15 19:25:29 -0700 | [diff] [blame] | 45 | Ping::start() |
| 46 | { |
| 47 | performPing(); |
Eric Newberry | 4164b8e | 2015-04-23 17:29:18 -0700 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | void |
Alexander Afanasyev | 1e7a7b2 | 2015-08-26 15:35:26 -0700 | [diff] [blame] | 51 | Ping::stop() |
| 52 | { |
| 53 | m_nextPingEvent.cancel(); |
| 54 | } |
| 55 | |
| 56 | void |
Eric Newberry | 4164b8e | 2015-04-23 17:29:18 -0700 | [diff] [blame] | 57 | Ping::performPing() |
| 58 | { |
| 59 | BOOST_ASSERT((m_options.nPings < 0) || (m_nSent < m_options.nPings)); |
| 60 | |
Davide Pesavento | bf2c517 | 2019-03-20 19:08:09 -0400 | [diff] [blame^] | 61 | Interest interest(makePingName(m_nextSeq)); |
Junxiao Shi | 20d5a0b | 2018-08-14 09:26:11 -0600 | [diff] [blame] | 62 | interest.setCanBePrefix(false); |
Eric Newberry | 4164b8e | 2015-04-23 17:29:18 -0700 | [diff] [blame] | 63 | interest.setMustBeFresh(!m_options.shouldAllowStaleData); |
| 64 | interest.setInterestLifetime(m_options.timeout); |
Eric Newberry | 4164b8e | 2015-04-23 17:29:18 -0700 | [diff] [blame] | 65 | |
Teng Liang | 6288486 | 2016-03-31 18:15:36 -0700 | [diff] [blame] | 66 | auto now = time::steady_clock::now(); |
Eric Newberry | 4164b8e | 2015-04-23 17:29:18 -0700 | [diff] [blame] | 67 | m_face.expressInterest(interest, |
Davide Pesavento | bf2c517 | 2019-03-20 19:08:09 -0400 | [diff] [blame^] | 68 | bind(&Ping::onData, this, m_nextSeq, now), |
| 69 | bind(&Ping::onNack, this, _2, m_nextSeq, now), |
| 70 | bind(&Ping::onTimeout, this, m_nextSeq)); |
Eric Newberry | 4164b8e | 2015-04-23 17:29:18 -0700 | [diff] [blame] | 71 | |
| 72 | ++m_nSent; |
| 73 | ++m_nextSeq; |
| 74 | ++m_nOutstanding; |
| 75 | |
| 76 | if ((m_options.nPings < 0) || (m_nSent < m_options.nPings)) { |
Davide Pesavento | bf2c517 | 2019-03-20 19:08:09 -0400 | [diff] [blame^] | 77 | m_nextPingEvent = m_scheduler.schedule(m_options.interval, [this] { performPing(); }); |
Eric Newberry | 4164b8e | 2015-04-23 17:29:18 -0700 | [diff] [blame] | 78 | } |
| 79 | else { |
| 80 | finish(); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | void |
Davide Pesavento | bf2c517 | 2019-03-20 19:08:09 -0400 | [diff] [blame^] | 85 | Ping::onData(uint64_t seq, const time::steady_clock::TimePoint& sendTime) |
Eric Newberry | 4164b8e | 2015-04-23 17:29:18 -0700 | [diff] [blame] | 86 | { |
| 87 | time::nanoseconds rtt = time::steady_clock::now() - sendTime; |
Teng Liang | 6288486 | 2016-03-31 18:15:36 -0700 | [diff] [blame] | 88 | afterData(seq, rtt); |
Teng Liang | 6288486 | 2016-03-31 18:15:36 -0700 | [diff] [blame] | 89 | finish(); |
| 90 | } |
| 91 | |
| 92 | void |
Davide Pesavento | bf2c517 | 2019-03-20 19:08:09 -0400 | [diff] [blame^] | 93 | Ping::onNack(const lp::Nack& nack, uint64_t seq, const time::steady_clock::TimePoint& sendTime) |
Teng Liang | 6288486 | 2016-03-31 18:15:36 -0700 | [diff] [blame] | 94 | { |
| 95 | time::nanoseconds rtt = time::steady_clock::now() - sendTime; |
Teng Liang | 6288486 | 2016-03-31 18:15:36 -0700 | [diff] [blame] | 96 | afterNack(seq, rtt, nack.getHeader()); |
Eric Newberry | 4164b8e | 2015-04-23 17:29:18 -0700 | [diff] [blame] | 97 | finish(); |
| 98 | } |
| 99 | |
| 100 | void |
Davide Pesavento | bf2c517 | 2019-03-20 19:08:09 -0400 | [diff] [blame^] | 101 | Ping::onTimeout(uint64_t seq) |
Eric Newberry | 4164b8e | 2015-04-23 17:29:18 -0700 | [diff] [blame] | 102 | { |
| 103 | afterTimeout(seq); |
Eric Newberry | 4164b8e | 2015-04-23 17:29:18 -0700 | [diff] [blame] | 104 | finish(); |
| 105 | } |
| 106 | |
| 107 | void |
| 108 | Ping::finish() |
| 109 | { |
| 110 | if (--m_nOutstanding >= 0) { |
| 111 | return; |
| 112 | } |
Eric Newberry | 4164b8e | 2015-04-23 17:29:18 -0700 | [diff] [blame] | 113 | afterFinish(); |
Eric Newberry | 4164b8e | 2015-04-23 17:29:18 -0700 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | Name |
| 117 | Ping::makePingName(uint64_t seq) const |
| 118 | { |
| 119 | Name name(m_options.prefix); |
| 120 | name.append("ping"); |
| 121 | if (!m_options.clientIdentifier.empty()) { |
| 122 | name.append(m_options.clientIdentifier); |
| 123 | } |
Davide Pesavento | bf2c517 | 2019-03-20 19:08:09 -0400 | [diff] [blame^] | 124 | name.append(to_string(seq)); |
Eric Newberry | 4164b8e | 2015-04-23 17:29:18 -0700 | [diff] [blame] | 125 | return name; |
| 126 | } |
| 127 | |
| 128 | } // namespace client |
| 129 | } // namespace ping |
| 130 | } // namespace ndn |