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