blob: f173a265d3ce790430117a4f1627809aeb479f39 [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>
Spencer Lee8e990232015-11-27 03:54:39 -070036#include <ndn-cxx/util/backports.hpp>
jeraldabraham420dbf02014-04-25 22:58:31 -070037
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -080038#include "logger.hpp"
39
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -080040namespace ndn {
41
jeraldabraham420dbf02014-04-25 22:58:31 -070042class NdnTrafficClient : boost::noncopyable
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -080043{
44public:
jeraldabrahamcc3c6c92014-03-28 02:21:45 -070045 explicit
Davide Pesaventod0b59982015-02-27 19:15:15 +010046 NdnTrafficClient(const char* programName)
jeraldabrahamcc3c6c92014-03-28 02:21:45 -070047 : m_programName(programName)
48 , m_logger("NdnTrafficClient")
Spencer Lee8e990232015-11-27 03:54:39 -070049 , m_instanceId(to_string(std::rand()))
jeraldabrahamcc3c6c92014-03-28 02:21:45 -070050 , m_hasError(false)
jeraldabraham420dbf02014-04-25 22:58:31 -070051 , m_hasQuietLogging(false)
jeraldabrahamcc3c6c92014-03-28 02:21:45 -070052 , m_interestInterval(getDefaultInterestInterval())
53 , m_nMaximumInterests(-1)
Alexander Afanasyev740812e2014-10-30 15:37:45 -070054 , m_face(m_ioService)
jeraldabrahamcc3c6c92014-03-28 02:21:45 -070055 , m_nInterestsSent(0)
56 , m_nInterestsReceived(0)
jeraldabrahamdbfee2e2014-04-04 01:18:14 -070057 , m_nContentInconsistencies(0)
jeraldabrahamcc3c6c92014-03-28 02:21:45 -070058 , m_minimumInterestRoundTripTime(std::numeric_limits<double>::max())
59 , m_maximumInterestRoundTripTime(0)
60 , m_totalInterestRoundTripTime(0)
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -080061 {
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -080062 }
63
64 class InterestTrafficConfiguration
65 {
66 public:
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -080067 InterestTrafficConfiguration()
jeraldabrahamcc3c6c92014-03-28 02:21:45 -070068 : m_trafficPercentage(-1)
69 , m_nameAppendBytes(-1)
70 , m_nameAppendSequenceNumber(-1)
71 , m_minSuffixComponents(-1)
72 , m_maxSuffixComponents(-1)
73 , m_excludeBeforeBytes(-1)
74 , m_excludeAfterBytes(-1)
75 , m_childSelector(-1)
76 , m_mustBeFresh(-1)
77 , m_nonceDuplicationPercentage(-1)
jeraldabrahamcc3c6c92014-03-28 02:21:45 -070078 , m_interestLifetime(getDefaultInterestLifetime())
jeraldabrahamdbfee2e2014-04-04 01:18:14 -070079 , m_nInterestsSent(0)
80 , m_nInterestsReceived(0)
jeraldabrahamcc3c6c92014-03-28 02:21:45 -070081 , m_minimumInterestRoundTripTime(std::numeric_limits<double>::max())
82 , m_maximumInterestRoundTripTime(0)
83 , m_totalInterestRoundTripTime(0)
84 , m_nContentInconsistencies(0)
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -080085 {
jeraldabrahamcc3c6c92014-03-28 02:21:45 -070086 }
87
Davide Pesaventod0b59982015-02-27 19:15:15 +010088 static time::milliseconds
jeraldabrahamcc3c6c92014-03-28 02:21:45 -070089 getDefaultInterestLifetime()
90 {
91 return time::milliseconds(-1);
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -080092 }
93
94 void
Alexander Afanasyevfda32a32014-03-20 10:50:00 -070095 printTrafficConfiguration(Logger& logger)
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -080096 {
Davide Pesaventod0b59982015-02-27 19:15:15 +010097 std::string detail;
98
jeraldabrahamcc3c6c92014-03-28 02:21:45 -070099 if (m_trafficPercentage > 0)
Spencer Lee8e990232015-11-27 03:54:39 -0700100 detail += "TrafficPercentage=" + to_string(m_trafficPercentage) + ", ";
Davide Pesaventod0b59982015-02-27 19:15:15 +0100101 if (!m_name.empty())
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700102 detail += "Name=" + m_name + ", ";
103 if (m_nameAppendBytes > 0)
Spencer Lee8e990232015-11-27 03:54:39 -0700104 detail += "NameAppendBytes=" + to_string(m_nameAppendBytes) + ", ";
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700105 if (m_nameAppendSequenceNumber > 0)
Spencer Lee8e990232015-11-27 03:54:39 -0700106 detail += "NameAppendSequenceNumber=" + to_string(m_nameAppendSequenceNumber) + ", ";
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700107 if (m_minSuffixComponents >= 0)
Spencer Lee8e990232015-11-27 03:54:39 -0700108 detail += "MinSuffixComponents=" + to_string(m_minSuffixComponents) + ", ";
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700109 if (m_maxSuffixComponents >= 0)
Spencer Lee8e990232015-11-27 03:54:39 -0700110 detail += "MaxSuffixComponents=" + to_string(m_maxSuffixComponents) + ", ";
Davide Pesaventod0b59982015-02-27 19:15:15 +0100111 if (!m_excludeBefore.empty())
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700112 detail += "ExcludeBefore=" + m_excludeBefore + ", ";
Davide Pesaventod0b59982015-02-27 19:15:15 +0100113 if (!m_excludeAfter.empty())
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700114 detail += "ExcludeAfter=" + m_excludeAfter + ", ";
115 if (m_excludeBeforeBytes > 0)
Spencer Lee8e990232015-11-27 03:54:39 -0700116 detail += "ExcludeBeforeBytes=" + to_string(m_excludeBeforeBytes) + ", ";
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700117 if (m_excludeAfterBytes > 0)
Spencer Lee8e990232015-11-27 03:54:39 -0700118 detail += "ExcludeAfterBytes=" + to_string(m_excludeAfterBytes) + ", ";
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700119 if (m_childSelector >= 0)
Spencer Lee8e990232015-11-27 03:54:39 -0700120 detail += "ChildSelector=" + to_string(m_childSelector) + ", ";
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700121 if (m_mustBeFresh >= 0)
Spencer Lee8e990232015-11-27 03:54:39 -0700122 detail += "MustBeFresh=" + to_string(m_mustBeFresh) + ", ";
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700123 if (m_nonceDuplicationPercentage > 0)
Spencer Lee8e990232015-11-27 03:54:39 -0700124 detail += "NonceDuplicationPercentage=" + to_string(m_nonceDuplicationPercentage) + ", ";
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700125 if (m_interestLifetime >= time::milliseconds(0))
Spencer Lee8e990232015-11-27 03:54:39 -0700126 detail += "InterestLifetime=" + to_string(m_interestLifetime.count()) + ", ";
Davide Pesaventod0b59982015-02-27 19:15:15 +0100127 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
Spencer Lee8e990232015-11-27 03:54:39 -0700201 logger.log("Line " + 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 {
Spencer Lee8e990232015-11-27 03:54:39 -0700206 logger.log("Line " + 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 = " +
Spencer Lee8e990232015-11-27 03:54:39 -0700325 to_string(m_trafficPatterns.size()), false, true);
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700326 m_logger.log("Total Interests Sent = " +
Spencer Lee8e990232015-11-27 03:54:39 -0700327 to_string(m_nInterestsSent), false, true);
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700328 m_logger.log("Total Responses Received = " +
Spencer Lee8e990232015-11-27 03:54:39 -0700329 to_string(m_nInterestsReceived), false, true);
Davide Pesaventod0b59982015-02-27 19:15:15 +0100330
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;
Spencer Lee8e990232015-11-27 03:54:39 -0700334 m_logger.log("Total Interest Loss = " + 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 = " +
Spencer Lee8e990232015-11-27 03:54:39 -0700346 to_string(inconsistency) + "%", false, true);
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700347 m_logger.log("Total Round Trip Time = " +
Spencer Lee8e990232015-11-27 03:54:39 -0700348 to_string(m_totalInterestRoundTripTime) + "ms", false, true);
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700349 m_logger.log("Average Round Trip Time = " +
Spencer Lee8e990232015-11-27 03:54:39 -0700350 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 #" +
Spencer Lee8e990232015-11-27 03:54:39 -0700355 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 = " +
Spencer Lee8e990232015-11-27 03:54:39 -0700358 to_string(m_trafficPatterns[patternId].m_nInterestsSent), false, true);
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700359 m_logger.log("Total Responses Received = " +
Spencer Lee8e990232015-11-27 03:54:39 -0700360 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 }
Spencer Lee8e990232015-11-27 03:54:39 -0700369 m_logger.log("Total Interest Loss = " + 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 = " +
Spencer Lee8e990232015-11-27 03:54:39 -0700380 to_string(inconsistency) + "%", false, true);
Alexander Afanasyevfda32a32014-03-20 10:50:00 -0700381 m_logger.log("Total Round Trip Time = " +
Spencer Lee8e990232015-11-27 03:54:39 -0700382 to_string(m_trafficPatterns[patternId].m_totalInterestRoundTripTime) +
Davide Pesaventod0b59982015-02-27 19:15:15 +0100383 "ms", false, true);
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700384 m_logger.log("Average Round Trip Time = " +
Spencer Lee8e990232015-11-27 03:54:39 -0700385 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 #" +
Spencer Lee8e990232015-11-27 03:54:39 -0700448 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
Eric Newberry3b284192015-07-06 21:44:46 -0700512 static name::Component
513 generateRandomNameComponent(size_t length)
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800514 {
Eric Newberry3b284192015-07-06 21:44:46 -0700515 Buffer buffer(length);
516 for (size_t i = 0; i < length; i++) {
517 buffer[i] = static_cast<uint8_t>(std::rand() % 256);
518 }
519 return name::Component(buffer);
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800520 }
521
522 void
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700523 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 Afanasyeva8f2a922014-02-26 14:21:56 -0800529 {
Spencer Lee8e990232015-11-27 03:54:39 -0700530 std::string logLine = "Data Received - PatternType=" + to_string(patternId + 1);
531 logLine += ", GlobalID=" + to_string(globalReference);
532 logLine += ", LocalID=" + to_string(localReference);
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700533 logLine += ", Name=" + interest.getName().toUri();
534
535 m_nInterestsReceived++;
536 m_trafficPatterns[patternId].m_nInterestsReceived++;
Davide Pesaventod0b59982015-02-27 19:15:15 +0100537 if (!m_trafficPatterns[patternId].m_expectedContent.empty())
jeraldabraham473ef3d2014-03-06 12:40:35 -0700538 {
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700539 std::string receivedContent = reinterpret_cast<const char*>(data.getContent().value());
540 int receivedContentLength = data.getContent().value_size();
Alexander Afanasyevfda32a32014-03-20 10:50:00 -0700541 receivedContent = receivedContent.substr(0, receivedContentLength);
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700542 if (receivedContent != m_trafficPatterns[patternId].m_expectedContent)
Alexander Afanasyevfda32a32014-03-20 10:50:00 -0700543 {
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700544 m_nContentInconsistencies++;
545 m_trafficPatterns[patternId].m_nContentInconsistencies++;
Alexander Afanasyevfda32a32014-03-20 10:50:00 -0700546 logLine += ", IsConsistent=No";
547 }
548 else
549 logLine += ", IsConsistent=Yes";
jeraldabraham473ef3d2014-03-06 12:40:35 -0700550 }
jeraldabraham473ef3d2014-03-06 12:40:35 -0700551 else
552 logLine += ", IsConsistent=NotChecked";
jeraldabraham420dbf02014-04-25 22:58:31 -0700553 if (!m_hasQuietLogging)
554 m_logger.log(logLine, true, false);
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700555 double roundTripTime = (time::steady_clock::now() - sentTime).count() / 1000000.0;
556 if (m_minimumInterestRoundTripTime > roundTripTime)
557 m_minimumInterestRoundTripTime = roundTripTime;
558 if (m_maximumInterestRoundTripTime < roundTripTime)
559 m_maximumInterestRoundTripTime = roundTripTime;
560 if (m_trafficPatterns[patternId].m_minimumInterestRoundTripTime > roundTripTime)
561 m_trafficPatterns[patternId].m_minimumInterestRoundTripTime = roundTripTime;
562 if (m_trafficPatterns[patternId].m_maximumInterestRoundTripTime < roundTripTime)
563 m_trafficPatterns[patternId].m_maximumInterestRoundTripTime = roundTripTime;
564 m_totalInterestRoundTripTime += roundTripTime;
565 m_trafficPatterns[patternId].m_totalInterestRoundTripTime += roundTripTime;
jeraldabrahamdbfee2e2014-04-04 01:18:14 -0700566 if (m_nMaximumInterests >= 0 && globalReference == m_nMaximumInterests)
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800567 {
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700568 logStatistics();
569 m_logger.shutdownLogger();
570 m_face.shutdown();
Alexander Afanasyev976c3972014-05-26 17:03:40 +0300571 m_ioService.stop();
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700572 }
573 }
574
575 void
576 onTimeout(const ndn::Interest& interest,
577 int globalReference,
578 int localReference,
579 int patternId)
580 {
Spencer Lee8e990232015-11-27 03:54:39 -0700581 std::string logLine = "Interest Timed Out - PatternType=" + to_string(patternId + 1);
582 logLine += ", GlobalID=" + to_string(globalReference);
583 logLine += ", LocalID=" + to_string(localReference);
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700584 logLine += ", Name=" + interest.getName().toUri();
585 m_logger.log(logLine, true, false);
jeraldabrahamdbfee2e2014-04-04 01:18:14 -0700586 if (m_nMaximumInterests >= 0 && globalReference == m_nMaximumInterests)
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700587 {
588 logStatistics();
589 m_logger.shutdownLogger();
590 m_face.shutdown();
Alexander Afanasyev976c3972014-05-26 17:03:40 +0300591 m_ioService.stop();
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700592 }
593 }
594
595 void
Davide Pesaventod0b59982015-02-27 19:15:15 +0100596 generateTraffic(boost::asio::deadline_timer* timer)
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700597 {
598 if (m_nMaximumInterests < 0 || m_nInterestsSent < m_nMaximumInterests)
599 {
600 int trafficKey = std::rand() % 100;
601 int cumulativePercentage = 0;
Davide Pesaventod0b59982015-02-27 19:15:15 +0100602 std::size_t patternId;
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700603 for (patternId = 0; patternId < m_trafficPatterns.size(); patternId++)
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800604 {
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700605 cumulativePercentage += m_trafficPatterns[patternId].m_trafficPercentage;
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800606 if (trafficKey <= cumulativePercentage)
607 {
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700608 Name interestName(m_trafficPatterns[patternId].m_name);
609 if (m_trafficPatterns[patternId].m_nameAppendBytes > 0)
610 interestName.append(
Eric Newberry3b284192015-07-06 21:44:46 -0700611 generateRandomNameComponent(m_trafficPatterns[patternId].m_nameAppendBytes));
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700612 if (m_trafficPatterns[patternId].m_nameAppendSequenceNumber >= 0)
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800613 {
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700614 interestName.append(
Spencer Lee8e990232015-11-27 03:54:39 -0700615 to_string(m_trafficPatterns[patternId].m_nameAppendSequenceNumber));
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700616 m_trafficPatterns[patternId].m_nameAppendSequenceNumber++;
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800617 }
Davide Pesaventod0b59982015-02-27 19:15:15 +0100618
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800619 Interest interest(interestName);
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700620 if (m_trafficPatterns[patternId].m_minSuffixComponents >= 0)
621 interest.setMinSuffixComponents(
622 m_trafficPatterns[patternId].m_minSuffixComponents);
623 if (m_trafficPatterns[patternId].m_maxSuffixComponents >= 0)
624 interest.setMaxSuffixComponents(
625 m_trafficPatterns[patternId].m_maxSuffixComponents);
Davide Pesaventod0b59982015-02-27 19:15:15 +0100626
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800627 Exclude exclude;
Davide Pesaventod0b59982015-02-27 19:15:15 +0100628 if (!m_trafficPatterns[patternId].m_excludeBefore.empty() &&
629 !m_trafficPatterns[patternId].m_excludeAfter.empty())
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800630 {
Alexander Afanasyevfda32a32014-03-20 10:50:00 -0700631 exclude.excludeRange(
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700632 name::Component(
633 m_trafficPatterns[patternId].m_excludeAfter),
634 name::Component(m_trafficPatterns[patternId].m_excludeBefore));
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800635 interest.setExclude(exclude);
636 }
Davide Pesaventod0b59982015-02-27 19:15:15 +0100637 else if (!m_trafficPatterns[patternId].m_excludeBefore.empty())
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800638 {
Alexander Afanasyevfda32a32014-03-20 10:50:00 -0700639 exclude.excludeBefore(
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700640 name::Component(m_trafficPatterns[patternId].m_excludeBefore));
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800641 interest.setExclude(exclude);
642 }
Davide Pesaventod0b59982015-02-27 19:15:15 +0100643 else if (!m_trafficPatterns[patternId].m_excludeAfter.empty())
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800644 {
Alexander Afanasyevfda32a32014-03-20 10:50:00 -0700645 exclude.excludeAfter(
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700646 name::Component(m_trafficPatterns[patternId].m_excludeAfter));
647 interest.setExclude(exclude);
648 }
649 if (m_trafficPatterns[patternId].m_excludeBeforeBytes > 0 &&
650 m_trafficPatterns[patternId].m_excludeAfterBytes > 0)
651 {
652 exclude.excludeRange(
Eric Newberry3b284192015-07-06 21:44:46 -0700653 generateRandomNameComponent(
654 m_trafficPatterns[patternId].m_excludeAfterBytes),
655 generateRandomNameComponent(
656 m_trafficPatterns[patternId].m_excludeBeforeBytes));
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700657 interest.setExclude(exclude);
658 }
659 else if (m_trafficPatterns[patternId].m_excludeBeforeBytes > 0)
660 {
661 exclude.excludeBefore(
Eric Newberry3b284192015-07-06 21:44:46 -0700662 generateRandomNameComponent(
663 m_trafficPatterns[patternId].m_excludeBeforeBytes));
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700664 interest.setExclude(exclude);
665 }
666 else if (m_trafficPatterns[patternId].m_excludeAfterBytes > 0)
667 {
668 exclude.excludeAfter(
Eric Newberry3b284192015-07-06 21:44:46 -0700669 generateRandomNameComponent(
670 m_trafficPatterns[patternId].m_excludeAfterBytes));
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800671 interest.setExclude(exclude);
672 }
673
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700674 if (m_trafficPatterns[patternId].m_childSelector >= 0)
675 interest.setChildSelector(m_trafficPatterns[patternId].m_childSelector);
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800676
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700677 if (m_trafficPatterns[patternId].m_mustBeFresh == 0)
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800678 interest.setMustBeFresh(false);
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700679 else if (m_trafficPatterns[patternId].m_mustBeFresh > 0)
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800680 interest.setMustBeFresh(true);
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700681 if (m_trafficPatterns[patternId].m_nonceDuplicationPercentage > 0)
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800682 {
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700683 int duplicationPercentage = std::rand() % 100;
684 if (m_trafficPatterns[patternId].m_nonceDuplicationPercentage <=
685 duplicationPercentage)
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800686 interest.setNonce(getOldNonce());
687 else
688 interest.setNonce(getNewNonce());
689 }
690 else
691 interest.setNonce(getNewNonce());
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700692 if (m_trafficPatterns[patternId].m_interestLifetime >= time::milliseconds(0))
693 interest.setInterestLifetime(m_trafficPatterns[patternId].m_interestLifetime);
694
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800695 try {
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700696 m_nInterestsSent++;
697 m_trafficPatterns[patternId].m_nInterestsSent++;
698 time::steady_clock::TimePoint sentTime = time::steady_clock::now();
699 m_face.expressInterest(interest,
700 bind(&NdnTrafficClient::onData,
701 this, _1, _2, m_nInterestsSent,
702 m_trafficPatterns[patternId].m_nInterestsSent,
703 patternId, sentTime),
704 bind(&NdnTrafficClient::onTimeout,
705 this, _1, m_nInterestsSent,
706 m_trafficPatterns[patternId].m_nInterestsSent,
Alexander Afanasyevfda32a32014-03-20 10:50:00 -0700707 patternId));
Davide Pesaventod0b59982015-02-27 19:15:15 +0100708
709 if (!m_hasQuietLogging) {
710 std::string logLine =
Spencer Lee8e990232015-11-27 03:54:39 -0700711 "Sending Interest - PatternType=" + to_string(patternId + 1) +
712 ", GlobalID=" + to_string(m_nInterestsSent) +
Davide Pesaventod0b59982015-02-27 19:15:15 +0100713 ", LocalID=" +
Spencer Lee8e990232015-11-27 03:54:39 -0700714 to_string(m_trafficPatterns[patternId].m_nInterestsSent) +
Davide Pesaventod0b59982015-02-27 19:15:15 +0100715 ", Name=" + interest.getName().toUri();
jeraldabraham420dbf02014-04-25 22:58:31 -0700716 m_logger.log(logLine, true, false);
Davide Pesaventod0b59982015-02-27 19:15:15 +0100717 }
718
719 timer->expires_at(timer->expires_at() +
720 boost::posix_time::millisec(m_interestInterval.count()));
721 timer->async_wait(bind(&NdnTrafficClient::generateTraffic, this, timer));
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800722 }
Davide Pesaventod0b59982015-02-27 19:15:15 +0100723 catch (const std::exception& e) {
724 m_logger.log("ERROR: " + std::string(e.what()), true, true);
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800725 }
726 break;
727 }
728 }
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700729 if (patternId == m_trafficPatterns.size())
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800730 {
Davide Pesaventod0b59982015-02-27 19:15:15 +0100731 timer->expires_at(timer->expires_at() +
732 boost::posix_time::millisec(m_interestInterval.count()));
733 timer->async_wait(bind(&NdnTrafficClient::generateTraffic, this, timer));
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800734 }
735 }
736 }
737
738 void
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700739 run()
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800740 {
Alexander Afanasyev976c3972014-05-26 17:03:40 +0300741 boost::asio::signal_set signalSet(m_ioService, SIGINT, SIGTERM);
Alexander Afanasyevfda32a32014-03-20 10:50:00 -0700742 signalSet.async_wait(bind(&NdnTrafficClient::signalHandler, this));
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700743 m_logger.initializeLog(m_instanceId);
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800744 initializeTrafficConfiguration();
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700745
746 if (m_nMaximumInterests == 0)
747 {
748 logStatistics();
749 m_logger.shutdownLogger();
750 return;
751 }
752
Davide Pesaventod0b59982015-02-27 19:15:15 +0100753 boost::asio::deadline_timer deadlineTimer(m_ioService,
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700754 boost::posix_time::millisec(m_interestInterval.count()));
Davide Pesaventod0b59982015-02-27 19:15:15 +0100755 deadlineTimer.async_wait(bind(&NdnTrafficClient::generateTraffic, this, &deadlineTimer));
756
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800757 try {
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700758 m_face.processEvents();
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800759 }
Davide Pesaventod0b59982015-02-27 19:15:15 +0100760 catch (const std::exception& e) {
761 m_logger.log("ERROR: " + std::string(e.what()), true, true);
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800762 m_logger.shutdownLogger();
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700763 m_hasError = true;
Alexander Afanasyev976c3972014-05-26 17:03:40 +0300764 m_ioService.stop();
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800765 }
766 }
767
768private:
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700769 std::string m_programName;
Alexander Afanasyev740812e2014-10-30 15:37:45 -0700770 Logger m_logger;
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700771 std::string m_instanceId;
772 bool m_hasError;
jeraldabraham420dbf02014-04-25 22:58:31 -0700773 bool m_hasQuietLogging;
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700774 time::milliseconds m_interestInterval;
775 int m_nMaximumInterests;
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700776 std::string m_configurationFile;
Alexander Afanasyev976c3972014-05-26 17:03:40 +0300777 boost::asio::io_service m_ioService;
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700778 Face m_face;
779 std::vector<InterestTrafficConfiguration> m_trafficPatterns;
780 std::vector<uint32_t> m_nonces;
781 int m_nInterestsSent;
782 int m_nInterestsReceived;
783 int m_nContentInconsistencies;
784
785 //round trip time is stored as milliseconds with fractional
786 //sub-milliseconds precision
787 double m_minimumInterestRoundTripTime;
788 double m_maximumInterestRoundTripTime;
789 double m_totalInterestRoundTripTime;
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800790};
791
792} // namespace ndn
793
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700794int
795main(int argc, char* argv[])
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800796{
Davide Pesaventod0b59982015-02-27 19:15:15 +0100797 std::srand(std::time(nullptr));
798
799 ndn::NdnTrafficClient client(argv[0]);
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700800 int option;
jeraldabraham420dbf02014-04-25 22:58:31 -0700801 while ((option = getopt(argc, argv, "hqi:c:")) != -1) {
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800802 switch (option) {
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700803 case 'h':
Davide Pesaventod0b59982015-02-27 19:15:15 +0100804 client.usage();
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800805 break;
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700806 case 'i':
Davide Pesaventod0b59982015-02-27 19:15:15 +0100807 client.setInterestInterval(atoi(optarg));
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800808 break;
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700809 case 'c':
Davide Pesaventod0b59982015-02-27 19:15:15 +0100810 client.setMaximumInterests(atoi(optarg));
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800811 break;
jeraldabraham420dbf02014-04-25 22:58:31 -0700812 case 'q':
Davide Pesaventod0b59982015-02-27 19:15:15 +0100813 client.setQuietLogging();
jeraldabraham420dbf02014-04-25 22:58:31 -0700814 break;
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700815 default:
Davide Pesaventod0b59982015-02-27 19:15:15 +0100816 client.usage();
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800817 break;
818 }
819 }
820
821 argc -= optind;
822 argv += optind;
823
Davide Pesaventod0b59982015-02-27 19:15:15 +0100824 if (!argc)
825 client.usage();
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800826
Davide Pesaventod0b59982015-02-27 19:15:15 +0100827 client.setConfigurationFile(argv[0]);
828 client.run();
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800829
Davide Pesaventod0b59982015-02-27 19:15:15 +0100830 return client.hasError() ? EXIT_FAILURE : EXIT_SUCCESS;
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800831}