blob: a37a4610e4cf0437bb3e7d79d7d6285155fcd1a7 [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 Pesaventob3570c62022-02-19 19:19:00 -05003 * Copyright (c) 2014-2022, 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
Davide Pesaventob3570c62022-02-19 19:19:00 -050035namespace ndn::ping::client {
Eric Newberry4164b8e2015-04-23 17:29:18 -070036
Alexander Afanasyev1e7a7b22015-08-26 15:35:26 -070037class Runner : noncopyable
38{
39public:
40 explicit
41 Runner(const Options& options)
42 : m_ping(m_face, options)
43 , m_statisticsCollector(m_ping, options)
44 , m_tracer(m_ping, options)
45 , m_signalSetInt(m_face.getIoService(), SIGINT)
46 , m_signalSetQuit(m_face.getIoService(), SIGQUIT)
47 {
Davide Pesaventof8d9a532021-07-03 16:04:12 -040048 m_signalSetInt.async_wait([this] (const auto& err, int) { onInterruptSignal(err); });
49 m_signalSetQuit.async_wait([this] (const auto& err, int) { onQuitSignal(err); });
50 m_ping.afterFinish.connect([this] { cancel(); });
Alexander Afanasyev1e7a7b22015-08-26 15:35:26 -070051 }
52
53 int
54 run()
55 {
56 try {
57 m_ping.start();
58 m_face.processEvents();
59 }
Davide Pesaventoc0702702017-08-24 22:04:00 -040060 catch (const std::exception& e) {
Alexander Afanasyev1e7a7b22015-08-26 15:35:26 -070061 m_tracer.onError(e.what());
62 return 2;
63 }
64
65 Statistics statistics = m_statisticsCollector.computeStatistics();
Davide Pesaventof8d9a532021-07-03 16:04:12 -040066 std::cout << statistics << "\n";
Alexander Afanasyev1e7a7b22015-08-26 15:35:26 -070067
68 if (statistics.nReceived == statistics.nSent) {
69 return 0;
70 }
71 else {
72 return 1;
73 }
74 }
75
76private:
77 void
78 cancel()
79 {
80 m_signalSetInt.cancel();
81 m_signalSetQuit.cancel();
82 m_ping.stop();
83 }
84
85 void
Davide Pesaventof8d9a532021-07-03 16:04:12 -040086 onInterruptSignal(const boost::system::error_code& errorCode)
Alexander Afanasyev1e7a7b22015-08-26 15:35:26 -070087 {
88 if (errorCode == boost::asio::error::operation_aborted) {
89 return;
90 }
91
92 cancel();
93 }
94
95 void
Davide Pesaventof8d9a532021-07-03 16:04:12 -040096 onQuitSignal(const boost::system::error_code& errorCode)
Alexander Afanasyev1e7a7b22015-08-26 15:35:26 -070097 {
98 if (errorCode == boost::asio::error::operation_aborted) {
99 return;
100 }
101
102 m_statisticsCollector.computeStatistics().printSummary(std::cout);
Davide Pesaventof8d9a532021-07-03 16:04:12 -0400103 m_signalSetQuit.async_wait([this] (const auto& err, int) { onQuitSignal(err); });
Davide Pesaventoc0702702017-08-24 22:04:00 -0400104 }
Alexander Afanasyev1e7a7b22015-08-26 15:35:26 -0700105
106private:
107 Face m_face;
108 Ping m_ping;
109 StatisticsCollector m_statisticsCollector;
110 Tracer m_tracer;
111
112 boost::asio::signal_set m_signalSetInt;
113 boost::asio::signal_set m_signalSetQuit;
114};
115
Davide Pesaventob3570c62022-02-19 19:19:00 -0500116[[noreturn]] static void
Eric Newberry4164b8e2015-04-23 17:29:18 -0700117usage(const boost::program_options::options_description& options)
118{
119 std::cout << "Usage: ndnping [options] ndn:/name/prefix\n"
Davide Pesaventoda85e252019-03-18 11:42:01 -0400120 "\n"
121 "Ping a NDN name prefix using Interests with name ndn:/name/prefix/ping/number.\n"
122 "The numbers in the Interests are randomly generated unless specified.\n"
123 "\n"
124 << options;
Eric Newberry4164b8e2015-04-23 17:29:18 -0700125 exit(2);
126}
127
Davide Pesaventoda85e252019-03-18 11:42:01 -0400128static int
Eric Newberry4164b8e2015-04-23 17:29:18 -0700129main(int argc, char* argv[])
130{
131 Options options;
132 options.shouldAllowStaleData = false;
133 options.nPings = -1;
Davide Pesaventob3570c62022-02-19 19:19:00 -0500134 options.interval = 1_s;
135 options.timeout = 4_s;
Eric Newberry4164b8e2015-04-23 17:29:18 -0700136 options.startSeq = 0;
137 options.shouldGenerateRandomSeq = true;
138 options.shouldPrintTimestamp = false;
139
140 std::string identifier;
141
142 namespace po = boost::program_options;
143
Davide Pesavento0da1f442019-07-26 17:38:13 -0400144 po::options_description visibleOptDesc("Options");
Eric Newberry4164b8e2015-04-23 17:29:18 -0700145 visibleOptDesc.add_options()
Davide Pesavento0da1f442019-07-26 17:38:13 -0400146 ("help,h", "print this message and exit")
147 ("version,V", "display version and exit")
Davide Pesaventob3570c62022-02-19 19:19:00 -0500148 ("interval,i", po::value<time::milliseconds::rep>()->default_value(options.interval.count()),
Davide Pesavento0da1f442019-07-26 17:38:13 -0400149 "ping interval, in milliseconds")
Davide Pesaventob3570c62022-02-19 19:19:00 -0500150 ("timeout,o", po::value<time::milliseconds::rep>()->default_value(options.timeout.count()),
Davide Pesavento0da1f442019-07-26 17:38:13 -0400151 "ping timeout, in milliseconds")
152 ("count,c", po::value<int>(&options.nPings), "number of pings to send (default = no limit)")
153 ("start,n", po::value<uint64_t>(&options.startSeq),
154 "set the starting sequence number, the number is incremented by 1 after each Interest")
Eric Newberry4164b8e2015-04-23 17:29:18 -0700155 ("identifier,p", po::value<std::string>(&identifier),
Davide Pesavento0da1f442019-07-26 17:38:13 -0400156 "add identifier to the Interest names before the sequence numbers to avoid conflicts")
157 ("cache,a", "allow routers to return stale Data from cache")
Eric Newberry4164b8e2015-04-23 17:29:18 -0700158 ("timestamp,t", "print timestamp with messages")
159 ;
Davide Pesavento0da1f442019-07-26 17:38:13 -0400160
161 po::options_description hiddenOptDesc;
Eric Newberry4164b8e2015-04-23 17:29:18 -0700162 hiddenOptDesc.add_options()
163 ("prefix", po::value<std::string>(), "prefix to send pings to")
164 ;
165
Davide Pesavento0da1f442019-07-26 17:38:13 -0400166 po::options_description optDesc;
Eric Newberry4164b8e2015-04-23 17:29:18 -0700167 optDesc.add(visibleOptDesc).add(hiddenOptDesc);
168
Davide Pesavento0da1f442019-07-26 17:38:13 -0400169 po::positional_options_description optPos;
170 optPos.add("prefix", -1);
Eric Newberry4164b8e2015-04-23 17:29:18 -0700171
Davide Pesavento0da1f442019-07-26 17:38:13 -0400172 try {
Eric Newberry4164b8e2015-04-23 17:29:18 -0700173 po::variables_map optVm;
174 po::store(po::command_line_parser(argc, argv).options(optDesc).positional(optPos).run(), optVm);
175 po::notify(optVm);
176
177 if (optVm.count("help") > 0) {
178 usage(visibleOptDesc);
179 }
180
181 if (optVm.count("version") > 0) {
Davide Pesaventof8d9a532021-07-03 16:04:12 -0400182 std::cout << "ndnping " << tools::VERSION << "\n";
Eric Newberry4164b8e2015-04-23 17:29:18 -0700183 exit(0);
184 }
185
186 if (optVm.count("prefix") > 0) {
187 options.prefix = Name(optVm["prefix"].as<std::string>());
188 }
189 else {
Davide Pesaventof8d9a532021-07-03 16:04:12 -0400190 std::cerr << "ERROR: No prefix specified\n";
Eric Newberry4164b8e2015-04-23 17:29:18 -0700191 usage(visibleOptDesc);
192 }
193
Davide Pesavento0da1f442019-07-26 17:38:13 -0400194 options.interval = time::milliseconds(optVm["interval"].as<time::milliseconds::rep>());
Davide Pesaventob3570c62022-02-19 19:19:00 -0500195 if (options.interval < 1_ms) {
196 std::cerr << "ERROR: Specified ping interval is less than the minimum (1 ms)\n";
Davide Pesavento0da1f442019-07-26 17:38:13 -0400197 usage(visibleOptDesc);
Eric Newberry4164b8e2015-04-23 17:29:18 -0700198 }
199
Davide Pesavento0da1f442019-07-26 17:38:13 -0400200 options.timeout = time::milliseconds(optVm["timeout"].as<time::milliseconds::rep>());
Eric Newberry4164b8e2015-04-23 17:29:18 -0700201
202 if (optVm.count("count") > 0) {
203 if (options.nPings <= 0) {
Davide Pesaventof8d9a532021-07-03 16:04:12 -0400204 std::cerr << "ERROR: Number of ping must be positive\n";
Eric Newberry4164b8e2015-04-23 17:29:18 -0700205 usage(visibleOptDesc);
206 }
207 }
208
209 if (optVm.count("start") > 0) {
210 options.shouldGenerateRandomSeq = false;
211 }
212
213 if (optVm.count("identifier") > 0) {
214 bool isIdentifierAcceptable = std::all_of(identifier.begin(), identifier.end(), &isalnum);
215 if (identifier.empty() || !isIdentifierAcceptable) {
Davide Pesaventof8d9a532021-07-03 16:04:12 -0400216 std::cerr << "ERROR: Unacceptable client identifier\n";
Eric Newberry4164b8e2015-04-23 17:29:18 -0700217 usage(visibleOptDesc);
218 }
219
220 options.clientIdentifier = name::Component(identifier);
221 }
222
223 if (optVm.count("cache") > 0) {
224 options.shouldAllowStaleData = true;
225 }
226
227 if (optVm.count("timestamp") > 0) {
228 options.shouldPrintTimestamp = true;
229 }
230 }
231 catch (const po::error& e) {
Davide Pesaventof8d9a532021-07-03 16:04:12 -0400232 std::cerr << "ERROR: " << e.what() << "\n";
Eric Newberry4164b8e2015-04-23 17:29:18 -0700233 usage(visibleOptDesc);
234 }
235
Davide Pesaventof8d9a532021-07-03 16:04:12 -0400236 std::cout << "PING " << options.prefix << "\n";
Alexander Afanasyev1e7a7b22015-08-26 15:35:26 -0700237 return Runner(options).run();
Eric Newberry4164b8e2015-04-23 17:29:18 -0700238}
239
Davide Pesaventob3570c62022-02-19 19:19:00 -0500240} // namespace ndn::ping::client
Eric Newberry4164b8e2015-04-23 17:29:18 -0700241
242int
Davide Pesaventoda85e252019-03-18 11:42:01 -0400243main(int argc, char* argv[])
Eric Newberry4164b8e2015-04-23 17:29:18 -0700244{
245 return ndn::ping::client::main(argc, argv);
246}