blob: d6faaf762b5ec7f5272b365531f80c9563b0c510 [file] [log] [blame]
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2/**
Eric Newberry976c2042016-06-19 23:37:35 -07003 * Copyright (C) 2014-2016 The 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)
Eric Newberry976c2042016-06-19 23:37:35 -070057 , m_nNacks(0)
jeraldabrahamdbfee2e2014-04-04 01:18:14 -070058 , m_nContentInconsistencies(0)
jeraldabrahamcc3c6c92014-03-28 02:21:45 -070059 , m_minimumInterestRoundTripTime(std::numeric_limits<double>::max())
60 , m_maximumInterestRoundTripTime(0)
61 , m_totalInterestRoundTripTime(0)
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -080062 {
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -080063 }
64
65 class InterestTrafficConfiguration
66 {
67 public:
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -080068 InterestTrafficConfiguration()
jeraldabrahamcc3c6c92014-03-28 02:21:45 -070069 : m_trafficPercentage(-1)
70 , m_nameAppendBytes(-1)
71 , m_nameAppendSequenceNumber(-1)
72 , m_minSuffixComponents(-1)
73 , m_maxSuffixComponents(-1)
74 , m_excludeBeforeBytes(-1)
75 , m_excludeAfterBytes(-1)
76 , m_childSelector(-1)
77 , m_mustBeFresh(-1)
78 , m_nonceDuplicationPercentage(-1)
jeraldabrahamcc3c6c92014-03-28 02:21:45 -070079 , m_interestLifetime(getDefaultInterestLifetime())
Eric Newberryeab462b2016-03-18 11:56:31 -070080 , m_nextHopFaceId(0)
jeraldabrahamdbfee2e2014-04-04 01:18:14 -070081 , m_nInterestsSent(0)
82 , m_nInterestsReceived(0)
Eric Newberry976c2042016-06-19 23:37:35 -070083 , m_nNacks(0)
jeraldabrahamcc3c6c92014-03-28 02:21:45 -070084 , m_minimumInterestRoundTripTime(std::numeric_limits<double>::max())
85 , m_maximumInterestRoundTripTime(0)
86 , m_totalInterestRoundTripTime(0)
87 , m_nContentInconsistencies(0)
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -080088 {
jeraldabrahamcc3c6c92014-03-28 02:21:45 -070089 }
90
Davide Pesaventod0b59982015-02-27 19:15:15 +010091 static time::milliseconds
jeraldabrahamcc3c6c92014-03-28 02:21:45 -070092 getDefaultInterestLifetime()
93 {
94 return time::milliseconds(-1);
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -080095 }
96
97 void
Alexander Afanasyevfda32a32014-03-20 10:50:00 -070098 printTrafficConfiguration(Logger& logger)
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -080099 {
Davide Pesaventod0b59982015-02-27 19:15:15 +0100100 std::string detail;
101
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700102 if (m_trafficPercentage > 0)
Spencer Lee8e990232015-11-27 03:54:39 -0700103 detail += "TrafficPercentage=" + to_string(m_trafficPercentage) + ", ";
Davide Pesaventod0b59982015-02-27 19:15:15 +0100104 if (!m_name.empty())
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700105 detail += "Name=" + m_name + ", ";
106 if (m_nameAppendBytes > 0)
Spencer Lee8e990232015-11-27 03:54:39 -0700107 detail += "NameAppendBytes=" + to_string(m_nameAppendBytes) + ", ";
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700108 if (m_nameAppendSequenceNumber > 0)
Spencer Lee8e990232015-11-27 03:54:39 -0700109 detail += "NameAppendSequenceNumber=" + to_string(m_nameAppendSequenceNumber) + ", ";
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700110 if (m_minSuffixComponents >= 0)
Spencer Lee8e990232015-11-27 03:54:39 -0700111 detail += "MinSuffixComponents=" + to_string(m_minSuffixComponents) + ", ";
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700112 if (m_maxSuffixComponents >= 0)
Spencer Lee8e990232015-11-27 03:54:39 -0700113 detail += "MaxSuffixComponents=" + to_string(m_maxSuffixComponents) + ", ";
Davide Pesaventod0b59982015-02-27 19:15:15 +0100114 if (!m_excludeBefore.empty())
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700115 detail += "ExcludeBefore=" + m_excludeBefore + ", ";
Davide Pesaventod0b59982015-02-27 19:15:15 +0100116 if (!m_excludeAfter.empty())
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700117 detail += "ExcludeAfter=" + m_excludeAfter + ", ";
Eric Newberry25e9ba02016-08-02 22:02:22 -0700118 if (!m_excludeRange.empty())
119 detail += "ExcludeRange=" + m_excludeRange + ", ";
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700120 if (m_excludeBeforeBytes > 0)
Spencer Lee8e990232015-11-27 03:54:39 -0700121 detail += "ExcludeBeforeBytes=" + to_string(m_excludeBeforeBytes) + ", ";
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700122 if (m_excludeAfterBytes > 0)
Spencer Lee8e990232015-11-27 03:54:39 -0700123 detail += "ExcludeAfterBytes=" + to_string(m_excludeAfterBytes) + ", ";
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700124 if (m_childSelector >= 0)
Spencer Lee8e990232015-11-27 03:54:39 -0700125 detail += "ChildSelector=" + to_string(m_childSelector) + ", ";
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700126 if (m_mustBeFresh >= 0)
Spencer Lee8e990232015-11-27 03:54:39 -0700127 detail += "MustBeFresh=" + to_string(m_mustBeFresh) + ", ";
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700128 if (m_nonceDuplicationPercentage > 0)
Spencer Lee8e990232015-11-27 03:54:39 -0700129 detail += "NonceDuplicationPercentage=" + to_string(m_nonceDuplicationPercentage) + ", ";
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700130 if (m_interestLifetime >= time::milliseconds(0))
Spencer Lee8e990232015-11-27 03:54:39 -0700131 detail += "InterestLifetime=" + to_string(m_interestLifetime.count()) + ", ";
Eric Newberryeab462b2016-03-18 11:56:31 -0700132 if (m_nextHopFaceId > 0)
133 detail += "NextHopFaceId=" + to_string(m_nextHopFaceId) + ", ";
Davide Pesaventod0b59982015-02-27 19:15:15 +0100134 if (!m_expectedContent.empty())
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700135 detail += "ExpectedContent=" + m_expectedContent + ", ";
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800136 if (detail.length() >= 2)
Davide Pesaventod0b59982015-02-27 19:15:15 +0100137 detail = detail.substr(0, detail.length() - 2); // Removing suffix ", "
138
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800139 logger.log(detail, false, false);
140 }
141
142 bool
Davide Pesaventod0b59982015-02-27 19:15:15 +0100143 extractParameterValue(const std::string& detail, std::string& parameter, std::string& value)
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800144 {
Eric Newberry25e9ba02016-08-02 22:02:22 -0700145 std::string allowedCharacters = ":/+.,_-%";
Davide Pesaventod0b59982015-02-27 19:15:15 +0100146 std::size_t i = 0;
147
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800148 parameter = "";
149 value = "";
Davide Pesaventod0b59982015-02-27 19:15:15 +0100150 while (detail[i] != '=' && i < detail.length()) {
151 parameter += detail[i];
152 i++;
153 }
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800154 if (i == detail.length())
155 return false;
Davide Pesaventod0b59982015-02-27 19:15:15 +0100156
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800157 i++;
jeraldabraham79c0c232014-03-31 21:43:59 -0700158 while ((std::isalnum(detail[i]) ||
Davide Pesaventod0b59982015-02-27 19:15:15 +0100159 allowedCharacters.find(detail[i]) != std::string::npos) &&
160 i < detail.length()) {
161 value += detail[i];
162 i++;
163 }
164
165 if (parameter.empty() || value.empty())
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800166 return false;
Davide Pesaventod0b59982015-02-27 19:15:15 +0100167 else
168 return true;
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800169 }
170
171 bool
Alexander Afanasyevfda32a32014-03-20 10:50:00 -0700172 processConfigurationDetail(const std::string& detail, Logger& logger, int lineNumber)
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800173 {
174 std::string parameter, value;
175 if (extractParameterValue(detail, parameter, value))
176 {
177 if (parameter == "TrafficPercentage")
Davide Pesaventod0b59982015-02-27 19:15:15 +0100178 m_trafficPercentage = std::stoi(value);
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800179 else if (parameter == "Name")
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700180 m_name = value;
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800181 else if (parameter == "NameAppendBytes")
Davide Pesaventod0b59982015-02-27 19:15:15 +0100182 m_nameAppendBytes = std::stoi(value);
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800183 else if (parameter == "NameAppendSequenceNumber")
Davide Pesaventod0b59982015-02-27 19:15:15 +0100184 m_nameAppendSequenceNumber = std::stoi(value);
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800185 else if (parameter == "MinSuffixComponents")
Davide Pesaventod0b59982015-02-27 19:15:15 +0100186 m_minSuffixComponents = std::stoi(value);
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800187 else if (parameter == "MaxSuffixComponents")
Davide Pesaventod0b59982015-02-27 19:15:15 +0100188 m_maxSuffixComponents = std::stoi(value);
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800189 else if (parameter == "ExcludeBefore")
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700190 m_excludeBefore = value;
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800191 else if (parameter == "ExcludeAfter")
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700192 m_excludeAfter = value;
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800193 else if (parameter == "ExcludeBeforeBytes")
Davide Pesaventod0b59982015-02-27 19:15:15 +0100194 m_excludeBeforeBytes = std::stoi(value);
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800195 else if (parameter == "ExcludeAfterBytes")
Davide Pesaventod0b59982015-02-27 19:15:15 +0100196 m_excludeAfterBytes = std::stoi(value);
Eric Newberry25e9ba02016-08-02 22:02:22 -0700197 else if (parameter == "ExcludeRange")
198 m_excludeRange = value;
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800199 else if (parameter == "ChildSelector")
Davide Pesaventod0b59982015-02-27 19:15:15 +0100200 m_childSelector = std::stoi(value);
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800201 else if (parameter == "MustBeFresh")
Davide Pesaventod0b59982015-02-27 19:15:15 +0100202 m_mustBeFresh = std::stoi(value);
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800203 else if (parameter == "NonceDuplicationPercentage")
Davide Pesaventod0b59982015-02-27 19:15:15 +0100204 m_nonceDuplicationPercentage = std::stoi(value);
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800205 else if (parameter == "InterestLifetime")
Davide Pesaventod0b59982015-02-27 19:15:15 +0100206 m_interestLifetime = time::milliseconds(std::stoi(value));
Eric Newberryeab462b2016-03-18 11:56:31 -0700207 else if (parameter == "NextHopFaceId")
208 m_nextHopFaceId = std::stoi(value);
jeraldabraham473ef3d2014-03-06 12:40:35 -0700209 else if (parameter == "ExpectedContent")
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700210 m_expectedContent = value;
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800211 else
Spencer Lee8e990232015-11-27 03:54:39 -0700212 logger.log("Line " + to_string(lineNumber) +
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700213 " \t- Invalid Parameter='" + parameter + "'", false, true);
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800214 }
215 else
216 {
Spencer Lee8e990232015-11-27 03:54:39 -0700217 logger.log("Line " + to_string(lineNumber) +
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700218 " \t- Improper Traffic Configuration Line- " + detail, false, true);
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800219 return false;
220 }
221 return true;
222 }
223
224 bool
225 checkTrafficDetailCorrectness()
226 {
227 return true;
228 }
229
Davide Pesaventod0b59982015-02-27 19:15:15 +0100230 private:
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700231 int m_trafficPercentage;
232 std::string m_name;
233 int m_nameAppendBytes;
234 int m_nameAppendSequenceNumber;
235 int m_minSuffixComponents;
236 int m_maxSuffixComponents;
237 std::string m_excludeBefore;
238 std::string m_excludeAfter;
Eric Newberry25e9ba02016-08-02 22:02:22 -0700239 std::string m_excludeRange;
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700240 int m_excludeBeforeBytes;
241 int m_excludeAfterBytes;
242 int m_childSelector;
243 int m_mustBeFresh;
244 int m_nonceDuplicationPercentage;
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700245 time::milliseconds m_interestLifetime;
Eric Newberryeab462b2016-03-18 11:56:31 -0700246 uint64_t m_nextHopFaceId;
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700247 int m_nInterestsSent;
248 int m_nInterestsReceived;
Eric Newberry976c2042016-06-19 23:37:35 -0700249 int m_nNacks;
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700250
251 //round trip time is stored as milliseconds with fractional
252 //sub-millisecond precision
253 double m_minimumInterestRoundTripTime;
254 double m_maximumInterestRoundTripTime;
255 double m_totalInterestRoundTripTime;
256
257 int m_nContentInconsistencies;
258 std::string m_expectedContent;
Davide Pesaventod0b59982015-02-27 19:15:15 +0100259
260 friend class NdnTrafficClient;
Alexander Afanasyevfda32a32014-03-20 10:50:00 -0700261 }; // class InterestTrafficConfiguration
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800262
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700263 bool
264 hasError() const
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800265 {
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700266 return m_hasError;
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800267 }
268
269 void
Davide Pesaventod0b59982015-02-27 19:15:15 +0100270 usage() const
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800271 {
Davide Pesaventod0b59982015-02-27 19:15:15 +0100272 std::cout << "Usage:\n"
273 << " " << m_programName << " [options] <Traffic_Configuration_File>\n"
274 << "\n"
275 << "Generate Interest traffic as per provided Traffic Configuration File.\n"
276 << "Interests are continuously generated unless a total number is specified.\n"
277 << "Set environment variable NDN_TRAFFIC_LOGFOLDER to redirect output to a log file.\n"
278 << "\n"
279 << "Options:\n"
280 << " [-i interval] - set interest generation interval in milliseconds (default "
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700281 << getDefaultInterestInterval() << ")\n"
Davide Pesaventod0b59982015-02-27 19:15:15 +0100282 << " [-c count] - set total number of interests to be generated\n"
283 << " [-q] - quiet mode: no interest reception/data generation logging\n"
284 << " [-h] - print this help text and exit\n";
285 exit(EXIT_FAILURE);
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800286 }
287
Davide Pesaventod0b59982015-02-27 19:15:15 +0100288 static time::milliseconds
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800289 getDefaultInterestInterval()
290 {
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700291 return time::milliseconds(1000);
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800292 }
293
294 void
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700295 setInterestInterval(int interestInterval)
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800296 {
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700297 if (interestInterval <= 0)
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800298 usage();
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700299 m_interestInterval = time::milliseconds(interestInterval);
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800300 }
301
302 void
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700303 setMaximumInterests(int maximumInterests)
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800304 {
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700305 if (maximumInterests <= 0)
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800306 usage();
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700307 m_nMaximumInterests = maximumInterests;
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800308 }
309
310 void
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700311 setConfigurationFile(char* configurationFile)
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800312 {
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700313 m_configurationFile = configurationFile;
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800314 }
315
316 void
jeraldabraham420dbf02014-04-25 22:58:31 -0700317 setQuietLogging()
318 {
319 m_hasQuietLogging = true;
320 }
321
322 void
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800323 signalHandler()
324 {
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800325 logStatistics();
Davide Pesaventod0b59982015-02-27 19:15:15 +0100326
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700327 m_logger.shutdownLogger();
328 m_face.shutdown();
Alexander Afanasyev976c3972014-05-26 17:03:40 +0300329 m_ioService.stop();
Davide Pesaventod0b59982015-02-27 19:15:15 +0100330
331 exit(m_hasError ? EXIT_FAILURE : EXIT_SUCCESS);
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800332 }
333
334 void
335 logStatistics()
336 {
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800337 m_logger.log("\n\n== Interest Traffic Report ==\n", false, true);
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700338 m_logger.log("Total Traffic Pattern Types = " +
Spencer Lee8e990232015-11-27 03:54:39 -0700339 to_string(m_trafficPatterns.size()), false, true);
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700340 m_logger.log("Total Interests Sent = " +
Spencer Lee8e990232015-11-27 03:54:39 -0700341 to_string(m_nInterestsSent), false, true);
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700342 m_logger.log("Total Responses Received = " +
Spencer Lee8e990232015-11-27 03:54:39 -0700343 to_string(m_nInterestsReceived), false, true);
Eric Newberry976c2042016-06-19 23:37:35 -0700344 m_logger.log("Total Nacks Received = " +
345 to_string(m_nNacks), false, true);
Davide Pesaventod0b59982015-02-27 19:15:15 +0100346
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700347 double loss = 0;
348 if (m_nInterestsSent > 0)
349 loss = (m_nInterestsSent - m_nInterestsReceived) * 100.0 / m_nInterestsSent;
Spencer Lee8e990232015-11-27 03:54:39 -0700350 m_logger.log("Total Interest Loss = " + to_string(loss) + "%", false, true);
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700351 if (m_nContentInconsistencies != 0 || m_nInterestsSent != m_nInterestsReceived)
352 m_hasError = true;
Davide Pesaventod0b59982015-02-27 19:15:15 +0100353
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700354 double average = 0;
355 double inconsistency = 0;
356 if (m_nInterestsReceived > 0)
Alexander Afanasyevfda32a32014-03-20 10:50:00 -0700357 {
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700358 average = m_totalInterestRoundTripTime / m_nInterestsReceived;
359 inconsistency = m_nContentInconsistencies * 100.0 / m_nInterestsReceived;
Alexander Afanasyevfda32a32014-03-20 10:50:00 -0700360 }
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700361 m_logger.log("Total Data Inconsistency = " +
Spencer Lee8e990232015-11-27 03:54:39 -0700362 to_string(inconsistency) + "%", false, true);
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700363 m_logger.log("Total Round Trip Time = " +
Spencer Lee8e990232015-11-27 03:54:39 -0700364 to_string(m_totalInterestRoundTripTime) + "ms", false, true);
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700365 m_logger.log("Average Round Trip Time = " +
Spencer Lee8e990232015-11-27 03:54:39 -0700366 to_string(average) + "ms\n", false, true);
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700367
Davide Pesaventod0b59982015-02-27 19:15:15 +0100368 for (std::size_t patternId = 0; patternId < m_trafficPatterns.size(); patternId++)
Alexander Afanasyevfda32a32014-03-20 10:50:00 -0700369 {
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700370 m_logger.log("Traffic Pattern Type #" +
Spencer Lee8e990232015-11-27 03:54:39 -0700371 to_string(patternId + 1), false, true);
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700372 m_trafficPatterns[patternId].printTrafficConfiguration(m_logger);
373 m_logger.log("Total Interests Sent = " +
Spencer Lee8e990232015-11-27 03:54:39 -0700374 to_string(m_trafficPatterns[patternId].m_nInterestsSent), false, true);
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700375 m_logger.log("Total Responses Received = " +
Spencer Lee8e990232015-11-27 03:54:39 -0700376 to_string(m_trafficPatterns[patternId].m_nInterestsReceived), false, true);
Eric Newberry976c2042016-06-19 23:37:35 -0700377 m_logger.log("Total Nacks Received = " +
378 to_string(m_trafficPatterns[patternId].m_nNacks), false, true);
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700379 loss = 0;
380 if (m_trafficPatterns[patternId].m_nInterestsSent > 0)
381 {
382 loss = (m_trafficPatterns[patternId].m_nInterestsSent -
383 m_trafficPatterns[patternId].m_nInterestsReceived);
384 loss *= 100.0;
385 loss /= m_trafficPatterns[patternId].m_nInterestsSent;
386 }
Spencer Lee8e990232015-11-27 03:54:39 -0700387 m_logger.log("Total Interest Loss = " + to_string(loss) + "%", false, true);
Alexander Afanasyevfda32a32014-03-20 10:50:00 -0700388 average = 0;
389 inconsistency = 0;
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700390 if (m_trafficPatterns[patternId].m_nInterestsReceived > 0)
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800391 {
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700392 average = (m_trafficPatterns[patternId].m_totalInterestRoundTripTime /
393 m_trafficPatterns[patternId].m_nInterestsReceived);
394 inconsistency = m_trafficPatterns[patternId].m_nContentInconsistencies;
Davide Pesaventod0b59982015-02-27 19:15:15 +0100395 inconsistency *= 100.0 / m_trafficPatterns[patternId].m_nInterestsReceived;
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800396 }
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700397 m_logger.log("Total Data Inconsistency = " +
Spencer Lee8e990232015-11-27 03:54:39 -0700398 to_string(inconsistency) + "%", false, true);
Alexander Afanasyevfda32a32014-03-20 10:50:00 -0700399 m_logger.log("Total Round Trip Time = " +
Spencer Lee8e990232015-11-27 03:54:39 -0700400 to_string(m_trafficPatterns[patternId].m_totalInterestRoundTripTime) +
Davide Pesaventod0b59982015-02-27 19:15:15 +0100401 "ms", false, true);
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700402 m_logger.log("Average Round Trip Time = " +
Spencer Lee8e990232015-11-27 03:54:39 -0700403 to_string(average) + "ms\n", false, true);
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800404 }
405 }
406
407 bool
408 checkTrafficPatternCorrectness()
409 {
410 return true;
411 }
412
413 void
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700414 parseConfigurationFile()
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800415 {
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800416 std::string patternLine;
417 std::ifstream patternFile;
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700418 m_logger.log("Analyzing Traffic Configuration File: " + m_configurationFile, true, true);
Davide Pesaventod0b59982015-02-27 19:15:15 +0100419
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700420 patternFile.open(m_configurationFile.c_str());
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800421 if (patternFile.is_open())
422 {
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700423 int lineNumber = 0;
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800424 while (getline(patternFile, patternLine))
425 {
426 lineNumber++;
427 if (std::isalpha(patternLine[0]))
428 {
429 InterestTrafficConfiguration interestData;
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700430 bool shouldSkipLine = false;
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800431 if (interestData.processConfigurationDetail(patternLine, m_logger, lineNumber))
432 {
433 while (getline(patternFile, patternLine) && std::isalpha(patternLine[0]))
434 {
435 lineNumber++;
Alexander Afanasyevfda32a32014-03-20 10:50:00 -0700436 if (!interestData.processConfigurationDetail(patternLine,
437 m_logger, lineNumber))
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800438 {
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700439 shouldSkipLine = true;
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800440 break;
441 }
442 }
443 lineNumber++;
444 }
445 else
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700446 shouldSkipLine = true;
447 if (!shouldSkipLine)
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800448 {
449 if (interestData.checkTrafficDetailCorrectness())
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700450 m_trafficPatterns.push_back(interestData);
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800451 }
452 }
453 }
454 patternFile.close();
455 if (!checkTrafficPatternCorrectness())
456 {
Alexander Afanasyevfda32a32014-03-20 10:50:00 -0700457 m_logger.log("ERROR - Traffic Configuration Provided Is Not Proper- " +
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700458 m_configurationFile, false, true);
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800459 m_logger.shutdownLogger();
Davide Pesaventod0b59982015-02-27 19:15:15 +0100460 exit(EXIT_FAILURE);
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800461 }
462 m_logger.log("Traffic Configuration File Processing Completed\n", true, false);
Davide Pesaventod0b59982015-02-27 19:15:15 +0100463 for (std::size_t patternId = 0; patternId < m_trafficPatterns.size(); patternId++)
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800464 {
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700465 m_logger.log("Traffic Pattern Type #" +
Spencer Lee8e990232015-11-27 03:54:39 -0700466 to_string(patternId + 1), false, false);
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700467 m_trafficPatterns[patternId].printTrafficConfiguration(m_logger);
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800468 m_logger.log("", false, false);
469 }
470 }
471 else
472 {
Alexander Afanasyevfda32a32014-03-20 10:50:00 -0700473 m_logger.log("ERROR - Unable To Open Traffic Configuration 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
480 void
481 initializeTrafficConfiguration()
482 {
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700483 if (boost::filesystem::exists(boost::filesystem::path(m_configurationFile)))
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800484 {
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700485 if (boost::filesystem::is_regular_file(boost::filesystem::path(m_configurationFile)))
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800486 {
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700487 parseConfigurationFile();
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800488 }
489 else
490 {
Alexander Afanasyevfda32a32014-03-20 10:50:00 -0700491 m_logger.log("ERROR - Traffic Configuration File Is Not A Regular File: " +
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700492 m_configurationFile, false, true);
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800493 m_logger.shutdownLogger();
Davide Pesaventod0b59982015-02-27 19:15:15 +0100494 exit(EXIT_FAILURE);
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800495 }
496 }
497 else
498 {
Alexander Afanasyevfda32a32014-03-20 10:50:00 -0700499 m_logger.log("ERROR - Traffic Configuration File Does Not Exist: " +
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700500 m_configurationFile, false, true);
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800501 m_logger.shutdownLogger();
Davide Pesaventod0b59982015-02-27 19:15:15 +0100502 exit(EXIT_FAILURE);
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800503 }
504 }
505
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700506 uint32_t
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800507 getOldNonce()
508 {
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700509 if (m_nonces.size() == 0)
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800510 return getNewNonce();
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700511 int randomNonceIndex = std::rand() % m_nonces.size();
512 return m_nonces[randomNonceIndex];
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800513 }
514
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700515 uint32_t
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800516 getNewNonce()
517 {
jeraldabraham473ef3d2014-03-06 12:40:35 -0700518 //Performance Enhancement
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700519 if (m_nonces.size() > 1000)
520 m_nonces.clear();
jeraldabraham473ef3d2014-03-06 12:40:35 -0700521
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700522 uint32_t randomNonce = static_cast<uint32_t>(std::rand());
523 while (std::find(m_nonces.begin(), m_nonces.end(), randomNonce) != m_nonces.end())
524 randomNonce = static_cast<uint32_t>(std::rand());
525
526 m_nonces.push_back(randomNonce);
527 return randomNonce;
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800528 }
529
Eric Newberry3b284192015-07-06 21:44:46 -0700530 static name::Component
531 generateRandomNameComponent(size_t length)
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800532 {
Eric Newberry3b284192015-07-06 21:44:46 -0700533 Buffer buffer(length);
534 for (size_t i = 0; i < length; i++) {
535 buffer[i] = static_cast<uint8_t>(std::rand() % 256);
536 }
537 return name::Component(buffer);
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800538 }
539
540 void
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700541 onData(const ndn::Interest& interest,
Eric Newberry976c2042016-06-19 23:37:35 -0700542 const ndn::Data& data,
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700543 int globalReference,
544 int localReference,
545 int patternId,
546 time::steady_clock::TimePoint sentTime)
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800547 {
Spencer Lee8e990232015-11-27 03:54:39 -0700548 std::string logLine = "Data Received - PatternType=" + to_string(patternId + 1);
549 logLine += ", GlobalID=" + to_string(globalReference);
550 logLine += ", LocalID=" + to_string(localReference);
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700551 logLine += ", Name=" + interest.getName().toUri();
552
553 m_nInterestsReceived++;
554 m_trafficPatterns[patternId].m_nInterestsReceived++;
Davide Pesaventod0b59982015-02-27 19:15:15 +0100555 if (!m_trafficPatterns[patternId].m_expectedContent.empty())
jeraldabraham473ef3d2014-03-06 12:40:35 -0700556 {
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700557 std::string receivedContent = reinterpret_cast<const char*>(data.getContent().value());
558 int receivedContentLength = data.getContent().value_size();
Alexander Afanasyevfda32a32014-03-20 10:50:00 -0700559 receivedContent = receivedContent.substr(0, receivedContentLength);
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700560 if (receivedContent != m_trafficPatterns[patternId].m_expectedContent)
Alexander Afanasyevfda32a32014-03-20 10:50:00 -0700561 {
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700562 m_nContentInconsistencies++;
563 m_trafficPatterns[patternId].m_nContentInconsistencies++;
Alexander Afanasyevfda32a32014-03-20 10:50:00 -0700564 logLine += ", IsConsistent=No";
565 }
566 else
567 logLine += ", IsConsistent=Yes";
jeraldabraham473ef3d2014-03-06 12:40:35 -0700568 }
jeraldabraham473ef3d2014-03-06 12:40:35 -0700569 else
570 logLine += ", IsConsistent=NotChecked";
jeraldabraham420dbf02014-04-25 22:58:31 -0700571 if (!m_hasQuietLogging)
572 m_logger.log(logLine, true, false);
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700573 double roundTripTime = (time::steady_clock::now() - sentTime).count() / 1000000.0;
574 if (m_minimumInterestRoundTripTime > roundTripTime)
575 m_minimumInterestRoundTripTime = roundTripTime;
576 if (m_maximumInterestRoundTripTime < roundTripTime)
577 m_maximumInterestRoundTripTime = roundTripTime;
578 if (m_trafficPatterns[patternId].m_minimumInterestRoundTripTime > roundTripTime)
579 m_trafficPatterns[patternId].m_minimumInterestRoundTripTime = roundTripTime;
580 if (m_trafficPatterns[patternId].m_maximumInterestRoundTripTime < roundTripTime)
581 m_trafficPatterns[patternId].m_maximumInterestRoundTripTime = roundTripTime;
582 m_totalInterestRoundTripTime += roundTripTime;
583 m_trafficPatterns[patternId].m_totalInterestRoundTripTime += roundTripTime;
jeraldabrahamdbfee2e2014-04-04 01:18:14 -0700584 if (m_nMaximumInterests >= 0 && globalReference == m_nMaximumInterests)
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800585 {
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700586 logStatistics();
587 m_logger.shutdownLogger();
588 m_face.shutdown();
Alexander Afanasyev976c3972014-05-26 17:03:40 +0300589 m_ioService.stop();
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700590 }
591 }
592
593 void
Eric Newberry976c2042016-06-19 23:37:35 -0700594 onNack(const ndn::Interest& interest,
595 const ndn::lp::Nack& nack,
596 int globalReference,
597 int localReference,
598 int patternId)
599 {
600 std::string logLine = "Interest Nack'd - PatternType=" + to_string(patternId + 1);
601 logLine += ", GlobalID=" + to_string(globalReference);
602 logLine += ", LocalID=" + to_string(localReference);
603 logLine += ", Name=" + interest.getName().toUri();
604 logLine += ", NackReason=" + to_string((int)nack.getReason());
605 m_logger.log(logLine, true, false);
606
607 m_nNacks++;
608 m_trafficPatterns[patternId].m_nNacks++;
609
610 if (m_nMaximumInterests >= 0 && globalReference == m_nMaximumInterests) {
611 logStatistics();
612 m_logger.shutdownLogger();
613 m_face.shutdown();
614 m_ioService.stop();
615 }
616 }
617
618 void
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700619 onTimeout(const ndn::Interest& interest,
620 int globalReference,
621 int localReference,
622 int patternId)
623 {
Spencer Lee8e990232015-11-27 03:54:39 -0700624 std::string logLine = "Interest Timed Out - PatternType=" + to_string(patternId + 1);
625 logLine += ", GlobalID=" + to_string(globalReference);
626 logLine += ", LocalID=" + to_string(localReference);
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700627 logLine += ", Name=" + interest.getName().toUri();
628 m_logger.log(logLine, true, false);
jeraldabrahamdbfee2e2014-04-04 01:18:14 -0700629 if (m_nMaximumInterests >= 0 && globalReference == m_nMaximumInterests)
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700630 {
631 logStatistics();
632 m_logger.shutdownLogger();
633 m_face.shutdown();
Alexander Afanasyev976c3972014-05-26 17:03:40 +0300634 m_ioService.stop();
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700635 }
636 }
637
638 void
Davide Pesaventod0b59982015-02-27 19:15:15 +0100639 generateTraffic(boost::asio::deadline_timer* timer)
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700640 {
641 if (m_nMaximumInterests < 0 || m_nInterestsSent < m_nMaximumInterests)
642 {
643 int trafficKey = std::rand() % 100;
644 int cumulativePercentage = 0;
Davide Pesaventod0b59982015-02-27 19:15:15 +0100645 std::size_t patternId;
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700646 for (patternId = 0; patternId < m_trafficPatterns.size(); patternId++)
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800647 {
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700648 cumulativePercentage += m_trafficPatterns[patternId].m_trafficPercentage;
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800649 if (trafficKey <= cumulativePercentage)
650 {
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700651 Name interestName(m_trafficPatterns[patternId].m_name);
652 if (m_trafficPatterns[patternId].m_nameAppendBytes > 0)
653 interestName.append(
Eric Newberry3b284192015-07-06 21:44:46 -0700654 generateRandomNameComponent(m_trafficPatterns[patternId].m_nameAppendBytes));
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700655 if (m_trafficPatterns[patternId].m_nameAppendSequenceNumber >= 0)
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800656 {
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700657 interestName.append(
Spencer Lee8e990232015-11-27 03:54:39 -0700658 to_string(m_trafficPatterns[patternId].m_nameAppendSequenceNumber));
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700659 m_trafficPatterns[patternId].m_nameAppendSequenceNumber++;
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800660 }
Davide Pesaventod0b59982015-02-27 19:15:15 +0100661
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800662 Interest interest(interestName);
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700663 if (m_trafficPatterns[patternId].m_minSuffixComponents >= 0)
664 interest.setMinSuffixComponents(
665 m_trafficPatterns[patternId].m_minSuffixComponents);
666 if (m_trafficPatterns[patternId].m_maxSuffixComponents >= 0)
667 interest.setMaxSuffixComponents(
668 m_trafficPatterns[patternId].m_maxSuffixComponents);
Davide Pesaventod0b59982015-02-27 19:15:15 +0100669
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800670 Exclude exclude;
Davide Pesaventod0b59982015-02-27 19:15:15 +0100671 if (!m_trafficPatterns[patternId].m_excludeBefore.empty() &&
672 !m_trafficPatterns[patternId].m_excludeAfter.empty())
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800673 {
Alexander Afanasyevfda32a32014-03-20 10:50:00 -0700674 exclude.excludeRange(
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700675 name::Component(
676 m_trafficPatterns[patternId].m_excludeAfter),
677 name::Component(m_trafficPatterns[patternId].m_excludeBefore));
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800678 interest.setExclude(exclude);
679 }
Davide Pesaventod0b59982015-02-27 19:15:15 +0100680 else if (!m_trafficPatterns[patternId].m_excludeBefore.empty())
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800681 {
Alexander Afanasyevfda32a32014-03-20 10:50:00 -0700682 exclude.excludeBefore(
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700683 name::Component(m_trafficPatterns[patternId].m_excludeBefore));
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800684 interest.setExclude(exclude);
685 }
Davide Pesaventod0b59982015-02-27 19:15:15 +0100686 else if (!m_trafficPatterns[patternId].m_excludeAfter.empty())
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800687 {
Alexander Afanasyevfda32a32014-03-20 10:50:00 -0700688 exclude.excludeAfter(
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700689 name::Component(m_trafficPatterns[patternId].m_excludeAfter));
690 interest.setExclude(exclude);
691 }
692 if (m_trafficPatterns[patternId].m_excludeBeforeBytes > 0 &&
693 m_trafficPatterns[patternId].m_excludeAfterBytes > 0)
694 {
695 exclude.excludeRange(
Eric Newberry3b284192015-07-06 21:44:46 -0700696 generateRandomNameComponent(
697 m_trafficPatterns[patternId].m_excludeAfterBytes),
698 generateRandomNameComponent(
699 m_trafficPatterns[patternId].m_excludeBeforeBytes));
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700700 interest.setExclude(exclude);
701 }
702 else if (m_trafficPatterns[patternId].m_excludeBeforeBytes > 0)
703 {
704 exclude.excludeBefore(
Eric Newberry3b284192015-07-06 21:44:46 -0700705 generateRandomNameComponent(
706 m_trafficPatterns[patternId].m_excludeBeforeBytes));
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700707 interest.setExclude(exclude);
708 }
709 else if (m_trafficPatterns[patternId].m_excludeAfterBytes > 0)
710 {
711 exclude.excludeAfter(
Eric Newberry3b284192015-07-06 21:44:46 -0700712 generateRandomNameComponent(
713 m_trafficPatterns[patternId].m_excludeAfterBytes));
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800714 interest.setExclude(exclude);
715 }
716
Eric Newberry25e9ba02016-08-02 22:02:22 -0700717 if (!m_trafficPatterns[patternId].m_excludeRange.empty()) {
718 auto& range = m_trafficPatterns[patternId].m_excludeRange;
719
720 if (range.find(",") != std::string::npos) {
721 std::string after = range.substr(0, range.find(","));
722 std::string before = range.substr(range.find(",") + 1, std::string::npos);
723 exclude.clear();
724 exclude.excludeRange(name::Component::fromEscapedString(after),
725 name::Component::fromEscapedString(before));
726 interest.setExclude(exclude);
727 }
728 }
729
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700730 if (m_trafficPatterns[patternId].m_childSelector >= 0)
731 interest.setChildSelector(m_trafficPatterns[patternId].m_childSelector);
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800732
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700733 if (m_trafficPatterns[patternId].m_mustBeFresh == 0)
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800734 interest.setMustBeFresh(false);
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700735 else if (m_trafficPatterns[patternId].m_mustBeFresh > 0)
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800736 interest.setMustBeFresh(true);
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700737 if (m_trafficPatterns[patternId].m_nonceDuplicationPercentage > 0)
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800738 {
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700739 int duplicationPercentage = std::rand() % 100;
740 if (m_trafficPatterns[patternId].m_nonceDuplicationPercentage <=
741 duplicationPercentage)
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800742 interest.setNonce(getOldNonce());
743 else
744 interest.setNonce(getNewNonce());
745 }
746 else
747 interest.setNonce(getNewNonce());
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700748 if (m_trafficPatterns[patternId].m_interestLifetime >= time::milliseconds(0))
749 interest.setInterestLifetime(m_trafficPatterns[patternId].m_interestLifetime);
750
Eric Newberryeab462b2016-03-18 11:56:31 -0700751 if (m_trafficPatterns[patternId].m_nextHopFaceId > 0) {
752 interest.setTag(make_shared<lp::NextHopFaceIdTag>(
753 m_trafficPatterns[patternId].m_nextHopFaceId));
754 }
755
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800756 try {
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700757 m_nInterestsSent++;
758 m_trafficPatterns[patternId].m_nInterestsSent++;
759 time::steady_clock::TimePoint sentTime = time::steady_clock::now();
760 m_face.expressInterest(interest,
761 bind(&NdnTrafficClient::onData,
762 this, _1, _2, m_nInterestsSent,
763 m_trafficPatterns[patternId].m_nInterestsSent,
764 patternId, sentTime),
Eric Newberry976c2042016-06-19 23:37:35 -0700765 bind(&NdnTrafficClient::onNack,
766 this, _1, _2, m_nInterestsSent,
767 m_trafficPatterns[patternId].m_nInterestsSent,
768 patternId),
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700769 bind(&NdnTrafficClient::onTimeout,
770 this, _1, m_nInterestsSent,
771 m_trafficPatterns[patternId].m_nInterestsSent,
Alexander Afanasyevfda32a32014-03-20 10:50:00 -0700772 patternId));
Davide Pesaventod0b59982015-02-27 19:15:15 +0100773
774 if (!m_hasQuietLogging) {
775 std::string logLine =
Spencer Lee8e990232015-11-27 03:54:39 -0700776 "Sending Interest - PatternType=" + to_string(patternId + 1) +
777 ", GlobalID=" + to_string(m_nInterestsSent) +
Davide Pesaventod0b59982015-02-27 19:15:15 +0100778 ", LocalID=" +
Spencer Lee8e990232015-11-27 03:54:39 -0700779 to_string(m_trafficPatterns[patternId].m_nInterestsSent) +
Davide Pesaventod0b59982015-02-27 19:15:15 +0100780 ", Name=" + interest.getName().toUri();
jeraldabraham420dbf02014-04-25 22:58:31 -0700781 m_logger.log(logLine, true, false);
Davide Pesaventod0b59982015-02-27 19:15:15 +0100782 }
783
784 timer->expires_at(timer->expires_at() +
785 boost::posix_time::millisec(m_interestInterval.count()));
786 timer->async_wait(bind(&NdnTrafficClient::generateTraffic, this, timer));
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800787 }
Davide Pesaventod0b59982015-02-27 19:15:15 +0100788 catch (const std::exception& e) {
789 m_logger.log("ERROR: " + std::string(e.what()), true, true);
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800790 }
791 break;
792 }
793 }
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700794 if (patternId == m_trafficPatterns.size())
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800795 {
Davide Pesaventod0b59982015-02-27 19:15:15 +0100796 timer->expires_at(timer->expires_at() +
797 boost::posix_time::millisec(m_interestInterval.count()));
798 timer->async_wait(bind(&NdnTrafficClient::generateTraffic, this, timer));
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800799 }
800 }
801 }
802
803 void
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700804 run()
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800805 {
Alexander Afanasyev976c3972014-05-26 17:03:40 +0300806 boost::asio::signal_set signalSet(m_ioService, SIGINT, SIGTERM);
Alexander Afanasyevfda32a32014-03-20 10:50:00 -0700807 signalSet.async_wait(bind(&NdnTrafficClient::signalHandler, this));
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700808 m_logger.initializeLog(m_instanceId);
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800809 initializeTrafficConfiguration();
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700810
811 if (m_nMaximumInterests == 0)
812 {
813 logStatistics();
814 m_logger.shutdownLogger();
815 return;
816 }
817
Davide Pesaventod0b59982015-02-27 19:15:15 +0100818 boost::asio::deadline_timer deadlineTimer(m_ioService,
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700819 boost::posix_time::millisec(m_interestInterval.count()));
Davide Pesaventod0b59982015-02-27 19:15:15 +0100820 deadlineTimer.async_wait(bind(&NdnTrafficClient::generateTraffic, this, &deadlineTimer));
821
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800822 try {
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700823 m_face.processEvents();
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800824 }
Davide Pesaventod0b59982015-02-27 19:15:15 +0100825 catch (const std::exception& e) {
826 m_logger.log("ERROR: " + std::string(e.what()), true, true);
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800827 m_logger.shutdownLogger();
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700828 m_hasError = true;
Alexander Afanasyev976c3972014-05-26 17:03:40 +0300829 m_ioService.stop();
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800830 }
831 }
832
833private:
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700834 std::string m_programName;
Alexander Afanasyev740812e2014-10-30 15:37:45 -0700835 Logger m_logger;
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700836 std::string m_instanceId;
837 bool m_hasError;
jeraldabraham420dbf02014-04-25 22:58:31 -0700838 bool m_hasQuietLogging;
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700839 time::milliseconds m_interestInterval;
840 int m_nMaximumInterests;
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700841 std::string m_configurationFile;
Alexander Afanasyev976c3972014-05-26 17:03:40 +0300842 boost::asio::io_service m_ioService;
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700843 Face m_face;
844 std::vector<InterestTrafficConfiguration> m_trafficPatterns;
845 std::vector<uint32_t> m_nonces;
846 int m_nInterestsSent;
847 int m_nInterestsReceived;
Eric Newberry976c2042016-06-19 23:37:35 -0700848 int m_nNacks;
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700849 int m_nContentInconsistencies;
850
851 //round trip time is stored as milliseconds with fractional
852 //sub-milliseconds precision
853 double m_minimumInterestRoundTripTime;
854 double m_maximumInterestRoundTripTime;
855 double m_totalInterestRoundTripTime;
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800856};
857
858} // namespace ndn
859
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700860int
861main(int argc, char* argv[])
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800862{
Davide Pesaventod0b59982015-02-27 19:15:15 +0100863 std::srand(std::time(nullptr));
864
865 ndn::NdnTrafficClient client(argv[0]);
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700866 int option;
jeraldabraham420dbf02014-04-25 22:58:31 -0700867 while ((option = getopt(argc, argv, "hqi:c:")) != -1) {
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800868 switch (option) {
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700869 case 'h':
Davide Pesaventod0b59982015-02-27 19:15:15 +0100870 client.usage();
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800871 break;
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700872 case 'i':
Davide Pesaventod0b59982015-02-27 19:15:15 +0100873 client.setInterestInterval(atoi(optarg));
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800874 break;
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700875 case 'c':
Davide Pesaventod0b59982015-02-27 19:15:15 +0100876 client.setMaximumInterests(atoi(optarg));
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800877 break;
jeraldabraham420dbf02014-04-25 22:58:31 -0700878 case 'q':
Davide Pesaventod0b59982015-02-27 19:15:15 +0100879 client.setQuietLogging();
jeraldabraham420dbf02014-04-25 22:58:31 -0700880 break;
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700881 default:
Davide Pesaventod0b59982015-02-27 19:15:15 +0100882 client.usage();
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800883 break;
884 }
885 }
886
887 argc -= optind;
888 argv += optind;
889
Davide Pesaventod0b59982015-02-27 19:15:15 +0100890 if (!argc)
891 client.usage();
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800892
Davide Pesaventod0b59982015-02-27 19:15:15 +0100893 client.setConfigurationFile(argv[0]);
894 client.run();
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800895
Davide Pesaventod0b59982015-02-27 19:15:15 +0100896 return client.hasError() ? EXIT_FAILURE : EXIT_SUCCESS;
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800897}