blob: b60be656a86575dd012f4741884a9878c9a81f83 [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())
Alexander Afanasyev1e7a7b22015-08-26 15:35:26 -070036 , m_nextPingEvent(m_scheduler)
Eric Newberry4164b8e2015-04-23 17:29:18 -070037{
38 if (m_options.shouldGenerateRandomSeq) {
39 m_nextSeq = random::generateWord64();
40 }
41}
42
43void
Eric Newberrya93680e2015-07-15 19:25:29 -070044Ping::start()
45{
46 performPing();
Eric Newberry4164b8e2015-04-23 17:29:18 -070047}
48
49void
Alexander Afanasyev1e7a7b22015-08-26 15:35:26 -070050Ping::stop()
51{
52 m_nextPingEvent.cancel();
53}
54
55void
Eric Newberry4164b8e2015-04-23 17:29:18 -070056Ping::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 Newberry4164b8e2015-04-23 17:29:18 -070065
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 Afanasyev1e7a7b22015-08-26 15:35:26 -070075 m_nextPingEvent = m_scheduler.scheduleEvent(m_options.interval, bind(&Ping::performPing, this));
Eric Newberry4164b8e2015-04-23 17:29:18 -070076 }
77 else {
78 finish();
79 }
80}
81
82void
83Ping::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
92void
93Ping::onTimeout(const Interest& interest, uint64_t seq)
94{
95 afterTimeout(seq);
96
97 finish();
98}
99
100void
101Ping::finish()
102{
103 if (--m_nOutstanding >= 0) {
104 return;
105 }
106
Eric Newberry4164b8e2015-04-23 17:29:18 -0700107 afterFinish();
Eric Newberry4164b8e2015-04-23 17:29:18 -0700108}
109
110Name
111Ping::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