blob: a642ed16eeee138bec579ccbea33cf9a11a7e1de [file] [log] [blame]
Eric Newberry94996d62015-05-07 13:48:46 -07001/* -*- 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
29namespace ndn {
30namespace ping {
31namespace server {
32
33static time::milliseconds
34getMinimumFreshnessPeriod()
35{
36 return time::milliseconds(1000);
37}
38
39static void
40usage(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
51static void
52printStatistics(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 */
63static void
64onSigInt(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 */
75static void
76onError(std::string msg)
77{
78 std::cerr << "ERROR: " << msg << std::endl;
79}
80
81int
82main(int argc, char* argv[])
83{
84 Options options;
85 options.freshnessPeriod = getMinimumFreshnessPeriod();
86 options.shouldLimitSatisfied = false;
87 options.nMaxPings = 0;
88 options.shouldPrintTimestamp = false;
Eric Newberry62f7f712015-05-17 12:15:52 -070089 options.payloadSize = 0;
Eric Newberry94996d62015-05-07 13:48:46 -070090
91 namespace po = boost::program_options;
92
93 po::options_description visibleOptDesc("Allowed options");
94 visibleOptDesc.add_options()
95 ("help,h", "print this message and exit")
96 ("version,V", "display version and exit")
97 ("freshness,x", po::value<int>(),
98 ("set freshness period in milliseconds (minimum " +
99 std::to_string(getMinimumFreshnessPeriod().count()) + " ms)").c_str())
100 ("satisfy,p", po::value<int>(&options.nMaxPings), "set maximum number of pings to be satisfied")
101 ("timestamp,t", "log timestamp with responses")
Eric Newberry62f7f712015-05-17 12:15:52 -0700102 ("size,s", po::value<int>(&options.payloadSize), "specify size of response payload")
Eric Newberry94996d62015-05-07 13:48:46 -0700103 ;
104 po::options_description hiddenOptDesc("Hidden options");
105 hiddenOptDesc.add_options()
106 ("prefix", po::value<std::string>(), "prefix to register")
107 ;
108
109 po::options_description optDesc("Allowed options");
110 optDesc.add(visibleOptDesc).add(hiddenOptDesc);
111
112 try {
113 po::positional_options_description optPos;
114 optPos.add("prefix", -1);
115
116 po::variables_map optVm;
117 po::store(po::command_line_parser(argc, argv).options(optDesc).positional(optPos).run(), optVm);
118 po::notify(optVm);
119
120 if (optVm.count("help") > 0) {
121 usage(visibleOptDesc);
122 }
123
124 if (optVm.count("version") > 0) {
125 std::cout << "ndnpingserver " << tools::VERSION << std::endl;
126 exit(0);
127 }
128
129 if (optVm.count("prefix") > 0) {
130 options.prefix = Name(optVm["prefix"].as<std::string>());
131 }
132 else {
133 std::cerr << "ERROR: No prefix specified" << std::endl;
134 usage(visibleOptDesc);
135 }
136
137 if (optVm.count("freshness") > 0) {
138 options.freshnessPeriod = time::milliseconds(optVm["freshness"].as<int>());
139
140 if (options.freshnessPeriod.count() < getMinimumFreshnessPeriod().count()) {
141 std::cerr << "ERROR: Specified FreshnessPeriod is less than the minimum "
142 << getMinimumFreshnessPeriod() << std::endl;
143 usage(visibleOptDesc);
144 }
145 }
146
147 if (optVm.count("satisfy") > 0) {
148 options.shouldLimitSatisfied = true;
149
150 if (options.nMaxPings < 1) {
151 std::cerr << "ERROR: Maximum number of pings to satisfy must be greater than 0" << std::endl;
152 usage(visibleOptDesc);
153 }
154 }
155
156 if (optVm.count("timestamp") > 0) {
157 options.shouldPrintTimestamp = true;
158 }
Eric Newberry62f7f712015-05-17 12:15:52 -0700159
160 if (optVm.count("size") > 0) {
161 if (options.payloadSize < 0) {
162 std::cerr << "ERROR: Payload size must be greater than or equal to 0" << std::endl;
163 usage(visibleOptDesc);
164 }
165 }
Eric Newberry94996d62015-05-07 13:48:46 -0700166 }
167 catch (const po::error& e) {
168 std::cerr << "ERROR: " << e.what() << std::endl;
169 usage(visibleOptDesc);
170 }
171
172 boost::asio::io_service ioService;
173 Face face(ioService);
174 PingServer pingServer(face, options);
175 Tracer tracer(pingServer, options);
176
177 boost::asio::signal_set signalSetInt(face.getIoService(), SIGINT);
178 signalSetInt.async_wait(bind(&onSigInt, ref(pingServer), ref(options), ref(face)));
179
180 std::cout << "PING SERVER " << options.prefix << std::endl;
181
182 try {
183 pingServer.run();
184 }
185 catch (std::exception& e) {
186 onError(e.what());
187 face.getIoService().stop();
188 return 1;
189 }
190
191 printStatistics(pingServer, options);
192 face.shutdown();
193 face.getIoService().stop();
194
195 return 0;
196}
197
198} // namespace server
199} // namespace ping
200} // namespace ndn
201
202int
203main(int argc, char** argv)
204{
205 return ndn::ping::server::main(argc, argv);
206}