blob: 3cc2aac05a8132f3a00fe736693b22055d59017a [file] [log] [blame]
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2/**
3 * Copyright (C) 2014 University of Arizona.
4 *
5 * GNU 3.0 License, see the LICENSE file for more information
6 *
7 * Author: Jerald Paul Abraham <jeraldabraham@email.arizona.edu>
8 */
9
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -080010#include <fstream>
jeraldabraham420dbf02014-04-25 22:58:31 -070011#include <sstream>
12#include <string>
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -080013#include <vector>
14
jeraldabraham420dbf02014-04-25 22:58:31 -070015#include <boost/asio.hpp>
16#include <boost/date_time/posix_time/posix_time.hpp>
17#include <boost/filesystem.hpp>
18#include <boost/lexical_cast.hpp>
19#include <boost/noncopyable.hpp>
20
21#include <ndn-cxx/exclude.hpp>
22#include <ndn-cxx/face.hpp>
23#include <ndn-cxx/name-component.hpp>
24
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -080025#include "logger.hpp"
26
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -080027namespace ndn {
28
jeraldabraham420dbf02014-04-25 22:58:31 -070029class NdnTrafficClient : boost::noncopyable
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -080030{
31public:
jeraldabrahamcc3c6c92014-03-28 02:21:45 -070032
33 explicit
Alexander Afanasyevfda32a32014-03-20 10:50:00 -070034 NdnTrafficClient(char* programName)
jeraldabrahamcc3c6c92014-03-28 02:21:45 -070035 : m_programName(programName)
36 , m_logger("NdnTrafficClient")
37 , m_hasError(false)
jeraldabraham420dbf02014-04-25 22:58:31 -070038 , m_hasQuietLogging(false)
jeraldabrahamcc3c6c92014-03-28 02:21:45 -070039 , m_ioService(new boost::asio::io_service)
40 , m_face(m_ioService)
41 , m_interestInterval(getDefaultInterestInterval())
42 , m_nMaximumInterests(-1)
43 , m_nInterestsSent(0)
44 , m_nInterestsReceived(0)
jeraldabrahamdbfee2e2014-04-04 01:18:14 -070045 , m_nContentInconsistencies(0)
jeraldabrahamcc3c6c92014-03-28 02:21:45 -070046 , m_minimumInterestRoundTripTime(std::numeric_limits<double>::max())
47 , m_maximumInterestRoundTripTime(0)
48 , m_totalInterestRoundTripTime(0)
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -080049 {
jeraldabrahamcc3c6c92014-03-28 02:21:45 -070050 m_instanceId = boost::lexical_cast<std::string>(std::rand());
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -080051 }
52
53 class InterestTrafficConfiguration
54 {
55 public:
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -080056 InterestTrafficConfiguration()
jeraldabrahamcc3c6c92014-03-28 02:21:45 -070057 : m_trafficPercentage(-1)
58 , m_nameAppendBytes(-1)
59 , m_nameAppendSequenceNumber(-1)
60 , m_minSuffixComponents(-1)
61 , m_maxSuffixComponents(-1)
62 , m_excludeBeforeBytes(-1)
63 , m_excludeAfterBytes(-1)
64 , m_childSelector(-1)
65 , m_mustBeFresh(-1)
66 , m_nonceDuplicationPercentage(-1)
67 , m_scope(-1)
68 , m_interestLifetime(getDefaultInterestLifetime())
jeraldabrahamdbfee2e2014-04-04 01:18:14 -070069 , m_nInterestsSent(0)
70 , m_nInterestsReceived(0)
jeraldabrahamcc3c6c92014-03-28 02:21:45 -070071 , m_minimumInterestRoundTripTime(std::numeric_limits<double>::max())
72 , m_maximumInterestRoundTripTime(0)
73 , m_totalInterestRoundTripTime(0)
74 , m_nContentInconsistencies(0)
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -080075 {
jeraldabrahamcc3c6c92014-03-28 02:21:45 -070076 }
77
78 time::milliseconds
79 getDefaultInterestLifetime()
80 {
81 return time::milliseconds(-1);
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -080082 }
83
84 void
Alexander Afanasyevfda32a32014-03-20 10:50:00 -070085 printTrafficConfiguration(Logger& logger)
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -080086 {
jeraldabrahamcc3c6c92014-03-28 02:21:45 -070087 std::string detail = "";
88 if (m_trafficPercentage > 0)
89 detail += "TrafficPercentage=" +
90 boost::lexical_cast<std::string>(m_trafficPercentage) + ", ";
91 if (m_name != "")
92 detail += "Name=" + m_name + ", ";
93 if (m_nameAppendBytes > 0)
94 detail += "NameAppendBytes=" + boost::lexical_cast<std::string>(m_nameAppendBytes) + ", ";
95 if (m_nameAppendSequenceNumber > 0)
96 detail += "NameAppendSequenceNumber=" +
97 boost::lexical_cast<std::string>(m_nameAppendSequenceNumber) + ", ";
98 if (m_minSuffixComponents >= 0)
99 detail += "MinSuffixComponents=" +
100 boost::lexical_cast<std::string>(m_minSuffixComponents) + ", ";
101 if (m_maxSuffixComponents >= 0)
102 detail += "MaxSuffixComponents=" +
103 boost::lexical_cast<std::string>(m_maxSuffixComponents) + ", ";
104 if (m_excludeBefore != "")
105 detail += "ExcludeBefore=" + m_excludeBefore + ", ";
106 if (m_excludeAfter != "")
107 detail += "ExcludeAfter=" + m_excludeAfter + ", ";
108 if (m_excludeBeforeBytes > 0)
109 detail += "ExcludeBeforeBytes=" +
110 boost::lexical_cast<std::string>(m_excludeBeforeBytes) + ", ";
111 if (m_excludeAfterBytes > 0)
112 detail += "ExcludeAfterBytes=" +
113 boost::lexical_cast<std::string>(m_excludeAfterBytes) + ", ";
114 if (m_childSelector >= 0)
115 detail += "ChildSelector=" +
116 boost::lexical_cast<std::string>(m_childSelector) + ", ";
117 if (m_mustBeFresh >= 0)
118 detail += "MustBeFresh=" +
119 boost::lexical_cast<std::string>(m_mustBeFresh) + ", ";
120 if (m_nonceDuplicationPercentage > 0)
121 detail += "NonceDuplicationPercentage=" +
122 boost::lexical_cast<std::string>(m_nonceDuplicationPercentage) + ", ";
123 if (m_scope >= 0)
124 detail += "Scope="+boost::lexical_cast<std::string>(m_scope) + ", ";
125 if (m_interestLifetime >= time::milliseconds(0))
126 detail += "InterestLifetime=" +
127 boost::lexical_cast<std::string>(m_interestLifetime.count()) + ", ";
128 if (m_expectedContent != "")
129 detail += "ExpectedContent=" + m_expectedContent + ", ";
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800130 if (detail.length() >= 2)
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700131 detail = detail.substr(0, detail.length() - 2); //Removing suffix ", "
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800132 logger.log(detail, false, false);
133 }
134
135 bool
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700136 extractParameterValue(const std::string& detail,
137 std::string& parameter,
138 std::string& value)
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800139 {
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800140 std::string allowedCharacters = ":/+._-%";
141 parameter = "";
142 value = "";
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700143 int i = 0;
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800144 while (detail[i] != '=' && i < detail.length())
145 {
146 parameter += detail[i];
147 i++;
148 }
149 if (i == detail.length())
150 return false;
151 i++;
jeraldabraham79c0c232014-03-31 21:43:59 -0700152 while ((std::isalnum(detail[i]) ||
153 allowedCharacters.find(detail[i]) != std::string::npos) &&
154 i < detail.length())
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800155 {
156 value += detail[i];
157 i++;
158 }
jeraldabraham79c0c232014-03-31 21:43:59 -0700159 if (parameter == "" || value == "")
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800160 return false;
161 return true;
162 }
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")
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700171 m_trafficPercentage = boost::lexical_cast<int>(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")
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700175 m_nameAppendBytes = boost::lexical_cast<int>(value);
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800176 else if (parameter == "NameAppendSequenceNumber")
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700177 m_nameAppendSequenceNumber = boost::lexical_cast<int>(value);
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800178 else if (parameter == "MinSuffixComponents")
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700179 m_minSuffixComponents = boost::lexical_cast<int>(value);
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800180 else if (parameter == "MaxSuffixComponents")
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700181 m_maxSuffixComponents = boost::lexical_cast<int>(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")
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700187 m_excludeBeforeBytes = boost::lexical_cast<int>(value);
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800188 else if (parameter == "ExcludeAfterBytes")
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700189 m_excludeAfterBytes = boost::lexical_cast<int>(value);
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800190 else if (parameter == "ChildSelector")
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700191 m_childSelector = boost::lexical_cast<int>(value);
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800192 else if (parameter == "MustBeFresh")
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700193 m_mustBeFresh = boost::lexical_cast<int>(value);
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800194 else if (parameter == "NonceDuplicationPercentage")
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700195 m_nonceDuplicationPercentage = boost::lexical_cast<int>(value);
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800196 else if (parameter == "Scope")
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700197 m_scope = boost::lexical_cast<int>(value);
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800198 else if (parameter == "InterestLifetime")
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700199 m_interestLifetime = time::milliseconds(boost::lexical_cast<int>(value));
jeraldabraham473ef3d2014-03-06 12:40:35 -0700200 else if (parameter == "ExpectedContent")
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700201 m_expectedContent = value;
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800202 else
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700203 logger.log("Line " + boost::lexical_cast<std::string>(lineNumber) +
204 " \t- Invalid Parameter='" + parameter + "'", false, true);
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800205 }
206 else
207 {
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700208 logger.log("Line " + boost::lexical_cast<std::string>(lineNumber) +
209 " \t- Improper Traffic Configuration Line- " + detail, false, true);
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800210 return false;
211 }
212 return true;
213 }
214
215 bool
216 checkTrafficDetailCorrectness()
217 {
218 return true;
219 }
220
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700221 int m_trafficPercentage;
222 std::string m_name;
223 int m_nameAppendBytes;
224 int m_nameAppendSequenceNumber;
225 int m_minSuffixComponents;
226 int m_maxSuffixComponents;
227 std::string m_excludeBefore;
228 std::string m_excludeAfter;
229 int m_excludeBeforeBytes;
230 int m_excludeAfterBytes;
231 int m_childSelector;
232 int m_mustBeFresh;
233 int m_nonceDuplicationPercentage;
234 int m_scope;
235 time::milliseconds m_interestLifetime;
236 int m_nInterestsSent;
237 int m_nInterestsReceived;
238
239 //round trip time is stored as milliseconds with fractional
240 //sub-millisecond precision
241 double m_minimumInterestRoundTripTime;
242 double m_maximumInterestRoundTripTime;
243 double m_totalInterestRoundTripTime;
244
245 int m_nContentInconsistencies;
246 std::string m_expectedContent;
Alexander 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
256 usage()
257 {
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700258 std::cout << "\nUsage: " << m_programName << " [options] <Traffic_Configuration_File>\n"
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800259 "Generate Interest Traffic as per provided Traffic Configuration File\n"
260 "Interests are continuously generated unless a total number is specified.\n"
261 "Set environment variable NDN_TRAFFIC_LOGFOLDER for redirecting output to a log.\n"
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700262 " [-i interval] - set interest generation interval in milliseconds (default "
263 << getDefaultInterestInterval() << ")\n"
264 " [-c count] - set total number of interests to be generated\n"
jeraldabraham420dbf02014-04-25 22:58:31 -0700265 " [-q] - quiet logging - no interest generation/data reception messages\n"
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700266 " [-h] - print help and exit\n\n";
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800267 exit(1);
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800268 }
269
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700270 time::milliseconds
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800271 getDefaultInterestInterval()
272 {
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700273 return time::milliseconds(1000);
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800274 }
275
276 void
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700277 setInterestInterval(int interestInterval)
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800278 {
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700279 if (interestInterval <= 0)
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800280 usage();
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700281 m_interestInterval = time::milliseconds(interestInterval);
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800282 }
283
284 void
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700285 setMaximumInterests(int maximumInterests)
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800286 {
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700287 if (maximumInterests <= 0)
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800288 usage();
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700289 m_nMaximumInterests = maximumInterests;
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800290 }
291
292 void
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700293 setConfigurationFile(char* configurationFile)
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800294 {
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700295 m_configurationFile = configurationFile;
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800296 }
297
298 void
jeraldabraham420dbf02014-04-25 22:58:31 -0700299 setQuietLogging()
300 {
301 m_hasQuietLogging = true;
302 }
303
304 void
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800305 signalHandler()
306 {
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800307 logStatistics();
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700308 m_logger.shutdownLogger();
309 m_face.shutdown();
310 m_ioService->stop();
311 if (m_hasError)
312 exit(1);
313 else
314 exit(0);
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800315 }
316
317 void
318 logStatistics()
319 {
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800320 m_logger.log("\n\n== Interest Traffic Report ==\n", false, true);
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700321 m_logger.log("Total Traffic Pattern Types = " +
322 boost::lexical_cast<std::string>(static_cast<int>(m_trafficPatterns.size())), false, true);
323 m_logger.log("Total Interests Sent = " +
324 boost::lexical_cast<std::string>(m_nInterestsSent), false, true);
325 m_logger.log("Total Responses Received = " +
326 boost::lexical_cast<std::string>(m_nInterestsReceived), false, true);
327 double loss = 0;
328 if (m_nInterestsSent > 0)
329 loss = (m_nInterestsSent - m_nInterestsReceived) * 100.0 / m_nInterestsSent;
330 m_logger.log("Total Interest Loss = " +
331 boost::lexical_cast<std::string>(loss) + "%", false, true);
332 if (m_nContentInconsistencies != 0 || m_nInterestsSent != m_nInterestsReceived)
333 m_hasError = true;
334 double average = 0;
335 double inconsistency = 0;
336 if (m_nInterestsReceived > 0)
Alexander Afanasyevfda32a32014-03-20 10:50:00 -0700337 {
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700338 average = m_totalInterestRoundTripTime / m_nInterestsReceived;
339 inconsistency = m_nContentInconsistencies * 100.0 / m_nInterestsReceived;
Alexander Afanasyevfda32a32014-03-20 10:50:00 -0700340 }
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700341 m_logger.log("Total Data Inconsistency = " +
342 boost::lexical_cast<std::string>(inconsistency) + "%", false, true);
343 m_logger.log("Total Round Trip Time = " +
344 boost::lexical_cast<std::string>(m_totalInterestRoundTripTime) + "ms", false, true);
345 m_logger.log("Average Round Trip Time = " +
346 boost::lexical_cast<std::string>(average) + "ms\n", false, true);
347
348 for (int patternId = 0; patternId < m_trafficPatterns.size(); patternId++)
Alexander Afanasyevfda32a32014-03-20 10:50:00 -0700349 {
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700350 m_logger.log("Traffic Pattern Type #" +
351 boost::lexical_cast<std::string>(patternId + 1), false, true);
352 m_trafficPatterns[patternId].printTrafficConfiguration(m_logger);
353 m_logger.log("Total Interests Sent = " +
354 boost::lexical_cast<std::string>(
355 m_trafficPatterns[patternId].m_nInterestsSent), false, true);
356 m_logger.log("Total Responses Received = " +
357 boost::lexical_cast<std::string>(
358 m_trafficPatterns[patternId].m_nInterestsReceived), false, true);
359 loss = 0;
360 if (m_trafficPatterns[patternId].m_nInterestsSent > 0)
361 {
362 loss = (m_trafficPatterns[patternId].m_nInterestsSent -
363 m_trafficPatterns[patternId].m_nInterestsReceived);
364 loss *= 100.0;
365 loss /= m_trafficPatterns[patternId].m_nInterestsSent;
366 }
367 m_logger.log("Total Interest Loss = " +
368 boost::lexical_cast<std::string>(loss) + "%", false, true);
Alexander Afanasyevfda32a32014-03-20 10:50:00 -0700369 average = 0;
370 inconsistency = 0;
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700371 if (m_trafficPatterns[patternId].m_nInterestsReceived > 0)
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800372 {
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700373 average = (m_trafficPatterns[patternId].m_totalInterestRoundTripTime /
374 m_trafficPatterns[patternId].m_nInterestsReceived);
375 inconsistency = m_trafficPatterns[patternId].m_nContentInconsistencies;
376 inconsistency =
377 inconsistency * 100.0 / m_trafficPatterns[patternId].m_nInterestsReceived;
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800378 }
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700379 m_logger.log("Total Data Inconsistency = " +
380 boost::lexical_cast<std::string>(inconsistency) + "%", false, true);
Alexander Afanasyevfda32a32014-03-20 10:50:00 -0700381 m_logger.log("Total Round Trip Time = " +
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700382 boost::lexical_cast<std::string>(
383 m_trafficPatterns[patternId].m_totalInterestRoundTripTime) + "ms", false, true);
384 m_logger.log("Average Round Trip Time = " +
385 boost::lexical_cast<std::string>(average) + "ms\n", false, true);
Alexander 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);
401 patternFile.open(m_configurationFile.c_str());
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800402 if (patternFile.is_open())
403 {
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700404 int patternId = 0;
405 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 patternId++;
414 if (interestData.processConfigurationDetail(patternLine, m_logger, lineNumber))
415 {
416 while (getline(patternFile, patternLine) && std::isalpha(patternLine[0]))
417 {
418 lineNumber++;
Alexander Afanasyevfda32a32014-03-20 10:50:00 -0700419 if (!interestData.processConfigurationDetail(patternLine,
420 m_logger, lineNumber))
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800421 {
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700422 shouldSkipLine = true;
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800423 break;
424 }
425 }
426 lineNumber++;
427 }
428 else
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700429 shouldSkipLine = true;
430 if (!shouldSkipLine)
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800431 {
432 if (interestData.checkTrafficDetailCorrectness())
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700433 m_trafficPatterns.push_back(interestData);
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800434 }
435 }
436 }
437 patternFile.close();
438 if (!checkTrafficPatternCorrectness())
439 {
Alexander Afanasyevfda32a32014-03-20 10:50:00 -0700440 m_logger.log("ERROR - Traffic Configuration Provided Is Not Proper- " +
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700441 m_configurationFile, false, true);
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800442 m_logger.shutdownLogger();
443 exit(1);
444 }
445 m_logger.log("Traffic Configuration File Processing Completed\n", true, false);
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700446 for (patternId = 0; patternId < m_trafficPatterns.size(); patternId++)
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800447 {
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700448 m_logger.log("Traffic Pattern Type #" +
449 boost::lexical_cast<std::string>(patternId + 1), false, false);
450 m_trafficPatterns[patternId].printTrafficConfiguration(m_logger);
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800451 m_logger.log("", false, false);
452 }
453 }
454 else
455 {
Alexander Afanasyevfda32a32014-03-20 10:50:00 -0700456 m_logger.log("ERROR - Unable To Open Traffic Configuration File: " +
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700457 m_configurationFile, false, true);
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800458 m_logger.shutdownLogger();
459 exit(1);
460 }
461 }
462
463 void
464 initializeTrafficConfiguration()
465 {
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700466 if (boost::filesystem::exists(boost::filesystem::path(m_configurationFile)))
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800467 {
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700468 if (boost::filesystem::is_regular_file(boost::filesystem::path(m_configurationFile)))
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800469 {
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700470 parseConfigurationFile();
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800471 }
472 else
473 {
Alexander Afanasyevfda32a32014-03-20 10:50:00 -0700474 m_logger.log("ERROR - Traffic Configuration File Is Not A Regular File: " +
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700475 m_configurationFile, false, true);
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800476 m_logger.shutdownLogger();
477 exit(1);
478 }
479 }
480 else
481 {
Alexander Afanasyevfda32a32014-03-20 10:50:00 -0700482 m_logger.log("ERROR - Traffic Configuration File Does Not Exist: " +
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700483 m_configurationFile, false, true);
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800484 m_logger.shutdownLogger();
485 exit(1);
486 }
487 }
488
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700489 uint32_t
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800490 getOldNonce()
491 {
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700492 if (m_nonces.size() == 0)
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800493 return getNewNonce();
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700494 int randomNonceIndex = std::rand() % m_nonces.size();
495 return m_nonces[randomNonceIndex];
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800496 }
497
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700498 uint32_t
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800499 getNewNonce()
500 {
jeraldabraham473ef3d2014-03-06 12:40:35 -0700501 //Performance Enhancement
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700502 if (m_nonces.size() > 1000)
503 m_nonces.clear();
jeraldabraham473ef3d2014-03-06 12:40:35 -0700504
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700505 uint32_t randomNonce = static_cast<uint32_t>(std::rand());
506 while (std::find(m_nonces.begin(), m_nonces.end(), randomNonce) != m_nonces.end())
507 randomNonce = static_cast<uint32_t>(std::rand());
508
509 m_nonces.push_back(randomNonce);
510 return randomNonce;
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800511 }
512
513 static std::string
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700514 getRandomByteString(int randomSize)
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800515 {
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700516 std::string randomString;
517 for (int i = 0; i < randomSize; i++)
518 randomString += static_cast<char>(std::rand() % 128);
519 return randomString;
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 {
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700530 std::string logLine =
531 "Data Received - PatternType=" + boost::lexical_cast<std::string>(patternId+1);
532 logLine += ", GlobalID=" + boost::lexical_cast<std::string>(globalReference);
533 logLine += ", LocalID=" + boost::lexical_cast<std::string>(localReference);
534 logLine += ", Name=" + interest.getName().toUri();
535
536 m_nInterestsReceived++;
537 m_trafficPatterns[patternId].m_nInterestsReceived++;
538 if (m_trafficPatterns[patternId].m_expectedContent != "")
jeraldabraham473ef3d2014-03-06 12:40:35 -0700539 {
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700540 std::string receivedContent = reinterpret_cast<const char*>(data.getContent().value());
541 int receivedContentLength = data.getContent().value_size();
Alexander Afanasyevfda32a32014-03-20 10:50:00 -0700542 receivedContent = receivedContent.substr(0, receivedContentLength);
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700543 if (receivedContent != m_trafficPatterns[patternId].m_expectedContent)
Alexander Afanasyevfda32a32014-03-20 10:50:00 -0700544 {
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700545 m_nContentInconsistencies++;
546 m_trafficPatterns[patternId].m_nContentInconsistencies++;
Alexander Afanasyevfda32a32014-03-20 10:50:00 -0700547 logLine += ", IsConsistent=No";
548 }
549 else
550 logLine += ", IsConsistent=Yes";
jeraldabraham473ef3d2014-03-06 12:40:35 -0700551 }
jeraldabraham473ef3d2014-03-06 12:40:35 -0700552 else
553 logLine += ", IsConsistent=NotChecked";
jeraldabraham420dbf02014-04-25 22:58:31 -0700554 if (!m_hasQuietLogging)
555 m_logger.log(logLine, true, false);
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700556 double roundTripTime = (time::steady_clock::now() - sentTime).count() / 1000000.0;
557 if (m_minimumInterestRoundTripTime > roundTripTime)
558 m_minimumInterestRoundTripTime = roundTripTime;
559 if (m_maximumInterestRoundTripTime < roundTripTime)
560 m_maximumInterestRoundTripTime = roundTripTime;
561 if (m_trafficPatterns[patternId].m_minimumInterestRoundTripTime > roundTripTime)
562 m_trafficPatterns[patternId].m_minimumInterestRoundTripTime = roundTripTime;
563 if (m_trafficPatterns[patternId].m_maximumInterestRoundTripTime < roundTripTime)
564 m_trafficPatterns[patternId].m_maximumInterestRoundTripTime = roundTripTime;
565 m_totalInterestRoundTripTime += roundTripTime;
566 m_trafficPatterns[patternId].m_totalInterestRoundTripTime += roundTripTime;
jeraldabrahamdbfee2e2014-04-04 01:18:14 -0700567 if (m_nMaximumInterests >= 0 && globalReference == m_nMaximumInterests)
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800568 {
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700569 logStatistics();
570 m_logger.shutdownLogger();
571 m_face.shutdown();
572 m_ioService->stop();
573 }
574 }
575
576 void
577 onTimeout(const ndn::Interest& interest,
578 int globalReference,
579 int localReference,
580 int patternId)
581 {
582 std::string logLine = "Interest Timed Out - PatternType=" +
583 boost::lexical_cast<std::string>(patternId + 1);
584 logLine += ", GlobalID=" + boost::lexical_cast<std::string>(globalReference);
585 logLine += ", LocalID=" + boost::lexical_cast<std::string>(localReference);
586 logLine += ", Name=" + interest.getName().toUri();
587 m_logger.log(logLine, true, false);
jeraldabrahamdbfee2e2014-04-04 01:18:14 -0700588 if (m_nMaximumInterests >= 0 && globalReference == m_nMaximumInterests)
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700589 {
590 logStatistics();
591 m_logger.shutdownLogger();
592 m_face.shutdown();
593 m_ioService->stop();
594 }
595 }
596
597 void
598 generateTraffic(boost::asio::deadline_timer* deadlineTimer)
599 {
600 if (m_nMaximumInterests < 0 || m_nInterestsSent < m_nMaximumInterests)
601 {
602 int trafficKey = std::rand() % 100;
603 int cumulativePercentage = 0;
604 int patternId;
605 for (patternId = 0; patternId < m_trafficPatterns.size(); patternId++)
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800606 {
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700607 cumulativePercentage += m_trafficPatterns[patternId].m_trafficPercentage;
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800608 if (trafficKey <= cumulativePercentage)
609 {
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700610 Name interestName(m_trafficPatterns[patternId].m_name);
611 if (m_trafficPatterns[patternId].m_nameAppendBytes > 0)
612 interestName.append(
613 getRandomByteString(m_trafficPatterns[patternId].m_nameAppendBytes));
614 if (m_trafficPatterns[patternId].m_nameAppendSequenceNumber >= 0)
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800615 {
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700616 interestName.append(
617 boost::lexical_cast<std::string>(
618 m_trafficPatterns[patternId].m_nameAppendSequenceNumber));
619 m_trafficPatterns[patternId].m_nameAppendSequenceNumber++;
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800620 }
621 Interest interest(interestName);
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700622 if (m_trafficPatterns[patternId].m_minSuffixComponents >= 0)
623 interest.setMinSuffixComponents(
624 m_trafficPatterns[patternId].m_minSuffixComponents);
625 if (m_trafficPatterns[patternId].m_maxSuffixComponents >= 0)
626 interest.setMaxSuffixComponents(
627 m_trafficPatterns[patternId].m_maxSuffixComponents);
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800628 Exclude exclude;
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700629 if (m_trafficPatterns[patternId].m_excludeBefore != "" &&
630 m_trafficPatterns[patternId].m_excludeAfter != "")
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800631 {
Alexander Afanasyevfda32a32014-03-20 10:50:00 -0700632 exclude.excludeRange(
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700633 name::Component(
634 m_trafficPatterns[patternId].m_excludeAfter),
635 name::Component(m_trafficPatterns[patternId].m_excludeBefore));
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800636 interest.setExclude(exclude);
637 }
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700638 else if (m_trafficPatterns[patternId].m_excludeBefore != "")
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800639 {
Alexander Afanasyevfda32a32014-03-20 10:50:00 -0700640 exclude.excludeBefore(
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700641 name::Component(m_trafficPatterns[patternId].m_excludeBefore));
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800642 interest.setExclude(exclude);
643 }
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700644 else if (m_trafficPatterns[patternId].m_excludeAfter != "")
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800645 {
Alexander Afanasyevfda32a32014-03-20 10:50:00 -0700646 exclude.excludeAfter(
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700647 name::Component(m_trafficPatterns[patternId].m_excludeAfter));
648 interest.setExclude(exclude);
649 }
650 if (m_trafficPatterns[patternId].m_excludeBeforeBytes > 0 &&
651 m_trafficPatterns[patternId].m_excludeAfterBytes > 0)
652 {
653 exclude.excludeRange(
654 name::Component(
655 getRandomByteString(
656 m_trafficPatterns[patternId].m_excludeAfterBytes)),
657 name::Component(
658 getRandomByteString(
659 m_trafficPatterns[patternId].m_excludeBeforeBytes)));
660 interest.setExclude(exclude);
661 }
662 else if (m_trafficPatterns[patternId].m_excludeBeforeBytes > 0)
663 {
664 exclude.excludeBefore(
665 name::Component(
666 getRandomByteString(
667 m_trafficPatterns[patternId].m_excludeBeforeBytes)));
668 interest.setExclude(exclude);
669 }
670 else if (m_trafficPatterns[patternId].m_excludeAfterBytes > 0)
671 {
672 exclude.excludeAfter(
673 name::Component(
674 getRandomByteString(
675 m_trafficPatterns[patternId].m_excludeAfterBytes)));
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800676 interest.setExclude(exclude);
677 }
678
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700679 if (m_trafficPatterns[patternId].m_childSelector >= 0)
680 interest.setChildSelector(m_trafficPatterns[patternId].m_childSelector);
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800681
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700682 if (m_trafficPatterns[patternId].m_mustBeFresh == 0)
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800683 interest.setMustBeFresh(false);
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700684 else if (m_trafficPatterns[patternId].m_mustBeFresh > 0)
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800685 interest.setMustBeFresh(true);
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700686 if (m_trafficPatterns[patternId].m_nonceDuplicationPercentage > 0)
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800687 {
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700688 int duplicationPercentage = std::rand() % 100;
689 if (m_trafficPatterns[patternId].m_nonceDuplicationPercentage <=
690 duplicationPercentage)
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800691 interest.setNonce(getOldNonce());
692 else
693 interest.setNonce(getNewNonce());
694 }
695 else
696 interest.setNonce(getNewNonce());
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700697 if (m_trafficPatterns[patternId].m_scope >= 0)
698 interest.setScope(m_trafficPatterns[patternId].m_scope);
699 if (m_trafficPatterns[patternId].m_interestLifetime >= time::milliseconds(0))
700 interest.setInterestLifetime(m_trafficPatterns[patternId].m_interestLifetime);
701
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800702 try {
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700703 m_nInterestsSent++;
704 m_trafficPatterns[patternId].m_nInterestsSent++;
705 time::steady_clock::TimePoint sentTime = time::steady_clock::now();
706 m_face.expressInterest(interest,
707 bind(&NdnTrafficClient::onData,
708 this, _1, _2, m_nInterestsSent,
709 m_trafficPatterns[patternId].m_nInterestsSent,
710 patternId, sentTime),
711 bind(&NdnTrafficClient::onTimeout,
712 this, _1, m_nInterestsSent,
713 m_trafficPatterns[patternId].m_nInterestsSent,
Alexander Afanasyevfda32a32014-03-20 10:50:00 -0700714 patternId));
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700715 std::string logLine = "";
716 logLine += "Sending Interest - PatternType=" +
717 boost::lexical_cast<std::string>(patternId+1);
718 logLine += ", GlobalID=" + boost::lexical_cast<std::string>(m_nInterestsSent);
719 logLine += ", LocalID=" + boost::lexical_cast<std::string>(
720 m_trafficPatterns[patternId].m_nInterestsSent);
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800721 logLine += ", Name="+interest.getName().toUri();
jeraldabraham420dbf02014-04-25 22:58:31 -0700722 if (!m_hasQuietLogging)
723 m_logger.log(logLine, true, false);
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800724 deadlineTimer->expires_at(deadlineTimer->expires_at() +
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700725 boost::posix_time::millisec(
726 m_interestInterval.count()));
Alexander Afanasyevfda32a32014-03-20 10:50:00 -0700727 deadlineTimer->async_wait(bind(&NdnTrafficClient::generateTraffic,
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700728 this, deadlineTimer));
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800729 }
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700730 catch (std::exception& e) {
731 m_logger.log("ERROR: " + static_cast<std::string>(e.what()), true, true);
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800732 }
733 break;
734 }
735 }
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700736 if (patternId == m_trafficPatterns.size())
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800737 {
738 deadlineTimer->expires_at(deadlineTimer->expires_at() +
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700739 boost::posix_time::millisec(
740 m_interestInterval.count()));
Alexander Afanasyevfda32a32014-03-20 10:50:00 -0700741 deadlineTimer->async_wait(bind(&NdnTrafficClient::generateTraffic,
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700742 this, deadlineTimer));
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800743 }
744 }
745 }
746
747 void
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700748 run()
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800749 {
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700750 boost::asio::signal_set signalSet(*m_ioService, SIGINT, SIGTERM);
Alexander Afanasyevfda32a32014-03-20 10:50:00 -0700751 signalSet.async_wait(bind(&NdnTrafficClient::signalHandler, this));
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700752 m_logger.initializeLog(m_instanceId);
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800753 initializeTrafficConfiguration();
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700754
755 if (m_nMaximumInterests == 0)
756 {
757 logStatistics();
758 m_logger.shutdownLogger();
759 return;
760 }
761
762 boost::asio::deadline_timer deadlineTimer(
763 *m_ioService,
764 boost::posix_time::millisec(m_interestInterval.count()));
Alexander Afanasyevfda32a32014-03-20 10:50:00 -0700765 deadlineTimer.async_wait(bind(&NdnTrafficClient::generateTraffic,
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700766 this, &deadlineTimer));
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800767 try {
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700768 m_face.processEvents();
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800769 }
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700770 catch(std::exception& e) {
771 m_logger.log("ERROR: " + static_cast<std::string>(e.what()), true, true);
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800772 m_logger.shutdownLogger();
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700773 m_hasError = true;
774 m_ioService->stop();
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800775 }
776 }
777
778private:
779
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700780 std::string m_programName;
781 std::string m_instanceId;
782 bool m_hasError;
jeraldabraham420dbf02014-04-25 22:58:31 -0700783 bool m_hasQuietLogging;
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700784 time::milliseconds m_interestInterval;
785 int m_nMaximumInterests;
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800786 Logger m_logger;
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700787 std::string m_configurationFile;
788 shared_ptr<boost::asio::io_service> m_ioService;
789 Face m_face;
790 std::vector<InterestTrafficConfiguration> m_trafficPatterns;
791 std::vector<uint32_t> m_nonces;
792 int m_nInterestsSent;
793 int m_nInterestsReceived;
794 int m_nContentInconsistencies;
795
796 //round trip time is stored as milliseconds with fractional
797 //sub-milliseconds precision
798 double m_minimumInterestRoundTripTime;
799 double m_maximumInterestRoundTripTime;
800 double m_totalInterestRoundTripTime;
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800801
802};
803
804} // namespace ndn
805
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700806int
807main(int argc, char* argv[])
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800808{
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700809 std::srand(std::time(0));
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800810 ndn::NdnTrafficClient ndnTrafficClient (argv[0]);
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700811 int option;
jeraldabraham420dbf02014-04-25 22:58:31 -0700812 while ((option = getopt(argc, argv, "hqi:c:")) != -1) {
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800813 switch (option) {
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700814 case 'h':
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800815 ndnTrafficClient.usage();
816 break;
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700817 case 'i':
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800818 ndnTrafficClient.setInterestInterval(atoi(optarg));
819 break;
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700820 case 'c':
821 ndnTrafficClient.setMaximumInterests(atoi(optarg));
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800822 break;
jeraldabraham420dbf02014-04-25 22:58:31 -0700823 case 'q':
824 ndnTrafficClient.setQuietLogging();
825 break;
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700826 default:
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800827 ndnTrafficClient.usage();
828 break;
829 }
830 }
831
832 argc -= optind;
833 argv += optind;
834
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700835 if (argv[0] == 0)
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800836 ndnTrafficClient.usage();
837
838 ndnTrafficClient.setConfigurationFile(argv[0]);
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700839 ndnTrafficClient.run();
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800840
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700841 if (ndnTrafficClient.hasError())
842 return 1;
843 else
844 return 0;
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800845}