blob: 8f1ba980455e17b34054d34fbe4b6721e83e6573 [file] [log] [blame]
Eric Newberry4164b8e2015-04-23 17:29:18 -07001/* -*- 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
25namespace ndn {
26namespace ping {
27namespace client {
28
29Ping::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
42void
43Ping::run()
44{
Eric Newberrya93680e2015-07-15 19:25:29 -070045 start();
Eric Newberry4164b8e2015-04-23 17:29:18 -070046
Eric Newberrya93680e2015-07-15 19:25:29 -070047 m_face.getIoService().run();
48}
49
50void
51Ping::start()
52{
53 performPing();
Eric Newberry4164b8e2015-04-23 17:29:18 -070054}
55
56void
57Ping::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
67 m_face.expressInterest(interest,
68 bind(&Ping::onData, this, _1, _2, m_nextSeq, time::steady_clock::now()),
69 bind(&Ping::onTimeout, this, _1, m_nextSeq));
70
71 ++m_nSent;
72 ++m_nextSeq;
73 ++m_nOutstanding;
74
75 if ((m_options.nPings < 0) || (m_nSent < m_options.nPings)) {
76 m_scheduler.scheduleEvent(m_options.interval, bind(&Ping::performPing, this));
77 }
78 else {
79 finish();
80 }
81}
82
83void
84Ping::onData(const Interest& interest, Data& data, uint64_t seq, const time::steady_clock::TimePoint& sendTime)
85{
86 time::nanoseconds rtt = time::steady_clock::now() - sendTime;
87
88 afterResponse(seq, rtt);
89
90 finish();
91}
92
93void
94Ping::onTimeout(const Interest& interest, uint64_t seq)
95{
96 afterTimeout(seq);
97
98 finish();
99}
100
101void
102Ping::finish()
103{
104 if (--m_nOutstanding >= 0) {
105 return;
106 }
107
108 m_face.shutdown();
109 afterFinish();
110 m_face.getIoService().stop();
111}
112
113Name
114Ping::makePingName(uint64_t seq) const
115{
116 Name name(m_options.prefix);
117 name.append("ping");
118 if (!m_options.clientIdentifier.empty()) {
119 name.append(m_options.clientIdentifier);
120 }
121 name.append(std::to_string(seq));
122
123 return name;
124}
125
126} // namespace client
127} // namespace ping
128} // namespace ndn