blob: 49ac492c25698053643b3e5ee6c32e3c40ed4f0f [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) 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#ifndef NDN_TOOLS_PING_CLIENT_PING_HPP
24#define NDN_TOOLS_PING_CLIENT_PING_HPP
25
26#include "core/common.hpp"
27
28namespace ndn {
29namespace ping {
30namespace client {
31
32typedef time::duration<double, time::milliseconds::period> Rtt;
33
34/**
35 * @brief options for ndnping client
36 */
37struct Options
38{
39 Name prefix; //!< prefix pinged
40 bool shouldAllowStaleData; //!< allow stale Data
41 bool shouldGenerateRandomSeq; //!< random ping sequence
42 bool shouldPrintTimestamp; //!< print timestamp
43 int nPings; //!< number of pings
44 time::milliseconds interval; //!< ping interval
45 time::milliseconds timeout; //!< timeout threshold
46 uint64_t startSeq; //!< start ping sequence number
47 name::Component clientIdentifier; //!< client identifier
48};
49
50/**
51 * @brief NDN modular ping client
52 */
53class Ping : noncopyable
54{
55public:
56 Ping(Face& face, const Options& options);
57
58 /**
Alexander Afanasyev1e7a7b22015-08-26 15:35:26 -070059 * @brief Signals on the successful return of a packet
60 *
Eric Newberry4164b8e2015-04-23 17:29:18 -070061 * @param seq ping sequence number
62 * @param rtt round trip time
63 */
64 signal::Signal<Ping, uint64_t, Rtt> afterResponse;
65
66 /**
Alexander Afanasyev1e7a7b22015-08-26 15:35:26 -070067 * @brief Signals on timeout of a packet
68 *
Eric Newberry4164b8e2015-04-23 17:29:18 -070069 * @param seq ping sequence number
70 */
71 signal::Signal<Ping, uint64_t> afterTimeout;
72
73 /**
Alexander Afanasyev1e7a7b22015-08-26 15:35:26 -070074 * @brief Signals when finished pinging
Eric Newberry4164b8e2015-04-23 17:29:18 -070075 */
76 signal::Signal<Ping> afterFinish;
77
78 /**
Alexander Afanasyev1e7a7b22015-08-26 15:35:26 -070079 * @brief Start sending ping interests
80 *
81 * @note This method is non-blocking and caller need to call face.processEvents()
Eric Newberrya93680e2015-07-15 19:25:29 -070082 */
83 void
84 start();
85
Alexander Afanasyev1e7a7b22015-08-26 15:35:26 -070086 /**
87 * @brief Stop sending ping interests
88 *
89 * This method cancels any future ping interests and does not affect already pending interests.
90 *
91 * @todo Cancel pending ping interest
92 */
93 void
94 stop();
95
Eric Newberry4164b8e2015-04-23 17:29:18 -070096private:
97 /**
Alexander Afanasyev1e7a7b22015-08-26 15:35:26 -070098 * @brief Creates a ping Name from the sequence number
99 *
Eric Newberry4164b8e2015-04-23 17:29:18 -0700100 * @param seq ping sequence number
101 */
102 Name
103 makePingName(uint64_t seq) const;
104
105 /**
Alexander Afanasyev1e7a7b22015-08-26 15:35:26 -0700106 * @brief Performs individual ping
Eric Newberry4164b8e2015-04-23 17:29:18 -0700107 */
108 void
109 performPing();
110
111 /**
Alexander Afanasyev1e7a7b22015-08-26 15:35:26 -0700112 * @brief Called when ping returned successfully
113 *
Eric Newberry4164b8e2015-04-23 17:29:18 -0700114 * @param interest NDN interest
115 * @param data returned data
116 * @param seq ping sequence number
117 * @param sendTime time ping sent
118 */
119 void
120 onData(const Interest& interest, Data& data, uint64_t seq, const time::steady_clock::TimePoint& sendTime);
121
122 /**
Alexander Afanasyev1e7a7b22015-08-26 15:35:26 -0700123 * @brief Called when ping timed out
124 *
Eric Newberry4164b8e2015-04-23 17:29:18 -0700125 * @param interest NDN interest
126 * @param seq ping sequence number
127 */
128 void
129 onTimeout(const Interest& interest, uint64_t seq);
130
131 /**
Alexander Afanasyev1e7a7b22015-08-26 15:35:26 -0700132 * @brief Called after ping received or timed out
Eric Newberry4164b8e2015-04-23 17:29:18 -0700133 */
134 void
135 finish();
136
137private:
138 const Options& m_options;
139 int m_nSent;
140 uint64_t m_nextSeq;
141 int m_nOutstanding;
142 Face& m_face;
143 scheduler::Scheduler m_scheduler;
Alexander Afanasyev1e7a7b22015-08-26 15:35:26 -0700144 scheduler::ScopedEventId m_nextPingEvent;
Eric Newberry4164b8e2015-04-23 17:29:18 -0700145};
146
147} // namespace client
148} // namespace ping
149} // namespace ndn
150
151#endif // NDN_TOOLS_PING_CLIENT_PING_HPP