Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Eric Newberry | c8e1858 | 2018-05-31 19:27:01 -0700 | [diff] [blame] | 2 | /* |
Alexander Lane | e9fe187 | 2023-07-25 20:00:23 -0400 | [diff] [blame] | 3 | * Copyright (c) 2014-2023, Arizona Board of Regents. |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 4 | * |
Davide Pesavento | d0b5998 | 2015-02-27 19:15:15 +0100 | [diff] [blame] | 5 | * 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 Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 17 | * |
| 18 | * Author: Jerald Paul Abraham <jeraldabraham@email.arizona.edu> |
| 19 | */ |
| 20 | |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 21 | #include "util.hpp" |
Eric Newberry | c8e1858 | 2018-05-31 19:27:01 -0700 | [diff] [blame] | 22 | |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 23 | #include <ndn-cxx/data.hpp> |
Eric Newberry | c8e1858 | 2018-05-31 19:27:01 -0700 | [diff] [blame] | 24 | #include <ndn-cxx/face.hpp> |
Davide Pesavento | ef06489 | 2022-04-05 02:26:03 -0400 | [diff] [blame] | 25 | #include <ndn-cxx/interest.hpp> |
Eric Newberry | c8e1858 | 2018-05-31 19:27:01 -0700 | [diff] [blame] | 26 | #include <ndn-cxx/security/key-chain.hpp> |
| 27 | #include <ndn-cxx/security/signing-info.hpp> |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 28 | #include <ndn-cxx/util/random.hpp> |
Davide Pesavento | ef06489 | 2022-04-05 02:26:03 -0400 | [diff] [blame] | 29 | #include <ndn-cxx/util/time.hpp> |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 30 | |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 31 | #include <limits> |
Davide Pesavento | ef06489 | 2022-04-05 02:26:03 -0400 | [diff] [blame] | 32 | #include <optional> |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 33 | #include <sstream> |
| 34 | #include <vector> |
jeraldabraham | 420dbf0 | 2014-04-25 22:58:31 -0700 | [diff] [blame] | 35 | |
Davide Pesavento | 915ce80 | 2023-09-17 18:10:52 -0400 | [diff] [blame^] | 36 | #include <boost/asio/io_context.hpp> |
Davide Pesavento | d0b5998 | 2015-02-27 19:15:15 +0100 | [diff] [blame] | 37 | #include <boost/asio/signal_set.hpp> |
Alexander Lane | e9fe187 | 2023-07-25 20:00:23 -0400 | [diff] [blame] | 38 | #include <boost/core/noncopyable.hpp> |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 39 | #include <boost/program_options/options_description.hpp> |
| 40 | #include <boost/program_options/parsers.hpp> |
| 41 | #include <boost/program_options/variables_map.hpp> |
| 42 | #include <boost/thread/thread.hpp> |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 43 | |
Davide Pesavento | ef06489 | 2022-04-05 02:26:03 -0400 | [diff] [blame] | 44 | using namespace ndn::time_literals; |
| 45 | using namespace std::string_literals; |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 46 | |
Davide Pesavento | ef06489 | 2022-04-05 02:26:03 -0400 | [diff] [blame] | 47 | namespace ndntg { |
| 48 | |
| 49 | namespace time = ndn::time; |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 50 | |
jeraldabraham | 420dbf0 | 2014-04-25 22:58:31 -0700 | [diff] [blame] | 51 | class NdnTrafficServer : boost::noncopyable |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 52 | { |
| 53 | public: |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 54 | explicit |
Alexander Lane | e9fe187 | 2023-07-25 20:00:23 -0400 | [diff] [blame] | 55 | NdnTrafficServer(std::string configFile) |
| 56 | : m_configurationFile(std::move(configFile)) |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 57 | { |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 58 | } |
| 59 | |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 60 | void |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 61 | setMaximumInterests(uint64_t maxInterests) |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 62 | { |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 63 | m_nMaximumInterests = maxInterests; |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | void |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 67 | setContentDelay(time::milliseconds delay) |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 68 | { |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 69 | BOOST_ASSERT(delay >= 0_ms); |
| 70 | m_contentDelay = delay; |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | void |
Alexander Lane | e9fe187 | 2023-07-25 20:00:23 -0400 | [diff] [blame] | 74 | setTimestampFormat(std::string format) |
| 75 | { |
| 76 | m_timestampFormat = std::move(format); |
| 77 | } |
| 78 | |
| 79 | void |
jeraldabraham | 420dbf0 | 2014-04-25 22:58:31 -0700 | [diff] [blame] | 80 | setQuietLogging() |
| 81 | { |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 82 | m_wantQuiet = true; |
jeraldabraham | 420dbf0 | 2014-04-25 22:58:31 -0700 | [diff] [blame] | 83 | } |
| 84 | |
Davide Pesavento | 306e5bc | 2019-01-26 16:20:34 -0500 | [diff] [blame] | 85 | int |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 86 | run() |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 87 | { |
Alexander Lane | e9fe187 | 2023-07-25 20:00:23 -0400 | [diff] [blame] | 88 | m_logger.initialize(std::to_string(ndn::random::generateWord32()), m_timestampFormat); |
Davide Pesavento | 306e5bc | 2019-01-26 16:20:34 -0500 | [diff] [blame] | 89 | |
| 90 | if (!readConfigurationFile(m_configurationFile, m_trafficPatterns, m_logger)) { |
| 91 | return 2; |
| 92 | } |
| 93 | |
| 94 | if (!checkTrafficPatternCorrectness()) { |
| 95 | m_logger.log("ERROR: Traffic configuration provided is not proper", false, true); |
| 96 | return 2; |
| 97 | } |
| 98 | |
Alexander Lane | e9fe187 | 2023-07-25 20:00:23 -0400 | [diff] [blame] | 99 | m_logger.log("Traffic configuration file processing completed\n", true, false); |
Davide Pesavento | 306e5bc | 2019-01-26 16:20:34 -0500 | [diff] [blame] | 100 | for (std::size_t i = 0; i < m_trafficPatterns.size(); i++) { |
Davide Pesavento | ef06489 | 2022-04-05 02:26:03 -0400 | [diff] [blame] | 101 | m_logger.log("Traffic Pattern Type #" + std::to_string(i + 1), false, false); |
Davide Pesavento | 306e5bc | 2019-01-26 16:20:34 -0500 | [diff] [blame] | 102 | m_trafficPatterns[i].printTrafficConfiguration(m_logger); |
| 103 | m_logger.log("", false, false); |
| 104 | } |
Davide Pesavento | d0b5998 | 2015-02-27 19:15:15 +0100 | [diff] [blame] | 105 | |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 106 | if (m_nMaximumInterests == 0) { |
| 107 | logStatistics(); |
Davide Pesavento | 306e5bc | 2019-01-26 16:20:34 -0500 | [diff] [blame] | 108 | return 0; |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 109 | } |
Davide Pesavento | d0b5998 | 2015-02-27 19:15:15 +0100 | [diff] [blame] | 110 | |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 111 | m_signalSet.async_wait([this] (const boost::system::error_code&, int) { |
| 112 | if (m_nMaximumInterests && m_nInterestsReceived < *m_nMaximumInterests) { |
| 113 | m_hasError = true; |
| 114 | } |
| 115 | stop(); |
| 116 | }); |
| 117 | |
| 118 | for (std::size_t id = 0; id < m_trafficPatterns.size(); id++) { |
| 119 | m_registeredPrefixes.push_back( |
| 120 | m_face.setInterestFilter(m_trafficPatterns[id].m_name, |
Davide Pesavento | ef06489 | 2022-04-05 02:26:03 -0400 | [diff] [blame] | 121 | [this, id] (auto&&, const auto& interest) { onInterest(interest, id); }, |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 122 | nullptr, |
Davide Pesavento | ef06489 | 2022-04-05 02:26:03 -0400 | [diff] [blame] | 123 | [this, id] (auto&&, const auto& reason) { onRegisterFailed(reason, id); })); |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | try { |
| 127 | m_face.processEvents(); |
Davide Pesavento | 306e5bc | 2019-01-26 16:20:34 -0500 | [diff] [blame] | 128 | return m_hasError ? 1 : 0; |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 129 | } |
| 130 | catch (const std::exception& e) { |
| 131 | m_logger.log("ERROR: "s + e.what(), true, true); |
Davide Pesavento | 915ce80 | 2023-09-17 18:10:52 -0400 | [diff] [blame^] | 132 | m_io.stop(); |
Davide Pesavento | 306e5bc | 2019-01-26 16:20:34 -0500 | [diff] [blame] | 133 | return 1; |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 134 | } |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 135 | } |
| 136 | |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 137 | private: |
| 138 | class DataTrafficConfiguration |
| 139 | { |
| 140 | public: |
| 141 | void |
| 142 | printTrafficConfiguration(Logger& logger) const |
| 143 | { |
| 144 | std::ostringstream os; |
| 145 | |
| 146 | if (!m_name.empty()) { |
| 147 | os << "Name=" << m_name << ", "; |
| 148 | } |
| 149 | if (m_contentDelay >= 0_ms) { |
| 150 | os << "ContentDelay=" << m_contentDelay.count() << ", "; |
| 151 | } |
| 152 | if (m_freshnessPeriod >= 0_ms) { |
| 153 | os << "FreshnessPeriod=" << m_freshnessPeriod.count() << ", "; |
| 154 | } |
| 155 | if (m_contentType) { |
| 156 | os << "ContentType=" << *m_contentType << ", "; |
| 157 | } |
| 158 | if (m_contentLength) { |
| 159 | os << "ContentBytes=" << *m_contentLength << ", "; |
| 160 | } |
| 161 | if (!m_content.empty()) { |
| 162 | os << "Content=" << m_content << ", "; |
| 163 | } |
| 164 | os << "SigningInfo=" << m_signingInfo; |
| 165 | |
| 166 | logger.log(os.str(), false, false); |
| 167 | } |
| 168 | |
| 169 | bool |
Davide Pesavento | 306e5bc | 2019-01-26 16:20:34 -0500 | [diff] [blame] | 170 | parseConfigurationLine(const std::string& line, Logger& logger, int lineNumber) |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 171 | { |
| 172 | std::string parameter, value; |
Davide Pesavento | 306e5bc | 2019-01-26 16:20:34 -0500 | [diff] [blame] | 173 | if (!extractParameterAndValue(line, parameter, value)) { |
Davide Pesavento | ef06489 | 2022-04-05 02:26:03 -0400 | [diff] [blame] | 174 | logger.log("Line " + std::to_string(lineNumber) + " - Invalid syntax: " + line, |
Davide Pesavento | 306e5bc | 2019-01-26 16:20:34 -0500 | [diff] [blame] | 175 | false, true); |
| 176 | return false; |
| 177 | } |
| 178 | |
| 179 | if (parameter == "Name") { |
| 180 | m_name = value; |
| 181 | } |
| 182 | else if (parameter == "ContentDelay") { |
| 183 | m_contentDelay = time::milliseconds(std::stoul(value)); |
| 184 | } |
| 185 | else if (parameter == "FreshnessPeriod") { |
| 186 | m_freshnessPeriod = time::milliseconds(std::stoul(value)); |
| 187 | } |
| 188 | else if (parameter == "ContentType") { |
| 189 | m_contentType = std::stoul(value); |
| 190 | } |
| 191 | else if (parameter == "ContentBytes") { |
| 192 | m_contentLength = std::stoul(value); |
| 193 | } |
| 194 | else if (parameter == "Content") { |
| 195 | m_content = value; |
| 196 | } |
| 197 | else if (parameter == "SigningInfo") { |
Davide Pesavento | ef06489 | 2022-04-05 02:26:03 -0400 | [diff] [blame] | 198 | m_signingInfo = ndn::security::SigningInfo(value); |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 199 | } |
| 200 | else { |
Davide Pesavento | ef06489 | 2022-04-05 02:26:03 -0400 | [diff] [blame] | 201 | logger.log("Line " + std::to_string(lineNumber) + " - Ignoring unknown parameter: " + parameter, |
Davide Pesavento | 306e5bc | 2019-01-26 16:20:34 -0500 | [diff] [blame] | 202 | false, true); |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 203 | } |
| 204 | return true; |
| 205 | } |
| 206 | |
| 207 | bool |
| 208 | checkTrafficDetailCorrectness() const |
| 209 | { |
| 210 | return true; |
| 211 | } |
| 212 | |
| 213 | public: |
| 214 | std::string m_name; |
| 215 | time::milliseconds m_contentDelay = -1_ms; |
| 216 | time::milliseconds m_freshnessPeriod = -1_ms; |
Davide Pesavento | ef06489 | 2022-04-05 02:26:03 -0400 | [diff] [blame] | 217 | std::optional<uint32_t> m_contentType; |
| 218 | std::optional<std::size_t> m_contentLength; |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 219 | std::string m_content; |
Davide Pesavento | ef06489 | 2022-04-05 02:26:03 -0400 | [diff] [blame] | 220 | ndn::security::SigningInfo m_signingInfo; |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 221 | uint64_t m_nInterestsReceived = 0; |
| 222 | }; |
| 223 | |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 224 | void |
| 225 | logStatistics() |
| 226 | { |
Alexander Lane | e9fe187 | 2023-07-25 20:00:23 -0400 | [diff] [blame] | 227 | using std::to_string; |
| 228 | |
| 229 | m_logger.log("\n\n== Traffic Report ==\n", false, true); |
| 230 | m_logger.log("Total Traffic Pattern Types = " + to_string(m_trafficPatterns.size()), false, true); |
| 231 | m_logger.log("Total Interests Received = " + to_string(m_nInterestsReceived) + "\n", false, true); |
Davide Pesavento | d0b5998 | 2015-02-27 19:15:15 +0100 | [diff] [blame] | 232 | |
Eric Newberry | c8e1858 | 2018-05-31 19:27:01 -0700 | [diff] [blame] | 233 | for (std::size_t patternId = 0; patternId < m_trafficPatterns.size(); patternId++) { |
Alexander Lane | e9fe187 | 2023-07-25 20:00:23 -0400 | [diff] [blame] | 234 | const auto& pattern = m_trafficPatterns[patternId]; |
| 235 | |
| 236 | m_logger.log("Traffic Pattern Type #" + to_string(patternId + 1), false, true); |
| 237 | pattern.printTrafficConfiguration(m_logger); |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 238 | m_logger.log("Total Interests Received = " + |
Alexander Lane | e9fe187 | 2023-07-25 20:00:23 -0400 | [diff] [blame] | 239 | to_string(pattern.m_nInterestsReceived) + "\n", false, true); |
Eric Newberry | c8e1858 | 2018-05-31 19:27:01 -0700 | [diff] [blame] | 240 | } |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | bool |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 244 | checkTrafficPatternCorrectness() const |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 245 | { |
Davide Pesavento | 306e5bc | 2019-01-26 16:20:34 -0500 | [diff] [blame] | 246 | // TODO |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 247 | return true; |
| 248 | } |
| 249 | |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 250 | static std::string |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 251 | getRandomByteString(std::size_t length) |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 252 | { |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 253 | // per ISO C++ std, cannot instantiate uniform_int_distribution with char |
| 254 | static std::uniform_int_distribution<short> dist(std::numeric_limits<char>::min(), |
| 255 | std::numeric_limits<char>::max()); |
| 256 | |
| 257 | std::string s; |
| 258 | s.reserve(length); |
| 259 | for (std::size_t i = 0; i < length; i++) { |
Davide Pesavento | ef06489 | 2022-04-05 02:26:03 -0400 | [diff] [blame] | 260 | s += static_cast<char>(dist(ndn::random::getRandomNumberEngine())); |
Eric Newberry | c8e1858 | 2018-05-31 19:27:01 -0700 | [diff] [blame] | 261 | } |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 262 | return s; |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | void |
Davide Pesavento | ef06489 | 2022-04-05 02:26:03 -0400 | [diff] [blame] | 266 | onInterest(const ndn::Interest& interest, std::size_t patternId) |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 267 | { |
Spencer Lee | e7a5b74 | 2015-10-29 02:18:11 -0700 | [diff] [blame] | 268 | auto& pattern = m_trafficPatterns[patternId]; |
| 269 | |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 270 | if (!m_nMaximumInterests || m_nInterestsReceived < *m_nMaximumInterests) { |
Davide Pesavento | ef06489 | 2022-04-05 02:26:03 -0400 | [diff] [blame] | 271 | ndn::Data data(interest.getName()); |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 272 | |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 273 | if (pattern.m_freshnessPeriod >= 0_ms) |
Eric Newberry | c8e1858 | 2018-05-31 19:27:01 -0700 | [diff] [blame] | 274 | data.setFreshnessPeriod(pattern.m_freshnessPeriod); |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 275 | |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 276 | if (pattern.m_contentType) |
| 277 | data.setContentType(*pattern.m_contentType); |
| 278 | |
Eric Newberry | c8e1858 | 2018-05-31 19:27:01 -0700 | [diff] [blame] | 279 | std::string content; |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 280 | if (pattern.m_contentLength > 0) |
| 281 | content = getRandomByteString(*pattern.m_contentLength); |
Eric Newberry | c8e1858 | 2018-05-31 19:27:01 -0700 | [diff] [blame] | 282 | if (!pattern.m_content.empty()) |
| 283 | content = pattern.m_content; |
Davide Pesavento | ef06489 | 2022-04-05 02:26:03 -0400 | [diff] [blame] | 284 | data.setContent(ndn::makeStringBlock(ndn::tlv::Content, content)); |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 285 | |
Eric Newberry | c8e1858 | 2018-05-31 19:27:01 -0700 | [diff] [blame] | 286 | m_keyChain.sign(data, pattern.m_signingInfo); |
Spencer Lee | e7a5b74 | 2015-10-29 02:18:11 -0700 | [diff] [blame] | 287 | |
Eric Newberry | c8e1858 | 2018-05-31 19:27:01 -0700 | [diff] [blame] | 288 | m_nInterestsReceived++; |
| 289 | pattern.m_nInterestsReceived++; |
Davide Pesavento | d0b5998 | 2015-02-27 19:15:15 +0100 | [diff] [blame] | 290 | |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 291 | if (!m_wantQuiet) { |
Alexander Lane | e9fe187 | 2023-07-25 20:00:23 -0400 | [diff] [blame] | 292 | auto logLine = "Interest Received - PatternType=" + std::to_string(patternId + 1) + |
Davide Pesavento | ef06489 | 2022-04-05 02:26:03 -0400 | [diff] [blame] | 293 | ", GlobalID=" + std::to_string(m_nInterestsReceived) + |
| 294 | ", LocalID=" + std::to_string(pattern.m_nInterestsReceived) + |
Alexander Lane | e9fe187 | 2023-07-25 20:00:23 -0400 | [diff] [blame] | 295 | ", Name=" + interest.getName().toUri(); |
Eric Newberry | c8e1858 | 2018-05-31 19:27:01 -0700 | [diff] [blame] | 296 | m_logger.log(logLine, true, false); |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 297 | } |
Eric Newberry | c8e1858 | 2018-05-31 19:27:01 -0700 | [diff] [blame] | 298 | |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 299 | if (pattern.m_contentDelay > 0_ms) |
| 300 | boost::this_thread::sleep_for(pattern.m_contentDelay); |
| 301 | if (m_contentDelay > 0_ms) |
| 302 | boost::this_thread::sleep_for(m_contentDelay); |
| 303 | |
Eric Newberry | c8e1858 | 2018-05-31 19:27:01 -0700 | [diff] [blame] | 304 | m_face.put(data); |
| 305 | } |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 306 | |
| 307 | if (m_nMaximumInterests && m_nInterestsReceived >= *m_nMaximumInterests) { |
Eric Newberry | c8e1858 | 2018-05-31 19:27:01 -0700 | [diff] [blame] | 308 | logStatistics(); |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 309 | m_registeredPrefixes.clear(); |
Eric Newberry | 5145940 | 2018-06-28 00:06:18 -0700 | [diff] [blame] | 310 | m_signalSet.cancel(); |
Eric Newberry | c8e1858 | 2018-05-31 19:27:01 -0700 | [diff] [blame] | 311 | } |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | void |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 315 | onRegisterFailed(const std::string& reason, std::size_t patternId) |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 316 | { |
Davide Pesavento | ef06489 | 2022-04-05 02:26:03 -0400 | [diff] [blame] | 317 | auto logLine = "Prefix registration failed - PatternType=" + std::to_string(patternId + 1) + |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 318 | ", Name=" + m_trafficPatterns[patternId].m_name + |
| 319 | ", Reason=" + reason; |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 320 | m_logger.log(logLine, true, true); |
Davide Pesavento | d0b5998 | 2015-02-27 19:15:15 +0100 | [diff] [blame] | 321 | |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 322 | m_nRegistrationsFailed++; |
Eric Newberry | c8e1858 | 2018-05-31 19:27:01 -0700 | [diff] [blame] | 323 | if (m_nRegistrationsFailed == m_trafficPatterns.size()) { |
| 324 | m_hasError = true; |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 325 | stop(); |
Eric Newberry | c8e1858 | 2018-05-31 19:27:01 -0700 | [diff] [blame] | 326 | } |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | void |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 330 | stop() |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 331 | { |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 332 | logStatistics(); |
| 333 | m_face.shutdown(); |
Davide Pesavento | 915ce80 | 2023-09-17 18:10:52 -0400 | [diff] [blame^] | 334 | m_io.stop(); |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 335 | } |
| 336 | |
| 337 | private: |
Alexander Lane | e9fe187 | 2023-07-25 20:00:23 -0400 | [diff] [blame] | 338 | Logger m_logger{"NdnTrafficServer"}; |
Davide Pesavento | 915ce80 | 2023-09-17 18:10:52 -0400 | [diff] [blame^] | 339 | boost::asio::io_context m_io; |
| 340 | boost::asio::signal_set m_signalSet{m_io, SIGINT, SIGTERM}; |
| 341 | ndn::Face m_face{m_io}; |
Davide Pesavento | ef06489 | 2022-04-05 02:26:03 -0400 | [diff] [blame] | 342 | ndn::KeyChain m_keyChain; |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 343 | |
| 344 | std::string m_configurationFile; |
Alexander Lane | e9fe187 | 2023-07-25 20:00:23 -0400 | [diff] [blame] | 345 | std::string m_timestampFormat; |
Davide Pesavento | ef06489 | 2022-04-05 02:26:03 -0400 | [diff] [blame] | 346 | std::optional<uint64_t> m_nMaximumInterests; |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 347 | time::milliseconds m_contentDelay = 0_ms; |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 348 | |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 349 | std::vector<DataTrafficConfiguration> m_trafficPatterns; |
Davide Pesavento | ef06489 | 2022-04-05 02:26:03 -0400 | [diff] [blame] | 350 | std::vector<ndn::ScopedRegisteredPrefixHandle> m_registeredPrefixes; |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 351 | uint64_t m_nRegistrationsFailed = 0; |
| 352 | uint64_t m_nInterestsReceived = 0; |
Davide Pesavento | ef06489 | 2022-04-05 02:26:03 -0400 | [diff] [blame] | 353 | |
| 354 | bool m_wantQuiet = false; |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 355 | bool m_hasError = false; |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 356 | }; |
| 357 | |
Davide Pesavento | ef06489 | 2022-04-05 02:26:03 -0400 | [diff] [blame] | 358 | } // namespace ndntg |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 359 | |
Alexander Lane | e9fe187 | 2023-07-25 20:00:23 -0400 | [diff] [blame] | 360 | namespace po = boost::program_options; |
| 361 | |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 362 | static void |
Davide Pesavento | ef06489 | 2022-04-05 02:26:03 -0400 | [diff] [blame] | 363 | usage(std::ostream& os, std::string_view programName, const po::options_description& desc) |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 364 | { |
| 365 | os << "Usage: " << programName << " [options] <Traffic_Configuration_File>\n" |
| 366 | << "\n" |
| 367 | << "Respond to Interests as per provided Traffic_Configuration_File.\n" |
| 368 | << "Multiple prefixes can be configured for handling.\n" |
| 369 | << "Set the environment variable NDN_TRAFFIC_LOGFOLDER to redirect output to a log file.\n" |
| 370 | << "\n" |
| 371 | << desc; |
| 372 | } |
| 373 | |
Alexander Afanasyev | fda32a3 | 2014-03-20 10:50:00 -0700 | [diff] [blame] | 374 | int |
| 375 | main(int argc, char* argv[]) |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 376 | { |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 377 | std::string configFile; |
Alexander Lane | e9fe187 | 2023-07-25 20:00:23 -0400 | [diff] [blame] | 378 | std::string timestampFormat; |
Davide Pesavento | d0b5998 | 2015-02-27 19:15:15 +0100 | [diff] [blame] | 379 | |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 380 | po::options_description visibleOptions("Options"); |
| 381 | visibleOptions.add_options() |
| 382 | ("help,h", "print this help message and exit") |
Alexander Lane | e9fe187 | 2023-07-25 20:00:23 -0400 | [diff] [blame] | 383 | ("count,c", po::value<int64_t>(), "maximum number of Interests to respond to") |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 384 | ("delay,d", po::value<ndn::time::milliseconds::rep>()->default_value(0), |
| 385 | "wait this amount of milliseconds before responding to each Interest") |
Alexander Lane | e9fe187 | 2023-07-25 20:00:23 -0400 | [diff] [blame] | 386 | ("timestamp-format,t", po::value<std::string>(×tampFormat), "format string for timestamp output") |
| 387 | ("quiet,q", po::bool_switch(), "turn off logging of Interest reception and Data generation") |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 388 | ; |
| 389 | |
| 390 | po::options_description hiddenOptions; |
| 391 | hiddenOptions.add_options() |
| 392 | ("config-file", po::value<std::string>(&configFile)) |
| 393 | ; |
| 394 | |
| 395 | po::positional_options_description posOptions; |
| 396 | posOptions.add("config-file", -1); |
| 397 | |
| 398 | po::options_description allOptions; |
| 399 | allOptions.add(visibleOptions).add(hiddenOptions); |
| 400 | |
| 401 | po::variables_map vm; |
| 402 | try { |
| 403 | po::store(po::command_line_parser(argc, argv).options(allOptions).positional(posOptions).run(), vm); |
| 404 | po::notify(vm); |
| 405 | } |
| 406 | catch (const po::error& e) { |
| 407 | std::cerr << "ERROR: " << e.what() << std::endl; |
| 408 | return 2; |
| 409 | } |
| 410 | catch (const boost::bad_any_cast& e) { |
| 411 | std::cerr << "ERROR: " << e.what() << std::endl; |
| 412 | return 2; |
| 413 | } |
| 414 | |
| 415 | if (vm.count("help") > 0) { |
| 416 | usage(std::cout, argv[0], visibleOptions); |
| 417 | return 0; |
| 418 | } |
| 419 | |
| 420 | if (configFile.empty()) { |
| 421 | usage(std::cerr, argv[0], visibleOptions); |
| 422 | return 2; |
| 423 | } |
| 424 | |
Alexander Lane | e9fe187 | 2023-07-25 20:00:23 -0400 | [diff] [blame] | 425 | ndntg::NdnTrafficServer server(std::move(configFile)); |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 426 | |
| 427 | if (vm.count("count") > 0) { |
Alexander Lane | e9fe187 | 2023-07-25 20:00:23 -0400 | [diff] [blame] | 428 | auto count = vm["count"].as<int64_t>(); |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 429 | if (count < 0) { |
Alexander Lane | e9fe187 | 2023-07-25 20:00:23 -0400 | [diff] [blame] | 430 | std::cerr << "ERROR: the argument for option '--count' cannot be negative\n"; |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 431 | return 2; |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 432 | } |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 433 | server.setMaximumInterests(static_cast<uint64_t>(count)); |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 434 | } |
| 435 | |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 436 | if (vm.count("delay") > 0) { |
| 437 | ndn::time::milliseconds delay(vm["delay"].as<ndn::time::milliseconds::rep>()); |
Davide Pesavento | ef06489 | 2022-04-05 02:26:03 -0400 | [diff] [blame] | 438 | if (delay < 0_ms) { |
Alexander Lane | e9fe187 | 2023-07-25 20:00:23 -0400 | [diff] [blame] | 439 | std::cerr << "ERROR: the argument for option '--delay' cannot be negative\n"; |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 440 | return 2; |
| 441 | } |
| 442 | server.setContentDelay(delay); |
Eric Newberry | c8e1858 | 2018-05-31 19:27:01 -0700 | [diff] [blame] | 443 | } |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 444 | |
Alexander Lane | e9fe187 | 2023-07-25 20:00:23 -0400 | [diff] [blame] | 445 | if (!timestampFormat.empty()) { |
| 446 | server.setTimestampFormat(std::move(timestampFormat)); |
| 447 | } |
| 448 | |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 449 | if (vm["quiet"].as<bool>()) { |
| 450 | server.setQuietLogging(); |
| 451 | } |
| 452 | |
Davide Pesavento | 306e5bc | 2019-01-26 16:20:34 -0500 | [diff] [blame] | 453 | return server.run(); |
Eric Newberry | c8e1858 | 2018-05-31 19:27:01 -0700 | [diff] [blame] | 454 | } |