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