Eric Newberry | 4164b8e | 2015-04-23 17:29:18 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2014-2015, Arizona Board of Regents. |
| 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 | |
| 34 | static time::milliseconds |
| 35 | getPingMinimumInterval() |
| 36 | { |
| 37 | return time::milliseconds(1000); |
| 38 | } |
| 39 | |
| 40 | static time::milliseconds |
| 41 | getDefaultPingTimeoutThreshold() |
| 42 | { |
| 43 | return time::milliseconds(4000); |
| 44 | } |
| 45 | |
| 46 | static void |
| 47 | usage(const boost::program_options::options_description& options) |
| 48 | { |
| 49 | std::cout << "Usage: ndnping [options] ndn:/name/prefix\n" |
| 50 | "\n" |
| 51 | "Ping a NDN name prefix using Interests with name ndn:/name/prefix/ping/number.\n" |
| 52 | "The numbers in the Interests are randomly generated unless specified.\n" |
| 53 | "\n"; |
| 54 | std::cout << options; |
| 55 | exit(2); |
| 56 | } |
| 57 | |
Eric Newberry | dce5a53 | 2015-05-06 10:46:52 -0700 | [diff] [blame^] | 58 | /** |
| 59 | * @brief SIGINT handler: print statistics and exit |
| 60 | */ |
Eric Newberry | 4164b8e | 2015-04-23 17:29:18 -0700 | [diff] [blame] | 61 | static void |
Eric Newberry | dce5a53 | 2015-05-06 10:46:52 -0700 | [diff] [blame^] | 62 | onSigInt(Face& face, StatisticsCollector& statisticsCollector) |
Eric Newberry | 4164b8e | 2015-04-23 17:29:18 -0700 | [diff] [blame] | 63 | { |
| 64 | face.shutdown(); |
| 65 | Statistics statistics = statisticsCollector.computeStatistics(); |
Eric Newberry | dce5a53 | 2015-05-06 10:46:52 -0700 | [diff] [blame^] | 66 | std::cout << statistics << std::endl; |
Eric Newberry | 4164b8e | 2015-04-23 17:29:18 -0700 | [diff] [blame] | 67 | |
| 68 | if (statistics.nReceived == statistics.nSent) { |
| 69 | exit(0); |
| 70 | } |
| 71 | else { |
| 72 | exit(1); |
| 73 | } |
| 74 | } |
| 75 | |
Eric Newberry | dce5a53 | 2015-05-06 10:46:52 -0700 | [diff] [blame^] | 76 | /** |
| 77 | * @brief SIGQUIT handler: print statistics summary and continue |
| 78 | */ |
| 79 | static void |
| 80 | onSigQuit(StatisticsCollector& statisticsCollector, boost::asio::signal_set& signalSet) |
| 81 | { |
| 82 | statisticsCollector.computeStatistics().printSummary(std::cout); |
| 83 | signalSet.async_wait(bind(&onSigQuit, ref(statisticsCollector), ref(signalSet))); |
| 84 | } |
| 85 | |
Eric Newberry | 4164b8e | 2015-04-23 17:29:18 -0700 | [diff] [blame] | 86 | int |
| 87 | main(int argc, char* argv[]) |
| 88 | { |
| 89 | Options options; |
| 90 | options.shouldAllowStaleData = false; |
| 91 | options.nPings = -1; |
| 92 | options.interval = time::milliseconds(getPingMinimumInterval()); |
| 93 | options.timeout = time::milliseconds(getDefaultPingTimeoutThreshold()); |
| 94 | options.startSeq = 0; |
| 95 | options.shouldGenerateRandomSeq = true; |
| 96 | options.shouldPrintTimestamp = false; |
| 97 | |
| 98 | std::string identifier; |
| 99 | |
| 100 | namespace po = boost::program_options; |
| 101 | |
| 102 | po::options_description visibleOptDesc("Allowed options"); |
| 103 | visibleOptDesc.add_options() |
| 104 | ("help,h", "print this message and exit") |
| 105 | ("version,V", "display version and exit") |
| 106 | ("interval,i", po::value<int>(), |
| 107 | ("set ping interval in milliseconds (minimum " + |
| 108 | std::to_string(getPingMinimumInterval().count()) + " ms)").c_str()) |
| 109 | ("timeout,o", po::value<int>(), |
| 110 | ("set ping timeout in milliseconds (default " + |
| 111 | std::to_string(getDefaultPingTimeoutThreshold().count()) + " ms)").c_str()) |
| 112 | ("count,c", po::value<int>(&options.nPings), "set total number of pings") |
| 113 | ("start,n", po::value<uint64_t>(&options.startSeq), |
| 114 | "set the starting seq number, the number is incremented by 1 after each Interest") |
| 115 | ("identifier,p", po::value<std::string>(&identifier), |
| 116 | "add identifier to the Interest names before the numbers to avoid conflict") |
| 117 | ("cache,a", "allows routers to return stale Data from cache") |
| 118 | ("timestamp,t", "print timestamp with messages") |
| 119 | ; |
| 120 | po::options_description hiddenOptDesc("Hidden options"); |
| 121 | hiddenOptDesc.add_options() |
| 122 | ("prefix", po::value<std::string>(), "prefix to send pings to") |
| 123 | ; |
| 124 | |
| 125 | po::options_description optDesc("Allowed options"); |
| 126 | optDesc.add(visibleOptDesc).add(hiddenOptDesc); |
| 127 | |
| 128 | try { |
| 129 | po::positional_options_description optPos; |
| 130 | optPos.add("prefix", -1); |
| 131 | |
| 132 | po::variables_map optVm; |
| 133 | po::store(po::command_line_parser(argc, argv).options(optDesc).positional(optPos).run(), optVm); |
| 134 | po::notify(optVm); |
| 135 | |
| 136 | if (optVm.count("help") > 0) { |
| 137 | usage(visibleOptDesc); |
| 138 | } |
| 139 | |
| 140 | if (optVm.count("version") > 0) { |
| 141 | std::cout << "ndnping " << tools::VERSION << std::endl; |
| 142 | exit(0); |
| 143 | } |
| 144 | |
| 145 | if (optVm.count("prefix") > 0) { |
| 146 | options.prefix = Name(optVm["prefix"].as<std::string>()); |
| 147 | } |
| 148 | else { |
| 149 | std::cerr << "ERROR: No prefix specified" << std::endl; |
| 150 | usage(visibleOptDesc); |
| 151 | } |
| 152 | |
| 153 | if (optVm.count("interval") > 0) { |
| 154 | options.interval = time::milliseconds(optVm["interval"].as<int>()); |
| 155 | |
| 156 | if (options.interval.count() < getPingMinimumInterval().count()) { |
| 157 | std::cerr << "ERROR: Specified ping interval is less than the minimum " << |
| 158 | getPingMinimumInterval() << std::endl; |
| 159 | usage(visibleOptDesc); |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | if (optVm.count("timeout") > 0) { |
| 164 | options.timeout = time::milliseconds(optVm["timeout"].as<int>()); |
| 165 | } |
| 166 | |
| 167 | if (optVm.count("count") > 0) { |
| 168 | if (options.nPings <= 0) { |
| 169 | std::cerr << "ERROR: Number of ping must be positive" << std::endl; |
| 170 | usage(visibleOptDesc); |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | if (optVm.count("start") > 0) { |
| 175 | options.shouldGenerateRandomSeq = false; |
| 176 | } |
| 177 | |
| 178 | if (optVm.count("identifier") > 0) { |
| 179 | bool isIdentifierAcceptable = std::all_of(identifier.begin(), identifier.end(), &isalnum); |
| 180 | if (identifier.empty() || !isIdentifierAcceptable) { |
| 181 | std::cerr << "ERROR: Unacceptable client identifier" << std::endl; |
| 182 | usage(visibleOptDesc); |
| 183 | } |
| 184 | |
| 185 | options.clientIdentifier = name::Component(identifier); |
| 186 | } |
| 187 | |
| 188 | if (optVm.count("cache") > 0) { |
| 189 | options.shouldAllowStaleData = true; |
| 190 | } |
| 191 | |
| 192 | if (optVm.count("timestamp") > 0) { |
| 193 | options.shouldPrintTimestamp = true; |
| 194 | } |
| 195 | } |
| 196 | catch (const po::error& e) { |
| 197 | std::cerr << "ERROR: " << e.what() << std::endl; |
| 198 | usage(visibleOptDesc); |
| 199 | } |
| 200 | |
| 201 | boost::asio::io_service ioService; |
| 202 | Face face(ioService); |
| 203 | Ping ping(face, options); |
| 204 | StatisticsCollector statisticsCollector(ping, options); |
| 205 | Tracer tracer(ping, options); |
| 206 | |
Eric Newberry | dce5a53 | 2015-05-06 10:46:52 -0700 | [diff] [blame^] | 207 | boost::asio::signal_set signalSetInt(face.getIoService(), SIGINT); |
| 208 | signalSetInt.async_wait(bind(&onSigInt, ref(face), ref(statisticsCollector))); |
| 209 | |
| 210 | boost::asio::signal_set signalSetQuit(face.getIoService(), SIGQUIT); |
| 211 | signalSetQuit.async_wait(bind(&onSigQuit, ref(statisticsCollector), ref(signalSetQuit))); |
Eric Newberry | 4164b8e | 2015-04-23 17:29:18 -0700 | [diff] [blame] | 212 | |
| 213 | std::cout << "PING " << options.prefix << std::endl; |
| 214 | |
| 215 | try { |
| 216 | ping.run(); |
| 217 | } |
| 218 | catch (std::exception& e) { |
| 219 | tracer.onError(e.what()); |
| 220 | face.getIoService().stop(); |
| 221 | return 2; |
| 222 | } |
| 223 | |
| 224 | Statistics statistics = statisticsCollector.computeStatistics(); |
| 225 | |
| 226 | std::cout << statistics << std::endl; |
| 227 | |
| 228 | if (statistics.nReceived == statistics.nSent) { |
| 229 | return 0; |
| 230 | } |
| 231 | else { |
| 232 | return 1; |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | } // namespace client |
| 237 | } // namespace ping |
| 238 | } // namespace ndn |
| 239 | |
| 240 | int |
| 241 | main(int argc, char** argv) |
| 242 | { |
| 243 | return ndn::ping::client::main(argc, argv); |
| 244 | } |