Eric Newberry | 4164b8e | 2015-04-23 17:29:18 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | da85e25 | 2019-03-18 11:42:01 -0400 | [diff] [blame] | 2 | /* |
Davide Pesavento | 5748e82 | 2024-01-26 18:40:22 -0500 | [diff] [blame] | 3 | * Copyright (c) 2014-2024, Arizona Board of Regents. |
Eric Newberry | 4164b8e | 2015-04-23 17:29:18 -0700 | [diff] [blame] | 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 "core/common.hpp" |
| 24 | #include "core/version.hpp" |
| 25 | |
| 26 | #include "ping.hpp" |
| 27 | #include "statistics-collector.hpp" |
| 28 | #include "tracer.hpp" |
| 29 | |
Davide Pesavento | a0e6b60 | 2021-01-21 19:47:04 -0500 | [diff] [blame] | 30 | #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 Pesavento | 5748e82 | 2024-01-26 18:40:22 -0500 | [diff] [blame] | 35 | #include <iostream> |
| 36 | |
Davide Pesavento | b3570c6 | 2022-02-19 19:19:00 -0500 | [diff] [blame] | 37 | namespace ndn::ping::client { |
Eric Newberry | 4164b8e | 2015-04-23 17:29:18 -0700 | [diff] [blame] | 38 | |
Alexander Afanasyev | 1e7a7b2 | 2015-08-26 15:35:26 -0700 | [diff] [blame] | 39 | class Runner : noncopyable |
| 40 | { |
| 41 | public: |
| 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) |
Davide Pesavento | 7e9d7e4 | 2023-11-11 15:00:03 -0500 | [diff] [blame] | 47 | , m_signalSetInt(m_face.getIoContext(), SIGINT) |
| 48 | , m_signalSetQuit(m_face.getIoContext(), SIGQUIT) |
Alexander Afanasyev | 1e7a7b2 | 2015-08-26 15:35:26 -0700 | [diff] [blame] | 49 | { |
Davide Pesavento | f8d9a53 | 2021-07-03 16:04:12 -0400 | [diff] [blame] | 50 | 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 Afanasyev | 1e7a7b2 | 2015-08-26 15:35:26 -0700 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | int |
| 56 | run() |
| 57 | { |
| 58 | try { |
| 59 | m_ping.start(); |
| 60 | m_face.processEvents(); |
| 61 | } |
Davide Pesavento | c070270 | 2017-08-24 22:04:00 -0400 | [diff] [blame] | 62 | catch (const std::exception& e) { |
Alexander Afanasyev | 1e7a7b2 | 2015-08-26 15:35:26 -0700 | [diff] [blame] | 63 | m_tracer.onError(e.what()); |
| 64 | return 2; |
| 65 | } |
| 66 | |
Davide Pesavento | c8625bb | 2024-02-12 15:06:31 -0500 | [diff] [blame^] | 67 | auto statistics = m_statisticsCollector.computeStatistics(); |
| 68 | statistics.printFull(std::cout); |
Alexander Afanasyev | 1e7a7b2 | 2015-08-26 15:35:26 -0700 | [diff] [blame] | 69 | |
| 70 | if (statistics.nReceived == statistics.nSent) { |
| 71 | return 0; |
| 72 | } |
| 73 | else { |
| 74 | return 1; |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | private: |
| 79 | void |
| 80 | cancel() |
| 81 | { |
| 82 | m_signalSetInt.cancel(); |
| 83 | m_signalSetQuit.cancel(); |
| 84 | m_ping.stop(); |
| 85 | } |
| 86 | |
| 87 | void |
Davide Pesavento | f8d9a53 | 2021-07-03 16:04:12 -0400 | [diff] [blame] | 88 | onInterruptSignal(const boost::system::error_code& errorCode) |
Alexander Afanasyev | 1e7a7b2 | 2015-08-26 15:35:26 -0700 | [diff] [blame] | 89 | { |
| 90 | if (errorCode == boost::asio::error::operation_aborted) { |
| 91 | return; |
| 92 | } |
| 93 | |
| 94 | cancel(); |
| 95 | } |
| 96 | |
| 97 | void |
Davide Pesavento | f8d9a53 | 2021-07-03 16:04:12 -0400 | [diff] [blame] | 98 | onQuitSignal(const boost::system::error_code& errorCode) |
Alexander Afanasyev | 1e7a7b2 | 2015-08-26 15:35:26 -0700 | [diff] [blame] | 99 | { |
| 100 | if (errorCode == boost::asio::error::operation_aborted) { |
| 101 | return; |
| 102 | } |
| 103 | |
| 104 | m_statisticsCollector.computeStatistics().printSummary(std::cout); |
Davide Pesavento | c8625bb | 2024-02-12 15:06:31 -0500 | [diff] [blame^] | 105 | |
Davide Pesavento | f8d9a53 | 2021-07-03 16:04:12 -0400 | [diff] [blame] | 106 | m_signalSetQuit.async_wait([this] (const auto& err, int) { onQuitSignal(err); }); |
Davide Pesavento | c070270 | 2017-08-24 22:04:00 -0400 | [diff] [blame] | 107 | } |
Alexander Afanasyev | 1e7a7b2 | 2015-08-26 15:35:26 -0700 | [diff] [blame] | 108 | |
| 109 | private: |
| 110 | Face m_face; |
| 111 | Ping m_ping; |
| 112 | StatisticsCollector m_statisticsCollector; |
| 113 | Tracer m_tracer; |
| 114 | |
| 115 | boost::asio::signal_set m_signalSetInt; |
| 116 | boost::asio::signal_set m_signalSetQuit; |
| 117 | }; |
| 118 | |
Davide Pesavento | b3570c6 | 2022-02-19 19:19:00 -0500 | [diff] [blame] | 119 | [[noreturn]] static void |
Eric Newberry | 4164b8e | 2015-04-23 17:29:18 -0700 | [diff] [blame] | 120 | usage(const boost::program_options::options_description& options) |
| 121 | { |
| 122 | std::cout << "Usage: ndnping [options] ndn:/name/prefix\n" |
Davide Pesavento | da85e25 | 2019-03-18 11:42:01 -0400 | [diff] [blame] | 123 | "\n" |
| 124 | "Ping a NDN name prefix using Interests with name ndn:/name/prefix/ping/number.\n" |
| 125 | "The numbers in the Interests are randomly generated unless specified.\n" |
| 126 | "\n" |
| 127 | << options; |
Eric Newberry | 4164b8e | 2015-04-23 17:29:18 -0700 | [diff] [blame] | 128 | exit(2); |
| 129 | } |
| 130 | |
Davide Pesavento | da85e25 | 2019-03-18 11:42:01 -0400 | [diff] [blame] | 131 | static int |
Eric Newberry | 4164b8e | 2015-04-23 17:29:18 -0700 | [diff] [blame] | 132 | main(int argc, char* argv[]) |
| 133 | { |
| 134 | Options options; |
| 135 | options.shouldAllowStaleData = false; |
| 136 | options.nPings = -1; |
Davide Pesavento | b3570c6 | 2022-02-19 19:19:00 -0500 | [diff] [blame] | 137 | options.interval = 1_s; |
| 138 | options.timeout = 4_s; |
Eric Newberry | 4164b8e | 2015-04-23 17:29:18 -0700 | [diff] [blame] | 139 | options.startSeq = 0; |
| 140 | options.shouldGenerateRandomSeq = true; |
| 141 | options.shouldPrintTimestamp = false; |
| 142 | |
| 143 | std::string identifier; |
| 144 | |
| 145 | namespace po = boost::program_options; |
| 146 | |
Davide Pesavento | 0da1f44 | 2019-07-26 17:38:13 -0400 | [diff] [blame] | 147 | po::options_description visibleOptDesc("Options"); |
Eric Newberry | 4164b8e | 2015-04-23 17:29:18 -0700 | [diff] [blame] | 148 | visibleOptDesc.add_options() |
Davide Pesavento | 0da1f44 | 2019-07-26 17:38:13 -0400 | [diff] [blame] | 149 | ("help,h", "print this message and exit") |
| 150 | ("version,V", "display version and exit") |
Davide Pesavento | b3570c6 | 2022-02-19 19:19:00 -0500 | [diff] [blame] | 151 | ("interval,i", po::value<time::milliseconds::rep>()->default_value(options.interval.count()), |
Davide Pesavento | 0da1f44 | 2019-07-26 17:38:13 -0400 | [diff] [blame] | 152 | "ping interval, in milliseconds") |
Davide Pesavento | b3570c6 | 2022-02-19 19:19:00 -0500 | [diff] [blame] | 153 | ("timeout,o", po::value<time::milliseconds::rep>()->default_value(options.timeout.count()), |
Davide Pesavento | 0da1f44 | 2019-07-26 17:38:13 -0400 | [diff] [blame] | 154 | "ping timeout, in milliseconds") |
| 155 | ("count,c", po::value<int>(&options.nPings), "number of pings to send (default = no limit)") |
| 156 | ("start,n", po::value<uint64_t>(&options.startSeq), |
| 157 | "set the starting sequence number, the number is incremented by 1 after each Interest") |
Eric Newberry | 4164b8e | 2015-04-23 17:29:18 -0700 | [diff] [blame] | 158 | ("identifier,p", po::value<std::string>(&identifier), |
Davide Pesavento | 0da1f44 | 2019-07-26 17:38:13 -0400 | [diff] [blame] | 159 | "add identifier to the Interest names before the sequence numbers to avoid conflicts") |
| 160 | ("cache,a", "allow routers to return stale Data from cache") |
Eric Newberry | 4164b8e | 2015-04-23 17:29:18 -0700 | [diff] [blame] | 161 | ("timestamp,t", "print timestamp with messages") |
| 162 | ; |
Davide Pesavento | 0da1f44 | 2019-07-26 17:38:13 -0400 | [diff] [blame] | 163 | |
| 164 | po::options_description hiddenOptDesc; |
Eric Newberry | 4164b8e | 2015-04-23 17:29:18 -0700 | [diff] [blame] | 165 | hiddenOptDesc.add_options() |
| 166 | ("prefix", po::value<std::string>(), "prefix to send pings to") |
| 167 | ; |
| 168 | |
Davide Pesavento | 0da1f44 | 2019-07-26 17:38:13 -0400 | [diff] [blame] | 169 | po::options_description optDesc; |
Eric Newberry | 4164b8e | 2015-04-23 17:29:18 -0700 | [diff] [blame] | 170 | optDesc.add(visibleOptDesc).add(hiddenOptDesc); |
| 171 | |
Davide Pesavento | 0da1f44 | 2019-07-26 17:38:13 -0400 | [diff] [blame] | 172 | po::positional_options_description optPos; |
| 173 | optPos.add("prefix", -1); |
Eric Newberry | 4164b8e | 2015-04-23 17:29:18 -0700 | [diff] [blame] | 174 | |
Davide Pesavento | 0da1f44 | 2019-07-26 17:38:13 -0400 | [diff] [blame] | 175 | try { |
Eric Newberry | 4164b8e | 2015-04-23 17:29:18 -0700 | [diff] [blame] | 176 | po::variables_map optVm; |
| 177 | po::store(po::command_line_parser(argc, argv).options(optDesc).positional(optPos).run(), optVm); |
| 178 | po::notify(optVm); |
| 179 | |
| 180 | if (optVm.count("help") > 0) { |
| 181 | usage(visibleOptDesc); |
| 182 | } |
| 183 | |
| 184 | if (optVm.count("version") > 0) { |
Davide Pesavento | f8d9a53 | 2021-07-03 16:04:12 -0400 | [diff] [blame] | 185 | std::cout << "ndnping " << tools::VERSION << "\n"; |
Eric Newberry | 4164b8e | 2015-04-23 17:29:18 -0700 | [diff] [blame] | 186 | exit(0); |
| 187 | } |
| 188 | |
| 189 | if (optVm.count("prefix") > 0) { |
| 190 | options.prefix = Name(optVm["prefix"].as<std::string>()); |
| 191 | } |
| 192 | else { |
Davide Pesavento | f8d9a53 | 2021-07-03 16:04:12 -0400 | [diff] [blame] | 193 | std::cerr << "ERROR: No prefix specified\n"; |
Eric Newberry | 4164b8e | 2015-04-23 17:29:18 -0700 | [diff] [blame] | 194 | usage(visibleOptDesc); |
| 195 | } |
| 196 | |
Davide Pesavento | 0da1f44 | 2019-07-26 17:38:13 -0400 | [diff] [blame] | 197 | options.interval = time::milliseconds(optVm["interval"].as<time::milliseconds::rep>()); |
Davide Pesavento | b3570c6 | 2022-02-19 19:19:00 -0500 | [diff] [blame] | 198 | if (options.interval < 1_ms) { |
| 199 | std::cerr << "ERROR: Specified ping interval is less than the minimum (1 ms)\n"; |
Davide Pesavento | 0da1f44 | 2019-07-26 17:38:13 -0400 | [diff] [blame] | 200 | usage(visibleOptDesc); |
Eric Newberry | 4164b8e | 2015-04-23 17:29:18 -0700 | [diff] [blame] | 201 | } |
| 202 | |
Davide Pesavento | 0da1f44 | 2019-07-26 17:38:13 -0400 | [diff] [blame] | 203 | options.timeout = time::milliseconds(optVm["timeout"].as<time::milliseconds::rep>()); |
Eric Newberry | 4164b8e | 2015-04-23 17:29:18 -0700 | [diff] [blame] | 204 | |
| 205 | if (optVm.count("count") > 0) { |
| 206 | if (options.nPings <= 0) { |
Davide Pesavento | f8d9a53 | 2021-07-03 16:04:12 -0400 | [diff] [blame] | 207 | std::cerr << "ERROR: Number of ping must be positive\n"; |
Eric Newberry | 4164b8e | 2015-04-23 17:29:18 -0700 | [diff] [blame] | 208 | usage(visibleOptDesc); |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | if (optVm.count("start") > 0) { |
| 213 | options.shouldGenerateRandomSeq = false; |
| 214 | } |
| 215 | |
| 216 | if (optVm.count("identifier") > 0) { |
| 217 | bool isIdentifierAcceptable = std::all_of(identifier.begin(), identifier.end(), &isalnum); |
| 218 | if (identifier.empty() || !isIdentifierAcceptable) { |
Davide Pesavento | f8d9a53 | 2021-07-03 16:04:12 -0400 | [diff] [blame] | 219 | std::cerr << "ERROR: Unacceptable client identifier\n"; |
Eric Newberry | 4164b8e | 2015-04-23 17:29:18 -0700 | [diff] [blame] | 220 | usage(visibleOptDesc); |
| 221 | } |
| 222 | |
| 223 | options.clientIdentifier = name::Component(identifier); |
| 224 | } |
| 225 | |
| 226 | if (optVm.count("cache") > 0) { |
| 227 | options.shouldAllowStaleData = true; |
| 228 | } |
| 229 | |
| 230 | if (optVm.count("timestamp") > 0) { |
| 231 | options.shouldPrintTimestamp = true; |
| 232 | } |
| 233 | } |
| 234 | catch (const po::error& e) { |
Davide Pesavento | f8d9a53 | 2021-07-03 16:04:12 -0400 | [diff] [blame] | 235 | std::cerr << "ERROR: " << e.what() << "\n"; |
Eric Newberry | 4164b8e | 2015-04-23 17:29:18 -0700 | [diff] [blame] | 236 | usage(visibleOptDesc); |
| 237 | } |
| 238 | |
Davide Pesavento | f8d9a53 | 2021-07-03 16:04:12 -0400 | [diff] [blame] | 239 | std::cout << "PING " << options.prefix << "\n"; |
Alexander Afanasyev | 1e7a7b2 | 2015-08-26 15:35:26 -0700 | [diff] [blame] | 240 | return Runner(options).run(); |
Eric Newberry | 4164b8e | 2015-04-23 17:29:18 -0700 | [diff] [blame] | 241 | } |
| 242 | |
Davide Pesavento | b3570c6 | 2022-02-19 19:19:00 -0500 | [diff] [blame] | 243 | } // namespace ndn::ping::client |
Eric Newberry | 4164b8e | 2015-04-23 17:29:18 -0700 | [diff] [blame] | 244 | |
| 245 | int |
Davide Pesavento | da85e25 | 2019-03-18 11:42:01 -0400 | [diff] [blame] | 246 | main(int argc, char* argv[]) |
Eric Newberry | 4164b8e | 2015-04-23 17:29:18 -0700 | [diff] [blame] | 247 | { |
| 248 | return ndn::ping::client::main(argc, argv); |
| 249 | } |