Eric Newberry | 94996d6 | 2015-05-07 13:48:46 -0700 | [diff] [blame^] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 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 Eric Newberry <enewberry@email.arizona.edu> |
| 20 | * @author Jerald Paul Abraham <jeraldabraham@email.arizona.edu> |
| 21 | */ |
| 22 | |
| 23 | #include "core/common.hpp" |
| 24 | #include "core/version.hpp" |
| 25 | |
| 26 | #include "ping-server.hpp" |
| 27 | #include "tracer.hpp" |
| 28 | |
| 29 | namespace ndn { |
| 30 | namespace ping { |
| 31 | namespace server { |
| 32 | |
| 33 | static time::milliseconds |
| 34 | getMinimumFreshnessPeriod() |
| 35 | { |
| 36 | return time::milliseconds(1000); |
| 37 | } |
| 38 | |
| 39 | static void |
| 40 | usage(const boost::program_options::options_description& options) |
| 41 | { |
| 42 | std::cout << "Usage: ndnpingserver [options] ndn:/name/prefix\n" |
| 43 | "\n" |
| 44 | "Starts a NDN ping server that responds to Interests with name" |
| 45 | "ndn:/name/prefix/ping/number.\n" |
| 46 | "\n"; |
| 47 | std::cout << options; |
| 48 | exit(2); |
| 49 | } |
| 50 | |
| 51 | static void |
| 52 | printStatistics(PingServer& pingServer, Options& options) |
| 53 | { |
| 54 | std::cout << "\n--- ping server " << options.prefix << " ---" << std::endl; |
| 55 | std::cout << pingServer.getNPings() << " packets processed" << std::endl; |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * @brief SIGINT handler: exits |
| 60 | * @param pingServer pingServer instance |
| 61 | * @param options options |
| 62 | */ |
| 63 | static void |
| 64 | onSigInt(PingServer& pingServer, Options& options, Face& face) |
| 65 | { |
| 66 | printStatistics(pingServer, options); |
| 67 | face.shutdown(); |
| 68 | face.getIoService().stop(); |
| 69 | exit(1); |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * @brief outputs errors to cerr |
| 74 | */ |
| 75 | static void |
| 76 | onError(std::string msg) |
| 77 | { |
| 78 | std::cerr << "ERROR: " << msg << std::endl; |
| 79 | } |
| 80 | |
| 81 | int |
| 82 | main(int argc, char* argv[]) |
| 83 | { |
| 84 | Options options; |
| 85 | options.freshnessPeriod = getMinimumFreshnessPeriod(); |
| 86 | options.shouldLimitSatisfied = false; |
| 87 | options.nMaxPings = 0; |
| 88 | options.shouldPrintTimestamp = false; |
| 89 | |
| 90 | namespace po = boost::program_options; |
| 91 | |
| 92 | po::options_description visibleOptDesc("Allowed options"); |
| 93 | visibleOptDesc.add_options() |
| 94 | ("help,h", "print this message and exit") |
| 95 | ("version,V", "display version and exit") |
| 96 | ("freshness,x", po::value<int>(), |
| 97 | ("set freshness period in milliseconds (minimum " + |
| 98 | std::to_string(getMinimumFreshnessPeriod().count()) + " ms)").c_str()) |
| 99 | ("satisfy,p", po::value<int>(&options.nMaxPings), "set maximum number of pings to be satisfied") |
| 100 | ("timestamp,t", "log timestamp with responses") |
| 101 | ; |
| 102 | po::options_description hiddenOptDesc("Hidden options"); |
| 103 | hiddenOptDesc.add_options() |
| 104 | ("prefix", po::value<std::string>(), "prefix to register") |
| 105 | ; |
| 106 | |
| 107 | po::options_description optDesc("Allowed options"); |
| 108 | optDesc.add(visibleOptDesc).add(hiddenOptDesc); |
| 109 | |
| 110 | try { |
| 111 | po::positional_options_description optPos; |
| 112 | optPos.add("prefix", -1); |
| 113 | |
| 114 | po::variables_map optVm; |
| 115 | po::store(po::command_line_parser(argc, argv).options(optDesc).positional(optPos).run(), optVm); |
| 116 | po::notify(optVm); |
| 117 | |
| 118 | if (optVm.count("help") > 0) { |
| 119 | usage(visibleOptDesc); |
| 120 | } |
| 121 | |
| 122 | if (optVm.count("version") > 0) { |
| 123 | std::cout << "ndnpingserver " << tools::VERSION << std::endl; |
| 124 | exit(0); |
| 125 | } |
| 126 | |
| 127 | if (optVm.count("prefix") > 0) { |
| 128 | options.prefix = Name(optVm["prefix"].as<std::string>()); |
| 129 | } |
| 130 | else { |
| 131 | std::cerr << "ERROR: No prefix specified" << std::endl; |
| 132 | usage(visibleOptDesc); |
| 133 | } |
| 134 | |
| 135 | if (optVm.count("freshness") > 0) { |
| 136 | options.freshnessPeriod = time::milliseconds(optVm["freshness"].as<int>()); |
| 137 | |
| 138 | if (options.freshnessPeriod.count() < getMinimumFreshnessPeriod().count()) { |
| 139 | std::cerr << "ERROR: Specified FreshnessPeriod is less than the minimum " |
| 140 | << getMinimumFreshnessPeriod() << std::endl; |
| 141 | usage(visibleOptDesc); |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | if (optVm.count("satisfy") > 0) { |
| 146 | options.shouldLimitSatisfied = true; |
| 147 | |
| 148 | if (options.nMaxPings < 1) { |
| 149 | std::cerr << "ERROR: Maximum number of pings to satisfy must be greater than 0" << std::endl; |
| 150 | usage(visibleOptDesc); |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | if (optVm.count("timestamp") > 0) { |
| 155 | options.shouldPrintTimestamp = true; |
| 156 | } |
| 157 | } |
| 158 | catch (const po::error& e) { |
| 159 | std::cerr << "ERROR: " << e.what() << std::endl; |
| 160 | usage(visibleOptDesc); |
| 161 | } |
| 162 | |
| 163 | boost::asio::io_service ioService; |
| 164 | Face face(ioService); |
| 165 | PingServer pingServer(face, options); |
| 166 | Tracer tracer(pingServer, options); |
| 167 | |
| 168 | boost::asio::signal_set signalSetInt(face.getIoService(), SIGINT); |
| 169 | signalSetInt.async_wait(bind(&onSigInt, ref(pingServer), ref(options), ref(face))); |
| 170 | |
| 171 | std::cout << "PING SERVER " << options.prefix << std::endl; |
| 172 | |
| 173 | try { |
| 174 | pingServer.run(); |
| 175 | } |
| 176 | catch (std::exception& e) { |
| 177 | onError(e.what()); |
| 178 | face.getIoService().stop(); |
| 179 | return 1; |
| 180 | } |
| 181 | |
| 182 | printStatistics(pingServer, options); |
| 183 | face.shutdown(); |
| 184 | face.getIoService().stop(); |
| 185 | |
| 186 | return 0; |
| 187 | } |
| 188 | |
| 189 | } // namespace server |
| 190 | } // namespace ping |
| 191 | } // namespace ndn |
| 192 | |
| 193 | int |
| 194 | main(int argc, char** argv) |
| 195 | { |
| 196 | return ndn::ping::server::main(argc, argv); |
| 197 | } |