blob: 6525b7ada6bd25a9882f84db7c07657851982655 [file] [log] [blame]
Davide Pesavento35185332019-01-14 04:00:15 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Eric Newberryc8e18582018-05-31 19:27:01 -07002/*
Davide Pesavento92669062022-03-10 19:09:44 -05003 * Copyright (c) 2014-2022, Arizona Board of Regents.
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -08004 *
Davide Pesaventod0b59982015-02-27 19:15:15 +01005 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -080017 *
18 * Author: Jerald Paul Abraham <jeraldabraham@email.arizona.edu>
19 */
20
Davide Pesavento35185332019-01-14 04:00:15 -050021#include "util.hpp"
Eric Newberryc8e18582018-05-31 19:27:01 -070022
Davide Pesavento35185332019-01-14 04:00:15 -050023#include <ndn-cxx/data.hpp>
Eric Newberryc8e18582018-05-31 19:27:01 -070024#include <ndn-cxx/face.hpp>
Davide Pesaventoef064892022-04-05 02:26:03 -040025#include <ndn-cxx/interest.hpp>
Eric Newberryc8e18582018-05-31 19:27:01 -070026#include <ndn-cxx/security/key-chain.hpp>
27#include <ndn-cxx/security/signing-info.hpp>
Davide Pesavento35185332019-01-14 04:00:15 -050028#include <ndn-cxx/util/random.hpp>
Davide Pesaventoef064892022-04-05 02:26:03 -040029#include <ndn-cxx/util/time.hpp>
Davide Pesavento35185332019-01-14 04:00:15 -050030
Davide Pesavento35185332019-01-14 04:00:15 -050031#include <limits>
Davide Pesaventoef064892022-04-05 02:26:03 -040032#include <optional>
Davide Pesavento35185332019-01-14 04:00:15 -050033#include <sstream>
Davide Pesaventoef064892022-04-05 02:26:03 -040034#include <string_view>
Davide Pesavento35185332019-01-14 04:00:15 -050035#include <vector>
jeraldabraham420dbf02014-04-25 22:58:31 -070036
Davide Pesaventod0b59982015-02-27 19:15:15 +010037#include <boost/asio/io_service.hpp>
38#include <boost/asio/signal_set.hpp>
jeraldabraham420dbf02014-04-25 22:58:31 -070039#include <boost/noncopyable.hpp>
Davide Pesavento35185332019-01-14 04:00:15 -050040#include <boost/program_options/options_description.hpp>
41#include <boost/program_options/parsers.hpp>
42#include <boost/program_options/variables_map.hpp>
43#include <boost/thread/thread.hpp>
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -080044
Davide Pesavento35185332019-01-14 04:00:15 -050045namespace po = boost::program_options;
Davide Pesaventoef064892022-04-05 02:26:03 -040046using namespace ndn::time_literals;
47using namespace std::string_literals;
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -080048
Davide Pesaventoef064892022-04-05 02:26:03 -040049namespace ndntg {
50
51namespace time = ndn::time;
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -080052
jeraldabraham420dbf02014-04-25 22:58:31 -070053class NdnTrafficServer : boost::noncopyable
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -080054{
55public:
jeraldabrahamcc3c6c92014-03-28 02:21:45 -070056 explicit
Davide Pesavento35185332019-01-14 04:00:15 -050057 NdnTrafficServer(const std::string& configFile)
58 : m_signalSet(m_ioService, SIGINT, SIGTERM)
59 , m_logger("NdnTrafficServer")
jeraldabrahamcc3c6c92014-03-28 02:21:45 -070060 , m_face(m_ioService)
Davide Pesavento35185332019-01-14 04:00:15 -050061 , m_configurationFile(configFile)
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -080062 {
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -080063 }
64
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -080065 void
Davide Pesavento35185332019-01-14 04:00:15 -050066 setMaximumInterests(uint64_t maxInterests)
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -080067 {
Davide Pesavento35185332019-01-14 04:00:15 -050068 m_nMaximumInterests = maxInterests;
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -080069 }
70
71 void
Davide Pesavento35185332019-01-14 04:00:15 -050072 setContentDelay(time::milliseconds delay)
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -080073 {
Davide Pesavento35185332019-01-14 04:00:15 -050074 BOOST_ASSERT(delay >= 0_ms);
75 m_contentDelay = delay;
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -080076 }
77
78 void
jeraldabraham420dbf02014-04-25 22:58:31 -070079 setQuietLogging()
80 {
Davide Pesavento35185332019-01-14 04:00:15 -050081 m_wantQuiet = true;
jeraldabraham420dbf02014-04-25 22:58:31 -070082 }
83
Davide Pesavento306e5bc2019-01-26 16:20:34 -050084 int
Davide Pesavento35185332019-01-14 04:00:15 -050085 run()
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -080086 {
Davide Pesaventoef064892022-04-05 02:26:03 -040087 m_logger.initializeLog(std::to_string(ndn::random::generateWord32()));
Davide Pesavento306e5bc2019-01-26 16:20:34 -050088
89 if (!readConfigurationFile(m_configurationFile, m_trafficPatterns, m_logger)) {
90 return 2;
91 }
92
93 if (!checkTrafficPatternCorrectness()) {
94 m_logger.log("ERROR: Traffic configuration provided is not proper", false, true);
95 return 2;
96 }
97
98 m_logger.log("Traffic configuration file processing completed.\n", true, false);
99 for (std::size_t i = 0; i < m_trafficPatterns.size(); i++) {
Davide Pesaventoef064892022-04-05 02:26:03 -0400100 m_logger.log("Traffic Pattern Type #" + std::to_string(i + 1), false, false);
Davide Pesavento306e5bc2019-01-26 16:20:34 -0500101 m_trafficPatterns[i].printTrafficConfiguration(m_logger);
102 m_logger.log("", false, false);
103 }
Davide Pesaventod0b59982015-02-27 19:15:15 +0100104
Davide Pesavento35185332019-01-14 04:00:15 -0500105 if (m_nMaximumInterests == 0) {
106 logStatistics();
Davide Pesavento306e5bc2019-01-26 16:20:34 -0500107 return 0;
Davide Pesavento35185332019-01-14 04:00:15 -0500108 }
Davide Pesaventod0b59982015-02-27 19:15:15 +0100109
Davide Pesavento35185332019-01-14 04:00:15 -0500110 m_signalSet.async_wait([this] (const boost::system::error_code&, int) {
111 if (m_nMaximumInterests && m_nInterestsReceived < *m_nMaximumInterests) {
112 m_hasError = true;
113 }
114 stop();
115 });
116
117 for (std::size_t id = 0; id < m_trafficPatterns.size(); id++) {
118 m_registeredPrefixes.push_back(
119 m_face.setInterestFilter(m_trafficPatterns[id].m_name,
Davide Pesaventoef064892022-04-05 02:26:03 -0400120 [this, id] (auto&&, const auto& interest) { onInterest(interest, id); },
Davide Pesavento35185332019-01-14 04:00:15 -0500121 nullptr,
Davide Pesaventoef064892022-04-05 02:26:03 -0400122 [this, id] (auto&&, const auto& reason) { onRegisterFailed(reason, id); }));
Davide Pesavento35185332019-01-14 04:00:15 -0500123 }
124
125 try {
126 m_face.processEvents();
Davide Pesavento306e5bc2019-01-26 16:20:34 -0500127 return m_hasError ? 1 : 0;
Davide Pesavento35185332019-01-14 04:00:15 -0500128 }
129 catch (const std::exception& e) {
130 m_logger.log("ERROR: "s + e.what(), true, true);
Davide Pesavento35185332019-01-14 04:00:15 -0500131 m_ioService.stop();
Davide Pesavento306e5bc2019-01-26 16:20:34 -0500132 return 1;
Davide Pesavento35185332019-01-14 04:00:15 -0500133 }
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800134 }
135
Davide Pesavento35185332019-01-14 04:00:15 -0500136private:
137 class DataTrafficConfiguration
138 {
139 public:
140 void
141 printTrafficConfiguration(Logger& logger) const
142 {
143 std::ostringstream os;
144
145 if (!m_name.empty()) {
146 os << "Name=" << m_name << ", ";
147 }
148 if (m_contentDelay >= 0_ms) {
149 os << "ContentDelay=" << m_contentDelay.count() << ", ";
150 }
151 if (m_freshnessPeriod >= 0_ms) {
152 os << "FreshnessPeriod=" << m_freshnessPeriod.count() << ", ";
153 }
154 if (m_contentType) {
155 os << "ContentType=" << *m_contentType << ", ";
156 }
157 if (m_contentLength) {
158 os << "ContentBytes=" << *m_contentLength << ", ";
159 }
160 if (!m_content.empty()) {
161 os << "Content=" << m_content << ", ";
162 }
163 os << "SigningInfo=" << m_signingInfo;
164
165 logger.log(os.str(), false, false);
166 }
167
168 bool
Davide Pesavento306e5bc2019-01-26 16:20:34 -0500169 parseConfigurationLine(const std::string& line, Logger& logger, int lineNumber)
Davide Pesavento35185332019-01-14 04:00:15 -0500170 {
171 std::string parameter, value;
Davide Pesavento306e5bc2019-01-26 16:20:34 -0500172 if (!extractParameterAndValue(line, parameter, value)) {
Davide Pesaventoef064892022-04-05 02:26:03 -0400173 logger.log("Line " + std::to_string(lineNumber) + " - Invalid syntax: " + line,
Davide Pesavento306e5bc2019-01-26 16:20:34 -0500174 false, true);
175 return false;
176 }
177
178 if (parameter == "Name") {
179 m_name = value;
180 }
181 else if (parameter == "ContentDelay") {
182 m_contentDelay = time::milliseconds(std::stoul(value));
183 }
184 else if (parameter == "FreshnessPeriod") {
185 m_freshnessPeriod = time::milliseconds(std::stoul(value));
186 }
187 else if (parameter == "ContentType") {
188 m_contentType = std::stoul(value);
189 }
190 else if (parameter == "ContentBytes") {
191 m_contentLength = std::stoul(value);
192 }
193 else if (parameter == "Content") {
194 m_content = value;
195 }
196 else if (parameter == "SigningInfo") {
Davide Pesaventoef064892022-04-05 02:26:03 -0400197 m_signingInfo = ndn::security::SigningInfo(value);
Davide Pesavento35185332019-01-14 04:00:15 -0500198 }
199 else {
Davide Pesaventoef064892022-04-05 02:26:03 -0400200 logger.log("Line " + std::to_string(lineNumber) + " - Ignoring unknown parameter: " + parameter,
Davide Pesavento306e5bc2019-01-26 16:20:34 -0500201 false, true);
Davide Pesavento35185332019-01-14 04:00:15 -0500202 }
203 return true;
204 }
205
206 bool
207 checkTrafficDetailCorrectness() const
208 {
209 return true;
210 }
211
212 public:
213 std::string m_name;
214 time::milliseconds m_contentDelay = -1_ms;
215 time::milliseconds m_freshnessPeriod = -1_ms;
Davide Pesaventoef064892022-04-05 02:26:03 -0400216 std::optional<uint32_t> m_contentType;
217 std::optional<std::size_t> m_contentLength;
Davide Pesavento35185332019-01-14 04:00:15 -0500218 std::string m_content;
Davide Pesaventoef064892022-04-05 02:26:03 -0400219 ndn::security::SigningInfo m_signingInfo;
Davide Pesavento35185332019-01-14 04:00:15 -0500220 uint64_t m_nInterestsReceived = 0;
221 };
222
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800223 void
224 logStatistics()
225 {
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800226 m_logger.log("\n\n== Interest Traffic Report ==\n", false, true);
Alexander Afanasyevfda32a32014-03-20 10:50:00 -0700227 m_logger.log("Total Traffic Pattern Types = " +
Davide Pesaventoef064892022-04-05 02:26:03 -0400228 std::to_string(m_trafficPatterns.size()), false, true);
Alexander Afanasyevfda32a32014-03-20 10:50:00 -0700229 m_logger.log("Total Interests Received = " +
Davide Pesaventoef064892022-04-05 02:26:03 -0400230 std::to_string(m_nInterestsReceived), false, true);
Davide Pesaventod0b59982015-02-27 19:15:15 +0100231
Eric Newberryc8e18582018-05-31 19:27:01 -0700232 for (std::size_t patternId = 0; patternId < m_trafficPatterns.size(); patternId++) {
Davide Pesaventoef064892022-04-05 02:26:03 -0400233 m_logger.log("\nTraffic Pattern Type #" + std::to_string(patternId + 1), false, true);
Eric Newberryc8e18582018-05-31 19:27:01 -0700234 m_trafficPatterns[patternId].printTrafficConfiguration(m_logger);
Davide Pesavento35185332019-01-14 04:00:15 -0500235 m_logger.log("Total Interests Received = " +
Davide Pesaventoef064892022-04-05 02:26:03 -0400236 std::to_string(m_trafficPatterns[patternId].m_nInterestsReceived) + "\n", false, true);
Eric Newberryc8e18582018-05-31 19:27:01 -0700237 }
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800238 }
239
240 bool
Davide Pesavento35185332019-01-14 04:00:15 -0500241 checkTrafficPatternCorrectness() const
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800242 {
Davide Pesavento306e5bc2019-01-26 16:20:34 -0500243 // TODO
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800244 return true;
245 }
246
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800247 static std::string
Davide Pesavento35185332019-01-14 04:00:15 -0500248 getRandomByteString(std::size_t length)
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800249 {
Davide Pesavento35185332019-01-14 04:00:15 -0500250 // per ISO C++ std, cannot instantiate uniform_int_distribution with char
251 static std::uniform_int_distribution<short> dist(std::numeric_limits<char>::min(),
252 std::numeric_limits<char>::max());
253
254 std::string s;
255 s.reserve(length);
256 for (std::size_t i = 0; i < length; i++) {
Davide Pesaventoef064892022-04-05 02:26:03 -0400257 s += static_cast<char>(dist(ndn::random::getRandomNumberEngine()));
Eric Newberryc8e18582018-05-31 19:27:01 -0700258 }
Davide Pesavento35185332019-01-14 04:00:15 -0500259 return s;
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800260 }
261
262 void
Davide Pesaventoef064892022-04-05 02:26:03 -0400263 onInterest(const ndn::Interest& interest, std::size_t patternId)
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800264 {
Spencer Leee7a5b742015-10-29 02:18:11 -0700265 auto& pattern = m_trafficPatterns[patternId];
266
Davide Pesavento35185332019-01-14 04:00:15 -0500267 if (!m_nMaximumInterests || m_nInterestsReceived < *m_nMaximumInterests) {
Davide Pesaventoef064892022-04-05 02:26:03 -0400268 ndn::Data data(interest.getName());
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800269
Davide Pesavento35185332019-01-14 04:00:15 -0500270 if (pattern.m_freshnessPeriod >= 0_ms)
Eric Newberryc8e18582018-05-31 19:27:01 -0700271 data.setFreshnessPeriod(pattern.m_freshnessPeriod);
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700272
Davide Pesavento35185332019-01-14 04:00:15 -0500273 if (pattern.m_contentType)
274 data.setContentType(*pattern.m_contentType);
275
Eric Newberryc8e18582018-05-31 19:27:01 -0700276 std::string content;
Davide Pesavento35185332019-01-14 04:00:15 -0500277 if (pattern.m_contentLength > 0)
278 content = getRandomByteString(*pattern.m_contentLength);
Eric Newberryc8e18582018-05-31 19:27:01 -0700279 if (!pattern.m_content.empty())
280 content = pattern.m_content;
Davide Pesaventoef064892022-04-05 02:26:03 -0400281 data.setContent(ndn::makeStringBlock(ndn::tlv::Content, content));
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700282
Eric Newberryc8e18582018-05-31 19:27:01 -0700283 m_keyChain.sign(data, pattern.m_signingInfo);
Spencer Leee7a5b742015-10-29 02:18:11 -0700284
Eric Newberryc8e18582018-05-31 19:27:01 -0700285 m_nInterestsReceived++;
286 pattern.m_nInterestsReceived++;
Davide Pesaventod0b59982015-02-27 19:15:15 +0100287
Davide Pesavento35185332019-01-14 04:00:15 -0500288 if (!m_wantQuiet) {
Davide Pesaventoef064892022-04-05 02:26:03 -0400289 auto logLine = "Interest received - PatternType=" + std::to_string(patternId + 1) +
290 ", GlobalID=" + std::to_string(m_nInterestsReceived) +
291 ", LocalID=" + std::to_string(pattern.m_nInterestsReceived) +
Davide Pesavento35185332019-01-14 04:00:15 -0500292 ", Name=" + pattern.m_name;
Eric Newberryc8e18582018-05-31 19:27:01 -0700293 m_logger.log(logLine, true, false);
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700294 }
Eric Newberryc8e18582018-05-31 19:27:01 -0700295
Davide Pesavento35185332019-01-14 04:00:15 -0500296 if (pattern.m_contentDelay > 0_ms)
297 boost::this_thread::sleep_for(pattern.m_contentDelay);
298 if (m_contentDelay > 0_ms)
299 boost::this_thread::sleep_for(m_contentDelay);
300
Eric Newberryc8e18582018-05-31 19:27:01 -0700301 m_face.put(data);
302 }
Davide Pesavento35185332019-01-14 04:00:15 -0500303
304 if (m_nMaximumInterests && m_nInterestsReceived >= *m_nMaximumInterests) {
Eric Newberryc8e18582018-05-31 19:27:01 -0700305 logStatistics();
Davide Pesavento35185332019-01-14 04:00:15 -0500306 m_registeredPrefixes.clear();
Eric Newberry51459402018-06-28 00:06:18 -0700307 m_signalSet.cancel();
Eric Newberryc8e18582018-05-31 19:27:01 -0700308 }
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800309 }
310
311 void
Davide Pesavento35185332019-01-14 04:00:15 -0500312 onRegisterFailed(const std::string& reason, std::size_t patternId)
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800313 {
Davide Pesaventoef064892022-04-05 02:26:03 -0400314 auto logLine = "Prefix registration failed - PatternType=" + std::to_string(patternId + 1) +
Davide Pesavento35185332019-01-14 04:00:15 -0500315 ", Name=" + m_trafficPatterns[patternId].m_name +
316 ", Reason=" + reason;
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800317 m_logger.log(logLine, true, true);
Davide Pesaventod0b59982015-02-27 19:15:15 +0100318
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700319 m_nRegistrationsFailed++;
Eric Newberryc8e18582018-05-31 19:27:01 -0700320 if (m_nRegistrationsFailed == m_trafficPatterns.size()) {
321 m_hasError = true;
Davide Pesavento35185332019-01-14 04:00:15 -0500322 stop();
Eric Newberryc8e18582018-05-31 19:27:01 -0700323 }
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800324 }
325
326 void
Davide Pesavento35185332019-01-14 04:00:15 -0500327 stop()
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800328 {
Davide Pesavento35185332019-01-14 04:00:15 -0500329 logStatistics();
330 m_face.shutdown();
331 m_ioService.stop();
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800332 }
333
334private:
Alexander Afanasyev976c3972014-05-26 17:03:40 +0300335 boost::asio::io_service m_ioService;
Eric Newberry51459402018-06-28 00:06:18 -0700336 boost::asio::signal_set m_signalSet;
Davide Pesavento35185332019-01-14 04:00:15 -0500337 Logger m_logger;
Davide Pesaventoef064892022-04-05 02:26:03 -0400338 ndn::Face m_face;
339 ndn::KeyChain m_keyChain;
Davide Pesavento35185332019-01-14 04:00:15 -0500340
341 std::string m_configurationFile;
Davide Pesaventoef064892022-04-05 02:26:03 -0400342 std::optional<uint64_t> m_nMaximumInterests;
Davide Pesavento35185332019-01-14 04:00:15 -0500343 time::milliseconds m_contentDelay = 0_ms;
Davide Pesavento35185332019-01-14 04:00:15 -0500344
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700345 std::vector<DataTrafficConfiguration> m_trafficPatterns;
Davide Pesaventoef064892022-04-05 02:26:03 -0400346 std::vector<ndn::ScopedRegisteredPrefixHandle> m_registeredPrefixes;
Davide Pesavento35185332019-01-14 04:00:15 -0500347 uint64_t m_nRegistrationsFailed = 0;
348 uint64_t m_nInterestsReceived = 0;
Davide Pesaventoef064892022-04-05 02:26:03 -0400349
350 bool m_wantQuiet = false;
Davide Pesavento35185332019-01-14 04:00:15 -0500351 bool m_hasError = false;
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800352};
353
Davide Pesaventoef064892022-04-05 02:26:03 -0400354} // namespace ndntg
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800355
Davide Pesavento35185332019-01-14 04:00:15 -0500356static void
Davide Pesaventoef064892022-04-05 02:26:03 -0400357usage(std::ostream& os, std::string_view programName, const po::options_description& desc)
Davide Pesavento35185332019-01-14 04:00:15 -0500358{
359 os << "Usage: " << programName << " [options] <Traffic_Configuration_File>\n"
360 << "\n"
361 << "Respond to Interests as per provided Traffic_Configuration_File.\n"
362 << "Multiple prefixes can be configured for handling.\n"
363 << "Set the environment variable NDN_TRAFFIC_LOGFOLDER to redirect output to a log file.\n"
364 << "\n"
365 << desc;
366}
367
Alexander Afanasyevfda32a32014-03-20 10:50:00 -0700368int
369main(int argc, char* argv[])
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800370{
Davide Pesavento35185332019-01-14 04:00:15 -0500371 std::string configFile;
Davide Pesaventod0b59982015-02-27 19:15:15 +0100372
Davide Pesavento35185332019-01-14 04:00:15 -0500373 po::options_description visibleOptions("Options");
374 visibleOptions.add_options()
375 ("help,h", "print this help message and exit")
376 ("count,c", po::value<int>(), "maximum number of Interests to respond to")
377 ("delay,d", po::value<ndn::time::milliseconds::rep>()->default_value(0),
378 "wait this amount of milliseconds before responding to each Interest")
379 ("quiet,q", po::bool_switch(), "turn off logging of Interest reception/Data generation")
380 ;
381
382 po::options_description hiddenOptions;
383 hiddenOptions.add_options()
384 ("config-file", po::value<std::string>(&configFile))
385 ;
386
387 po::positional_options_description posOptions;
388 posOptions.add("config-file", -1);
389
390 po::options_description allOptions;
391 allOptions.add(visibleOptions).add(hiddenOptions);
392
393 po::variables_map vm;
394 try {
395 po::store(po::command_line_parser(argc, argv).options(allOptions).positional(posOptions).run(), vm);
396 po::notify(vm);
397 }
398 catch (const po::error& e) {
399 std::cerr << "ERROR: " << e.what() << std::endl;
400 return 2;
401 }
402 catch (const boost::bad_any_cast& e) {
403 std::cerr << "ERROR: " << e.what() << std::endl;
404 return 2;
405 }
406
407 if (vm.count("help") > 0) {
408 usage(std::cout, argv[0], visibleOptions);
409 return 0;
410 }
411
412 if (configFile.empty()) {
413 usage(std::cerr, argv[0], visibleOptions);
414 return 2;
415 }
416
Davide Pesaventoef064892022-04-05 02:26:03 -0400417 ndntg::NdnTrafficServer server(configFile);
Davide Pesavento35185332019-01-14 04:00:15 -0500418
419 if (vm.count("count") > 0) {
420 int count = vm["count"].as<int>();
421 if (count < 0) {
422 std::cerr << "ERROR: the argument for option '--count' cannot be negative" << std::endl;
423 return 2;
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800424 }
Davide Pesavento35185332019-01-14 04:00:15 -0500425 server.setMaximumInterests(static_cast<uint64_t>(count));
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800426 }
427
Davide Pesavento35185332019-01-14 04:00:15 -0500428 if (vm.count("delay") > 0) {
429 ndn::time::milliseconds delay(vm["delay"].as<ndn::time::milliseconds::rep>());
Davide Pesaventoef064892022-04-05 02:26:03 -0400430 if (delay < 0_ms) {
Davide Pesavento35185332019-01-14 04:00:15 -0500431 std::cerr << "ERROR: the argument for option '--delay' cannot be negative" << std::endl;
432 return 2;
433 }
434 server.setContentDelay(delay);
Eric Newberryc8e18582018-05-31 19:27:01 -0700435 }
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800436
Davide Pesavento35185332019-01-14 04:00:15 -0500437 if (vm["quiet"].as<bool>()) {
438 server.setQuietLogging();
439 }
440
Davide Pesavento306e5bc2019-01-26 16:20:34 -0500441 return server.run();
Eric Newberryc8e18582018-05-31 19:27:01 -0700442}