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