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 | |
| 58 | static void |
| 59 | signalHandler(Face& face, StatisticsCollector& statisticsCollector) |
| 60 | { |
| 61 | face.shutdown(); |
| 62 | Statistics statistics = statisticsCollector.computeStatistics(); |
| 63 | std::cout << statistics << "\n"; |
| 64 | |
| 65 | if (statistics.nReceived == statistics.nSent) { |
| 66 | exit(0); |
| 67 | } |
| 68 | else { |
| 69 | exit(1); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | int |
| 74 | main(int argc, char* argv[]) |
| 75 | { |
| 76 | Options options; |
| 77 | options.shouldAllowStaleData = false; |
| 78 | options.nPings = -1; |
| 79 | options.interval = time::milliseconds(getPingMinimumInterval()); |
| 80 | options.timeout = time::milliseconds(getDefaultPingTimeoutThreshold()); |
| 81 | options.startSeq = 0; |
| 82 | options.shouldGenerateRandomSeq = true; |
| 83 | options.shouldPrintTimestamp = false; |
| 84 | |
| 85 | std::string identifier; |
| 86 | |
| 87 | namespace po = boost::program_options; |
| 88 | |
| 89 | po::options_description visibleOptDesc("Allowed options"); |
| 90 | visibleOptDesc.add_options() |
| 91 | ("help,h", "print this message and exit") |
| 92 | ("version,V", "display version and exit") |
| 93 | ("interval,i", po::value<int>(), |
| 94 | ("set ping interval in milliseconds (minimum " + |
| 95 | std::to_string(getPingMinimumInterval().count()) + " ms)").c_str()) |
| 96 | ("timeout,o", po::value<int>(), |
| 97 | ("set ping timeout in milliseconds (default " + |
| 98 | std::to_string(getDefaultPingTimeoutThreshold().count()) + " ms)").c_str()) |
| 99 | ("count,c", po::value<int>(&options.nPings), "set total number of pings") |
| 100 | ("start,n", po::value<uint64_t>(&options.startSeq), |
| 101 | "set the starting seq number, the number is incremented by 1 after each Interest") |
| 102 | ("identifier,p", po::value<std::string>(&identifier), |
| 103 | "add identifier to the Interest names before the numbers to avoid conflict") |
| 104 | ("cache,a", "allows routers to return stale Data from cache") |
| 105 | ("timestamp,t", "print timestamp with messages") |
| 106 | ; |
| 107 | po::options_description hiddenOptDesc("Hidden options"); |
| 108 | hiddenOptDesc.add_options() |
| 109 | ("prefix", po::value<std::string>(), "prefix to send pings to") |
| 110 | ; |
| 111 | |
| 112 | po::options_description optDesc("Allowed options"); |
| 113 | optDesc.add(visibleOptDesc).add(hiddenOptDesc); |
| 114 | |
| 115 | try { |
| 116 | po::positional_options_description optPos; |
| 117 | optPos.add("prefix", -1); |
| 118 | |
| 119 | po::variables_map optVm; |
| 120 | po::store(po::command_line_parser(argc, argv).options(optDesc).positional(optPos).run(), optVm); |
| 121 | po::notify(optVm); |
| 122 | |
| 123 | if (optVm.count("help") > 0) { |
| 124 | usage(visibleOptDesc); |
| 125 | } |
| 126 | |
| 127 | if (optVm.count("version") > 0) { |
| 128 | std::cout << "ndnping " << tools::VERSION << std::endl; |
| 129 | exit(0); |
| 130 | } |
| 131 | |
| 132 | if (optVm.count("prefix") > 0) { |
| 133 | options.prefix = Name(optVm["prefix"].as<std::string>()); |
| 134 | } |
| 135 | else { |
| 136 | std::cerr << "ERROR: No prefix specified" << std::endl; |
| 137 | usage(visibleOptDesc); |
| 138 | } |
| 139 | |
| 140 | if (optVm.count("interval") > 0) { |
| 141 | options.interval = time::milliseconds(optVm["interval"].as<int>()); |
| 142 | |
| 143 | if (options.interval.count() < getPingMinimumInterval().count()) { |
| 144 | std::cerr << "ERROR: Specified ping interval is less than the minimum " << |
| 145 | getPingMinimumInterval() << std::endl; |
| 146 | usage(visibleOptDesc); |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | if (optVm.count("timeout") > 0) { |
| 151 | options.timeout = time::milliseconds(optVm["timeout"].as<int>()); |
| 152 | } |
| 153 | |
| 154 | if (optVm.count("count") > 0) { |
| 155 | if (options.nPings <= 0) { |
| 156 | std::cerr << "ERROR: Number of ping must be positive" << std::endl; |
| 157 | usage(visibleOptDesc); |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | if (optVm.count("start") > 0) { |
| 162 | options.shouldGenerateRandomSeq = false; |
| 163 | } |
| 164 | |
| 165 | if (optVm.count("identifier") > 0) { |
| 166 | bool isIdentifierAcceptable = std::all_of(identifier.begin(), identifier.end(), &isalnum); |
| 167 | if (identifier.empty() || !isIdentifierAcceptable) { |
| 168 | std::cerr << "ERROR: Unacceptable client identifier" << std::endl; |
| 169 | usage(visibleOptDesc); |
| 170 | } |
| 171 | |
| 172 | options.clientIdentifier = name::Component(identifier); |
| 173 | } |
| 174 | |
| 175 | if (optVm.count("cache") > 0) { |
| 176 | options.shouldAllowStaleData = true; |
| 177 | } |
| 178 | |
| 179 | if (optVm.count("timestamp") > 0) { |
| 180 | options.shouldPrintTimestamp = true; |
| 181 | } |
| 182 | } |
| 183 | catch (const po::error& e) { |
| 184 | std::cerr << "ERROR: " << e.what() << std::endl; |
| 185 | usage(visibleOptDesc); |
| 186 | } |
| 187 | |
| 188 | boost::asio::io_service ioService; |
| 189 | Face face(ioService); |
| 190 | Ping ping(face, options); |
| 191 | StatisticsCollector statisticsCollector(ping, options); |
| 192 | Tracer tracer(ping, options); |
| 193 | |
| 194 | boost::asio::signal_set signalSet(face.getIoService(), SIGINT); |
| 195 | signalSet.async_wait(bind(&signalHandler, ref(face), ref(statisticsCollector))); |
| 196 | |
| 197 | std::cout << "PING " << options.prefix << std::endl; |
| 198 | |
| 199 | try { |
| 200 | ping.run(); |
| 201 | } |
| 202 | catch (std::exception& e) { |
| 203 | tracer.onError(e.what()); |
| 204 | face.getIoService().stop(); |
| 205 | return 2; |
| 206 | } |
| 207 | |
| 208 | Statistics statistics = statisticsCollector.computeStatistics(); |
| 209 | |
| 210 | std::cout << statistics << std::endl; |
| 211 | |
| 212 | if (statistics.nReceived == statistics.nSent) { |
| 213 | return 0; |
| 214 | } |
| 215 | else { |
| 216 | return 1; |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | } // namespace client |
| 221 | } // namespace ping |
| 222 | } // namespace ndn |
| 223 | |
| 224 | int |
| 225 | main(int argc, char** argv) |
| 226 | { |
| 227 | return ndn::ping::client::main(argc, argv); |
| 228 | } |