blob: f49c972585d6e1ef789759d075c5de028ef1bb9c [file] [log] [blame]
Eric Newberry4164b8e2015-04-23 17:29:18 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesaventoda85e252019-03-18 11:42:01 -04002/*
Davide Pesaventoa0e6b602021-01-21 19:47:04 -05003 * Copyright (c) 2014-2021, 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>
21 */
22
23#include "core/common.hpp"
24#include "core/version.hpp"
25
26#include "ping.hpp"
27#include "statistics-collector.hpp"
28#include "tracer.hpp"
29
Davide Pesaventoa0e6b602021-01-21 19:47:04 -050030#include <boost/asio/signal_set.hpp>
31#include <boost/program_options/options_description.hpp>
32#include <boost/program_options/parsers.hpp>
33#include <boost/program_options/variables_map.hpp>
34
Eric Newberry4164b8e2015-04-23 17:29:18 -070035namespace ndn {
36namespace ping {
37namespace client {
38
Alexander Afanasyev1e7a7b22015-08-26 15:35:26 -070039class Runner : noncopyable
40{
41public:
42 explicit
43 Runner(const Options& options)
44 : m_ping(m_face, options)
45 , m_statisticsCollector(m_ping, options)
46 , m_tracer(m_ping, options)
47 , m_signalSetInt(m_face.getIoService(), SIGINT)
48 , m_signalSetQuit(m_face.getIoService(), SIGQUIT)
49 {
Davide Pesaventof8d9a532021-07-03 16:04:12 -040050 m_signalSetInt.async_wait([this] (const auto& err, int) { onInterruptSignal(err); });
51 m_signalSetQuit.async_wait([this] (const auto& err, int) { onQuitSignal(err); });
52 m_ping.afterFinish.connect([this] { cancel(); });
Alexander Afanasyev1e7a7b22015-08-26 15:35:26 -070053 }
54
55 int
56 run()
57 {
58 try {
59 m_ping.start();
60 m_face.processEvents();
61 }
Davide Pesaventoc0702702017-08-24 22:04:00 -040062 catch (const std::exception& e) {
Alexander Afanasyev1e7a7b22015-08-26 15:35:26 -070063 m_tracer.onError(e.what());
64 return 2;
65 }
66
67 Statistics statistics = m_statisticsCollector.computeStatistics();
Davide Pesaventof8d9a532021-07-03 16:04:12 -040068 std::cout << statistics << "\n";
Alexander Afanasyev1e7a7b22015-08-26 15:35:26 -070069
70 if (statistics.nReceived == statistics.nSent) {
71 return 0;
72 }
73 else {
74 return 1;
75 }
76 }
77
78private:
79 void
80 cancel()
81 {
82 m_signalSetInt.cancel();
83 m_signalSetQuit.cancel();
84 m_ping.stop();
85 }
86
87 void
Davide Pesaventof8d9a532021-07-03 16:04:12 -040088 onInterruptSignal(const boost::system::error_code& errorCode)
Alexander Afanasyev1e7a7b22015-08-26 15:35:26 -070089 {
90 if (errorCode == boost::asio::error::operation_aborted) {
91 return;
92 }
93
94 cancel();
95 }
96
97 void
Davide Pesaventof8d9a532021-07-03 16:04:12 -040098 onQuitSignal(const boost::system::error_code& errorCode)
Alexander Afanasyev1e7a7b22015-08-26 15:35:26 -070099 {
100 if (errorCode == boost::asio::error::operation_aborted) {
101 return;
102 }
103
104 m_statisticsCollector.computeStatistics().printSummary(std::cout);
Davide Pesaventof8d9a532021-07-03 16:04:12 -0400105 m_signalSetQuit.async_wait([this] (const auto& err, int) { onQuitSignal(err); });
Davide Pesaventoc0702702017-08-24 22:04:00 -0400106 }
Alexander Afanasyev1e7a7b22015-08-26 15:35:26 -0700107
108private:
109 Face m_face;
110 Ping m_ping;
111 StatisticsCollector m_statisticsCollector;
112 Tracer m_tracer;
113
114 boost::asio::signal_set m_signalSetInt;
115 boost::asio::signal_set m_signalSetQuit;
116};
117
Eric Newberry4164b8e2015-04-23 17:29:18 -0700118static time::milliseconds
Eric Newberrya66c29b2015-05-07 17:35:43 -0700119getMinimumPingInterval()
120{
121 return time::milliseconds(1);
122}
123
124static time::milliseconds
125getDefaultPingInterval()
Eric Newberry4164b8e2015-04-23 17:29:18 -0700126{
127 return time::milliseconds(1000);
128}
129
130static time::milliseconds
131getDefaultPingTimeoutThreshold()
132{
133 return time::milliseconds(4000);
134}
135
136static void
137usage(const boost::program_options::options_description& options)
138{
139 std::cout << "Usage: ndnping [options] ndn:/name/prefix\n"
Davide Pesaventoda85e252019-03-18 11:42:01 -0400140 "\n"
141 "Ping a NDN name prefix using Interests with name ndn:/name/prefix/ping/number.\n"
142 "The numbers in the Interests are randomly generated unless specified.\n"
143 "\n"
144 << options;
Eric Newberry4164b8e2015-04-23 17:29:18 -0700145 exit(2);
146}
147
Davide Pesaventoda85e252019-03-18 11:42:01 -0400148static int
Eric Newberry4164b8e2015-04-23 17:29:18 -0700149main(int argc, char* argv[])
150{
151 Options options;
152 options.shouldAllowStaleData = false;
153 options.nPings = -1;
Eric Newberrya66c29b2015-05-07 17:35:43 -0700154 options.interval = time::milliseconds(getDefaultPingInterval());
Eric Newberry4164b8e2015-04-23 17:29:18 -0700155 options.timeout = time::milliseconds(getDefaultPingTimeoutThreshold());
156 options.startSeq = 0;
157 options.shouldGenerateRandomSeq = true;
158 options.shouldPrintTimestamp = false;
159
160 std::string identifier;
161
162 namespace po = boost::program_options;
163
Davide Pesavento0da1f442019-07-26 17:38:13 -0400164 po::options_description visibleOptDesc("Options");
Eric Newberry4164b8e2015-04-23 17:29:18 -0700165 visibleOptDesc.add_options()
Davide Pesavento0da1f442019-07-26 17:38:13 -0400166 ("help,h", "print this message and exit")
167 ("version,V", "display version and exit")
168 ("interval,i", po::value<time::milliseconds::rep>()->default_value(getDefaultPingInterval().count()),
169 "ping interval, in milliseconds")
170 ("timeout,o", po::value<time::milliseconds::rep>()->default_value(getDefaultPingTimeoutThreshold().count()),
171 "ping timeout, in milliseconds")
172 ("count,c", po::value<int>(&options.nPings), "number of pings to send (default = no limit)")
173 ("start,n", po::value<uint64_t>(&options.startSeq),
174 "set the starting sequence number, the number is incremented by 1 after each Interest")
Eric Newberry4164b8e2015-04-23 17:29:18 -0700175 ("identifier,p", po::value<std::string>(&identifier),
Davide Pesavento0da1f442019-07-26 17:38:13 -0400176 "add identifier to the Interest names before the sequence numbers to avoid conflicts")
177 ("cache,a", "allow routers to return stale Data from cache")
Eric Newberry4164b8e2015-04-23 17:29:18 -0700178 ("timestamp,t", "print timestamp with messages")
179 ;
Davide Pesavento0da1f442019-07-26 17:38:13 -0400180
181 po::options_description hiddenOptDesc;
Eric Newberry4164b8e2015-04-23 17:29:18 -0700182 hiddenOptDesc.add_options()
183 ("prefix", po::value<std::string>(), "prefix to send pings to")
184 ;
185
Davide Pesavento0da1f442019-07-26 17:38:13 -0400186 po::options_description optDesc;
Eric Newberry4164b8e2015-04-23 17:29:18 -0700187 optDesc.add(visibleOptDesc).add(hiddenOptDesc);
188
Davide Pesavento0da1f442019-07-26 17:38:13 -0400189 po::positional_options_description optPos;
190 optPos.add("prefix", -1);
Eric Newberry4164b8e2015-04-23 17:29:18 -0700191
Davide Pesavento0da1f442019-07-26 17:38:13 -0400192 try {
Eric Newberry4164b8e2015-04-23 17:29:18 -0700193 po::variables_map optVm;
194 po::store(po::command_line_parser(argc, argv).options(optDesc).positional(optPos).run(), optVm);
195 po::notify(optVm);
196
197 if (optVm.count("help") > 0) {
198 usage(visibleOptDesc);
199 }
200
201 if (optVm.count("version") > 0) {
Davide Pesaventof8d9a532021-07-03 16:04:12 -0400202 std::cout << "ndnping " << tools::VERSION << "\n";
Eric Newberry4164b8e2015-04-23 17:29:18 -0700203 exit(0);
204 }
205
206 if (optVm.count("prefix") > 0) {
207 options.prefix = Name(optVm["prefix"].as<std::string>());
208 }
209 else {
Davide Pesaventof8d9a532021-07-03 16:04:12 -0400210 std::cerr << "ERROR: No prefix specified\n";
Eric Newberry4164b8e2015-04-23 17:29:18 -0700211 usage(visibleOptDesc);
212 }
213
Davide Pesavento0da1f442019-07-26 17:38:13 -0400214 options.interval = time::milliseconds(optVm["interval"].as<time::milliseconds::rep>());
215 if (options.interval < getMinimumPingInterval()) {
Davide Pesaventof8d9a532021-07-03 16:04:12 -0400216 std::cerr << "ERROR: Specified ping interval is less than the minimum "
217 << getMinimumPingInterval() << "\n";
Davide Pesavento0da1f442019-07-26 17:38:13 -0400218 usage(visibleOptDesc);
Eric Newberry4164b8e2015-04-23 17:29:18 -0700219 }
220
Davide Pesavento0da1f442019-07-26 17:38:13 -0400221 options.timeout = time::milliseconds(optVm["timeout"].as<time::milliseconds::rep>());
Eric Newberry4164b8e2015-04-23 17:29:18 -0700222
223 if (optVm.count("count") > 0) {
224 if (options.nPings <= 0) {
Davide Pesaventof8d9a532021-07-03 16:04:12 -0400225 std::cerr << "ERROR: Number of ping must be positive\n";
Eric Newberry4164b8e2015-04-23 17:29:18 -0700226 usage(visibleOptDesc);
227 }
228 }
229
230 if (optVm.count("start") > 0) {
231 options.shouldGenerateRandomSeq = false;
232 }
233
234 if (optVm.count("identifier") > 0) {
235 bool isIdentifierAcceptable = std::all_of(identifier.begin(), identifier.end(), &isalnum);
236 if (identifier.empty() || !isIdentifierAcceptable) {
Davide Pesaventof8d9a532021-07-03 16:04:12 -0400237 std::cerr << "ERROR: Unacceptable client identifier\n";
Eric Newberry4164b8e2015-04-23 17:29:18 -0700238 usage(visibleOptDesc);
239 }
240
241 options.clientIdentifier = name::Component(identifier);
242 }
243
244 if (optVm.count("cache") > 0) {
245 options.shouldAllowStaleData = true;
246 }
247
248 if (optVm.count("timestamp") > 0) {
249 options.shouldPrintTimestamp = true;
250 }
251 }
252 catch (const po::error& e) {
Davide Pesaventof8d9a532021-07-03 16:04:12 -0400253 std::cerr << "ERROR: " << e.what() << "\n";
Eric Newberry4164b8e2015-04-23 17:29:18 -0700254 usage(visibleOptDesc);
255 }
256
Davide Pesaventof8d9a532021-07-03 16:04:12 -0400257 std::cout << "PING " << options.prefix << "\n";
Alexander Afanasyev1e7a7b22015-08-26 15:35:26 -0700258 return Runner(options).run();
Eric Newberry4164b8e2015-04-23 17:29:18 -0700259}
260
261} // namespace client
262} // namespace ping
263} // namespace ndn
264
265int
Davide Pesaventoda85e252019-03-18 11:42:01 -0400266main(int argc, char* argv[])
Eric Newberry4164b8e2015-04-23 17:29:18 -0700267{
268 return ndn::ping::client::main(argc, argv);
269}