blob: dbc2d45019ae9cea68006c6d549a6fabce52a1ae [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{
45 performPing();
46
47 m_face.processEvents();
48}
49
50void
51Ping::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);
Eric Newberry4164b8e2015-04-23 17:29:18 -070060
61 m_face.expressInterest(interest,
62 bind(&Ping::onData, this, _1, _2, m_nextSeq, time::steady_clock::now()),
63 bind(&Ping::onTimeout, this, _1, m_nextSeq));
64
65 ++m_nSent;
66 ++m_nextSeq;
67 ++m_nOutstanding;
68
69 if ((m_options.nPings < 0) || (m_nSent < m_options.nPings)) {
70 m_scheduler.scheduleEvent(m_options.interval, bind(&Ping::performPing, this));
71 }
72 else {
73 finish();
74 }
75}
76
77void
78Ping::onData(const Interest& interest, Data& data, uint64_t seq, const time::steady_clock::TimePoint& sendTime)
79{
80 time::nanoseconds rtt = time::steady_clock::now() - sendTime;
81
82 afterResponse(seq, rtt);
83
84 finish();
85}
86
87void
88Ping::onTimeout(const Interest& interest, uint64_t seq)
89{
90 afterTimeout(seq);
91
92 finish();
93}
94
95void
96Ping::finish()
97{
98 if (--m_nOutstanding >= 0) {
99 return;
100 }
101
102 m_face.shutdown();
103 afterFinish();
104 m_face.getIoService().stop();
105}
106
107Name
108Ping::makePingName(uint64_t seq) const
109{
110 Name name(m_options.prefix);
111 name.append("ping");
112 if (!m_options.clientIdentifier.empty()) {
113 name.append(m_options.clientIdentifier);
114 }
115 name.append(std::to_string(seq));
116
117 return name;
118}
119
120} // namespace client
121} // namespace ping
122} // namespace ndn