blob: cfd693326e5ee8339d8139ef8fb12d3bb853448a [file] [log] [blame]
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2/**
Davide Pesaventod0b59982015-02-27 19:15:15 +01003 * Copyright (C) 2014-2015 University of Arizona.
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -08004 *
Davide Pesaventod0b59982015-02-27 19:15:15 +01005 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -080017 *
18 * Author: Jerald Paul Abraham <jeraldabraham@email.arizona.edu>
19 */
20
Davide Pesaventod0b59982015-02-27 19:15:15 +010021#include <cctype>
22#include <cstdlib>
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -080023#include <fstream>
jeraldabraham420dbf02014-04-25 22:58:31 -070024#include <string>
Davide Pesaventod0b59982015-02-27 19:15:15 +010025#include <unistd.h>
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -080026#include <vector>
27
jeraldabraham420dbf02014-04-25 22:58:31 -070028#include <boost/asio.hpp>
29#include <boost/date_time/posix_time/posix_time.hpp>
30#include <boost/filesystem.hpp>
jeraldabraham420dbf02014-04-25 22:58:31 -070031#include <boost/noncopyable.hpp>
32
33#include <ndn-cxx/exclude.hpp>
34#include <ndn-cxx/face.hpp>
35#include <ndn-cxx/name-component.hpp>
36
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -080037#include "logger.hpp"
38
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -080039namespace ndn {
40
jeraldabraham420dbf02014-04-25 22:58:31 -070041class NdnTrafficClient : boost::noncopyable
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -080042{
43public:
jeraldabrahamcc3c6c92014-03-28 02:21:45 -070044 explicit
Davide Pesaventod0b59982015-02-27 19:15:15 +010045 NdnTrafficClient(const char* programName)
jeraldabrahamcc3c6c92014-03-28 02:21:45 -070046 : m_programName(programName)
47 , m_logger("NdnTrafficClient")
Davide Pesaventod0b59982015-02-27 19:15:15 +010048 , m_instanceId(std::to_string(std::rand()))
jeraldabrahamcc3c6c92014-03-28 02:21:45 -070049 , m_hasError(false)
jeraldabraham420dbf02014-04-25 22:58:31 -070050 , m_hasQuietLogging(false)
jeraldabrahamcc3c6c92014-03-28 02:21:45 -070051 , m_interestInterval(getDefaultInterestInterval())
52 , m_nMaximumInterests(-1)
Alexander Afanasyev740812e2014-10-30 15:37:45 -070053 , m_face(m_ioService)
jeraldabrahamcc3c6c92014-03-28 02:21:45 -070054 , m_nInterestsSent(0)
55 , m_nInterestsReceived(0)
jeraldabrahamdbfee2e2014-04-04 01:18:14 -070056 , m_nContentInconsistencies(0)
jeraldabrahamcc3c6c92014-03-28 02:21:45 -070057 , m_minimumInterestRoundTripTime(std::numeric_limits<double>::max())
58 , m_maximumInterestRoundTripTime(0)
59 , m_totalInterestRoundTripTime(0)
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -080060 {
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -080061 }
62
63 class InterestTrafficConfiguration
64 {
65 public:
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -080066 InterestTrafficConfiguration()
jeraldabrahamcc3c6c92014-03-28 02:21:45 -070067 : m_trafficPercentage(-1)
68 , m_nameAppendBytes(-1)
69 , m_nameAppendSequenceNumber(-1)
70 , m_minSuffixComponents(-1)
71 , m_maxSuffixComponents(-1)
72 , m_excludeBeforeBytes(-1)
73 , m_excludeAfterBytes(-1)
74 , m_childSelector(-1)
75 , m_mustBeFresh(-1)
76 , m_nonceDuplicationPercentage(-1)
jeraldabrahamcc3c6c92014-03-28 02:21:45 -070077 , m_interestLifetime(getDefaultInterestLifetime())
jeraldabrahamdbfee2e2014-04-04 01:18:14 -070078 , m_nInterestsSent(0)
79 , m_nInterestsReceived(0)
jeraldabrahamcc3c6c92014-03-28 02:21:45 -070080 , m_minimumInterestRoundTripTime(std::numeric_limits<double>::max())
81 , m_maximumInterestRoundTripTime(0)
82 , m_totalInterestRoundTripTime(0)
83 , m_nContentInconsistencies(0)
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -080084 {
jeraldabrahamcc3c6c92014-03-28 02:21:45 -070085 }
86
Davide Pesaventod0b59982015-02-27 19:15:15 +010087 static time::milliseconds
jeraldabrahamcc3c6c92014-03-28 02:21:45 -070088 getDefaultInterestLifetime()
89 {
90 return time::milliseconds(-1);
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -080091 }
92
93 void
Alexander Afanasyevfda32a32014-03-20 10:50:00 -070094 printTrafficConfiguration(Logger& logger)
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -080095 {
Davide Pesaventod0b59982015-02-27 19:15:15 +010096 std::string detail;
97
jeraldabrahamcc3c6c92014-03-28 02:21:45 -070098 if (m_trafficPercentage > 0)
Davide Pesaventod0b59982015-02-27 19:15:15 +010099 detail += "TrafficPercentage=" + std::to_string(m_trafficPercentage) + ", ";
100 if (!m_name.empty())
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700101 detail += "Name=" + m_name + ", ";
102 if (m_nameAppendBytes > 0)
Davide Pesaventod0b59982015-02-27 19:15:15 +0100103 detail += "NameAppendBytes=" + std::to_string(m_nameAppendBytes) + ", ";
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700104 if (m_nameAppendSequenceNumber > 0)
Davide Pesaventod0b59982015-02-27 19:15:15 +0100105 detail += "NameAppendSequenceNumber=" + std::to_string(m_nameAppendSequenceNumber) + ", ";
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700106 if (m_minSuffixComponents >= 0)
Davide Pesaventod0b59982015-02-27 19:15:15 +0100107 detail += "MinSuffixComponents=" + std::to_string(m_minSuffixComponents) + ", ";
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700108 if (m_maxSuffixComponents >= 0)
Davide Pesaventod0b59982015-02-27 19:15:15 +0100109 detail += "MaxSuffixComponents=" + std::to_string(m_maxSuffixComponents) + ", ";
110 if (!m_excludeBefore.empty())
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700111 detail += "ExcludeBefore=" + m_excludeBefore + ", ";
Davide Pesaventod0b59982015-02-27 19:15:15 +0100112 if (!m_excludeAfter.empty())
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700113 detail += "ExcludeAfter=" + m_excludeAfter + ", ";
114 if (m_excludeBeforeBytes > 0)
Davide Pesaventod0b59982015-02-27 19:15:15 +0100115 detail += "ExcludeBeforeBytes=" + std::to_string(m_excludeBeforeBytes) + ", ";
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700116 if (m_excludeAfterBytes > 0)
Davide Pesaventod0b59982015-02-27 19:15:15 +0100117 detail += "ExcludeAfterBytes=" + std::to_string(m_excludeAfterBytes) + ", ";
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700118 if (m_childSelector >= 0)
Davide Pesaventod0b59982015-02-27 19:15:15 +0100119 detail += "ChildSelector=" + std::to_string(m_childSelector) + ", ";
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700120 if (m_mustBeFresh >= 0)
Davide Pesaventod0b59982015-02-27 19:15:15 +0100121 detail += "MustBeFresh=" + std::to_string(m_mustBeFresh) + ", ";
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700122 if (m_nonceDuplicationPercentage > 0)
123 detail += "NonceDuplicationPercentage=" +
Davide Pesaventod0b59982015-02-27 19:15:15 +0100124 std::to_string(m_nonceDuplicationPercentage) + ", ";
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700125 if (m_interestLifetime >= time::milliseconds(0))
Davide Pesaventod0b59982015-02-27 19:15:15 +0100126 detail += "InterestLifetime=" + std::to_string(m_interestLifetime.count()) + ", ";
127 if (!m_expectedContent.empty())
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700128 detail += "ExpectedContent=" + m_expectedContent + ", ";
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800129 if (detail.length() >= 2)
Davide Pesaventod0b59982015-02-27 19:15:15 +0100130 detail = detail.substr(0, detail.length() - 2); // Removing suffix ", "
131
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800132 logger.log(detail, false, false);
133 }
134
135 bool
Davide Pesaventod0b59982015-02-27 19:15:15 +0100136 extractParameterValue(const std::string& detail, std::string& parameter, std::string& value)
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800137 {
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800138 std::string allowedCharacters = ":/+._-%";
Davide Pesaventod0b59982015-02-27 19:15:15 +0100139 std::size_t i = 0;
140
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800141 parameter = "";
142 value = "";
Davide Pesaventod0b59982015-02-27 19:15:15 +0100143 while (detail[i] != '=' && i < detail.length()) {
144 parameter += detail[i];
145 i++;
146 }
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800147 if (i == detail.length())
148 return false;
Davide Pesaventod0b59982015-02-27 19:15:15 +0100149
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800150 i++;
jeraldabraham79c0c232014-03-31 21:43:59 -0700151 while ((std::isalnum(detail[i]) ||
Davide Pesaventod0b59982015-02-27 19:15:15 +0100152 allowedCharacters.find(detail[i]) != std::string::npos) &&
153 i < detail.length()) {
154 value += detail[i];
155 i++;
156 }
157
158 if (parameter.empty() || value.empty())
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800159 return false;
Davide Pesaventod0b59982015-02-27 19:15:15 +0100160 else
161 return true;
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800162 }
163
164 bool
Alexander Afanasyevfda32a32014-03-20 10:50:00 -0700165 processConfigurationDetail(const std::string& detail, Logger& logger, int lineNumber)
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800166 {
167 std::string parameter, value;
168 if (extractParameterValue(detail, parameter, value))
169 {
170 if (parameter == "TrafficPercentage")
Davide Pesaventod0b59982015-02-27 19:15:15 +0100171 m_trafficPercentage = std::stoi(value);
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800172 else if (parameter == "Name")
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700173 m_name = value;
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800174 else if (parameter == "NameAppendBytes")
Davide Pesaventod0b59982015-02-27 19:15:15 +0100175 m_nameAppendBytes = std::stoi(value);
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800176 else if (parameter == "NameAppendSequenceNumber")
Davide Pesaventod0b59982015-02-27 19:15:15 +0100177 m_nameAppendSequenceNumber = std::stoi(value);
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800178 else if (parameter == "MinSuffixComponents")
Davide Pesaventod0b59982015-02-27 19:15:15 +0100179 m_minSuffixComponents = std::stoi(value);
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800180 else if (parameter == "MaxSuffixComponents")
Davide Pesaventod0b59982015-02-27 19:15:15 +0100181 m_maxSuffixComponents = std::stoi(value);
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800182 else if (parameter == "ExcludeBefore")
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700183 m_excludeBefore = value;
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800184 else if (parameter == "ExcludeAfter")
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700185 m_excludeAfter = value;
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800186 else if (parameter == "ExcludeBeforeBytes")
Davide Pesaventod0b59982015-02-27 19:15:15 +0100187 m_excludeBeforeBytes = std::stoi(value);
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800188 else if (parameter == "ExcludeAfterBytes")
Davide Pesaventod0b59982015-02-27 19:15:15 +0100189 m_excludeAfterBytes = std::stoi(value);
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800190 else if (parameter == "ChildSelector")
Davide Pesaventod0b59982015-02-27 19:15:15 +0100191 m_childSelector = std::stoi(value);
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800192 else if (parameter == "MustBeFresh")
Davide Pesaventod0b59982015-02-27 19:15:15 +0100193 m_mustBeFresh = std::stoi(value);
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800194 else if (parameter == "NonceDuplicationPercentage")
Davide Pesaventod0b59982015-02-27 19:15:15 +0100195 m_nonceDuplicationPercentage = std::stoi(value);
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800196 else if (parameter == "InterestLifetime")
Davide Pesaventod0b59982015-02-27 19:15:15 +0100197 m_interestLifetime = time::milliseconds(std::stoi(value));
jeraldabraham473ef3d2014-03-06 12:40:35 -0700198 else if (parameter == "ExpectedContent")
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700199 m_expectedContent = value;
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800200 else
Davide Pesaventod0b59982015-02-27 19:15:15 +0100201 logger.log("Line " + std::to_string(lineNumber) +
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700202 " \t- Invalid Parameter='" + parameter + "'", false, true);
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800203 }
204 else
205 {
Davide Pesaventod0b59982015-02-27 19:15:15 +0100206 logger.log("Line " + std::to_string(lineNumber) +
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700207 " \t- Improper Traffic Configuration Line- " + detail, false, true);
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800208 return false;
209 }
210 return true;
211 }
212
213 bool
214 checkTrafficDetailCorrectness()
215 {
216 return true;
217 }
218
Davide Pesaventod0b59982015-02-27 19:15:15 +0100219 private:
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700220 int m_trafficPercentage;
221 std::string m_name;
222 int m_nameAppendBytes;
223 int m_nameAppendSequenceNumber;
224 int m_minSuffixComponents;
225 int m_maxSuffixComponents;
226 std::string m_excludeBefore;
227 std::string m_excludeAfter;
228 int m_excludeBeforeBytes;
229 int m_excludeAfterBytes;
230 int m_childSelector;
231 int m_mustBeFresh;
232 int m_nonceDuplicationPercentage;
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700233 time::milliseconds m_interestLifetime;
234 int m_nInterestsSent;
235 int m_nInterestsReceived;
236
237 //round trip time is stored as milliseconds with fractional
238 //sub-millisecond precision
239 double m_minimumInterestRoundTripTime;
240 double m_maximumInterestRoundTripTime;
241 double m_totalInterestRoundTripTime;
242
243 int m_nContentInconsistencies;
244 std::string m_expectedContent;
Davide Pesaventod0b59982015-02-27 19:15:15 +0100245
246 friend class NdnTrafficClient;
Alexander Afanasyevfda32a32014-03-20 10:50:00 -0700247 }; // class InterestTrafficConfiguration
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800248
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700249 bool
250 hasError() const
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800251 {
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700252 return m_hasError;
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800253 }
254
255 void
Davide Pesaventod0b59982015-02-27 19:15:15 +0100256 usage() const
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800257 {
Davide Pesaventod0b59982015-02-27 19:15:15 +0100258 std::cout << "Usage:\n"
259 << " " << m_programName << " [options] <Traffic_Configuration_File>\n"
260 << "\n"
261 << "Generate Interest traffic as per provided Traffic Configuration File.\n"
262 << "Interests are continuously generated unless a total number is specified.\n"
263 << "Set environment variable NDN_TRAFFIC_LOGFOLDER to redirect output to a log file.\n"
264 << "\n"
265 << "Options:\n"
266 << " [-i interval] - set interest generation interval in milliseconds (default "
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700267 << getDefaultInterestInterval() << ")\n"
Davide Pesaventod0b59982015-02-27 19:15:15 +0100268 << " [-c count] - set total number of interests to be generated\n"
269 << " [-q] - quiet mode: no interest reception/data generation logging\n"
270 << " [-h] - print this help text and exit\n";
271 exit(EXIT_FAILURE);
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800272 }
273
Davide Pesaventod0b59982015-02-27 19:15:15 +0100274 static time::milliseconds
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800275 getDefaultInterestInterval()
276 {
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700277 return time::milliseconds(1000);
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800278 }
279
280 void
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700281 setInterestInterval(int interestInterval)
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800282 {
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700283 if (interestInterval <= 0)
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800284 usage();
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700285 m_interestInterval = time::milliseconds(interestInterval);
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800286 }
287
288 void
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700289 setMaximumInterests(int maximumInterests)
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800290 {
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700291 if (maximumInterests <= 0)
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800292 usage();
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700293 m_nMaximumInterests = maximumInterests;
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800294 }
295
296 void
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700297 setConfigurationFile(char* configurationFile)
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800298 {
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700299 m_configurationFile = configurationFile;
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800300 }
301
302 void
jeraldabraham420dbf02014-04-25 22:58:31 -0700303 setQuietLogging()
304 {
305 m_hasQuietLogging = true;
306 }
307
308 void
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800309 signalHandler()
310 {
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800311 logStatistics();
Davide Pesaventod0b59982015-02-27 19:15:15 +0100312
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700313 m_logger.shutdownLogger();
314 m_face.shutdown();
Alexander Afanasyev976c3972014-05-26 17:03:40 +0300315 m_ioService.stop();
Davide Pesaventod0b59982015-02-27 19:15:15 +0100316
317 exit(m_hasError ? EXIT_FAILURE : EXIT_SUCCESS);
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800318 }
319
320 void
321 logStatistics()
322 {
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800323 m_logger.log("\n\n== Interest Traffic Report ==\n", false, true);
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700324 m_logger.log("Total Traffic Pattern Types = " +
Davide Pesaventod0b59982015-02-27 19:15:15 +0100325 std::to_string(m_trafficPatterns.size()), false, true);
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700326 m_logger.log("Total Interests Sent = " +
Davide Pesaventod0b59982015-02-27 19:15:15 +0100327 std::to_string(m_nInterestsSent), false, true);
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700328 m_logger.log("Total Responses Received = " +
Davide Pesaventod0b59982015-02-27 19:15:15 +0100329 std::to_string(m_nInterestsReceived), false, true);
330
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700331 double loss = 0;
332 if (m_nInterestsSent > 0)
333 loss = (m_nInterestsSent - m_nInterestsReceived) * 100.0 / m_nInterestsSent;
Davide Pesaventod0b59982015-02-27 19:15:15 +0100334 m_logger.log("Total Interest Loss = " + std::to_string(loss) + "%", false, true);
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700335 if (m_nContentInconsistencies != 0 || m_nInterestsSent != m_nInterestsReceived)
336 m_hasError = true;
Davide Pesaventod0b59982015-02-27 19:15:15 +0100337
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700338 double average = 0;
339 double inconsistency = 0;
340 if (m_nInterestsReceived > 0)
Alexander Afanasyevfda32a32014-03-20 10:50:00 -0700341 {
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700342 average = m_totalInterestRoundTripTime / m_nInterestsReceived;
343 inconsistency = m_nContentInconsistencies * 100.0 / m_nInterestsReceived;
Alexander Afanasyevfda32a32014-03-20 10:50:00 -0700344 }
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700345 m_logger.log("Total Data Inconsistency = " +
Davide Pesaventod0b59982015-02-27 19:15:15 +0100346 std::to_string(inconsistency) + "%", false, true);
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700347 m_logger.log("Total Round Trip Time = " +
Davide Pesaventod0b59982015-02-27 19:15:15 +0100348 std::to_string(m_totalInterestRoundTripTime) + "ms", false, true);
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700349 m_logger.log("Average Round Trip Time = " +
Davide Pesaventod0b59982015-02-27 19:15:15 +0100350 std::to_string(average) + "ms\n", false, true);
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700351
Davide Pesaventod0b59982015-02-27 19:15:15 +0100352 for (std::size_t patternId = 0; patternId < m_trafficPatterns.size(); patternId++)
Alexander Afanasyevfda32a32014-03-20 10:50:00 -0700353 {
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700354 m_logger.log("Traffic Pattern Type #" +
Davide Pesaventod0b59982015-02-27 19:15:15 +0100355 std::to_string(patternId + 1), false, true);
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700356 m_trafficPatterns[patternId].printTrafficConfiguration(m_logger);
357 m_logger.log("Total Interests Sent = " +
Davide Pesaventod0b59982015-02-27 19:15:15 +0100358 std::to_string(m_trafficPatterns[patternId].m_nInterestsSent), false, true);
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700359 m_logger.log("Total Responses Received = " +
Davide Pesaventod0b59982015-02-27 19:15:15 +0100360 std::to_string(m_trafficPatterns[patternId].m_nInterestsReceived), false, true);
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700361 loss = 0;
362 if (m_trafficPatterns[patternId].m_nInterestsSent > 0)
363 {
364 loss = (m_trafficPatterns[patternId].m_nInterestsSent -
365 m_trafficPatterns[patternId].m_nInterestsReceived);
366 loss *= 100.0;
367 loss /= m_trafficPatterns[patternId].m_nInterestsSent;
368 }
Davide Pesaventod0b59982015-02-27 19:15:15 +0100369 m_logger.log("Total Interest Loss = " + std::to_string(loss) + "%", false, true);
Alexander Afanasyevfda32a32014-03-20 10:50:00 -0700370 average = 0;
371 inconsistency = 0;
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700372 if (m_trafficPatterns[patternId].m_nInterestsReceived > 0)
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800373 {
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700374 average = (m_trafficPatterns[patternId].m_totalInterestRoundTripTime /
375 m_trafficPatterns[patternId].m_nInterestsReceived);
376 inconsistency = m_trafficPatterns[patternId].m_nContentInconsistencies;
Davide Pesaventod0b59982015-02-27 19:15:15 +0100377 inconsistency *= 100.0 / m_trafficPatterns[patternId].m_nInterestsReceived;
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800378 }
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700379 m_logger.log("Total Data Inconsistency = " +
Davide Pesaventod0b59982015-02-27 19:15:15 +0100380 std::to_string(inconsistency) + "%", false, true);
Alexander Afanasyevfda32a32014-03-20 10:50:00 -0700381 m_logger.log("Total Round Trip Time = " +
Davide Pesaventod0b59982015-02-27 19:15:15 +0100382 std::to_string(m_trafficPatterns[patternId].m_totalInterestRoundTripTime) +
383 "ms", false, true);
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700384 m_logger.log("Average Round Trip Time = " +
Davide Pesaventod0b59982015-02-27 19:15:15 +0100385 std::to_string(average) + "ms\n", false, true);
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800386 }
387 }
388
389 bool
390 checkTrafficPatternCorrectness()
391 {
392 return true;
393 }
394
395 void
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700396 parseConfigurationFile()
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800397 {
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800398 std::string patternLine;
399 std::ifstream patternFile;
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700400 m_logger.log("Analyzing Traffic Configuration File: " + m_configurationFile, true, true);
Davide Pesaventod0b59982015-02-27 19:15:15 +0100401
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700402 patternFile.open(m_configurationFile.c_str());
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800403 if (patternFile.is_open())
404 {
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700405 int lineNumber = 0;
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800406 while (getline(patternFile, patternLine))
407 {
408 lineNumber++;
409 if (std::isalpha(patternLine[0]))
410 {
411 InterestTrafficConfiguration interestData;
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700412 bool shouldSkipLine = false;
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800413 if (interestData.processConfigurationDetail(patternLine, m_logger, lineNumber))
414 {
415 while (getline(patternFile, patternLine) && std::isalpha(patternLine[0]))
416 {
417 lineNumber++;
Alexander Afanasyevfda32a32014-03-20 10:50:00 -0700418 if (!interestData.processConfigurationDetail(patternLine,
419 m_logger, lineNumber))
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800420 {
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700421 shouldSkipLine = true;
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800422 break;
423 }
424 }
425 lineNumber++;
426 }
427 else
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700428 shouldSkipLine = true;
429 if (!shouldSkipLine)
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800430 {
431 if (interestData.checkTrafficDetailCorrectness())
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700432 m_trafficPatterns.push_back(interestData);
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800433 }
434 }
435 }
436 patternFile.close();
437 if (!checkTrafficPatternCorrectness())
438 {
Alexander Afanasyevfda32a32014-03-20 10:50:00 -0700439 m_logger.log("ERROR - Traffic Configuration Provided Is Not Proper- " +
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700440 m_configurationFile, false, true);
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800441 m_logger.shutdownLogger();
Davide Pesaventod0b59982015-02-27 19:15:15 +0100442 exit(EXIT_FAILURE);
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800443 }
444 m_logger.log("Traffic Configuration File Processing Completed\n", true, false);
Davide Pesaventod0b59982015-02-27 19:15:15 +0100445 for (std::size_t patternId = 0; patternId < m_trafficPatterns.size(); patternId++)
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800446 {
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700447 m_logger.log("Traffic Pattern Type #" +
Davide Pesaventod0b59982015-02-27 19:15:15 +0100448 std::to_string(patternId + 1), false, false);
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700449 m_trafficPatterns[patternId].printTrafficConfiguration(m_logger);
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800450 m_logger.log("", false, false);
451 }
452 }
453 else
454 {
Alexander Afanasyevfda32a32014-03-20 10:50:00 -0700455 m_logger.log("ERROR - Unable To Open Traffic Configuration File: " +
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700456 m_configurationFile, false, true);
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800457 m_logger.shutdownLogger();
Davide Pesaventod0b59982015-02-27 19:15:15 +0100458 exit(EXIT_FAILURE);
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800459 }
460 }
461
462 void
463 initializeTrafficConfiguration()
464 {
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700465 if (boost::filesystem::exists(boost::filesystem::path(m_configurationFile)))
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800466 {
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700467 if (boost::filesystem::is_regular_file(boost::filesystem::path(m_configurationFile)))
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800468 {
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700469 parseConfigurationFile();
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800470 }
471 else
472 {
Alexander Afanasyevfda32a32014-03-20 10:50:00 -0700473 m_logger.log("ERROR - Traffic Configuration File Is Not A Regular File: " +
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700474 m_configurationFile, false, true);
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800475 m_logger.shutdownLogger();
Davide Pesaventod0b59982015-02-27 19:15:15 +0100476 exit(EXIT_FAILURE);
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800477 }
478 }
479 else
480 {
Alexander Afanasyevfda32a32014-03-20 10:50:00 -0700481 m_logger.log("ERROR - Traffic Configuration File Does Not Exist: " +
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700482 m_configurationFile, false, true);
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800483 m_logger.shutdownLogger();
Davide Pesaventod0b59982015-02-27 19:15:15 +0100484 exit(EXIT_FAILURE);
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800485 }
486 }
487
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700488 uint32_t
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800489 getOldNonce()
490 {
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700491 if (m_nonces.size() == 0)
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800492 return getNewNonce();
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700493 int randomNonceIndex = std::rand() % m_nonces.size();
494 return m_nonces[randomNonceIndex];
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800495 }
496
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700497 uint32_t
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800498 getNewNonce()
499 {
jeraldabraham473ef3d2014-03-06 12:40:35 -0700500 //Performance Enhancement
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700501 if (m_nonces.size() > 1000)
502 m_nonces.clear();
jeraldabraham473ef3d2014-03-06 12:40:35 -0700503
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700504 uint32_t randomNonce = static_cast<uint32_t>(std::rand());
505 while (std::find(m_nonces.begin(), m_nonces.end(), randomNonce) != m_nonces.end())
506 randomNonce = static_cast<uint32_t>(std::rand());
507
508 m_nonces.push_back(randomNonce);
509 return randomNonce;
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800510 }
511
512 static std::string
Davide Pesaventod0b59982015-02-27 19:15:15 +0100513 getRandomByteString(std::size_t randomSize)
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800514 {
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700515 std::string randomString;
Davide Pesaventod0b59982015-02-27 19:15:15 +0100516 for (std::size_t i = 0; i < randomSize; i++)
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700517 randomString += static_cast<char>(std::rand() % 128);
518 return randomString;
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800519 }
520
521 void
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700522 onData(const ndn::Interest& interest,
523 ndn::Data& data,
524 int globalReference,
525 int localReference,
526 int patternId,
527 time::steady_clock::TimePoint sentTime)
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800528 {
Davide Pesaventod0b59982015-02-27 19:15:15 +0100529 std::string logLine = "Data Received - PatternType=" + std::to_string(patternId + 1);
530 logLine += ", GlobalID=" + std::to_string(globalReference);
531 logLine += ", LocalID=" + std::to_string(localReference);
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700532 logLine += ", Name=" + interest.getName().toUri();
533
534 m_nInterestsReceived++;
535 m_trafficPatterns[patternId].m_nInterestsReceived++;
Davide Pesaventod0b59982015-02-27 19:15:15 +0100536 if (!m_trafficPatterns[patternId].m_expectedContent.empty())
jeraldabraham473ef3d2014-03-06 12:40:35 -0700537 {
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700538 std::string receivedContent = reinterpret_cast<const char*>(data.getContent().value());
539 int receivedContentLength = data.getContent().value_size();
Alexander Afanasyevfda32a32014-03-20 10:50:00 -0700540 receivedContent = receivedContent.substr(0, receivedContentLength);
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700541 if (receivedContent != m_trafficPatterns[patternId].m_expectedContent)
Alexander Afanasyevfda32a32014-03-20 10:50:00 -0700542 {
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700543 m_nContentInconsistencies++;
544 m_trafficPatterns[patternId].m_nContentInconsistencies++;
Alexander Afanasyevfda32a32014-03-20 10:50:00 -0700545 logLine += ", IsConsistent=No";
546 }
547 else
548 logLine += ", IsConsistent=Yes";
jeraldabraham473ef3d2014-03-06 12:40:35 -0700549 }
jeraldabraham473ef3d2014-03-06 12:40:35 -0700550 else
551 logLine += ", IsConsistent=NotChecked";
jeraldabraham420dbf02014-04-25 22:58:31 -0700552 if (!m_hasQuietLogging)
553 m_logger.log(logLine, true, false);
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700554 double roundTripTime = (time::steady_clock::now() - sentTime).count() / 1000000.0;
555 if (m_minimumInterestRoundTripTime > roundTripTime)
556 m_minimumInterestRoundTripTime = roundTripTime;
557 if (m_maximumInterestRoundTripTime < roundTripTime)
558 m_maximumInterestRoundTripTime = roundTripTime;
559 if (m_trafficPatterns[patternId].m_minimumInterestRoundTripTime > roundTripTime)
560 m_trafficPatterns[patternId].m_minimumInterestRoundTripTime = roundTripTime;
561 if (m_trafficPatterns[patternId].m_maximumInterestRoundTripTime < roundTripTime)
562 m_trafficPatterns[patternId].m_maximumInterestRoundTripTime = roundTripTime;
563 m_totalInterestRoundTripTime += roundTripTime;
564 m_trafficPatterns[patternId].m_totalInterestRoundTripTime += roundTripTime;
jeraldabrahamdbfee2e2014-04-04 01:18:14 -0700565 if (m_nMaximumInterests >= 0 && globalReference == m_nMaximumInterests)
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800566 {
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700567 logStatistics();
568 m_logger.shutdownLogger();
569 m_face.shutdown();
Alexander Afanasyev976c3972014-05-26 17:03:40 +0300570 m_ioService.stop();
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700571 }
572 }
573
574 void
575 onTimeout(const ndn::Interest& interest,
576 int globalReference,
577 int localReference,
578 int patternId)
579 {
Davide Pesaventod0b59982015-02-27 19:15:15 +0100580 std::string logLine = "Interest Timed Out - PatternType=" + std::to_string(patternId + 1);
581 logLine += ", GlobalID=" + std::to_string(globalReference);
582 logLine += ", LocalID=" + std::to_string(localReference);
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700583 logLine += ", Name=" + interest.getName().toUri();
584 m_logger.log(logLine, true, false);
jeraldabrahamdbfee2e2014-04-04 01:18:14 -0700585 if (m_nMaximumInterests >= 0 && globalReference == m_nMaximumInterests)
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700586 {
587 logStatistics();
588 m_logger.shutdownLogger();
589 m_face.shutdown();
Alexander Afanasyev976c3972014-05-26 17:03:40 +0300590 m_ioService.stop();
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700591 }
592 }
593
594 void
Davide Pesaventod0b59982015-02-27 19:15:15 +0100595 generateTraffic(boost::asio::deadline_timer* timer)
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700596 {
597 if (m_nMaximumInterests < 0 || m_nInterestsSent < m_nMaximumInterests)
598 {
599 int trafficKey = std::rand() % 100;
600 int cumulativePercentage = 0;
Davide Pesaventod0b59982015-02-27 19:15:15 +0100601 std::size_t patternId;
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700602 for (patternId = 0; patternId < m_trafficPatterns.size(); patternId++)
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800603 {
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700604 cumulativePercentage += m_trafficPatterns[patternId].m_trafficPercentage;
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800605 if (trafficKey <= cumulativePercentage)
606 {
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700607 Name interestName(m_trafficPatterns[patternId].m_name);
608 if (m_trafficPatterns[patternId].m_nameAppendBytes > 0)
609 interestName.append(
610 getRandomByteString(m_trafficPatterns[patternId].m_nameAppendBytes));
611 if (m_trafficPatterns[patternId].m_nameAppendSequenceNumber >= 0)
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800612 {
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700613 interestName.append(
Davide Pesaventod0b59982015-02-27 19:15:15 +0100614 std::to_string(m_trafficPatterns[patternId].m_nameAppendSequenceNumber));
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700615 m_trafficPatterns[patternId].m_nameAppendSequenceNumber++;
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800616 }
Davide Pesaventod0b59982015-02-27 19:15:15 +0100617
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800618 Interest interest(interestName);
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700619 if (m_trafficPatterns[patternId].m_minSuffixComponents >= 0)
620 interest.setMinSuffixComponents(
621 m_trafficPatterns[patternId].m_minSuffixComponents);
622 if (m_trafficPatterns[patternId].m_maxSuffixComponents >= 0)
623 interest.setMaxSuffixComponents(
624 m_trafficPatterns[patternId].m_maxSuffixComponents);
Davide Pesaventod0b59982015-02-27 19:15:15 +0100625
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800626 Exclude exclude;
Davide Pesaventod0b59982015-02-27 19:15:15 +0100627 if (!m_trafficPatterns[patternId].m_excludeBefore.empty() &&
628 !m_trafficPatterns[patternId].m_excludeAfter.empty())
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800629 {
Alexander Afanasyevfda32a32014-03-20 10:50:00 -0700630 exclude.excludeRange(
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700631 name::Component(
632 m_trafficPatterns[patternId].m_excludeAfter),
633 name::Component(m_trafficPatterns[patternId].m_excludeBefore));
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800634 interest.setExclude(exclude);
635 }
Davide Pesaventod0b59982015-02-27 19:15:15 +0100636 else if (!m_trafficPatterns[patternId].m_excludeBefore.empty())
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800637 {
Alexander Afanasyevfda32a32014-03-20 10:50:00 -0700638 exclude.excludeBefore(
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700639 name::Component(m_trafficPatterns[patternId].m_excludeBefore));
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800640 interest.setExclude(exclude);
641 }
Davide Pesaventod0b59982015-02-27 19:15:15 +0100642 else if (!m_trafficPatterns[patternId].m_excludeAfter.empty())
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800643 {
Alexander Afanasyevfda32a32014-03-20 10:50:00 -0700644 exclude.excludeAfter(
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700645 name::Component(m_trafficPatterns[patternId].m_excludeAfter));
646 interest.setExclude(exclude);
647 }
648 if (m_trafficPatterns[patternId].m_excludeBeforeBytes > 0 &&
649 m_trafficPatterns[patternId].m_excludeAfterBytes > 0)
650 {
651 exclude.excludeRange(
652 name::Component(
653 getRandomByteString(
654 m_trafficPatterns[patternId].m_excludeAfterBytes)),
655 name::Component(
656 getRandomByteString(
657 m_trafficPatterns[patternId].m_excludeBeforeBytes)));
658 interest.setExclude(exclude);
659 }
660 else if (m_trafficPatterns[patternId].m_excludeBeforeBytes > 0)
661 {
662 exclude.excludeBefore(
663 name::Component(
664 getRandomByteString(
665 m_trafficPatterns[patternId].m_excludeBeforeBytes)));
666 interest.setExclude(exclude);
667 }
668 else if (m_trafficPatterns[patternId].m_excludeAfterBytes > 0)
669 {
670 exclude.excludeAfter(
671 name::Component(
672 getRandomByteString(
673 m_trafficPatterns[patternId].m_excludeAfterBytes)));
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800674 interest.setExclude(exclude);
675 }
676
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700677 if (m_trafficPatterns[patternId].m_childSelector >= 0)
678 interest.setChildSelector(m_trafficPatterns[patternId].m_childSelector);
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800679
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700680 if (m_trafficPatterns[patternId].m_mustBeFresh == 0)
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800681 interest.setMustBeFresh(false);
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700682 else if (m_trafficPatterns[patternId].m_mustBeFresh > 0)
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800683 interest.setMustBeFresh(true);
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700684 if (m_trafficPatterns[patternId].m_nonceDuplicationPercentage > 0)
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800685 {
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700686 int duplicationPercentage = std::rand() % 100;
687 if (m_trafficPatterns[patternId].m_nonceDuplicationPercentage <=
688 duplicationPercentage)
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800689 interest.setNonce(getOldNonce());
690 else
691 interest.setNonce(getNewNonce());
692 }
693 else
694 interest.setNonce(getNewNonce());
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700695 if (m_trafficPatterns[patternId].m_interestLifetime >= time::milliseconds(0))
696 interest.setInterestLifetime(m_trafficPatterns[patternId].m_interestLifetime);
697
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800698 try {
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700699 m_nInterestsSent++;
700 m_trafficPatterns[patternId].m_nInterestsSent++;
701 time::steady_clock::TimePoint sentTime = time::steady_clock::now();
702 m_face.expressInterest(interest,
703 bind(&NdnTrafficClient::onData,
704 this, _1, _2, m_nInterestsSent,
705 m_trafficPatterns[patternId].m_nInterestsSent,
706 patternId, sentTime),
707 bind(&NdnTrafficClient::onTimeout,
708 this, _1, m_nInterestsSent,
709 m_trafficPatterns[patternId].m_nInterestsSent,
Alexander Afanasyevfda32a32014-03-20 10:50:00 -0700710 patternId));
Davide Pesaventod0b59982015-02-27 19:15:15 +0100711
712 if (!m_hasQuietLogging) {
713 std::string logLine =
714 "Sending Interest - PatternType=" + std::to_string(patternId + 1) +
715 ", GlobalID=" + std::to_string(m_nInterestsSent) +
716 ", LocalID=" +
717 std::to_string(m_trafficPatterns[patternId].m_nInterestsSent) +
718 ", Name=" + interest.getName().toUri();
jeraldabraham420dbf02014-04-25 22:58:31 -0700719 m_logger.log(logLine, true, false);
Davide Pesaventod0b59982015-02-27 19:15:15 +0100720 }
721
722 timer->expires_at(timer->expires_at() +
723 boost::posix_time::millisec(m_interestInterval.count()));
724 timer->async_wait(bind(&NdnTrafficClient::generateTraffic, this, timer));
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800725 }
Davide Pesaventod0b59982015-02-27 19:15:15 +0100726 catch (const std::exception& e) {
727 m_logger.log("ERROR: " + std::string(e.what()), true, true);
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800728 }
729 break;
730 }
731 }
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700732 if (patternId == m_trafficPatterns.size())
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800733 {
Davide Pesaventod0b59982015-02-27 19:15:15 +0100734 timer->expires_at(timer->expires_at() +
735 boost::posix_time::millisec(m_interestInterval.count()));
736 timer->async_wait(bind(&NdnTrafficClient::generateTraffic, this, timer));
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800737 }
738 }
739 }
740
741 void
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700742 run()
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800743 {
Alexander Afanasyev976c3972014-05-26 17:03:40 +0300744 boost::asio::signal_set signalSet(m_ioService, SIGINT, SIGTERM);
Alexander Afanasyevfda32a32014-03-20 10:50:00 -0700745 signalSet.async_wait(bind(&NdnTrafficClient::signalHandler, this));
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700746 m_logger.initializeLog(m_instanceId);
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800747 initializeTrafficConfiguration();
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700748
749 if (m_nMaximumInterests == 0)
750 {
751 logStatistics();
752 m_logger.shutdownLogger();
753 return;
754 }
755
Davide Pesaventod0b59982015-02-27 19:15:15 +0100756 boost::asio::deadline_timer deadlineTimer(m_ioService,
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700757 boost::posix_time::millisec(m_interestInterval.count()));
Davide Pesaventod0b59982015-02-27 19:15:15 +0100758 deadlineTimer.async_wait(bind(&NdnTrafficClient::generateTraffic, this, &deadlineTimer));
759
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800760 try {
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700761 m_face.processEvents();
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800762 }
Davide Pesaventod0b59982015-02-27 19:15:15 +0100763 catch (const std::exception& e) {
764 m_logger.log("ERROR: " + std::string(e.what()), true, true);
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800765 m_logger.shutdownLogger();
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700766 m_hasError = true;
Alexander Afanasyev976c3972014-05-26 17:03:40 +0300767 m_ioService.stop();
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800768 }
769 }
770
771private:
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700772 std::string m_programName;
Alexander Afanasyev740812e2014-10-30 15:37:45 -0700773 Logger m_logger;
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700774 std::string m_instanceId;
775 bool m_hasError;
jeraldabraham420dbf02014-04-25 22:58:31 -0700776 bool m_hasQuietLogging;
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700777 time::milliseconds m_interestInterval;
778 int m_nMaximumInterests;
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700779 std::string m_configurationFile;
Alexander Afanasyev976c3972014-05-26 17:03:40 +0300780 boost::asio::io_service m_ioService;
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700781 Face m_face;
782 std::vector<InterestTrafficConfiguration> m_trafficPatterns;
783 std::vector<uint32_t> m_nonces;
784 int m_nInterestsSent;
785 int m_nInterestsReceived;
786 int m_nContentInconsistencies;
787
788 //round trip time is stored as milliseconds with fractional
789 //sub-milliseconds precision
790 double m_minimumInterestRoundTripTime;
791 double m_maximumInterestRoundTripTime;
792 double m_totalInterestRoundTripTime;
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800793};
794
795} // namespace ndn
796
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700797int
798main(int argc, char* argv[])
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800799{
Davide Pesaventod0b59982015-02-27 19:15:15 +0100800 std::srand(std::time(nullptr));
801
802 ndn::NdnTrafficClient client(argv[0]);
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700803 int option;
jeraldabraham420dbf02014-04-25 22:58:31 -0700804 while ((option = getopt(argc, argv, "hqi:c:")) != -1) {
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800805 switch (option) {
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700806 case 'h':
Davide Pesaventod0b59982015-02-27 19:15:15 +0100807 client.usage();
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800808 break;
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700809 case 'i':
Davide Pesaventod0b59982015-02-27 19:15:15 +0100810 client.setInterestInterval(atoi(optarg));
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800811 break;
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700812 case 'c':
Davide Pesaventod0b59982015-02-27 19:15:15 +0100813 client.setMaximumInterests(atoi(optarg));
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800814 break;
jeraldabraham420dbf02014-04-25 22:58:31 -0700815 case 'q':
Davide Pesaventod0b59982015-02-27 19:15:15 +0100816 client.setQuietLogging();
jeraldabraham420dbf02014-04-25 22:58:31 -0700817 break;
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700818 default:
Davide Pesaventod0b59982015-02-27 19:15:15 +0100819 client.usage();
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800820 break;
821 }
822 }
823
824 argc -= optind;
825 argv += optind;
826
Davide Pesaventod0b59982015-02-27 19:15:15 +0100827 if (!argc)
828 client.usage();
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800829
Davide Pesaventod0b59982015-02-27 19:15:15 +0100830 client.setConfigurationFile(argv[0]);
831 client.run();
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800832
Davide Pesaventod0b59982015-02-27 19:15:15 +0100833 return client.hasError() ? EXIT_FAILURE : EXIT_SUCCESS;
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800834}