Client Tool Complete, Server Tool Work In Progress
diff --git a/ndn-traffic-client.cpp b/ndn-traffic-client.cpp
index f28b5d6..6df1ed1 100644
--- a/ndn-traffic-client.cpp
+++ b/ndn-traffic-client.cpp
@@ -11,8 +11,12 @@
#include <vector>
#include <boost/asio.hpp>
#include <boost/filesystem.hpp>
+#include <boost/bind.hpp>
+#include <boost/date_time/posix_time/posix_time.hpp>
#include <ndn-cpp-dev/face.hpp>
+#include <ndn-cpp-dev/exclude.hpp>
+#include <ndn-cpp-dev/name-component.hpp>
#include <ndn-cpp-dev/security/key-chain.hpp>
using namespace ndn;
@@ -33,16 +37,36 @@
logFile_.close();
}
+ static std::string
+ getTimestamp()
+ {
+ boost::posix_time::ptime now;
+ now = boost::posix_time::second_clock::local_time();
+ return to_simple_string(now);
+ }
+
void
- log( std::string logLine )
+ log( std::string logLine, bool printTime, bool printToConsole )
{
if( logLocation_.length() > 0 )
{
+ if (printTime)
+ logFile_ << getTimestamp() << " - ";
logFile_ << logLine << std::endl;
logFile_.flush();
+ if (printToConsole)
+ {
+ if (printTime)
+ std::cout << getTimestamp() << " - ";
+ std::cout << logLine << std::endl;
+ }
}
else
+ {
+ if (printTime)
+ std::cout << getTimestamp() << " - ";
std::cout << logLine << std::endl;
+ }
}
void
@@ -94,7 +118,7 @@
{
public:
- NdnTrafficClient( char* programName )
+ NdnTrafficClient( char* programName ) : ioService_(new boost::asio::io_service), face_(ioService_)
{
std::srand(std::time(0));
instanceId_ = toString(std::rand());
@@ -102,8 +126,11 @@
interestInterval_ = getDefaultInterestInterval();
interestCount_ = getDefaultInterestCount();
configurationFile_ = "";
- ioService_ = ptr_lib::make_shared<boost::asio::io_service>();
- face_ = Face(ioService_);
+ totalInterestSent_ = 0;
+ totalInterestReceived_ = 0;
+ minimumInterestRoundTripTime_ = std::numeric_limits<double>::max();
+ maximumInterestRoundTripTime_ = 0;
+ totalInterestRoundTripTime_ = 0;
}
NdnTrafficClient()
@@ -131,7 +158,18 @@
mustBeFresh = -1;
nonceDuplicationPercentage = -1;
scope = -1;
- interestLifetime = -1;
+ interestLifetime = getDefaultInterestLifetime();
+ totalInterestSent = 0;
+ totalInterestReceived = 0;
+ minimumInterestRoundTripTime = std::numeric_limits<double>::max();
+ maximumInterestRoundTripTime = 0;
+ totalInterestRoundTripTime = 0;
+ }
+
+ int
+ getDefaultInterestLifetime()
+ {
+ return 4000;
}
void
@@ -171,7 +209,7 @@
detail += "InterestLifetime="+toString(interestLifetime)+", ";
if (detail.length() >= 0)
detail = detail.substr(0, detail.length()-2);
- logger.log(detail);
+ logger.log(detail, false, false);
}
bool
@@ -237,11 +275,11 @@
else if (parameter == "InterestLifetime")
interestLifetime = toInteger(value);
else
- logger.log("Line "+toString(lineNumber)+" \t- Invalid Parameter='"+parameter+"'");
+ logger.log("Line "+toString(lineNumber)+" \t- Invalid Parameter='"+parameter+"'", false, true);
}
else
{
- logger.log("Line "+toString(lineNumber)+" \t: Improper Traffic Configuration Line- "+detail);
+ logger.log("Line "+toString(lineNumber)+" \t- Improper Traffic Configuration Line- "+detail, false, true);
return false;
}
return true;
@@ -268,6 +306,11 @@
int nonceDuplicationPercentage;
int scope;
int interestLifetime;
+ int totalInterestSent;
+ int totalInterestReceived;
+ double minimumInterestRoundTripTime;
+ double maximumInterestRoundTripTime;
+ double totalInterestRoundTripTime;
};
@@ -353,7 +396,7 @@
bool skipLine;
std::string patternLine;
std::ifstream patternFile;
- logger_.log("Analyzing Traffic Configuration File: " + configurationFile_);
+ logger_.log("Analyzing Traffic Configuration File: " + configurationFile_, true, true);
patternFile.open(configurationFile_.c_str());
if (patternFile.is_open())
{
@@ -392,9 +435,8 @@
patternFile.close();
if (!checkTrafficPatternCorrectness())
{
- logger_.log("ERROR - Traffic Configuration Provided Is Not Proper- " + configurationFile_);
+ logger_.log("ERROR - Traffic Configuration Provided Is Not Proper- " + configurationFile_, false, true);
logger_.shutdownLogger();
- std::cout << "ERROR - Check Log File For Details." << std::endl;
exit(1);
}
for (patternId = 0; patternId < trafficPattern_.size(); patternId++)
@@ -402,9 +444,8 @@
}
else
{
- logger_.log("ERROR - Unable To Open Traffic Configuration File: " + configurationFile_);
+ logger_.log("ERROR - Unable To Open Traffic Configuration File: " + configurationFile_, false, true);
logger_.shutdownLogger();
- std::cout << "ERROR - Check Log File For Details." << std::endl;
exit(1);
}
}
@@ -420,21 +461,245 @@
}
else
{
- logger_.log("ERROR - Traffic Configuration File Is Not A Regular File: " + configurationFile_);
+ logger_.log("ERROR - Traffic Configuration File Is Not A Regular File: " + configurationFile_, false, true);
logger_.shutdownLogger();
- std::cout << "ERROR - Check Log File For Details." << std::endl;
exit(1);
}
}
else
{
- logger_.log("ERROR - Traffic Configuration File Does Not Exist: " + configurationFile_);
+ logger_.log("ERROR - Traffic Configuration File Does Not Exist: " + configurationFile_, false, true);
logger_.shutdownLogger();
- std::cout << "ERROR - Check Log File For Details." << std::endl;
exit(1);
}
}
+ int
+ getOldNonce()
+ {
+ int randomNonceKey;
+ if (nonceList_.size() == 0)
+ return getNewNonce();
+ std::srand(std::time(0));
+ randomNonceKey = std::rand() % nonceList_.size();
+ return nonceList_[randomNonceKey];
+ }
+
+ int
+ getNewNonce()
+ {
+ int randomNonceKey, i;
+ bool isOld;
+ isOld = true;
+ std::srand(std::time(0));
+ do
+ {
+ randomNonceKey = std::rand();
+ isOld = false;
+ for (i=0; i<nonceList_.size(); i++)
+ if (nonceList_[i] == randomNonceKey)
+ isOld = true;
+ } while(isOld);
+ nonceList_.push_back(randomNonceKey);
+ return randomNonceKey;
+ }
+
+ static std::string
+ getRandomByteString( int randomSize )
+ {
+ int i;
+ std::string characterSet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvw0123456789";
+ std::string randomData;
+ for (i=0; i<randomSize; i++)
+ randomData += characterSet[std::rand() % characterSet.length()];
+ return randomData;
+ }
+
+ void
+ onData( ndn::Face &face,
+ const ndn::Interest& interest,
+ ndn::Data& data,
+ int globalReference,
+ int localReference,
+ int patternId,
+ boost::posix_time::ptime sentTime )
+ {
+ double roundTripTime;
+ std::string logLine;
+ logLine = "";
+ logLine += "Data Received - GlobalID="+toString(globalReference);
+ logLine += ", LocalID="+toString(localReference);
+ logLine += ", PatternID="+toString(patternId);
+ logLine += ", Name="+interest.getName().toUri();
+ logger_.log(logLine, true, false);
+ boost::posix_time::time_duration roundTripDuration;
+ totalInterestReceived_++;
+ trafficPattern_[patternId].totalInterestReceived++;
+ roundTripDuration = boost::posix_time::microsec_clock::local_time() - sentTime;
+ roundTripTime = roundTripDuration.total_microseconds()/1000.0;
+ if (minimumInterestRoundTripTime_ > roundTripTime)
+ minimumInterestRoundTripTime_ = roundTripTime;
+ if (maximumInterestRoundTripTime_ < roundTripTime)
+ maximumInterestRoundTripTime_ = roundTripTime;
+ if (trafficPattern_[patternId].minimumInterestRoundTripTime > roundTripTime)
+ trafficPattern_[patternId].minimumInterestRoundTripTime = roundTripTime;
+ if (trafficPattern_[patternId].maximumInterestRoundTripTime < roundTripTime)
+ trafficPattern_[patternId].maximumInterestRoundTripTime = roundTripTime;
+ totalInterestRoundTripTime_ += roundTripTime;
+ trafficPattern_[patternId].totalInterestRoundTripTime += roundTripTime;
+ }
+
+ void
+ onTimeout( ndn::Face &face,
+ const ndn::Interest& interest,
+ int globalReference,
+ int localReference,
+ int patternId)
+ {
+ std::string logLine;
+ logLine = "";
+ logLine += "Interest Timed Out - GlobalID="+toString(globalReference);
+ logLine += ", LocalID="+toString(localReference);
+ logLine += ", PatternID="+toString(patternId);
+ logLine += ", Name="+interest.getName().toUri();
+ logger_.log(logLine, true, false);
+ }
+
+ void
+ generateTraffic( const boost::system::error_code& errorCode,
+ boost::asio::deadline_timer* deadlineTimer )
+ {
+ if ((interestCount_ < 0) || (totalInterestSent_ < interestCount_))
+ {
+ int trafficKey, patternId, cumulativePercentage;
+ std::srand(std::time(0));
+ trafficKey = std::rand() % 100;
+ cumulativePercentage = 0;
+ std::cout << trafficKey << std::endl;
+ for (patternId=0; patternId<trafficPattern_.size(); patternId++)
+ {
+ cumulativePercentage += trafficPattern_[patternId].trafficPercentage;
+ if (trafficKey <= cumulativePercentage)
+ {
+ Name interestName(trafficPattern_[patternId].name);
+ if (trafficPattern_[patternId].nameAppendBytes > 0)
+ interestName.append(getRandomByteString(trafficPattern_[patternId].nameAppendBytes));
+ if (trafficPattern_[patternId].nameAppendSequenceNumber >= 0)
+ {
+ interestName.append(toString(trafficPattern_[patternId].nameAppendSequenceNumber));
+ trafficPattern_[patternId].nameAppendSequenceNumber++;
+ }
+ Interest interest(interestName);
+ if (trafficPattern_[patternId].minSuffixComponents > 0)
+ interest.setMinSuffixComponents(trafficPattern_[patternId].minSuffixComponents);
+ if (trafficPattern_[patternId].maxSuffixComponents > 0)
+ interest.setMaxSuffixComponents(trafficPattern_[patternId].maxSuffixComponents);
+ Exclude exclude;
+ if (trafficPattern_[patternId].excludeBefore != "" && trafficPattern_[patternId].excludeAfter != "")
+ {
+ exclude.excludeRange(name::Component(trafficPattern_[patternId].excludeAfter),
+ name::Component(trafficPattern_[patternId].excludeBefore));
+ interest.setExclude(exclude);
+ }
+ else if (trafficPattern_[patternId].excludeBefore != "")
+ {
+ exclude.excludeBefore(name::Component(trafficPattern_[patternId].excludeBefore));
+ interest.setExclude(exclude);
+ }
+ else if (trafficPattern_[patternId].excludeAfter != "")
+ {
+ exclude.excludeAfter(name::Component(trafficPattern_[patternId].excludeAfter));
+ interest.setExclude(exclude);
+ }
+ if (trafficPattern_[patternId].excludeBeforeBytes > 0 && trafficPattern_[patternId].excludeAfterBytes > 0)
+ {
+ exclude.excludeRange(name::Component(getRandomByteString(trafficPattern_[patternId].excludeAfterBytes)),
+ name::Component(getRandomByteString(trafficPattern_[patternId].excludeBeforeBytes)));
+ interest.setExclude(exclude);
+ }
+ else if (trafficPattern_[patternId].excludeBeforeBytes > 0)
+ {
+ exclude.excludeBefore(name::Component(getRandomByteString(trafficPattern_[patternId].excludeBeforeBytes)));
+ interest.setExclude(exclude);
+ }
+ else if (trafficPattern_[patternId].excludeAfterBytes > 0)
+ {
+ exclude.excludeAfter(name::Component(getRandomByteString(trafficPattern_[patternId].excludeAfterBytes)));
+ interest.setExclude(exclude);
+ }
+
+ if (trafficPattern_[patternId].childSelector >= 0)
+ interest.setChildSelector(trafficPattern_[patternId].childSelector);
+
+ if (trafficPattern_[patternId].mustBeFresh == 0)
+ interest.setMustBeFresh(false);
+ else if (trafficPattern_[patternId].mustBeFresh == 1)
+ interest.setMustBeFresh(true);
+ if (trafficPattern_[patternId].nonceDuplicationPercentage > 0)
+ {
+ int duplicationKey;
+ std::srand(std::time(0));
+ duplicationKey = std::rand() % 100;
+ if (trafficPattern_[patternId].nonceDuplicationPercentage <= duplicationKey)
+ interest.setNonce(getOldNonce());
+ else
+ interest.setNonce(getNewNonce());
+ }
+ else
+ interest.setNonce(getNewNonce());
+ if (trafficPattern_[patternId].scope >= 0)
+ interest.setScope(trafficPattern_[patternId].scope);
+ if (trafficPattern_[patternId].interestLifetime > 0)
+ interest.setInterestLifetime(trafficPattern_[patternId].interestLifetime);
+ try {
+ totalInterestSent_++;
+ trafficPattern_[patternId].totalInterestSent++;
+ boost::posix_time::ptime sentTime;
+ sentTime = boost::posix_time::microsec_clock::local_time();
+ face_.expressInterest(interest,
+ func_lib::bind( &NdnTrafficClient::onData,
+ this, boost::ref(face_),
+ _1, _2, totalInterestSent_,
+ trafficPattern_[patternId].totalInterestSent,
+ patternId,
+ sentTime),
+ func_lib::bind( &NdnTrafficClient::onTimeout,
+ this, boost::ref(face_),
+ _1, totalInterestSent_,
+ trafficPattern_[patternId].totalInterestSent,
+ patternId));
+ std::string logLine;
+ logLine = "";
+ logLine += "Sending Interest - GlobalID="+toString(totalInterestSent_);
+ logLine += ", LocalID="+toString(trafficPattern_[patternId].totalInterestSent);
+ logLine += ", PatternID="+toString(patternId);
+ logLine += ", Name="+interest.getName().toUri();
+ logger_.log(logLine, true, false);
+ deadlineTimer->expires_at(deadlineTimer->expires_at() +
+ boost::posix_time::millisec(interestInterval_));
+ deadlineTimer->async_wait(boost::bind(&NdnTrafficClient::generateTraffic,
+ this,
+ boost::asio::placeholders::error,
+ deadlineTimer));
+ }
+ catch (std::exception &e) {
+ logger_.log("ERROR: "+(std::string)e.what(), true, true);
+ }
+ break;
+ }
+ }
+ if (patternId==trafficPattern_.size())
+ {
+ deadlineTimer->expires_at(deadlineTimer->expires_at() +
+ boost::posix_time::millisec(interestInterval_));
+ deadlineTimer->async_wait(boost::bind(&NdnTrafficClient::generateTraffic,
+ this,
+ boost::asio::placeholders::error,
+ deadlineTimer));
+ }
+ }
+ }
+
void
initialize()
{
@@ -442,6 +707,19 @@
signalSet.async_wait(boost::bind(&NdnTrafficClient::signalHandler, this));
logger_.initializeLog(instanceId_);
initializeTrafficConfiguration();
+ boost::asio::deadline_timer deadlineTimer(*ioService_,
+ boost::posix_time::millisec(interestInterval_));
+ deadlineTimer.async_wait(boost::bind(&NdnTrafficClient::generateTraffic,
+ this,
+ boost::asio::placeholders::error,
+ &deadlineTimer));
+ try {
+ face_.processEvents();
+ }
+ catch(std::exception &e) {
+ logger_.log("ERROR: "+(std::string)e.what(), true, true);
+ logger_.shutdownLogger();
+ }
}
private:
@@ -454,8 +732,14 @@
Logger logger_;
std::string configurationFile_;
ptr_lib::shared_ptr<boost::asio::io_service> ioService_;
- std::vector<InterestTrafficConfiguration> trafficPattern_;
Face face_;
+ std::vector<InterestTrafficConfiguration> trafficPattern_;
+ std::vector<int> nonceList_;
+ int totalInterestSent_;
+ int totalInterestReceived_;
+ double minimumInterestRoundTripTime_;
+ double maximumInterestRoundTripTime_;
+ double totalInterestRoundTripTime_;
};
diff --git a/ndn-traffic-server.cpp b/ndn-traffic-server.cpp
index a6d8cf5..4b12510 100644
--- a/ndn-traffic-server.cpp
+++ b/ndn-traffic-server.cpp
@@ -18,7 +18,7 @@
{
public:
- NdnTrafficServer( char* programName )
+ NdnTrafficServer( char* programName ) : ioService_(new boost::asio::io_service), face_(ioService_)
{
std::stringstream randomId;
std::srand(std::time(0));
@@ -28,8 +28,6 @@
contentDelayTime_ = getDefaultContentDelayTime();
logLocation_ = "";
configurationFile_ = "";
- ioService_ = ptr_lib::make_shared<boost::asio::io_service>();
- face_ = Face(ioService_);
}
NdnTrafficServer()