Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /** |
| 3 | * Copyright (C) 2014 University of Arizona. |
| 4 | * |
| 5 | * GNU 3.0 License, see the LICENSE file for more information |
| 6 | * |
| 7 | * Author: Jerald Paul Abraham <jeraldabraham@email.arizona.edu> |
| 8 | */ |
| 9 | |
| 10 | #include <sstream> |
jeraldabraham | 420dbf0 | 2014-04-25 22:58:31 -0700 | [diff] [blame^] | 11 | |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 12 | #include <boost/asio.hpp> |
| 13 | #include <boost/filesystem.hpp> |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 14 | #include <boost/lexical_cast.hpp> |
jeraldabraham | 420dbf0 | 2014-04-25 22:58:31 -0700 | [diff] [blame^] | 15 | #include <boost/noncopyable.hpp> |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 16 | |
jeraldabraham | 420dbf0 | 2014-04-25 22:58:31 -0700 | [diff] [blame^] | 17 | #include <ndn-cxx/face.hpp> |
| 18 | #include <ndn-cxx/security/key-chain.hpp> |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 19 | |
| 20 | #include "logger.hpp" |
| 21 | |
| 22 | namespace ndn { |
| 23 | |
jeraldabraham | 420dbf0 | 2014-04-25 22:58:31 -0700 | [diff] [blame^] | 24 | class NdnTrafficServer : boost::noncopyable |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 25 | { |
| 26 | public: |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 27 | |
| 28 | explicit |
Alexander Afanasyev | fda32a3 | 2014-03-20 10:50:00 -0700 | [diff] [blame] | 29 | NdnTrafficServer(char* programName) |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 30 | : m_logger("NdnTrafficServer") |
| 31 | , m_programName(programName) |
| 32 | , m_hasError(false) |
jeraldabraham | 420dbf0 | 2014-04-25 22:58:31 -0700 | [diff] [blame^] | 33 | , m_hasQuietLogging(false) |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 34 | , m_nRegistrationsFailed(0) |
| 35 | , m_nMaximumInterests(-1) |
| 36 | , m_nInterestsReceived(0) |
| 37 | , m_contentDelay(time::milliseconds(-1)) |
| 38 | , m_ioService(new boost::asio::io_service) |
| 39 | , m_face(m_ioService) |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 40 | { |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 41 | m_instanceId = boost::lexical_cast<std::string>(std::rand()); |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | class DataTrafficConfiguration |
| 45 | { |
| 46 | public: |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 47 | DataTrafficConfiguration() |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 48 | : m_contentType(-1) |
| 49 | , m_freshnessPeriod(time::milliseconds(-1)) |
| 50 | , m_contentBytes(-1) |
| 51 | , m_contentDelay(time::milliseconds(-1)) |
| 52 | , m_nInterestsReceived(0) |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 53 | { |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | void |
Alexander Afanasyev | fda32a3 | 2014-03-20 10:50:00 -0700 | [diff] [blame] | 57 | printTrafficConfiguration(Logger& logger) |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 58 | { |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 59 | std::string detail = ""; |
| 60 | if (m_name != "") |
| 61 | detail += "Name=" + m_name + ", "; |
| 62 | if (m_contentType >= 0) |
| 63 | detail += "ContentType=" + boost::lexical_cast<std::string>(m_contentType) + ", "; |
| 64 | if (m_freshnessPeriod >= time::milliseconds(0)) |
| 65 | detail += "FreshnessPeriod=" + |
| 66 | boost::lexical_cast<std::string>(static_cast<int>(m_freshnessPeriod.count())) + ", "; |
| 67 | if (m_contentBytes >= 0) |
| 68 | detail += "ContentBytes=" + boost::lexical_cast<std::string>(m_contentBytes) + ", "; |
| 69 | if (m_contentDelay >= time::milliseconds(0)) |
| 70 | detail += "ContentDelay=" + |
| 71 | boost::lexical_cast<std::string>(m_contentDelay.count()) + ", "; |
| 72 | if (m_content != "") |
| 73 | detail += "Content=" + m_content + ", "; |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 74 | if (detail.length() >= 2) |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 75 | detail = detail.substr(0, detail.length() - 2); |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 76 | logger.log(detail, false, false); |
| 77 | } |
| 78 | |
| 79 | |
| 80 | bool |
Alexander Afanasyev | fda32a3 | 2014-03-20 10:50:00 -0700 | [diff] [blame] | 81 | extractParameterValue(const std::string& detail, |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 82 | std::string& parameter, |
| 83 | std::string& value) |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 84 | { |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 85 | std::string allowedCharacters = ":/+._-%"; |
| 86 | parameter = ""; |
| 87 | value = ""; |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 88 | int i = 0; |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 89 | while (detail[i] != '=' && i < detail.length()) |
| 90 | { |
| 91 | parameter += detail[i]; |
| 92 | i++; |
| 93 | } |
| 94 | if (i == detail.length()) |
| 95 | return false; |
| 96 | i++; |
Alexander Afanasyev | fda32a3 | 2014-03-20 10:50:00 -0700 | [diff] [blame] | 97 | while ((std::isalnum(detail[i]) || |
| 98 | allowedCharacters.find(detail[i]) != std::string::npos) && |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 99 | i < detail.length()) |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 100 | { |
| 101 | value += detail[i]; |
| 102 | i++; |
| 103 | } |
| 104 | if(parameter == "" || value == "") |
| 105 | return false; |
| 106 | return true; |
| 107 | } |
| 108 | |
| 109 | bool |
Alexander Afanasyev | fda32a3 | 2014-03-20 10:50:00 -0700 | [diff] [blame] | 110 | processConfigurationDetail(const std::string& detail, |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 111 | Logger& logger, |
| 112 | int lineNumber) |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 113 | { |
| 114 | std::string parameter, value; |
| 115 | if (extractParameterValue(detail, parameter, value)) |
| 116 | { |
| 117 | if (parameter == "Name") |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 118 | m_name = value; |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 119 | else if (parameter == "ContentType") |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 120 | m_contentType = boost::lexical_cast<int>(value); |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 121 | else if (parameter == "FreshnessPeriod") |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 122 | m_freshnessPeriod = time::milliseconds(boost::lexical_cast<int>(value)); |
| 123 | else if (parameter == "ContentDelay") |
| 124 | m_contentDelay = time::milliseconds(boost::lexical_cast<int>(value)); |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 125 | else if (parameter == "ContentBytes") |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 126 | m_contentBytes = boost::lexical_cast<int>(value); |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 127 | else if (parameter == "Content") |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 128 | m_content = value; |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 129 | else |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 130 | logger.log("Line " + boost::lexical_cast<std::string>(lineNumber) + |
| 131 | " \t- Invalid Parameter='" + parameter + "'", false, true); |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 132 | } |
| 133 | else |
| 134 | { |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 135 | logger.log("Line " + boost::lexical_cast<std::string>(lineNumber) + |
| 136 | " \t- Improper Traffic Configuration Line - " + detail, false, true); |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 137 | return false; |
| 138 | } |
| 139 | return true; |
| 140 | } |
| 141 | |
| 142 | bool |
| 143 | checkTrafficDetailCorrectness() |
| 144 | { |
| 145 | return true; |
| 146 | } |
| 147 | |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 148 | std::string m_name; |
| 149 | int m_contentType; |
| 150 | time::milliseconds m_freshnessPeriod; |
| 151 | int m_contentBytes; |
| 152 | time::milliseconds m_contentDelay; |
| 153 | std::string m_content; |
| 154 | int m_nInterestsReceived; |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 155 | |
| 156 | }; |
| 157 | |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 158 | void |
| 159 | usage() |
| 160 | { |
| 161 | |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 162 | std::cout << "\nUsage: " << m_programName << " [options] <Traffic_Configuration_File>\n" |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 163 | "Respond to Interest as per provided Traffic Configuration File\n" |
| 164 | "Multiple Prefixes can be configured for handling.\n" |
| 165 | "Set environment variable NDN_TRAFFIC_LOGFOLDER for redirecting output to a log.\n" |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 166 | " [-d interval] - set delay before responding to interest in milliseconds\n" |
| 167 | " [-c count] - specify maximum number of interests to be satisfied\n" |
jeraldabraham | 420dbf0 | 2014-04-25 22:58:31 -0700 | [diff] [blame^] | 168 | " [-q] - quiet logging - no interest reception/data generation messages\n" |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 169 | " [-h] - print help and exit\n\n"; |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 170 | exit(1); |
| 171 | |
| 172 | } |
| 173 | |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 174 | void |
| 175 | setMaximumInterests(int maximumInterests) |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 176 | { |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 177 | if (maximumInterests < 0) |
| 178 | usage(); |
| 179 | m_nMaximumInterests = maximumInterests; |
| 180 | } |
| 181 | |
| 182 | bool |
| 183 | hasError() const |
| 184 | { |
| 185 | return m_hasError; |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | void |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 189 | setContentDelay(int contentDelay) |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 190 | { |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 191 | if (contentDelay < 0) |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 192 | usage(); |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 193 | m_contentDelay = time::milliseconds(contentDelay); |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | void |
Alexander Afanasyev | fda32a3 | 2014-03-20 10:50:00 -0700 | [diff] [blame] | 197 | setConfigurationFile(char* configurationFile) |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 198 | { |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 199 | m_configurationFile = configurationFile; |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | void |
jeraldabraham | 420dbf0 | 2014-04-25 22:58:31 -0700 | [diff] [blame^] | 203 | setQuietLogging() |
| 204 | { |
| 205 | m_hasQuietLogging = true; |
| 206 | } |
| 207 | |
| 208 | void |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 209 | signalHandler() |
| 210 | { |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 211 | logStatistics(); |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 212 | m_logger.shutdownLogger(); |
| 213 | m_face.shutdown(); |
| 214 | m_ioService->stop(); |
| 215 | if (m_hasError) |
| 216 | exit(1); |
| 217 | else |
| 218 | exit(0); |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | void |
| 222 | logStatistics() |
| 223 | { |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 224 | m_logger.log("\n\n== Interest Traffic Report ==\n", false, true); |
Alexander Afanasyev | fda32a3 | 2014-03-20 10:50:00 -0700 | [diff] [blame] | 225 | m_logger.log("Total Traffic Pattern Types = " + |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 226 | boost::lexical_cast<std::string>(m_trafficPatterns.size()), false, true); |
Alexander Afanasyev | fda32a3 | 2014-03-20 10:50:00 -0700 | [diff] [blame] | 227 | m_logger.log("Total Interests Received = " + |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 228 | boost::lexical_cast<std::string>(m_nInterestsReceived), false, true); |
| 229 | if (m_nInterestsReceived < m_nMaximumInterests) |
| 230 | m_hasError = true; |
| 231 | for (int patternId = 0; patternId < m_trafficPatterns.size(); patternId++) |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 232 | { |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 233 | m_logger.log("\nTraffic Pattern Type #" + |
| 234 | boost::lexical_cast<std::string>(patternId + 1), false, true); |
| 235 | m_trafficPatterns[patternId].printTrafficConfiguration(m_logger); |
Alexander Afanasyev | fda32a3 | 2014-03-20 10:50:00 -0700 | [diff] [blame] | 236 | m_logger.log("Total Interests Received = " + |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 237 | boost::lexical_cast<std::string>( |
| 238 | m_trafficPatterns[patternId].m_nInterestsReceived) + "\n", false, true); |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 239 | } |
| 240 | } |
| 241 | |
| 242 | bool |
| 243 | checkTrafficPatternCorrectness() |
| 244 | { |
| 245 | return true; |
| 246 | } |
| 247 | |
| 248 | void |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 249 | parseConfigurationFile() |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 250 | { |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 251 | std::string patternLine; |
| 252 | std::ifstream patternFile; |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 253 | m_logger.log("Analyzing Traffic Configuration File: " + m_configurationFile, true, true); |
| 254 | patternFile.open(m_configurationFile.c_str()); |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 255 | if (patternFile.is_open()) |
| 256 | { |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 257 | int patternId = 0; |
| 258 | int lineNumber = 0; |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 259 | while (getline(patternFile, patternLine)) |
| 260 | { |
| 261 | lineNumber++; |
| 262 | if (std::isalpha(patternLine[0])) |
| 263 | { |
| 264 | DataTrafficConfiguration dataData; |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 265 | bool shouldSkipLine = false; |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 266 | patternId++; |
| 267 | if (dataData.processConfigurationDetail(patternLine, m_logger, lineNumber)) |
| 268 | { |
| 269 | while (getline(patternFile, patternLine) && std::isalpha(patternLine[0])) |
| 270 | { |
| 271 | lineNumber++; |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 272 | if (!dataData.processConfigurationDetail(patternLine, |
| 273 | m_logger, |
| 274 | lineNumber)) |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 275 | { |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 276 | shouldSkipLine = true; |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 277 | break; |
| 278 | } |
| 279 | } |
| 280 | lineNumber++; |
| 281 | } |
| 282 | else |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 283 | shouldSkipLine = true; |
| 284 | if (!shouldSkipLine) |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 285 | { |
| 286 | if (dataData.checkTrafficDetailCorrectness()) |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 287 | m_trafficPatterns.push_back(dataData); |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 288 | } |
| 289 | } |
| 290 | } |
| 291 | patternFile.close(); |
| 292 | if (!checkTrafficPatternCorrectness()) |
| 293 | { |
Alexander Afanasyev | fda32a3 | 2014-03-20 10:50:00 -0700 | [diff] [blame] | 294 | m_logger.log("ERROR - Traffic Configuration Provided Is Not Proper- " + |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 295 | m_configurationFile, false, true); |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 296 | m_logger.shutdownLogger(); |
| 297 | exit(1); |
| 298 | } |
| 299 | m_logger.log("Traffic Configuration File Processing Completed\n", true, false); |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 300 | for (patternId = 0; patternId < m_trafficPatterns.size(); patternId++) |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 301 | { |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 302 | m_logger.log("Traffic Pattern Type #" + |
| 303 | boost::lexical_cast<std::string>(patternId + 1), false, false); |
| 304 | m_trafficPatterns[patternId].printTrafficConfiguration(m_logger); |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 305 | m_logger.log("", false, false); |
| 306 | } |
| 307 | } |
| 308 | else |
| 309 | { |
Alexander Afanasyev | fda32a3 | 2014-03-20 10:50:00 -0700 | [diff] [blame] | 310 | m_logger.log("ERROR - Unable To Open Traffic Configuration File: " + |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 311 | m_configurationFile, false, true); |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 312 | m_logger.shutdownLogger(); |
| 313 | exit(1); |
| 314 | } |
| 315 | } |
| 316 | |
| 317 | void |
| 318 | initializeTrafficConfiguration() |
| 319 | { |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 320 | if (boost::filesystem::exists(boost::filesystem::path(m_configurationFile))) |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 321 | { |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 322 | if (boost::filesystem::is_regular_file(boost::filesystem::path(m_configurationFile))) |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 323 | { |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 324 | parseConfigurationFile(); |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 325 | } |
| 326 | else |
| 327 | { |
Alexander Afanasyev | fda32a3 | 2014-03-20 10:50:00 -0700 | [diff] [blame] | 328 | m_logger.log("ERROR - Traffic Configuration File Is Not A Regular File: " + |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 329 | m_configurationFile, false, true); |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 330 | m_logger.shutdownLogger(); |
| 331 | exit(1); |
| 332 | } |
| 333 | } |
| 334 | else |
| 335 | { |
Alexander Afanasyev | fda32a3 | 2014-03-20 10:50:00 -0700 | [diff] [blame] | 336 | m_logger.log("ERROR - Traffic Configuration File Does Not Exist: " + |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 337 | m_configurationFile, false, true); |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 338 | m_logger.shutdownLogger(); |
| 339 | exit(1); |
| 340 | } |
| 341 | } |
| 342 | |
| 343 | static std::string |
Alexander Afanasyev | fda32a3 | 2014-03-20 10:50:00 -0700 | [diff] [blame] | 344 | getRandomByteString(int randomSize) |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 345 | { |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 346 | std::string randomString; |
| 347 | for (int i = 0; i < randomSize; i++) |
| 348 | randomString += static_cast<char>(std::rand() % 128); |
| 349 | return randomString; |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | void |
Alexander Afanasyev | fda32a3 | 2014-03-20 10:50:00 -0700 | [diff] [blame] | 353 | onInterest(const Name& name, const Interest& interest, int patternId) |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 354 | { |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 355 | if (m_nMaximumInterests < 0 || m_nInterestsReceived < m_nMaximumInterests) |
| 356 | { |
| 357 | Data data(interest.getName()); |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 358 | |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 359 | if (m_trafficPatterns[patternId].m_contentType >= 0) |
| 360 | data.setContentType(m_trafficPatterns[patternId].m_contentType); |
| 361 | |
| 362 | if (m_trafficPatterns[patternId].m_freshnessPeriod >= time::milliseconds(0)) |
| 363 | data.setFreshnessPeriod(m_trafficPatterns[patternId].m_freshnessPeriod); |
| 364 | |
| 365 | std::string content = ""; |
| 366 | if (m_trafficPatterns[patternId].m_contentBytes >= 0) |
| 367 | content = getRandomByteString(m_trafficPatterns[patternId].m_contentBytes); |
| 368 | if (m_trafficPatterns[patternId].m_content != "") |
| 369 | content = m_trafficPatterns[patternId].m_content; |
| 370 | |
| 371 | data.setContent(reinterpret_cast<const uint8_t*>(content.c_str()), content.length()); |
| 372 | m_keyChain.sign(data); |
| 373 | m_nInterestsReceived++; |
| 374 | m_trafficPatterns[patternId].m_nInterestsReceived++; |
| 375 | std::string logLine = "Interest Received - PatternType=" + |
| 376 | boost::lexical_cast<std::string>(patternId + 1); |
| 377 | logLine += ", GlobalID=" + boost::lexical_cast<std::string>(m_nInterestsReceived); |
| 378 | logLine += ", LocalID=" + |
| 379 | boost::lexical_cast<std::string>(m_trafficPatterns[patternId].m_nInterestsReceived); |
| 380 | logLine += ", Name=" + m_trafficPatterns[patternId].m_name; |
jeraldabraham | 420dbf0 | 2014-04-25 22:58:31 -0700 | [diff] [blame^] | 381 | if (!m_hasQuietLogging) |
| 382 | m_logger.log(logLine, true, false); |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 383 | if (m_trafficPatterns[patternId].m_contentDelay > time::milliseconds(-1)) |
| 384 | usleep(m_trafficPatterns[patternId].m_contentDelay.count() * 1000); |
| 385 | if (m_contentDelay > time::milliseconds(-1)) |
| 386 | usleep(m_contentDelay.count() * 1000); |
| 387 | m_face.put(data); |
| 388 | } |
| 389 | if (m_nMaximumInterests >= 0 && m_nInterestsReceived == m_nMaximumInterests) |
| 390 | { |
| 391 | logStatistics(); |
| 392 | m_logger.shutdownLogger(); |
| 393 | m_face.shutdown(); |
| 394 | m_ioService->stop(); |
| 395 | } |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 396 | } |
| 397 | |
| 398 | void |
Alexander Afanasyev | fda32a3 | 2014-03-20 10:50:00 -0700 | [diff] [blame] | 399 | onRegisterFailed(const ndn::Name& prefix, const std::string& reason, int patternId) |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 400 | { |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 401 | std::string logLine = ""; |
| 402 | logLine += "Prefix Registration Failed - PatternType=" + |
| 403 | boost::lexical_cast<std::string>(patternId + 1); |
| 404 | logLine += ", Name=" + m_trafficPatterns[patternId].m_name; |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 405 | m_logger.log(logLine, true, true); |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 406 | m_nRegistrationsFailed++; |
| 407 | if (m_nRegistrationsFailed == m_trafficPatterns.size()) |
| 408 | { |
| 409 | m_hasError = true; |
| 410 | signalHandler(); |
| 411 | } |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 412 | } |
| 413 | |
| 414 | void |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 415 | run() |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 416 | { |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 417 | boost::asio::signal_set signalSet(*m_ioService, SIGINT, SIGTERM); |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 418 | signalSet.async_wait(boost::bind(&NdnTrafficServer::signalHandler, this)); |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 419 | m_logger.initializeLog(m_instanceId); |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 420 | initializeTrafficConfiguration(); |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 421 | if (m_nMaximumInterests == 0) |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 422 | { |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 423 | logStatistics(); |
| 424 | m_logger.shutdownLogger(); |
| 425 | return; |
| 426 | } |
| 427 | |
| 428 | for (int patternId = 0; patternId < m_trafficPatterns.size(); patternId++) |
| 429 | { |
| 430 | m_face.setInterestFilter(m_trafficPatterns[patternId].m_name, |
| 431 | bind(&NdnTrafficServer::onInterest, |
| 432 | this, _1, _2, |
| 433 | patternId), |
| 434 | bind(&NdnTrafficServer::onRegisterFailed, |
| 435 | this, _1, _2, |
| 436 | patternId)); |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 437 | } |
| 438 | |
| 439 | try { |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 440 | m_face.processEvents(); |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 441 | } |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 442 | catch (std::exception& e) { |
| 443 | m_logger.log("ERROR: " + static_cast<std::string>(e.what()), true, true); |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 444 | m_logger.shutdownLogger(); |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 445 | m_hasError = true; |
| 446 | m_ioService->stop(); |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 447 | } |
| 448 | } |
| 449 | |
| 450 | private: |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 451 | KeyChain m_keyChain; |
| 452 | std::string m_programName; |
| 453 | bool m_hasError; |
jeraldabraham | 420dbf0 | 2014-04-25 22:58:31 -0700 | [diff] [blame^] | 454 | bool m_hasQuietLogging; |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 455 | std::string m_instanceId; |
| 456 | time::milliseconds m_contentDelay; |
| 457 | int m_nRegistrationsFailed; |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 458 | Logger m_logger; |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 459 | std::string m_configurationFile; |
| 460 | shared_ptr<boost::asio::io_service> m_ioService; |
| 461 | Face m_face; |
| 462 | std::vector<DataTrafficConfiguration> m_trafficPatterns; |
| 463 | int m_nMaximumInterests; |
| 464 | int m_nInterestsReceived; |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 465 | }; |
| 466 | |
| 467 | } // namespace ndn |
| 468 | |
Alexander Afanasyev | fda32a3 | 2014-03-20 10:50:00 -0700 | [diff] [blame] | 469 | int |
| 470 | main(int argc, char* argv[]) |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 471 | { |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 472 | std::srand(std::time(0)); |
| 473 | ndn::NdnTrafficServer ndnTrafficServer(argv[0]); |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 474 | int option; |
jeraldabraham | 420dbf0 | 2014-04-25 22:58:31 -0700 | [diff] [blame^] | 475 | while ((option = getopt(argc, argv, "hqc:d:")) != -1) { |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 476 | switch (option) { |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 477 | case 'h': |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 478 | ndnTrafficServer.usage(); |
| 479 | break; |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 480 | case 'c': |
| 481 | ndnTrafficServer.setMaximumInterests(atoi(optarg)); |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 482 | break; |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 483 | case 'd': |
| 484 | ndnTrafficServer.setContentDelay(atoi(optarg)); |
| 485 | break; |
jeraldabraham | 420dbf0 | 2014-04-25 22:58:31 -0700 | [diff] [blame^] | 486 | case 'q': |
| 487 | ndnTrafficServer.setQuietLogging(); |
| 488 | break; |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 489 | default: |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 490 | ndnTrafficServer.usage(); |
| 491 | break; |
| 492 | } |
| 493 | } |
| 494 | |
| 495 | argc -= optind; |
| 496 | argv += optind; |
| 497 | |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 498 | if (argv[0] == 0) |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 499 | ndnTrafficServer.usage(); |
| 500 | |
| 501 | ndnTrafficServer.setConfigurationFile(argv[0]); |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 502 | ndnTrafficServer.run(); |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 503 | |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 504 | if (ndnTrafficServer.hasError()) |
| 505 | return 1; |
| 506 | else |
| 507 | return 0; |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 508 | } |