blob: 966d29ef3ad7fe33a99de43110b36cf8baaf05c0 [file] [log] [blame]
Eric Newberry4164b8e2015-04-23 17:29:18 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Teng Liang62884862016-03-31 18:15:36 -07003 * Copyright (c) 2014-2016, 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"
25
26namespace ndn {
27namespace ping {
28namespace client {
29
30Ping::Ping(Face& face, const Options& options)
31 : m_options(options)
32 , m_nSent(0)
33 , m_nextSeq(options.startSeq)
34 , m_nOutstanding(0)
35 , m_face(face)
36 , m_scheduler(m_face.getIoService())
Alexander Afanasyev1e7a7b22015-08-26 15:35:26 -070037 , m_nextPingEvent(m_scheduler)
Eric Newberry4164b8e2015-04-23 17:29:18 -070038{
39 if (m_options.shouldGenerateRandomSeq) {
40 m_nextSeq = random::generateWord64();
41 }
42}
43
44void
Eric Newberrya93680e2015-07-15 19:25:29 -070045Ping::start()
46{
47 performPing();
Eric Newberry4164b8e2015-04-23 17:29:18 -070048}
49
50void
Alexander Afanasyev1e7a7b22015-08-26 15:35:26 -070051Ping::stop()
52{
53 m_nextPingEvent.cancel();
54}
55
56void
Eric Newberry4164b8e2015-04-23 17:29:18 -070057Ping::performPing()
58{
59 BOOST_ASSERT((m_options.nPings < 0) || (m_nSent < m_options.nPings));
60
61 Name pingPacketName = makePingName(m_nextSeq);
62
63 Interest interest(pingPacketName);
64 interest.setMustBeFresh(!m_options.shouldAllowStaleData);
65 interest.setInterestLifetime(m_options.timeout);
Eric Newberry4164b8e2015-04-23 17:29:18 -070066
Teng Liang62884862016-03-31 18:15:36 -070067 auto now = time::steady_clock::now();
Eric Newberry4164b8e2015-04-23 17:29:18 -070068 m_face.expressInterest(interest,
Teng Liang62884862016-03-31 18:15:36 -070069 bind(&Ping::onData, this, _1, _2, m_nextSeq, now),
70 bind(&Ping::onNack, this, _1, _2, m_nextSeq, now),
Eric Newberry4164b8e2015-04-23 17:29:18 -070071 bind(&Ping::onTimeout, this, _1, m_nextSeq));
72
73 ++m_nSent;
74 ++m_nextSeq;
75 ++m_nOutstanding;
76
77 if ((m_options.nPings < 0) || (m_nSent < m_options.nPings)) {
Alexander Afanasyev1e7a7b22015-08-26 15:35:26 -070078 m_nextPingEvent = m_scheduler.scheduleEvent(m_options.interval, bind(&Ping::performPing, this));
Eric Newberry4164b8e2015-04-23 17:29:18 -070079 }
80 else {
81 finish();
82 }
83}
84
85void
Teng Liang62884862016-03-31 18:15:36 -070086Ping::onData(const Interest& interest,
87 const Data& data,
88 uint64_t seq,
89 const time::steady_clock::TimePoint& sendTime)
Eric Newberry4164b8e2015-04-23 17:29:18 -070090{
91 time::nanoseconds rtt = time::steady_clock::now() - sendTime;
92
Teng Liang62884862016-03-31 18:15:36 -070093 afterData(seq, rtt);
94
95 finish();
96}
97
98void
99Ping::onNack(const Interest& interest,
100 const lp::Nack& nack,
101 uint64_t seq,
102 const time::steady_clock::TimePoint& sendTime)
103{
104 time::nanoseconds rtt = time::steady_clock::now() - sendTime;
105
106 afterNack(seq, rtt, nack.getHeader());
Eric Newberry4164b8e2015-04-23 17:29:18 -0700107
108 finish();
109}
110
111void
112Ping::onTimeout(const Interest& interest, uint64_t seq)
113{
114 afterTimeout(seq);
115
116 finish();
117}
118
119void
120Ping::finish()
121{
122 if (--m_nOutstanding >= 0) {
123 return;
124 }
125
Eric Newberry4164b8e2015-04-23 17:29:18 -0700126 afterFinish();
Eric Newberry4164b8e2015-04-23 17:29:18 -0700127}
128
129Name
130Ping::makePingName(uint64_t seq) const
131{
132 Name name(m_options.prefix);
133 name.append("ping");
134 if (!m_options.clientIdentifier.empty()) {
135 name.append(m_options.clientIdentifier);
136 }
137 name.append(std::to_string(seq));
138
139 return name;
140}
141
142} // namespace client
143} // namespace ping
144} // namespace ndn