Eric Newberry | 4164b8e | 2015-04-23 17:29:18 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2014-2015, Arizona Board of Regents. |
| 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> |
| 21 | */ |
| 22 | |
| 23 | #include "ping.hpp" |
| 24 | |
| 25 | namespace ndn { |
| 26 | namespace ping { |
| 27 | namespace client { |
| 28 | |
| 29 | Ping::Ping(Face& face, const Options& options) |
| 30 | : m_options(options) |
| 31 | , m_nSent(0) |
| 32 | , m_nextSeq(options.startSeq) |
| 33 | , m_nOutstanding(0) |
| 34 | , m_face(face) |
| 35 | , m_scheduler(m_face.getIoService()) |
Alexander Afanasyev | 1e7a7b2 | 2015-08-26 15:35:26 -0700 | [diff] [blame] | 36 | , m_nextPingEvent(m_scheduler) |
Eric Newberry | 4164b8e | 2015-04-23 17:29:18 -0700 | [diff] [blame] | 37 | { |
| 38 | if (m_options.shouldGenerateRandomSeq) { |
| 39 | m_nextSeq = random::generateWord64(); |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | void |
Eric Newberry | a93680e | 2015-07-15 19:25:29 -0700 | [diff] [blame] | 44 | Ping::start() |
| 45 | { |
| 46 | performPing(); |
Eric Newberry | 4164b8e | 2015-04-23 17:29:18 -0700 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | void |
Alexander Afanasyev | 1e7a7b2 | 2015-08-26 15:35:26 -0700 | [diff] [blame] | 50 | Ping::stop() |
| 51 | { |
| 52 | m_nextPingEvent.cancel(); |
| 53 | } |
| 54 | |
| 55 | void |
Eric Newberry | 4164b8e | 2015-04-23 17:29:18 -0700 | [diff] [blame] | 56 | Ping::performPing() |
| 57 | { |
| 58 | BOOST_ASSERT((m_options.nPings < 0) || (m_nSent < m_options.nPings)); |
| 59 | |
| 60 | Name pingPacketName = makePingName(m_nextSeq); |
| 61 | |
| 62 | Interest interest(pingPacketName); |
| 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 | |
| 66 | m_face.expressInterest(interest, |
| 67 | bind(&Ping::onData, this, _1, _2, m_nextSeq, time::steady_clock::now()), |
| 68 | bind(&Ping::onTimeout, this, _1, m_nextSeq)); |
| 69 | |
| 70 | ++m_nSent; |
| 71 | ++m_nextSeq; |
| 72 | ++m_nOutstanding; |
| 73 | |
| 74 | if ((m_options.nPings < 0) || (m_nSent < m_options.nPings)) { |
Alexander Afanasyev | 1e7a7b2 | 2015-08-26 15:35:26 -0700 | [diff] [blame] | 75 | m_nextPingEvent = m_scheduler.scheduleEvent(m_options.interval, bind(&Ping::performPing, this)); |
Eric Newberry | 4164b8e | 2015-04-23 17:29:18 -0700 | [diff] [blame] | 76 | } |
| 77 | else { |
| 78 | finish(); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | void |
| 83 | Ping::onData(const Interest& interest, Data& data, uint64_t seq, const time::steady_clock::TimePoint& sendTime) |
| 84 | { |
| 85 | time::nanoseconds rtt = time::steady_clock::now() - sendTime; |
| 86 | |
| 87 | afterResponse(seq, rtt); |
| 88 | |
| 89 | finish(); |
| 90 | } |
| 91 | |
| 92 | void |
| 93 | Ping::onTimeout(const Interest& interest, uint64_t seq) |
| 94 | { |
| 95 | afterTimeout(seq); |
| 96 | |
| 97 | finish(); |
| 98 | } |
| 99 | |
| 100 | void |
| 101 | Ping::finish() |
| 102 | { |
| 103 | if (--m_nOutstanding >= 0) { |
| 104 | return; |
| 105 | } |
| 106 | |
Eric Newberry | 4164b8e | 2015-04-23 17:29:18 -0700 | [diff] [blame] | 107 | afterFinish(); |
Eric Newberry | 4164b8e | 2015-04-23 17:29:18 -0700 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | Name |
| 111 | Ping::makePingName(uint64_t seq) const |
| 112 | { |
| 113 | Name name(m_options.prefix); |
| 114 | name.append("ping"); |
| 115 | if (!m_options.clientIdentifier.empty()) { |
| 116 | name.append(m_options.clientIdentifier); |
| 117 | } |
| 118 | name.append(std::to_string(seq)); |
| 119 | |
| 120 | return name; |
| 121 | } |
| 122 | |
| 123 | } // namespace client |
| 124 | } // namespace ping |
| 125 | } // namespace ndn |