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