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 <string> |
| 11 | #include <sstream> |
| 12 | #include <fstream> |
| 13 | #include <vector> |
| 14 | |
| 15 | #include "logger.hpp" |
| 16 | |
| 17 | #include <boost/asio.hpp> |
| 18 | #include <boost/filesystem.hpp> |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 19 | #include <boost/lexical_cast.hpp> |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 20 | #include <boost/date_time/posix_time/posix_time.hpp> |
| 21 | |
| 22 | #include <ndn-cpp-dev/face.hpp> |
| 23 | #include <ndn-cpp-dev/exclude.hpp> |
| 24 | #include <ndn-cpp-dev/name-component.hpp> |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 25 | |
| 26 | namespace ndn { |
| 27 | |
| 28 | class NdnTrafficClient |
| 29 | { |
| 30 | public: |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 31 | |
| 32 | explicit |
Alexander Afanasyev | fda32a3 | 2014-03-20 10:50:00 -0700 | [diff] [blame] | 33 | NdnTrafficClient(char* programName) |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 34 | : m_programName(programName) |
| 35 | , m_logger("NdnTrafficClient") |
| 36 | , m_hasError(false) |
| 37 | , m_ioService(new boost::asio::io_service) |
| 38 | , m_face(m_ioService) |
| 39 | , m_interestInterval(getDefaultInterestInterval()) |
| 40 | , m_nMaximumInterests(-1) |
| 41 | , m_nInterestsSent(0) |
| 42 | , m_nInterestsReceived(0) |
| 43 | , m_minimumInterestRoundTripTime(std::numeric_limits<double>::max()) |
| 44 | , m_maximumInterestRoundTripTime(0) |
| 45 | , m_totalInterestRoundTripTime(0) |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 46 | { |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 47 | m_instanceId = boost::lexical_cast<std::string>(std::rand()); |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | class InterestTrafficConfiguration |
| 51 | { |
| 52 | public: |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 53 | InterestTrafficConfiguration() |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 54 | : m_trafficPercentage(-1) |
| 55 | , m_nameAppendBytes(-1) |
| 56 | , m_nameAppendSequenceNumber(-1) |
| 57 | , m_minSuffixComponents(-1) |
| 58 | , m_maxSuffixComponents(-1) |
| 59 | , m_excludeBeforeBytes(-1) |
| 60 | , m_excludeAfterBytes(-1) |
| 61 | , m_childSelector(-1) |
| 62 | , m_mustBeFresh(-1) |
| 63 | , m_nonceDuplicationPercentage(-1) |
| 64 | , m_scope(-1) |
| 65 | , m_interestLifetime(getDefaultInterestLifetime()) |
| 66 | , m_nInterestsSent(-1) |
| 67 | , m_nInterestsReceived(-1) |
| 68 | , m_minimumInterestRoundTripTime(std::numeric_limits<double>::max()) |
| 69 | , m_maximumInterestRoundTripTime(0) |
| 70 | , m_totalInterestRoundTripTime(0) |
| 71 | , m_nContentInconsistencies(0) |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 72 | { |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | time::milliseconds |
| 76 | getDefaultInterestLifetime() |
| 77 | { |
| 78 | return time::milliseconds(-1); |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | void |
Alexander Afanasyev | fda32a3 | 2014-03-20 10:50:00 -0700 | [diff] [blame] | 82 | printTrafficConfiguration(Logger& logger) |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 83 | { |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 84 | std::string detail = ""; |
| 85 | if (m_trafficPercentage > 0) |
| 86 | detail += "TrafficPercentage=" + |
| 87 | boost::lexical_cast<std::string>(m_trafficPercentage) + ", "; |
| 88 | if (m_name != "") |
| 89 | detail += "Name=" + m_name + ", "; |
| 90 | if (m_nameAppendBytes > 0) |
| 91 | detail += "NameAppendBytes=" + boost::lexical_cast<std::string>(m_nameAppendBytes) + ", "; |
| 92 | if (m_nameAppendSequenceNumber > 0) |
| 93 | detail += "NameAppendSequenceNumber=" + |
| 94 | boost::lexical_cast<std::string>(m_nameAppendSequenceNumber) + ", "; |
| 95 | if (m_minSuffixComponents >= 0) |
| 96 | detail += "MinSuffixComponents=" + |
| 97 | boost::lexical_cast<std::string>(m_minSuffixComponents) + ", "; |
| 98 | if (m_maxSuffixComponents >= 0) |
| 99 | detail += "MaxSuffixComponents=" + |
| 100 | boost::lexical_cast<std::string>(m_maxSuffixComponents) + ", "; |
| 101 | if (m_excludeBefore != "") |
| 102 | detail += "ExcludeBefore=" + m_excludeBefore + ", "; |
| 103 | if (m_excludeAfter != "") |
| 104 | detail += "ExcludeAfter=" + m_excludeAfter + ", "; |
| 105 | if (m_excludeBeforeBytes > 0) |
| 106 | detail += "ExcludeBeforeBytes=" + |
| 107 | boost::lexical_cast<std::string>(m_excludeBeforeBytes) + ", "; |
| 108 | if (m_excludeAfterBytes > 0) |
| 109 | detail += "ExcludeAfterBytes=" + |
| 110 | boost::lexical_cast<std::string>(m_excludeAfterBytes) + ", "; |
| 111 | if (m_childSelector >= 0) |
| 112 | detail += "ChildSelector=" + |
| 113 | boost::lexical_cast<std::string>(m_childSelector) + ", "; |
| 114 | if (m_mustBeFresh >= 0) |
| 115 | detail += "MustBeFresh=" + |
| 116 | boost::lexical_cast<std::string>(m_mustBeFresh) + ", "; |
| 117 | if (m_nonceDuplicationPercentage > 0) |
| 118 | detail += "NonceDuplicationPercentage=" + |
| 119 | boost::lexical_cast<std::string>(m_nonceDuplicationPercentage) + ", "; |
| 120 | if (m_scope >= 0) |
| 121 | detail += "Scope="+boost::lexical_cast<std::string>(m_scope) + ", "; |
| 122 | if (m_interestLifetime >= time::milliseconds(0)) |
| 123 | detail += "InterestLifetime=" + |
| 124 | boost::lexical_cast<std::string>(m_interestLifetime.count()) + ", "; |
| 125 | if (m_expectedContent != "") |
| 126 | detail += "ExpectedContent=" + m_expectedContent + ", "; |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 127 | if (detail.length() >= 2) |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 128 | detail = detail.substr(0, detail.length() - 2); //Removing suffix ", " |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 129 | logger.log(detail, false, false); |
| 130 | } |
| 131 | |
| 132 | bool |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 133 | extractParameterValue(const std::string& detail, |
| 134 | std::string& parameter, |
| 135 | std::string& value) |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 136 | { |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 137 | std::string allowedCharacters = ":/+._-%"; |
| 138 | parameter = ""; |
| 139 | value = ""; |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 140 | int i = 0; |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 141 | while (detail[i] != '=' && i < detail.length()) |
| 142 | { |
| 143 | parameter += detail[i]; |
| 144 | i++; |
| 145 | } |
| 146 | if (i == detail.length()) |
| 147 | return false; |
| 148 | i++; |
jeraldabraham | 79c0c23 | 2014-03-31 21:43:59 -0700 | [diff] [blame^] | 149 | while ((std::isalnum(detail[i]) || |
| 150 | allowedCharacters.find(detail[i]) != std::string::npos) && |
| 151 | i < detail.length()) |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 152 | { |
| 153 | value += detail[i]; |
| 154 | i++; |
| 155 | } |
jeraldabraham | 79c0c23 | 2014-03-31 21:43:59 -0700 | [diff] [blame^] | 156 | if (parameter == "" || value == "") |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 157 | return false; |
| 158 | return true; |
| 159 | } |
| 160 | |
| 161 | bool |
Alexander Afanasyev | fda32a3 | 2014-03-20 10:50:00 -0700 | [diff] [blame] | 162 | processConfigurationDetail(const std::string& detail, Logger& logger, int lineNumber) |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 163 | { |
| 164 | std::string parameter, value; |
| 165 | if (extractParameterValue(detail, parameter, value)) |
| 166 | { |
| 167 | if (parameter == "TrafficPercentage") |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 168 | m_trafficPercentage = boost::lexical_cast<int>(value); |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 169 | else if (parameter == "Name") |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 170 | m_name = value; |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 171 | else if (parameter == "NameAppendBytes") |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 172 | m_nameAppendBytes = boost::lexical_cast<int>(value); |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 173 | else if (parameter == "NameAppendSequenceNumber") |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 174 | m_nameAppendSequenceNumber = boost::lexical_cast<int>(value); |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 175 | else if (parameter == "MinSuffixComponents") |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 176 | m_minSuffixComponents = boost::lexical_cast<int>(value); |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 177 | else if (parameter == "MaxSuffixComponents") |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 178 | m_maxSuffixComponents = boost::lexical_cast<int>(value); |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 179 | else if (parameter == "ExcludeBefore") |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 180 | m_excludeBefore = value; |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 181 | else if (parameter == "ExcludeAfter") |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 182 | m_excludeAfter = value; |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 183 | else if (parameter == "ExcludeBeforeBytes") |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 184 | m_excludeBeforeBytes = boost::lexical_cast<int>(value); |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 185 | else if (parameter == "ExcludeAfterBytes") |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 186 | m_excludeAfterBytes = boost::lexical_cast<int>(value); |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 187 | else if (parameter == "ChildSelector") |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 188 | m_childSelector = boost::lexical_cast<int>(value); |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 189 | else if (parameter == "MustBeFresh") |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 190 | m_mustBeFresh = boost::lexical_cast<int>(value); |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 191 | else if (parameter == "NonceDuplicationPercentage") |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 192 | m_nonceDuplicationPercentage = boost::lexical_cast<int>(value); |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 193 | else if (parameter == "Scope") |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 194 | m_scope = boost::lexical_cast<int>(value); |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 195 | else if (parameter == "InterestLifetime") |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 196 | m_interestLifetime = time::milliseconds(boost::lexical_cast<int>(value)); |
jeraldabraham | 473ef3d | 2014-03-06 12:40:35 -0700 | [diff] [blame] | 197 | else if (parameter == "ExpectedContent") |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 198 | m_expectedContent = value; |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 199 | else |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 200 | logger.log("Line " + boost::lexical_cast<std::string>(lineNumber) + |
| 201 | " \t- Invalid Parameter='" + parameter + "'", false, true); |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 202 | } |
| 203 | else |
| 204 | { |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 205 | logger.log("Line " + boost::lexical_cast<std::string>(lineNumber) + |
| 206 | " \t- Improper Traffic Configuration Line- " + detail, false, true); |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 207 | return false; |
| 208 | } |
| 209 | return true; |
| 210 | } |
| 211 | |
| 212 | bool |
| 213 | checkTrafficDetailCorrectness() |
| 214 | { |
| 215 | return true; |
| 216 | } |
| 217 | |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 218 | int m_trafficPercentage; |
| 219 | std::string m_name; |
| 220 | int m_nameAppendBytes; |
| 221 | int m_nameAppendSequenceNumber; |
| 222 | int m_minSuffixComponents; |
| 223 | int m_maxSuffixComponents; |
| 224 | std::string m_excludeBefore; |
| 225 | std::string m_excludeAfter; |
| 226 | int m_excludeBeforeBytes; |
| 227 | int m_excludeAfterBytes; |
| 228 | int m_childSelector; |
| 229 | int m_mustBeFresh; |
| 230 | int m_nonceDuplicationPercentage; |
| 231 | int m_scope; |
| 232 | time::milliseconds m_interestLifetime; |
| 233 | int m_nInterestsSent; |
| 234 | int m_nInterestsReceived; |
| 235 | |
| 236 | //round trip time is stored as milliseconds with fractional |
| 237 | //sub-millisecond precision |
| 238 | double m_minimumInterestRoundTripTime; |
| 239 | double m_maximumInterestRoundTripTime; |
| 240 | double m_totalInterestRoundTripTime; |
| 241 | |
| 242 | int m_nContentInconsistencies; |
| 243 | std::string m_expectedContent; |
Alexander Afanasyev | fda32a3 | 2014-03-20 10:50:00 -0700 | [diff] [blame] | 244 | }; // class InterestTrafficConfiguration |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 245 | |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 246 | bool |
| 247 | hasError() const |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 248 | { |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 249 | return m_hasError; |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | void |
| 253 | usage() |
| 254 | { |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 255 | std::cout << "\nUsage: " << m_programName << " [options] <Traffic_Configuration_File>\n" |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 256 | "Generate Interest Traffic as per provided Traffic Configuration File\n" |
| 257 | "Interests are continuously generated unless a total number is specified.\n" |
| 258 | "Set environment variable NDN_TRAFFIC_LOGFOLDER for redirecting output to a log.\n" |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 259 | " [-i interval] - set interest generation interval in milliseconds (default " |
| 260 | << getDefaultInterestInterval() << ")\n" |
| 261 | " [-c count] - set total number of interests to be generated\n" |
| 262 | " [-h] - print help and exit\n\n"; |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 263 | exit(1); |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 264 | } |
| 265 | |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 266 | time::milliseconds |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 267 | getDefaultInterestInterval() |
| 268 | { |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 269 | return time::milliseconds(1000); |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | void |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 273 | setInterestInterval(int interestInterval) |
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 | if (interestInterval <= 0) |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 276 | usage(); |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 277 | m_interestInterval = time::milliseconds(interestInterval); |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | void |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 281 | setMaximumInterests(int maximumInterests) |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 282 | { |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 283 | if (maximumInterests <= 0) |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 284 | usage(); |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 285 | m_nMaximumInterests = maximumInterests; |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | void |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 289 | setConfigurationFile(char* configurationFile) |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 290 | { |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 291 | m_configurationFile = configurationFile; |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | void |
| 295 | signalHandler() |
| 296 | { |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 297 | logStatistics(); |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 298 | m_logger.shutdownLogger(); |
| 299 | m_face.shutdown(); |
| 300 | m_ioService->stop(); |
| 301 | if (m_hasError) |
| 302 | exit(1); |
| 303 | else |
| 304 | exit(0); |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | void |
| 308 | logStatistics() |
| 309 | { |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 310 | m_logger.log("\n\n== Interest Traffic Report ==\n", false, true); |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 311 | m_logger.log("Total Traffic Pattern Types = " + |
| 312 | boost::lexical_cast<std::string>(static_cast<int>(m_trafficPatterns.size())), false, true); |
| 313 | m_logger.log("Total Interests Sent = " + |
| 314 | boost::lexical_cast<std::string>(m_nInterestsSent), false, true); |
| 315 | m_logger.log("Total Responses Received = " + |
| 316 | boost::lexical_cast<std::string>(m_nInterestsReceived), false, true); |
| 317 | double loss = 0; |
| 318 | if (m_nInterestsSent > 0) |
| 319 | loss = (m_nInterestsSent - m_nInterestsReceived) * 100.0 / m_nInterestsSent; |
| 320 | m_logger.log("Total Interest Loss = " + |
| 321 | boost::lexical_cast<std::string>(loss) + "%", false, true); |
| 322 | if (m_nContentInconsistencies != 0 || m_nInterestsSent != m_nInterestsReceived) |
| 323 | m_hasError = true; |
| 324 | double average = 0; |
| 325 | double inconsistency = 0; |
| 326 | if (m_nInterestsReceived > 0) |
Alexander Afanasyev | fda32a3 | 2014-03-20 10:50:00 -0700 | [diff] [blame] | 327 | { |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 328 | average = m_totalInterestRoundTripTime / m_nInterestsReceived; |
| 329 | inconsistency = m_nContentInconsistencies * 100.0 / m_nInterestsReceived; |
Alexander Afanasyev | fda32a3 | 2014-03-20 10:50:00 -0700 | [diff] [blame] | 330 | } |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 331 | m_logger.log("Total Data Inconsistency = " + |
| 332 | boost::lexical_cast<std::string>(inconsistency) + "%", false, true); |
| 333 | m_logger.log("Total Round Trip Time = " + |
| 334 | boost::lexical_cast<std::string>(m_totalInterestRoundTripTime) + "ms", false, true); |
| 335 | m_logger.log("Average Round Trip Time = " + |
| 336 | boost::lexical_cast<std::string>(average) + "ms\n", false, true); |
| 337 | |
| 338 | for (int patternId = 0; patternId < m_trafficPatterns.size(); patternId++) |
Alexander Afanasyev | fda32a3 | 2014-03-20 10:50:00 -0700 | [diff] [blame] | 339 | { |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 340 | m_logger.log("Traffic Pattern Type #" + |
| 341 | boost::lexical_cast<std::string>(patternId + 1), false, true); |
| 342 | m_trafficPatterns[patternId].printTrafficConfiguration(m_logger); |
| 343 | m_logger.log("Total Interests Sent = " + |
| 344 | boost::lexical_cast<std::string>( |
| 345 | m_trafficPatterns[patternId].m_nInterestsSent), false, true); |
| 346 | m_logger.log("Total Responses Received = " + |
| 347 | boost::lexical_cast<std::string>( |
| 348 | m_trafficPatterns[patternId].m_nInterestsReceived), false, true); |
| 349 | loss = 0; |
| 350 | if (m_trafficPatterns[patternId].m_nInterestsSent > 0) |
| 351 | { |
| 352 | loss = (m_trafficPatterns[patternId].m_nInterestsSent - |
| 353 | m_trafficPatterns[patternId].m_nInterestsReceived); |
| 354 | loss *= 100.0; |
| 355 | loss /= m_trafficPatterns[patternId].m_nInterestsSent; |
| 356 | } |
| 357 | m_logger.log("Total Interest Loss = " + |
| 358 | boost::lexical_cast<std::string>(loss) + "%", false, true); |
Alexander Afanasyev | fda32a3 | 2014-03-20 10:50:00 -0700 | [diff] [blame] | 359 | average = 0; |
| 360 | inconsistency = 0; |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 361 | if (m_trafficPatterns[patternId].m_nInterestsReceived > 0) |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 362 | { |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 363 | average = (m_trafficPatterns[patternId].m_totalInterestRoundTripTime / |
| 364 | m_trafficPatterns[patternId].m_nInterestsReceived); |
| 365 | inconsistency = m_trafficPatterns[patternId].m_nContentInconsistencies; |
| 366 | inconsistency = |
| 367 | inconsistency * 100.0 / m_trafficPatterns[patternId].m_nInterestsReceived; |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 368 | } |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 369 | m_logger.log("Total Data Inconsistency = " + |
| 370 | boost::lexical_cast<std::string>(inconsistency) + "%", false, true); |
Alexander Afanasyev | fda32a3 | 2014-03-20 10:50:00 -0700 | [diff] [blame] | 371 | m_logger.log("Total Round Trip Time = " + |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 372 | boost::lexical_cast<std::string>( |
| 373 | m_trafficPatterns[patternId].m_totalInterestRoundTripTime) + "ms", false, true); |
| 374 | m_logger.log("Average Round Trip Time = " + |
| 375 | boost::lexical_cast<std::string>(average) + "ms\n", false, true); |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 376 | } |
| 377 | } |
| 378 | |
| 379 | bool |
| 380 | checkTrafficPatternCorrectness() |
| 381 | { |
| 382 | return true; |
| 383 | } |
| 384 | |
| 385 | void |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 386 | parseConfigurationFile() |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 387 | { |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 388 | std::string patternLine; |
| 389 | std::ifstream patternFile; |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 390 | m_logger.log("Analyzing Traffic Configuration File: " + m_configurationFile, true, true); |
| 391 | patternFile.open(m_configurationFile.c_str()); |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 392 | if (patternFile.is_open()) |
| 393 | { |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 394 | int patternId = 0; |
| 395 | int lineNumber = 0; |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 396 | while (getline(patternFile, patternLine)) |
| 397 | { |
| 398 | lineNumber++; |
| 399 | if (std::isalpha(patternLine[0])) |
| 400 | { |
| 401 | InterestTrafficConfiguration interestData; |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 402 | bool shouldSkipLine = false; |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 403 | patternId++; |
| 404 | if (interestData.processConfigurationDetail(patternLine, m_logger, lineNumber)) |
| 405 | { |
| 406 | while (getline(patternFile, patternLine) && std::isalpha(patternLine[0])) |
| 407 | { |
| 408 | lineNumber++; |
Alexander Afanasyev | fda32a3 | 2014-03-20 10:50:00 -0700 | [diff] [blame] | 409 | if (!interestData.processConfigurationDetail(patternLine, |
| 410 | m_logger, lineNumber)) |
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 | shouldSkipLine = true; |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 413 | break; |
| 414 | } |
| 415 | } |
| 416 | lineNumber++; |
| 417 | } |
| 418 | else |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 419 | shouldSkipLine = true; |
| 420 | if (!shouldSkipLine) |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 421 | { |
| 422 | if (interestData.checkTrafficDetailCorrectness()) |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 423 | m_trafficPatterns.push_back(interestData); |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 424 | } |
| 425 | } |
| 426 | } |
| 427 | patternFile.close(); |
| 428 | if (!checkTrafficPatternCorrectness()) |
| 429 | { |
Alexander Afanasyev | fda32a3 | 2014-03-20 10:50:00 -0700 | [diff] [blame] | 430 | m_logger.log("ERROR - Traffic Configuration Provided Is Not Proper- " + |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 431 | m_configurationFile, false, true); |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 432 | m_logger.shutdownLogger(); |
| 433 | exit(1); |
| 434 | } |
| 435 | m_logger.log("Traffic Configuration File Processing Completed\n", true, false); |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 436 | for (patternId = 0; patternId < m_trafficPatterns.size(); patternId++) |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 437 | { |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 438 | m_logger.log("Traffic Pattern Type #" + |
| 439 | boost::lexical_cast<std::string>(patternId + 1), false, false); |
| 440 | m_trafficPatterns[patternId].printTrafficConfiguration(m_logger); |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 441 | m_logger.log("", false, false); |
| 442 | } |
| 443 | } |
| 444 | else |
| 445 | { |
Alexander Afanasyev | fda32a3 | 2014-03-20 10:50:00 -0700 | [diff] [blame] | 446 | m_logger.log("ERROR - Unable To Open Traffic Configuration File: " + |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 447 | m_configurationFile, false, true); |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 448 | m_logger.shutdownLogger(); |
| 449 | exit(1); |
| 450 | } |
| 451 | } |
| 452 | |
| 453 | void |
| 454 | initializeTrafficConfiguration() |
| 455 | { |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 456 | if (boost::filesystem::exists(boost::filesystem::path(m_configurationFile))) |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 457 | { |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 458 | if (boost::filesystem::is_regular_file(boost::filesystem::path(m_configurationFile))) |
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 | parseConfigurationFile(); |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 461 | } |
| 462 | else |
| 463 | { |
Alexander Afanasyev | fda32a3 | 2014-03-20 10:50:00 -0700 | [diff] [blame] | 464 | m_logger.log("ERROR - Traffic Configuration File Is Not A Regular File: " + |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 465 | m_configurationFile, false, true); |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 466 | m_logger.shutdownLogger(); |
| 467 | exit(1); |
| 468 | } |
| 469 | } |
| 470 | else |
| 471 | { |
Alexander Afanasyev | fda32a3 | 2014-03-20 10:50:00 -0700 | [diff] [blame] | 472 | m_logger.log("ERROR - Traffic Configuration File Does Not Exist: " + |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 473 | m_configurationFile, false, true); |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 474 | m_logger.shutdownLogger(); |
| 475 | exit(1); |
| 476 | } |
| 477 | } |
| 478 | |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 479 | uint32_t |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 480 | getOldNonce() |
| 481 | { |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 482 | if (m_nonces.size() == 0) |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 483 | return getNewNonce(); |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 484 | int randomNonceIndex = std::rand() % m_nonces.size(); |
| 485 | return m_nonces[randomNonceIndex]; |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 486 | } |
| 487 | |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 488 | uint32_t |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 489 | getNewNonce() |
| 490 | { |
jeraldabraham | 473ef3d | 2014-03-06 12:40:35 -0700 | [diff] [blame] | 491 | //Performance Enhancement |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 492 | if (m_nonces.size() > 1000) |
| 493 | m_nonces.clear(); |
jeraldabraham | 473ef3d | 2014-03-06 12:40:35 -0700 | [diff] [blame] | 494 | |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 495 | uint32_t randomNonce = static_cast<uint32_t>(std::rand()); |
| 496 | while (std::find(m_nonces.begin(), m_nonces.end(), randomNonce) != m_nonces.end()) |
| 497 | randomNonce = static_cast<uint32_t>(std::rand()); |
| 498 | |
| 499 | m_nonces.push_back(randomNonce); |
| 500 | return randomNonce; |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 501 | } |
| 502 | |
| 503 | static std::string |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 504 | getRandomByteString(int randomSize) |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 505 | { |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 506 | std::string randomString; |
| 507 | for (int i = 0; i < randomSize; i++) |
| 508 | randomString += static_cast<char>(std::rand() % 128); |
| 509 | return randomString; |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 510 | } |
| 511 | |
| 512 | void |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 513 | onData(const ndn::Interest& interest, |
| 514 | ndn::Data& data, |
| 515 | int globalReference, |
| 516 | int localReference, |
| 517 | int patternId, |
| 518 | time::steady_clock::TimePoint sentTime) |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 519 | { |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 520 | std::string logLine = |
| 521 | "Data Received - PatternType=" + boost::lexical_cast<std::string>(patternId+1); |
| 522 | logLine += ", GlobalID=" + boost::lexical_cast<std::string>(globalReference); |
| 523 | logLine += ", LocalID=" + boost::lexical_cast<std::string>(localReference); |
| 524 | logLine += ", Name=" + interest.getName().toUri(); |
| 525 | |
| 526 | m_nInterestsReceived++; |
| 527 | m_trafficPatterns[patternId].m_nInterestsReceived++; |
| 528 | if (m_trafficPatterns[patternId].m_expectedContent != "") |
jeraldabraham | 473ef3d | 2014-03-06 12:40:35 -0700 | [diff] [blame] | 529 | { |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 530 | std::string receivedContent = reinterpret_cast<const char*>(data.getContent().value()); |
| 531 | int receivedContentLength = data.getContent().value_size(); |
Alexander Afanasyev | fda32a3 | 2014-03-20 10:50:00 -0700 | [diff] [blame] | 532 | receivedContent = receivedContent.substr(0, receivedContentLength); |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 533 | if (receivedContent != m_trafficPatterns[patternId].m_expectedContent) |
Alexander Afanasyev | fda32a3 | 2014-03-20 10:50:00 -0700 | [diff] [blame] | 534 | { |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 535 | m_nContentInconsistencies++; |
| 536 | m_trafficPatterns[patternId].m_nContentInconsistencies++; |
Alexander Afanasyev | fda32a3 | 2014-03-20 10:50:00 -0700 | [diff] [blame] | 537 | logLine += ", IsConsistent=No"; |
| 538 | } |
| 539 | else |
| 540 | logLine += ", IsConsistent=Yes"; |
jeraldabraham | 473ef3d | 2014-03-06 12:40:35 -0700 | [diff] [blame] | 541 | } |
jeraldabraham | 473ef3d | 2014-03-06 12:40:35 -0700 | [diff] [blame] | 542 | else |
| 543 | logLine += ", IsConsistent=NotChecked"; |
| 544 | m_logger.log(logLine, true, false); |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 545 | double roundTripTime = (time::steady_clock::now() - sentTime).count() / 1000000.0; |
| 546 | if (m_minimumInterestRoundTripTime > roundTripTime) |
| 547 | m_minimumInterestRoundTripTime = roundTripTime; |
| 548 | if (m_maximumInterestRoundTripTime < roundTripTime) |
| 549 | m_maximumInterestRoundTripTime = roundTripTime; |
| 550 | if (m_trafficPatterns[patternId].m_minimumInterestRoundTripTime > roundTripTime) |
| 551 | m_trafficPatterns[patternId].m_minimumInterestRoundTripTime = roundTripTime; |
| 552 | if (m_trafficPatterns[patternId].m_maximumInterestRoundTripTime < roundTripTime) |
| 553 | m_trafficPatterns[patternId].m_maximumInterestRoundTripTime = roundTripTime; |
| 554 | m_totalInterestRoundTripTime += roundTripTime; |
| 555 | m_trafficPatterns[patternId].m_totalInterestRoundTripTime += roundTripTime; |
| 556 | if (m_nMaximumInterests >= 0 && m_nInterestsSent == m_nMaximumInterests) |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 557 | { |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 558 | logStatistics(); |
| 559 | m_logger.shutdownLogger(); |
| 560 | m_face.shutdown(); |
| 561 | m_ioService->stop(); |
| 562 | } |
| 563 | } |
| 564 | |
| 565 | void |
| 566 | onTimeout(const ndn::Interest& interest, |
| 567 | int globalReference, |
| 568 | int localReference, |
| 569 | int patternId) |
| 570 | { |
| 571 | std::string logLine = "Interest Timed Out - PatternType=" + |
| 572 | boost::lexical_cast<std::string>(patternId + 1); |
| 573 | logLine += ", GlobalID=" + boost::lexical_cast<std::string>(globalReference); |
| 574 | logLine += ", LocalID=" + boost::lexical_cast<std::string>(localReference); |
| 575 | logLine += ", Name=" + interest.getName().toUri(); |
| 576 | m_logger.log(logLine, true, false); |
| 577 | if (m_nMaximumInterests >= 0 && m_nInterestsSent == m_nMaximumInterests) |
| 578 | { |
| 579 | logStatistics(); |
| 580 | m_logger.shutdownLogger(); |
| 581 | m_face.shutdown(); |
| 582 | m_ioService->stop(); |
| 583 | } |
| 584 | } |
| 585 | |
| 586 | void |
| 587 | generateTraffic(boost::asio::deadline_timer* deadlineTimer) |
| 588 | { |
| 589 | if (m_nMaximumInterests < 0 || m_nInterestsSent < m_nMaximumInterests) |
| 590 | { |
| 591 | int trafficKey = std::rand() % 100; |
| 592 | int cumulativePercentage = 0; |
| 593 | int patternId; |
| 594 | for (patternId = 0; patternId < m_trafficPatterns.size(); patternId++) |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 595 | { |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 596 | cumulativePercentage += m_trafficPatterns[patternId].m_trafficPercentage; |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 597 | if (trafficKey <= cumulativePercentage) |
| 598 | { |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 599 | Name interestName(m_trafficPatterns[patternId].m_name); |
| 600 | if (m_trafficPatterns[patternId].m_nameAppendBytes > 0) |
| 601 | interestName.append( |
| 602 | getRandomByteString(m_trafficPatterns[patternId].m_nameAppendBytes)); |
| 603 | if (m_trafficPatterns[patternId].m_nameAppendSequenceNumber >= 0) |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 604 | { |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 605 | interestName.append( |
| 606 | boost::lexical_cast<std::string>( |
| 607 | m_trafficPatterns[patternId].m_nameAppendSequenceNumber)); |
| 608 | m_trafficPatterns[patternId].m_nameAppendSequenceNumber++; |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 609 | } |
| 610 | Interest interest(interestName); |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 611 | if (m_trafficPatterns[patternId].m_minSuffixComponents >= 0) |
| 612 | interest.setMinSuffixComponents( |
| 613 | m_trafficPatterns[patternId].m_minSuffixComponents); |
| 614 | if (m_trafficPatterns[patternId].m_maxSuffixComponents >= 0) |
| 615 | interest.setMaxSuffixComponents( |
| 616 | m_trafficPatterns[patternId].m_maxSuffixComponents); |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 617 | Exclude exclude; |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 618 | if (m_trafficPatterns[patternId].m_excludeBefore != "" && |
| 619 | m_trafficPatterns[patternId].m_excludeAfter != "") |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 620 | { |
Alexander Afanasyev | fda32a3 | 2014-03-20 10:50:00 -0700 | [diff] [blame] | 621 | exclude.excludeRange( |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 622 | name::Component( |
| 623 | m_trafficPatterns[patternId].m_excludeAfter), |
| 624 | name::Component(m_trafficPatterns[patternId].m_excludeBefore)); |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 625 | interest.setExclude(exclude); |
| 626 | } |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 627 | else if (m_trafficPatterns[patternId].m_excludeBefore != "") |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 628 | { |
Alexander Afanasyev | fda32a3 | 2014-03-20 10:50:00 -0700 | [diff] [blame] | 629 | exclude.excludeBefore( |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 630 | name::Component(m_trafficPatterns[patternId].m_excludeBefore)); |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 631 | interest.setExclude(exclude); |
| 632 | } |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 633 | else if (m_trafficPatterns[patternId].m_excludeAfter != "") |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 634 | { |
Alexander Afanasyev | fda32a3 | 2014-03-20 10:50:00 -0700 | [diff] [blame] | 635 | exclude.excludeAfter( |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 636 | name::Component(m_trafficPatterns[patternId].m_excludeAfter)); |
| 637 | interest.setExclude(exclude); |
| 638 | } |
| 639 | if (m_trafficPatterns[patternId].m_excludeBeforeBytes > 0 && |
| 640 | m_trafficPatterns[patternId].m_excludeAfterBytes > 0) |
| 641 | { |
| 642 | exclude.excludeRange( |
| 643 | name::Component( |
| 644 | getRandomByteString( |
| 645 | m_trafficPatterns[patternId].m_excludeAfterBytes)), |
| 646 | name::Component( |
| 647 | getRandomByteString( |
| 648 | m_trafficPatterns[patternId].m_excludeBeforeBytes))); |
| 649 | interest.setExclude(exclude); |
| 650 | } |
| 651 | else if (m_trafficPatterns[patternId].m_excludeBeforeBytes > 0) |
| 652 | { |
| 653 | exclude.excludeBefore( |
| 654 | name::Component( |
| 655 | getRandomByteString( |
| 656 | m_trafficPatterns[patternId].m_excludeBeforeBytes))); |
| 657 | interest.setExclude(exclude); |
| 658 | } |
| 659 | else if (m_trafficPatterns[patternId].m_excludeAfterBytes > 0) |
| 660 | { |
| 661 | exclude.excludeAfter( |
| 662 | name::Component( |
| 663 | getRandomByteString( |
| 664 | m_trafficPatterns[patternId].m_excludeAfterBytes))); |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 665 | interest.setExclude(exclude); |
| 666 | } |
| 667 | |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 668 | if (m_trafficPatterns[patternId].m_childSelector >= 0) |
| 669 | interest.setChildSelector(m_trafficPatterns[patternId].m_childSelector); |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 670 | |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 671 | if (m_trafficPatterns[patternId].m_mustBeFresh == 0) |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 672 | interest.setMustBeFresh(false); |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 673 | else if (m_trafficPatterns[patternId].m_mustBeFresh > 0) |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 674 | interest.setMustBeFresh(true); |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 675 | if (m_trafficPatterns[patternId].m_nonceDuplicationPercentage > 0) |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 676 | { |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 677 | int duplicationPercentage = std::rand() % 100; |
| 678 | if (m_trafficPatterns[patternId].m_nonceDuplicationPercentage <= |
| 679 | duplicationPercentage) |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 680 | interest.setNonce(getOldNonce()); |
| 681 | else |
| 682 | interest.setNonce(getNewNonce()); |
| 683 | } |
| 684 | else |
| 685 | interest.setNonce(getNewNonce()); |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 686 | if (m_trafficPatterns[patternId].m_scope >= 0) |
| 687 | interest.setScope(m_trafficPatterns[patternId].m_scope); |
| 688 | if (m_trafficPatterns[patternId].m_interestLifetime >= time::milliseconds(0)) |
| 689 | interest.setInterestLifetime(m_trafficPatterns[patternId].m_interestLifetime); |
| 690 | |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 691 | try { |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 692 | m_nInterestsSent++; |
| 693 | m_trafficPatterns[patternId].m_nInterestsSent++; |
| 694 | time::steady_clock::TimePoint sentTime = time::steady_clock::now(); |
| 695 | m_face.expressInterest(interest, |
| 696 | bind(&NdnTrafficClient::onData, |
| 697 | this, _1, _2, m_nInterestsSent, |
| 698 | m_trafficPatterns[patternId].m_nInterestsSent, |
| 699 | patternId, sentTime), |
| 700 | bind(&NdnTrafficClient::onTimeout, |
| 701 | this, _1, m_nInterestsSent, |
| 702 | m_trafficPatterns[patternId].m_nInterestsSent, |
Alexander Afanasyev | fda32a3 | 2014-03-20 10:50:00 -0700 | [diff] [blame] | 703 | patternId)); |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 704 | std::string logLine = ""; |
| 705 | logLine += "Sending Interest - PatternType=" + |
| 706 | boost::lexical_cast<std::string>(patternId+1); |
| 707 | logLine += ", GlobalID=" + boost::lexical_cast<std::string>(m_nInterestsSent); |
| 708 | logLine += ", LocalID=" + boost::lexical_cast<std::string>( |
| 709 | m_trafficPatterns[patternId].m_nInterestsSent); |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 710 | logLine += ", Name="+interest.getName().toUri(); |
| 711 | m_logger.log(logLine, true, false); |
| 712 | deadlineTimer->expires_at(deadlineTimer->expires_at() + |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 713 | boost::posix_time::millisec( |
| 714 | m_interestInterval.count())); |
Alexander Afanasyev | fda32a3 | 2014-03-20 10:50:00 -0700 | [diff] [blame] | 715 | deadlineTimer->async_wait(bind(&NdnTrafficClient::generateTraffic, |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 716 | this, deadlineTimer)); |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 717 | } |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 718 | catch (std::exception& e) { |
| 719 | m_logger.log("ERROR: " + static_cast<std::string>(e.what()), true, true); |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 720 | } |
| 721 | break; |
| 722 | } |
| 723 | } |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 724 | if (patternId == m_trafficPatterns.size()) |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 725 | { |
| 726 | deadlineTimer->expires_at(deadlineTimer->expires_at() + |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 727 | boost::posix_time::millisec( |
| 728 | m_interestInterval.count())); |
Alexander Afanasyev | fda32a3 | 2014-03-20 10:50:00 -0700 | [diff] [blame] | 729 | deadlineTimer->async_wait(bind(&NdnTrafficClient::generateTraffic, |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 730 | this, deadlineTimer)); |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 731 | } |
| 732 | } |
| 733 | } |
| 734 | |
| 735 | void |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 736 | run() |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 737 | { |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 738 | boost::asio::signal_set signalSet(*m_ioService, SIGINT, SIGTERM); |
Alexander Afanasyev | fda32a3 | 2014-03-20 10:50:00 -0700 | [diff] [blame] | 739 | signalSet.async_wait(bind(&NdnTrafficClient::signalHandler, this)); |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 740 | m_logger.initializeLog(m_instanceId); |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 741 | initializeTrafficConfiguration(); |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 742 | |
| 743 | if (m_nMaximumInterests == 0) |
| 744 | { |
| 745 | logStatistics(); |
| 746 | m_logger.shutdownLogger(); |
| 747 | return; |
| 748 | } |
| 749 | |
| 750 | boost::asio::deadline_timer deadlineTimer( |
| 751 | *m_ioService, |
| 752 | boost::posix_time::millisec(m_interestInterval.count())); |
Alexander Afanasyev | fda32a3 | 2014-03-20 10:50:00 -0700 | [diff] [blame] | 753 | deadlineTimer.async_wait(bind(&NdnTrafficClient::generateTraffic, |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 754 | this, &deadlineTimer)); |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 755 | try { |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 756 | m_face.processEvents(); |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 757 | } |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 758 | catch(std::exception& e) { |
| 759 | m_logger.log("ERROR: " + static_cast<std::string>(e.what()), true, true); |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 760 | m_logger.shutdownLogger(); |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 761 | m_hasError = true; |
| 762 | m_ioService->stop(); |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 763 | } |
| 764 | } |
| 765 | |
| 766 | private: |
| 767 | |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 768 | std::string m_programName; |
| 769 | std::string m_instanceId; |
| 770 | bool m_hasError; |
| 771 | time::milliseconds m_interestInterval; |
| 772 | int m_nMaximumInterests; |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 773 | Logger m_logger; |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 774 | std::string m_configurationFile; |
| 775 | shared_ptr<boost::asio::io_service> m_ioService; |
| 776 | Face m_face; |
| 777 | std::vector<InterestTrafficConfiguration> m_trafficPatterns; |
| 778 | std::vector<uint32_t> m_nonces; |
| 779 | int m_nInterestsSent; |
| 780 | int m_nInterestsReceived; |
| 781 | int m_nContentInconsistencies; |
| 782 | |
| 783 | //round trip time is stored as milliseconds with fractional |
| 784 | //sub-milliseconds precision |
| 785 | double m_minimumInterestRoundTripTime; |
| 786 | double m_maximumInterestRoundTripTime; |
| 787 | double m_totalInterestRoundTripTime; |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 788 | |
| 789 | }; |
| 790 | |
| 791 | } // namespace ndn |
| 792 | |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 793 | int |
| 794 | main(int argc, char* argv[]) |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 795 | { |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 796 | std::srand(std::time(0)); |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 797 | ndn::NdnTrafficClient ndnTrafficClient (argv[0]); |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 798 | int option; |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 799 | while ((option = getopt(argc, argv, "hi:c:")) != -1) { |
| 800 | switch (option) { |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 801 | case 'h': |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 802 | ndnTrafficClient.usage(); |
| 803 | break; |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 804 | case 'i': |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 805 | ndnTrafficClient.setInterestInterval(atoi(optarg)); |
| 806 | break; |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 807 | case 'c': |
| 808 | ndnTrafficClient.setMaximumInterests(atoi(optarg)); |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 809 | break; |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 810 | default: |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 811 | ndnTrafficClient.usage(); |
| 812 | break; |
| 813 | } |
| 814 | } |
| 815 | |
| 816 | argc -= optind; |
| 817 | argv += optind; |
| 818 | |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 819 | if (argv[0] == 0) |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 820 | ndnTrafficClient.usage(); |
| 821 | |
| 822 | ndnTrafficClient.setConfigurationFile(argv[0]); |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 823 | ndnTrafficClient.run(); |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 824 | |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 825 | if (ndnTrafficClient.hasError()) |
| 826 | return 1; |
| 827 | else |
| 828 | return 0; |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 829 | } |