blob: c6290b0ae2a02d0969bb83a7ff3e0bb09053ca5c [file] [log] [blame]
Eric Newberry4164b8e2015-04-23 17:29:18 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Junxiao Shi20b22972017-05-24 21:04:16 +00003 * Copyright (c) 2014-2017, 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"
Junxiao Shi20b22972017-05-24 21:04:16 +000025#include <ndn-cxx/util/random.hpp>
Eric Newberry4164b8e2015-04-23 17:29:18 -070026
27namespace ndn {
28namespace ping {
29namespace client {
30
31Ping::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())
Alexander Afanasyev1e7a7b22015-08-26 15:35:26 -070038 , m_nextPingEvent(m_scheduler)
Eric Newberry4164b8e2015-04-23 17:29:18 -070039{
40 if (m_options.shouldGenerateRandomSeq) {
41 m_nextSeq = random::generateWord64();
42 }
43}
44
45void
Eric Newberrya93680e2015-07-15 19:25:29 -070046Ping::start()
47{
48 performPing();
Eric Newberry4164b8e2015-04-23 17:29:18 -070049}
50
51void
Alexander Afanasyev1e7a7b22015-08-26 15:35:26 -070052Ping::stop()
53{
54 m_nextPingEvent.cancel();
55}
56
57void
Eric Newberry4164b8e2015-04-23 17:29:18 -070058Ping::performPing()
59{
60 BOOST_ASSERT((m_options.nPings < 0) || (m_nSent < m_options.nPings));
61
62 Name pingPacketName = makePingName(m_nextSeq);
63
64 Interest interest(pingPacketName);
65 interest.setMustBeFresh(!m_options.shouldAllowStaleData);
66 interest.setInterestLifetime(m_options.timeout);
Eric Newberry4164b8e2015-04-23 17:29:18 -070067
Teng Liang62884862016-03-31 18:15:36 -070068 auto now = time::steady_clock::now();
Eric Newberry4164b8e2015-04-23 17:29:18 -070069 m_face.expressInterest(interest,
Teng Liang62884862016-03-31 18:15:36 -070070 bind(&Ping::onData, this, _1, _2, m_nextSeq, now),
71 bind(&Ping::onNack, this, _1, _2, m_nextSeq, now),
Eric Newberry4164b8e2015-04-23 17:29:18 -070072 bind(&Ping::onTimeout, this, _1, m_nextSeq));
73
74 ++m_nSent;
75 ++m_nextSeq;
76 ++m_nOutstanding;
77
78 if ((m_options.nPings < 0) || (m_nSent < m_options.nPings)) {
Alexander Afanasyev1e7a7b22015-08-26 15:35:26 -070079 m_nextPingEvent = m_scheduler.scheduleEvent(m_options.interval, bind(&Ping::performPing, this));
Eric Newberry4164b8e2015-04-23 17:29:18 -070080 }
81 else {
82 finish();
83 }
84}
85
86void
Teng Liang62884862016-03-31 18:15:36 -070087Ping::onData(const Interest& interest,
88 const Data& data,
89 uint64_t seq,
90 const time::steady_clock::TimePoint& sendTime)
Eric Newberry4164b8e2015-04-23 17:29:18 -070091{
92 time::nanoseconds rtt = time::steady_clock::now() - sendTime;
93
Teng Liang62884862016-03-31 18:15:36 -070094 afterData(seq, rtt);
95
96 finish();
97}
98
99void
100Ping::onNack(const Interest& interest,
101 const lp::Nack& nack,
102 uint64_t seq,
103 const time::steady_clock::TimePoint& sendTime)
104{
105 time::nanoseconds rtt = time::steady_clock::now() - sendTime;
106
107 afterNack(seq, rtt, nack.getHeader());
Eric Newberry4164b8e2015-04-23 17:29:18 -0700108
109 finish();
110}
111
112void
113Ping::onTimeout(const Interest& interest, uint64_t seq)
114{
115 afterTimeout(seq);
116
117 finish();
118}
119
120void
121Ping::finish()
122{
123 if (--m_nOutstanding >= 0) {
124 return;
125 }
126
Eric Newberry4164b8e2015-04-23 17:29:18 -0700127 afterFinish();
Eric Newberry4164b8e2015-04-23 17:29:18 -0700128}
129
130Name
131Ping::makePingName(uint64_t seq) const
132{
133 Name name(m_options.prefix);
134 name.append("ping");
135 if (!m_options.clientIdentifier.empty()) {
136 name.append(m_options.clientIdentifier);
137 }
138 name.append(std::to_string(seq));
139
140 return name;
141}
142
143} // namespace client
144} // namespace ping
145} // namespace ndn