blob: 41ec49059e82e1155895e7de93582a1a9d78ca23 [file] [log] [blame]
Eric Newberry94996d62015-05-07 13:48:46 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesavento5923bf82018-08-09 01:55:44 -04002/*
Davide Pesavento7e9d7e42023-11-11 15:00:03 -05003 * Copyright (c) 2015-2023, Arizona Board of Regents.
Eric Newberry94996d62015-05-07 13:48:46 -07004 *
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 Pesaventoa0e6b602021-01-21 19:47:04 -050029#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
Davide Pesaventob3570c62022-02-19 19:19:00 -050034namespace ndn::ping::server {
Eric Newberry94996d62015-05-07 13:48:46 -070035
Davide Pesavento5923bf82018-08-09 01:55:44 -040036namespace po = boost::program_options;
37
Alexander Afanasyev1e7a7b22015-08-26 15:35:26 -070038class Runner : noncopyable
39{
40public:
41 explicit
42 Runner(const Options& options)
43 : m_options(options)
44 , m_pingServer(m_face, m_keyChain, options)
45 , m_tracer(m_pingServer, options)
Davide Pesavento7e9d7e42023-11-11 15:00:03 -050046 , m_signalSet(m_face.getIoContext(), SIGINT)
Alexander Afanasyev1e7a7b22015-08-26 15:35:26 -070047 {
Alexander Afanasyev1e7a7b22015-08-26 15:35:26 -070048 m_pingServer.afterFinish.connect([this] {
Davide Pesavento5923bf82018-08-09 01:55:44 -040049 m_pingServer.stop();
50 m_signalSet.cancel();
51 });
52
53 m_signalSet.async_wait([this] (const auto& ec, auto) {
54 if (ec != boost::asio::error::operation_aborted) {
55 m_pingServer.stop();
56 }
57 });
Alexander Afanasyev1e7a7b22015-08-26 15:35:26 -070058 }
59
60 int
61 run()
62 {
Davide Pesavento5923bf82018-08-09 01:55:44 -040063 std::cout << "PING SERVER " << m_options.prefix << std::endl;
64
Alexander Afanasyev1e7a7b22015-08-26 15:35:26 -070065 try {
66 m_pingServer.start();
67 m_face.processEvents();
68 }
Davide Pesavento5923bf82018-08-09 01:55:44 -040069 catch (const std::exception& e) {
Alexander Afanasyev1e7a7b22015-08-26 15:35:26 -070070 std::cerr << "ERROR: " << e.what() << std::endl;
71 return 1;
72 }
73
Davide Pesavento5923bf82018-08-09 01:55:44 -040074 std::cout << "\n--- " << m_options.prefix << " ping server statistics ---\n"
75 << m_pingServer.getNPings() << " packets processed" << std::endl;
Alexander Afanasyev1e7a7b22015-08-26 15:35:26 -070076 return 0;
77 }
78
79private:
Alexander Afanasyev1e7a7b22015-08-26 15:35:26 -070080 const Options& m_options;
Davide Pesavento5923bf82018-08-09 01:55:44 -040081
Alexander Afanasyev1e7a7b22015-08-26 15:35:26 -070082 Face m_face;
83 KeyChain m_keyChain;
84 PingServer m_pingServer;
85 Tracer m_tracer;
86
Davide Pesavento5923bf82018-08-09 01:55:44 -040087 boost::asio::signal_set m_signalSet;
Alexander Afanasyev1e7a7b22015-08-26 15:35:26 -070088};
89
Eric Newberry94996d62015-05-07 13:48:46 -070090static void
Davide Pesaventob3570c62022-02-19 19:19:00 -050091usage(std::ostream& os, std::string_view programName, const po::options_description& options)
Eric Newberry94996d62015-05-07 13:48:46 -070092{
Davide Pesavento5923bf82018-08-09 01:55:44 -040093 os << "Usage: " << programName << " [options] <prefix>\n"
94 << "\n"
95 << "Starts a NDN ping server that responds to Interests under name ndn:<prefix>/ping\n"
96 << "\n"
97 << options;
Eric Newberry94996d62015-05-07 13:48:46 -070098}
99
Davide Pesavento5923bf82018-08-09 01:55:44 -0400100static int
Eric Newberry94996d62015-05-07 13:48:46 -0700101main(int argc, char* argv[])
102{
103 Options options;
Davide Pesavento5923bf82018-08-09 01:55:44 -0400104 std::string prefix;
105 auto nMaxPings = static_cast<std::make_signed_t<size_t>>(options.nMaxPings);
106 auto payloadSize = static_cast<std::make_signed_t<size_t>>(options.payloadSize);
Eric Newberry94996d62015-05-07 13:48:46 -0700107
Davide Pesavento5923bf82018-08-09 01:55:44 -0400108 po::options_description visibleDesc("Options");
109 visibleDesc.add_options()
110 ("help,h", "print this help message and exit")
Davide Pesavento0d4b1822019-07-27 19:32:05 -0400111 ("freshness,f", po::value<time::milliseconds::rep>()->default_value(options.freshnessPeriod.count()),
Davide Pesavento5923bf82018-08-09 01:55:44 -0400112 "FreshnessPeriod of the ping response, in milliseconds")
113 ("satisfy,p", po::value(&nMaxPings)->default_value(nMaxPings),
Davide Pesavento0d4b1822019-07-27 19:32:05 -0400114 "maximum number of pings to satisfy (0 = no limit)")
Davide Pesavento5923bf82018-08-09 01:55:44 -0400115 ("size,s", po::value(&payloadSize)->default_value(payloadSize),
116 "size of response payload")
Davide Pesavento2bdda1d2018-08-09 19:35:49 -0400117 ("timestamp,t", po::bool_switch(&options.wantTimestamp),
118 "prepend a timestamp to each log message")
119 ("quiet,q", po::bool_switch(&options.wantQuiet),
120 "do not print a log message each time a ping packet is received")
Davide Pesavento5923bf82018-08-09 01:55:44 -0400121 ("version,V", "print program version and exit")
122 ;
Eric Newberry94996d62015-05-07 13:48:46 -0700123
Davide Pesavento5923bf82018-08-09 01:55:44 -0400124 po::options_description hiddenDesc;
125 hiddenDesc.add_options()
126 ("prefix", po::value<std::string>(&prefix));
Eric Newberry94996d62015-05-07 13:48:46 -0700127
Davide Pesavento5923bf82018-08-09 01:55:44 -0400128 po::options_description optDesc;
Davide Pesavento814ad342021-01-26 14:36:11 -0500129 optDesc.add(visibleDesc).add(hiddenDesc);
Davide Pesavento0d4b1822019-07-27 19:32:05 -0400130
131 po::positional_options_description posDesc;
132 posDesc.add("prefix", -1);
Davide Pesavento5923bf82018-08-09 01:55:44 -0400133
134 po::variables_map vm;
Eric Newberry94996d62015-05-07 13:48:46 -0700135 try {
Davide Pesavento5923bf82018-08-09 01:55:44 -0400136 po::store(po::command_line_parser(argc, argv).options(optDesc).positional(posDesc).run(), vm);
137 po::notify(vm);
Eric Newberry94996d62015-05-07 13:48:46 -0700138 }
139 catch (const po::error& e) {
Davide Pesavento5923bf82018-08-09 01:55:44 -0400140 std::cerr << "ERROR: " << e.what() << "\n\n";
141 usage(std::cerr, argv[0], visibleDesc);
142 return 2;
143 }
144 catch (const boost::bad_any_cast& e) {
145 std::cerr << "ERROR: " << e.what() << "\n\n";
146 usage(std::cerr, argv[0], visibleDesc);
147 return 2;
Eric Newberry94996d62015-05-07 13:48:46 -0700148 }
149
Davide Pesavento5923bf82018-08-09 01:55:44 -0400150 if (vm.count("help") > 0) {
151 usage(std::cout, argv[0], visibleDesc);
152 return 0;
153 }
154
155 if (vm.count("version") > 0) {
156 std::cout << "ndnpingserver " << tools::VERSION << std::endl;
157 return 0;
158 }
159
160 if (prefix.empty()) {
161 std::cerr << "ERROR: no name prefix specified\n\n";
162 usage(std::cerr, argv[0], visibleDesc);
163 return 2;
164 }
165 options.prefix = prefix;
166
167 options.freshnessPeriod = time::milliseconds(vm["freshness"].as<time::milliseconds::rep>());
168 if (options.freshnessPeriod < 0_ms) {
169 std::cerr << "ERROR: FreshnessPeriod cannot be negative" << std::endl;
170 return 2;
171 }
172
173 if (nMaxPings < 0) {
174 std::cerr << "ERROR: maximum number of pings to satisfy cannot be negative" << std::endl;
175 return 2;
176 }
177 options.nMaxPings = static_cast<size_t>(nMaxPings);
178
179 if (payloadSize < 0) {
180 std::cerr << "ERROR: payload size cannot be negative" << std::endl;
181 return 2;
182 }
183 options.payloadSize = static_cast<size_t>(payloadSize);
184
Alexander Afanasyev1e7a7b22015-08-26 15:35:26 -0700185 return Runner(options).run();
Eric Newberry94996d62015-05-07 13:48:46 -0700186}
187
Davide Pesaventob3570c62022-02-19 19:19:00 -0500188} // namespace ndn::ping::server
Eric Newberry94996d62015-05-07 13:48:46 -0700189
190int
Davide Pesaventoda85e252019-03-18 11:42:01 -0400191main(int argc, char* argv[])
Eric Newberry94996d62015-05-07 13:48:46 -0700192{
193 return ndn::ping::server::main(argc, argv);
Alexander Afanasyev1e7a7b22015-08-26 15:35:26 -0700194}