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