Eric Newberry | 94996d6 | 2015-05-07 13:48:46 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | 5923bf8 | 2018-08-09 01:55:44 -0400 | [diff] [blame] | 2 | /* |
Davide Pesavento | a0e6b60 | 2021-01-21 19:47:04 -0500 | [diff] [blame] | 3 | * Copyright (c) 2015-2021, Arizona Board of Regents. |
Eric Newberry | 94996d6 | 2015-05-07 13:48:46 -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 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 | |
Davide Pesavento | a0e6b60 | 2021-01-21 19:47:04 -0500 | [diff] [blame] | 29 | #include <boost/asio/signal_set.hpp> |
| 30 | #include <boost/program_options/options_description.hpp> |
| 31 | #include <boost/program_options/parsers.hpp> |
| 32 | #include <boost/program_options/variables_map.hpp> |
| 33 | |
Eric Newberry | 94996d6 | 2015-05-07 13:48:46 -0700 | [diff] [blame] | 34 | namespace ndn { |
| 35 | namespace ping { |
| 36 | namespace server { |
| 37 | |
Davide Pesavento | 5923bf8 | 2018-08-09 01:55:44 -0400 | [diff] [blame] | 38 | namespace po = boost::program_options; |
| 39 | |
Alexander Afanasyev | 1e7a7b2 | 2015-08-26 15:35:26 -0700 | [diff] [blame] | 40 | class Runner : noncopyable |
| 41 | { |
| 42 | public: |
| 43 | explicit |
| 44 | Runner(const Options& options) |
| 45 | : m_options(options) |
| 46 | , m_pingServer(m_face, m_keyChain, options) |
| 47 | , m_tracer(m_pingServer, options) |
Davide Pesavento | 5923bf8 | 2018-08-09 01:55:44 -0400 | [diff] [blame] | 48 | , m_signalSet(m_face.getIoService(), SIGINT) |
Alexander Afanasyev | 1e7a7b2 | 2015-08-26 15:35:26 -0700 | [diff] [blame] | 49 | { |
Alexander Afanasyev | 1e7a7b2 | 2015-08-26 15:35:26 -0700 | [diff] [blame] | 50 | m_pingServer.afterFinish.connect([this] { |
Davide Pesavento | 5923bf8 | 2018-08-09 01:55:44 -0400 | [diff] [blame] | 51 | m_pingServer.stop(); |
| 52 | m_signalSet.cancel(); |
| 53 | }); |
| 54 | |
| 55 | m_signalSet.async_wait([this] (const auto& ec, auto) { |
| 56 | if (ec != boost::asio::error::operation_aborted) { |
| 57 | m_pingServer.stop(); |
| 58 | } |
| 59 | }); |
Alexander Afanasyev | 1e7a7b2 | 2015-08-26 15:35:26 -0700 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | int |
| 63 | run() |
| 64 | { |
Davide Pesavento | 5923bf8 | 2018-08-09 01:55:44 -0400 | [diff] [blame] | 65 | std::cout << "PING SERVER " << m_options.prefix << std::endl; |
| 66 | |
Alexander Afanasyev | 1e7a7b2 | 2015-08-26 15:35:26 -0700 | [diff] [blame] | 67 | try { |
| 68 | m_pingServer.start(); |
| 69 | m_face.processEvents(); |
| 70 | } |
Davide Pesavento | 5923bf8 | 2018-08-09 01:55:44 -0400 | [diff] [blame] | 71 | catch (const std::exception& e) { |
Alexander Afanasyev | 1e7a7b2 | 2015-08-26 15:35:26 -0700 | [diff] [blame] | 72 | std::cerr << "ERROR: " << e.what() << std::endl; |
| 73 | return 1; |
| 74 | } |
| 75 | |
Davide Pesavento | 5923bf8 | 2018-08-09 01:55:44 -0400 | [diff] [blame] | 76 | std::cout << "\n--- " << m_options.prefix << " ping server statistics ---\n" |
| 77 | << m_pingServer.getNPings() << " packets processed" << std::endl; |
Alexander Afanasyev | 1e7a7b2 | 2015-08-26 15:35:26 -0700 | [diff] [blame] | 78 | return 0; |
| 79 | } |
| 80 | |
| 81 | private: |
Alexander Afanasyev | 1e7a7b2 | 2015-08-26 15:35:26 -0700 | [diff] [blame] | 82 | const Options& m_options; |
Davide Pesavento | 5923bf8 | 2018-08-09 01:55:44 -0400 | [diff] [blame] | 83 | |
Alexander Afanasyev | 1e7a7b2 | 2015-08-26 15:35:26 -0700 | [diff] [blame] | 84 | Face m_face; |
| 85 | KeyChain m_keyChain; |
| 86 | PingServer m_pingServer; |
| 87 | Tracer m_tracer; |
| 88 | |
Davide Pesavento | 5923bf8 | 2018-08-09 01:55:44 -0400 | [diff] [blame] | 89 | boost::asio::signal_set m_signalSet; |
Alexander Afanasyev | 1e7a7b2 | 2015-08-26 15:35:26 -0700 | [diff] [blame] | 90 | }; |
| 91 | |
Eric Newberry | 94996d6 | 2015-05-07 13:48:46 -0700 | [diff] [blame] | 92 | static void |
Davide Pesavento | 5923bf8 | 2018-08-09 01:55:44 -0400 | [diff] [blame] | 93 | usage(std::ostream& os, const std::string& programName, const po::options_description& options) |
Eric Newberry | 94996d6 | 2015-05-07 13:48:46 -0700 | [diff] [blame] | 94 | { |
Davide Pesavento | 5923bf8 | 2018-08-09 01:55:44 -0400 | [diff] [blame] | 95 | os << "Usage: " << programName << " [options] <prefix>\n" |
| 96 | << "\n" |
| 97 | << "Starts a NDN ping server that responds to Interests under name ndn:<prefix>/ping\n" |
| 98 | << "\n" |
| 99 | << options; |
Eric Newberry | 94996d6 | 2015-05-07 13:48:46 -0700 | [diff] [blame] | 100 | } |
| 101 | |
Davide Pesavento | 5923bf8 | 2018-08-09 01:55:44 -0400 | [diff] [blame] | 102 | static int |
Eric Newberry | 94996d6 | 2015-05-07 13:48:46 -0700 | [diff] [blame] | 103 | main(int argc, char* argv[]) |
| 104 | { |
| 105 | Options options; |
Davide Pesavento | 5923bf8 | 2018-08-09 01:55:44 -0400 | [diff] [blame] | 106 | std::string prefix; |
| 107 | auto nMaxPings = static_cast<std::make_signed_t<size_t>>(options.nMaxPings); |
| 108 | auto payloadSize = static_cast<std::make_signed_t<size_t>>(options.payloadSize); |
Eric Newberry | 94996d6 | 2015-05-07 13:48:46 -0700 | [diff] [blame] | 109 | |
Davide Pesavento | 5923bf8 | 2018-08-09 01:55:44 -0400 | [diff] [blame] | 110 | po::options_description visibleDesc("Options"); |
| 111 | visibleDesc.add_options() |
| 112 | ("help,h", "print this help message and exit") |
Davide Pesavento | 0d4b182 | 2019-07-27 19:32:05 -0400 | [diff] [blame] | 113 | ("freshness,f", po::value<time::milliseconds::rep>()->default_value(options.freshnessPeriod.count()), |
Davide Pesavento | 5923bf8 | 2018-08-09 01:55:44 -0400 | [diff] [blame] | 114 | "FreshnessPeriod of the ping response, in milliseconds") |
| 115 | ("satisfy,p", po::value(&nMaxPings)->default_value(nMaxPings), |
Davide Pesavento | 0d4b182 | 2019-07-27 19:32:05 -0400 | [diff] [blame] | 116 | "maximum number of pings to satisfy (0 = no limit)") |
Davide Pesavento | 5923bf8 | 2018-08-09 01:55:44 -0400 | [diff] [blame] | 117 | ("size,s", po::value(&payloadSize)->default_value(payloadSize), |
| 118 | "size of response payload") |
Davide Pesavento | 2bdda1d | 2018-08-09 19:35:49 -0400 | [diff] [blame] | 119 | ("timestamp,t", po::bool_switch(&options.wantTimestamp), |
| 120 | "prepend a timestamp to each log message") |
| 121 | ("quiet,q", po::bool_switch(&options.wantQuiet), |
| 122 | "do not print a log message each time a ping packet is received") |
Davide Pesavento | 5923bf8 | 2018-08-09 01:55:44 -0400 | [diff] [blame] | 123 | ("version,V", "print program version and exit") |
| 124 | ; |
Eric Newberry | 94996d6 | 2015-05-07 13:48:46 -0700 | [diff] [blame] | 125 | |
Davide Pesavento | 5923bf8 | 2018-08-09 01:55:44 -0400 | [diff] [blame] | 126 | po::options_description hiddenDesc; |
| 127 | hiddenDesc.add_options() |
| 128 | ("prefix", po::value<std::string>(&prefix)); |
Eric Newberry | 94996d6 | 2015-05-07 13:48:46 -0700 | [diff] [blame] | 129 | |
Davide Pesavento | 5923bf8 | 2018-08-09 01:55:44 -0400 | [diff] [blame] | 130 | po::options_description optDesc; |
Davide Pesavento | 814ad34 | 2021-01-26 14:36:11 -0500 | [diff] [blame^] | 131 | optDesc.add(visibleDesc).add(hiddenDesc); |
Davide Pesavento | 0d4b182 | 2019-07-27 19:32:05 -0400 | [diff] [blame] | 132 | |
| 133 | po::positional_options_description posDesc; |
| 134 | posDesc.add("prefix", -1); |
Davide Pesavento | 5923bf8 | 2018-08-09 01:55:44 -0400 | [diff] [blame] | 135 | |
| 136 | po::variables_map vm; |
Eric Newberry | 94996d6 | 2015-05-07 13:48:46 -0700 | [diff] [blame] | 137 | try { |
Davide Pesavento | 5923bf8 | 2018-08-09 01:55:44 -0400 | [diff] [blame] | 138 | po::store(po::command_line_parser(argc, argv).options(optDesc).positional(posDesc).run(), vm); |
| 139 | po::notify(vm); |
Eric Newberry | 94996d6 | 2015-05-07 13:48:46 -0700 | [diff] [blame] | 140 | } |
| 141 | catch (const po::error& e) { |
Davide Pesavento | 5923bf8 | 2018-08-09 01:55:44 -0400 | [diff] [blame] | 142 | std::cerr << "ERROR: " << e.what() << "\n\n"; |
| 143 | usage(std::cerr, argv[0], visibleDesc); |
| 144 | return 2; |
| 145 | } |
| 146 | catch (const boost::bad_any_cast& e) { |
| 147 | std::cerr << "ERROR: " << e.what() << "\n\n"; |
| 148 | usage(std::cerr, argv[0], visibleDesc); |
| 149 | return 2; |
Eric Newberry | 94996d6 | 2015-05-07 13:48:46 -0700 | [diff] [blame] | 150 | } |
| 151 | |
Davide Pesavento | 5923bf8 | 2018-08-09 01:55:44 -0400 | [diff] [blame] | 152 | if (vm.count("help") > 0) { |
| 153 | usage(std::cout, argv[0], visibleDesc); |
| 154 | return 0; |
| 155 | } |
| 156 | |
| 157 | if (vm.count("version") > 0) { |
| 158 | std::cout << "ndnpingserver " << tools::VERSION << std::endl; |
| 159 | return 0; |
| 160 | } |
| 161 | |
| 162 | if (prefix.empty()) { |
| 163 | std::cerr << "ERROR: no name prefix specified\n\n"; |
| 164 | usage(std::cerr, argv[0], visibleDesc); |
| 165 | return 2; |
| 166 | } |
| 167 | options.prefix = prefix; |
| 168 | |
| 169 | options.freshnessPeriod = time::milliseconds(vm["freshness"].as<time::milliseconds::rep>()); |
| 170 | if (options.freshnessPeriod < 0_ms) { |
| 171 | std::cerr << "ERROR: FreshnessPeriod cannot be negative" << std::endl; |
| 172 | return 2; |
| 173 | } |
| 174 | |
| 175 | if (nMaxPings < 0) { |
| 176 | std::cerr << "ERROR: maximum number of pings to satisfy cannot be negative" << std::endl; |
| 177 | return 2; |
| 178 | } |
| 179 | options.nMaxPings = static_cast<size_t>(nMaxPings); |
| 180 | |
| 181 | if (payloadSize < 0) { |
| 182 | std::cerr << "ERROR: payload size cannot be negative" << std::endl; |
| 183 | return 2; |
| 184 | } |
| 185 | options.payloadSize = static_cast<size_t>(payloadSize); |
| 186 | |
Alexander Afanasyev | 1e7a7b2 | 2015-08-26 15:35:26 -0700 | [diff] [blame] | 187 | return Runner(options).run(); |
Eric Newberry | 94996d6 | 2015-05-07 13:48:46 -0700 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | } // namespace server |
| 191 | } // namespace ping |
| 192 | } // namespace ndn |
| 193 | |
| 194 | int |
Davide Pesavento | da85e25 | 2019-03-18 11:42:01 -0400 | [diff] [blame] | 195 | main(int argc, char* argv[]) |
Eric Newberry | 94996d6 | 2015-05-07 13:48:46 -0700 | [diff] [blame] | 196 | { |
| 197 | return ndn::ping::server::main(argc, argv); |
Alexander Afanasyev | 1e7a7b2 | 2015-08-26 15:35:26 -0700 | [diff] [blame] | 198 | } |