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