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