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()) |
| 36 | { |
| 37 | if (m_options.shouldGenerateRandomSeq) { |
| 38 | m_nextSeq = random::generateWord64(); |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | void |
| 43 | Ping::run() |
| 44 | { |
| 45 | performPing(); |
| 46 | |
| 47 | m_face.processEvents(); |
| 48 | } |
| 49 | |
| 50 | void |
| 51 | Ping::performPing() |
| 52 | { |
| 53 | BOOST_ASSERT((m_options.nPings < 0) || (m_nSent < m_options.nPings)); |
| 54 | |
| 55 | Name pingPacketName = makePingName(m_nextSeq); |
| 56 | |
| 57 | Interest interest(pingPacketName); |
| 58 | interest.setMustBeFresh(!m_options.shouldAllowStaleData); |
| 59 | interest.setInterestLifetime(m_options.timeout); |
| 60 | interest.setNonce(m_nextSeq); |
| 61 | |
| 62 | m_face.expressInterest(interest, |
| 63 | bind(&Ping::onData, this, _1, _2, m_nextSeq, time::steady_clock::now()), |
| 64 | bind(&Ping::onTimeout, this, _1, m_nextSeq)); |
| 65 | |
| 66 | ++m_nSent; |
| 67 | ++m_nextSeq; |
| 68 | ++m_nOutstanding; |
| 69 | |
| 70 | if ((m_options.nPings < 0) || (m_nSent < m_options.nPings)) { |
| 71 | m_scheduler.scheduleEvent(m_options.interval, bind(&Ping::performPing, this)); |
| 72 | } |
| 73 | else { |
| 74 | finish(); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | void |
| 79 | Ping::onData(const Interest& interest, Data& data, uint64_t seq, const time::steady_clock::TimePoint& sendTime) |
| 80 | { |
| 81 | time::nanoseconds rtt = time::steady_clock::now() - sendTime; |
| 82 | |
| 83 | afterResponse(seq, rtt); |
| 84 | |
| 85 | finish(); |
| 86 | } |
| 87 | |
| 88 | void |
| 89 | Ping::onTimeout(const Interest& interest, uint64_t seq) |
| 90 | { |
| 91 | afterTimeout(seq); |
| 92 | |
| 93 | finish(); |
| 94 | } |
| 95 | |
| 96 | void |
| 97 | Ping::finish() |
| 98 | { |
| 99 | if (--m_nOutstanding >= 0) { |
| 100 | return; |
| 101 | } |
| 102 | |
| 103 | m_face.shutdown(); |
| 104 | afterFinish(); |
| 105 | m_face.getIoService().stop(); |
| 106 | } |
| 107 | |
| 108 | Name |
| 109 | Ping::makePingName(uint64_t seq) const |
| 110 | { |
| 111 | Name name(m_options.prefix); |
| 112 | name.append("ping"); |
| 113 | if (!m_options.clientIdentifier.empty()) { |
| 114 | name.append(m_options.clientIdentifier); |
| 115 | } |
| 116 | name.append(std::to_string(seq)); |
| 117 | |
| 118 | return name; |
| 119 | } |
| 120 | |
| 121 | } // namespace client |
| 122 | } // namespace ping |
| 123 | } // namespace ndn |