/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
/**
 * Copyright (C) 2014 University of Arizona.
 *
 * GNU 3.0 License, see the LICENSE file for more information
 *
 * Author: Jerald Paul Abraham <jeraldabraham@email.arizona.edu>
 */

#include <string>
#include <sstream>
#include <fstream>
#include <vector>

#include "logger.hpp"

#include <boost/asio.hpp>
#include <boost/filesystem.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>

namespace ndn {

class NdnTrafficClient
{
public:
  NdnTrafficClient(char* programName)
    : m_logger("NDNTrafficClient")
    , ioService_(new boost::asio::io_service)
    , face_(ioService_)
    , keyChain_()
  {
    std::srand(std::time(0));
    instanceId_ = toString(std::rand());
    programName_ = programName;
    interestInterval_ = getDefaultInterestInterval();
    interestCount_ = getDefaultInterestCount();
    configurationFile_ = "";
    totalInterestSent_ = 0;
    totalInterestReceived_ = 0;
    minimumInterestRoundTripTime_ = std::numeric_limits<double>::max();
    maximumInterestRoundTripTime_ = 0;
    totalInterestRoundTripTime_ = 0;
  }

  class InterestTrafficConfiguration
  {
  public:
    InterestTrafficConfiguration()
    {
      trafficPercentage = -1;
      name = "";
      nameAppendBytes = -1;
      nameAppendSequenceNumber = -1;
      minSuffixComponents = -1;
      maxSuffixComponents = -1;
      excludeBefore = "";
      excludeAfter = "";
      excludeBeforeBytes = -1;
      excludeAfterBytes = -1;
      childSelector = -1;
      mustBeFresh = -1;
      nonceDuplicationPercentage = -1;
      scope = -1;
      interestLifetime = -1;
      totalInterestSent = 0;
      totalInterestReceived = 0;
      minimumInterestRoundTripTime = std::numeric_limits<double>::max();
      maximumInterestRoundTripTime = 0;
      totalInterestRoundTripTime = 0;
      contentInconsistencies = 0;
      expectedContent = "";
    }

    void
    printTrafficConfiguration(Logger& logger)
    {
      std::string detail;
      detail = "";
      if (trafficPercentage > 0)
        detail += "TrafficPercentage="+toString(trafficPercentage)+", ";
      if (name != "")
        detail += "Name="+name+", ";
      if (nameAppendBytes > 0)
        detail += "NameAppendBytes="+toString(nameAppendBytes)+", ";
      if (nameAppendSequenceNumber > 0)
        detail += "NameAppendSequenceNumber="+toString(nameAppendSequenceNumber)+", ";
      if (minSuffixComponents >= 0)
        detail += "MinSuffixComponents="+toString(minSuffixComponents)+", ";
      if (maxSuffixComponents >= 0)
        detail += "MaxSuffixComponents="+toString(maxSuffixComponents)+", ";
      if (excludeBefore != "")
        detail += "ExcludeBefore="+excludeBefore+", ";
      if (excludeAfter != "")
        detail += "ExcludeAfter="+excludeAfter+", ";
      if (excludeBeforeBytes > 0)
        detail += "ExcludeBeforeBytes="+toString(excludeBeforeBytes)+", ";
      if (excludeAfterBytes > 0)
        detail += "ExcludeAfterBytes="+toString(excludeAfterBytes)+", ";
      if (childSelector >= 0)
        detail += "ChildSelector="+toString(childSelector)+", ";
      if (mustBeFresh >= 0)
        detail += "MustBeFresh="+toString(mustBeFresh)+", ";
      if (nonceDuplicationPercentage > 0)
        detail += "NonceDuplicationPercentage="+toString(nonceDuplicationPercentage)+", ";
      if (scope >= 0)
        detail += "Scope="+toString(scope)+", ";
      if (interestLifetime >= 0)
        detail += "InterestLifetime="+toString(interestLifetime)+", ";
      if (expectedContent != "")
        detail += "ExpectedContent="+expectedContent+", ";
      if (detail.length() >= 2)
        detail = detail.substr(0, detail.length()-2);
      logger.log(detail, false, false);
    }

    bool
    extractParameterValue(const std::string& detail, std::string& parameter, std::string& value)
    {
      int i;
      std::string allowedCharacters = ":/+._-%";
      parameter = "";
      value = "";
      i = 0;
      while (detail[i] != '=' && i < detail.length())
        {
          parameter += detail[i];
          i++;
        }
      if (i == detail.length())
        return false;
      i++;
      while ((std::isalnum(detail[i]) || allowedCharacters.find(detail[i]) != std::string::npos) &&
             i < detail.length())
        {
          value += detail[i];
          i++;
        }
      if(parameter == "" || value == "")
        return false;
      return true;
    }

    bool
    processConfigurationDetail(const std::string& detail, Logger& logger, int lineNumber)
    {
      std::string parameter, value;
      if (extractParameterValue(detail, parameter, value))
        {
          if (parameter == "TrafficPercentage")
            trafficPercentage = toInteger(value);
          else if (parameter == "Name")
            name = value;
          else if (parameter == "NameAppendBytes")
            nameAppendBytes = toInteger(value);
          else if (parameter == "NameAppendSequenceNumber")
            nameAppendSequenceNumber = toInteger(value);
          else if (parameter == "MinSuffixComponents")
            minSuffixComponents = toInteger(value);
          else if (parameter == "MaxSuffixComponents")
            maxSuffixComponents = toInteger(value);
          else if (parameter == "ExcludeBefore")
            excludeBefore = value;
          else if (parameter == "ExcludeAfter")
            excludeAfter = value;
          else if (parameter == "ExcludeBeforeBytes")
            excludeBeforeBytes = toInteger(value);
          else if (parameter == "ExcludeAfterBytes")
            excludeAfterBytes = toInteger(value);
          else if (parameter == "ChildSelector")
            childSelector = toInteger(value);
          else if (parameter == "MustBeFresh")
            mustBeFresh = toInteger(value);
          else if (parameter == "NonceDuplicationPercentage")
            nonceDuplicationPercentage = toInteger(value);
          else if (parameter == "Scope")
            scope = toInteger(value);
          else if (parameter == "InterestLifetime")
            interestLifetime = toInteger(value);
          else if (parameter == "ExpectedContent")
            expectedContent = value;
          else
            logger.log("Line "+toString(lineNumber) +
                       " \t- Invalid Parameter='"+parameter+"'", false, true);
        }
      else
        {
          logger.log("Line "+toString(lineNumber) +
                     " \t- Improper Traffic Configuration Line- "+detail, false, true);
          return false;
        }
      return true;
    }

    bool
    checkTrafficDetailCorrectness()
    {
      return true;
    }

    int trafficPercentage;
    std::string name;
    int nameAppendBytes;
    int nameAppendSequenceNumber;
    int minSuffixComponents;
    int maxSuffixComponents;
    std::string excludeBefore;
    std::string excludeAfter;
    int excludeBeforeBytes;
    int excludeAfterBytes;
    int childSelector;
    int mustBeFresh;
    int nonceDuplicationPercentage;
    int scope;
    int interestLifetime;
    int totalInterestSent;
    int totalInterestReceived;
    double minimumInterestRoundTripTime;
    double maximumInterestRoundTripTime;
    double totalInterestRoundTripTime;
    int contentInconsistencies;
    std::string expectedContent;
  }; // class InterestTrafficConfiguration

  int
  getDefaultInterestLifetime()
  {
    return 4000;
  }

  static std::string
  toString(int integerValue)
  {
    std::stringstream stream;
    stream << integerValue;
    return stream.str();
  }

  static std::string
  toString(double doubleValue)
  {
    std::stringstream stream;
    stream << doubleValue;
    return stream.str();
  }

  static int
  toInteger(const std::string& stringValue)
  {
    int integerValue;
    std::stringstream stream(stringValue);
    stream >> integerValue;
    return integerValue;
  }

  void
  usage()
  {

    std::cout << "\nUsage: " << programName_ << " [options] <Traffic_Configuration_File>\n"
      "Generate Interest Traffic as per provided Traffic Configuration File\n"
      "Interests are continuously generated unless a total number is specified.\n"
      "Set environment variable NDN_TRAFFIC_LOGFOLDER for redirecting output to a log.\n"
      "  [-i interval] - set interest generation interval in milliseconds (minimum "
              << getDefaultInterestInterval() << " milliseconds)\n"
      "  [-c count] - set total number of interests to be generated\n"
      "  [-h] - print help and exit\n\n";
    exit(1);

  }

  int
  getDefaultInterestInterval()
  {
    return 1000;
  }

  int
  getDefaultInterestCount()
  {
    return -1;
  }

  void
  setInterestInterval( int interestInterval )
  {
    if (interestInterval < 0)
      usage();
    interestInterval_ = interestInterval;
  }

  void
  setInterestCount( int interestCount )
  {
    if (interestCount < 0)
      usage();
    interestCount_ = interestCount;
  }

  void
  setConfigurationFile( char* configurationFile )
  {
    configurationFile_ = configurationFile;
  }

  void
  signalHandler()
  {
    m_logger.shutdownLogger();
    face_.shutdown();
    ioService_.reset();
    logStatistics();
    exit(1);
  }

  void
  logStatistics()
  {
    int patternId;
    double loss, average, inconsistency;

    m_logger.log("\n\n== Interest Traffic Report ==\n", false, true);
    m_logger.log("Total Traffic Pattern Types = " + toString((int)trafficPattern_.size()), false, true);
    m_logger.log("Total Interests Sent        = " + toString(totalInterestSent_), false, true);
    m_logger.log("Total Responses Received    = " + toString(totalInterestReceived_), false, true);
    if (totalInterestSent_ > 0)
      loss = (totalInterestSent_-totalInterestReceived_)*100.0/totalInterestSent_;
    else
      loss = 0;
    m_logger.log("Total Interest Loss         = " + toString(loss)+"%", false, true);
    if (totalInterestReceived_ > 0)
      {
        average = totalInterestRoundTripTime_/totalInterestReceived_;
        inconsistency = contentInconsistencies_*100.0/totalInterestReceived_;
      }
    else
      {
        average = 0;
        inconsistency = 0;
      }
    m_logger.log("Total Data Inconsistency    = " + toString(inconsistency)+"%", false, true);
    m_logger.log("Total Round Trip Time       = " +
                 toString(totalInterestRoundTripTime_)+"ms", false, true);
    m_logger.log("Average Round Trip Time     = " + toString(average)+"ms\n", false, true);

    for (patternId=0; patternId<trafficPattern_.size(); patternId++)
      {
        m_logger.log("Traffic Pattern Type #" + toString(patternId+1), false, true);
        trafficPattern_[patternId].printTrafficConfiguration(m_logger);
        m_logger.log("Total Interests Sent        = " +
                     toString(trafficPattern_[patternId].totalInterestSent), false, true);
        m_logger.log("Total Responses Received    = " +
                     toString(trafficPattern_[patternId].totalInterestReceived), false, true);
        if (trafficPattern_[patternId].totalInterestSent > 0)
          {
            loss = (trafficPattern_[patternId].totalInterestSent -
                    trafficPattern_[patternId].totalInterestReceived);
            loss *= 100.0;
            loss /= trafficPattern_[patternId].totalInterestSent;
          }
        else
          loss = 0;
        m_logger.log("Total Interest Loss         = " + toString(loss)+"%", false, true);
        if (trafficPattern_[patternId].totalInterestReceived > 0)
          {
            average = (trafficPattern_[patternId].totalInterestRoundTripTime /
                       trafficPattern_[patternId].totalInterestReceived);
            inconsistency = trafficPattern_[patternId].contentInconsistencies;
            inconsistency = inconsistency * 100.0 / trafficPattern_[patternId].totalInterestReceived;
          }
        else
          {
            average = 0;
            inconsistency = 0;
          }
        m_logger.log("Total Data Inconsistency    = " + toString(inconsistency)+"%", false, true);
        m_logger.log("Total Round Trip Time       = " +
                     toString(trafficPattern_[patternId].totalInterestRoundTripTime)+"ms",
                     false, true);
        m_logger.log("Average Round Trip Time     = " + toString(average)+"ms\n", false, true);
      }
  }

  bool
  checkTrafficPatternCorrectness()
  {
    return true;
  }

  void
  analyzeConfigurationFile()
  {
    int patternId;
    int lineNumber;
    bool skipLine;
    std::string patternLine;
    std::ifstream patternFile;
    m_logger.log("Analyzing Traffic Configuration File: " + configurationFile_, true, true);
    patternFile.open(configurationFile_.c_str());
    if (patternFile.is_open())
      {
        patternId = 0;
        lineNumber = 0;
        while (getline(patternFile, patternLine))
          {
            lineNumber++;
            if (std::isalpha(patternLine[0]))
              {
                InterestTrafficConfiguration interestData;
                skipLine = false;
                patternId++;
                if (interestData.processConfigurationDetail(patternLine, m_logger, lineNumber))
                  {
                    while (getline(patternFile, patternLine) && std::isalpha(patternLine[0]))
                      {
                        lineNumber++;
                        if (!interestData.processConfigurationDetail(patternLine,
                                                                     m_logger, lineNumber))
                          {
                            skipLine = true;
                            break;
                          }
                      }
                    lineNumber++;
                  }
                else
                  skipLine = true;
                if( !skipLine )
                  {
                    if (interestData.checkTrafficDetailCorrectness())
                      trafficPattern_.push_back(interestData);
                  }
              }
          }
        patternFile.close();
        if (!checkTrafficPatternCorrectness())
          {
            m_logger.log("ERROR - Traffic Configuration Provided Is Not Proper- " +
                         configurationFile_, false, true);
            m_logger.shutdownLogger();
            exit(1);
          }
        m_logger.log("Traffic Configuration File Processing Completed\n", true, false);
        for (patternId=0; patternId<trafficPattern_.size(); patternId++)
          {
            m_logger.log("Traffic Pattern Type #"+toString(patternId+1), false, false);
            trafficPattern_[patternId].printTrafficConfiguration(m_logger);
            m_logger.log("", false, false);
          }
      }
    else
      {
        m_logger.log("ERROR - Unable To Open Traffic Configuration File: " +
                     configurationFile_, false, true);
        m_logger.shutdownLogger();
        exit(1);
      }
  }

  void
  initializeTrafficConfiguration()
  {
    if (boost::filesystem::exists(boost::filesystem::path(configurationFile_)))
      {
        if(boost::filesystem::is_regular_file(boost::filesystem::path(configurationFile_)))
          {
            analyzeConfigurationFile();
          }
        else
          {
            m_logger.log("ERROR - Traffic Configuration File Is Not A Regular File: " +
                         configurationFile_, false, true);
            m_logger.shutdownLogger();
            exit(1);
          }
      }
    else
      {
        m_logger.log("ERROR - Traffic Configuration File Does Not Exist: " +
                     configurationFile_, false, true);
        m_logger.shutdownLogger();
        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));

    //Performance Enhancement
    if (nonceList_.size() > 1000)
      nonceList_.clear();

    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;
    int receivedContentLength;
    std::string receivedContent;
    std::string logLine;
    logLine = "";
    logLine += "Data Received      - PatternType="+toString(patternId+1);
    logLine += ", GlobalID="+toString(globalReference);
    logLine += ", LocalID="+toString(localReference);
    logLine += ", Name="+interest.getName().toUri();
    boost::posix_time::time_duration roundTripDuration;
    totalInterestReceived_++;
    trafficPattern_[patternId].totalInterestReceived++;
    if (trafficPattern_[patternId].expectedContent != "")
      {
        receivedContent = (char*)(data.getContent().value());
        receivedContentLength = data.getContent().value_size();
        receivedContent = receivedContent.substr(0, receivedContentLength);
        if (receivedContent != trafficPattern_[patternId].expectedContent)
          {
            contentInconsistencies_++;
            trafficPattern_[patternId].contentInconsistencies++;
            logLine += ", IsConsistent=No";
          }
        else
          logLine += ", IsConsistent=Yes";
      }
    else
      logLine += ", IsConsistent=NotChecked";
    m_logger.log(logLine, true, false);
    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;
    if (totalInterestSent_ == interestCount_)
      signalHandler();
  }

  void
  onTimeout( ndn::Face &face,
             const ndn::Interest& interest,
             int globalReference,
             int localReference,
             int patternId)
  {
    std::string logLine;
    logLine = "";
    logLine += "Interest Timed Out - PatternType="+toString(patternId+1);
    logLine += ", GlobalID="+toString(globalReference);
    logLine += ", LocalID="+toString(localReference);
    logLine += ", Name="+interest.getName().toUri();
    m_logger.log(logLine, true, false);
    if (totalInterestSent_ == interestCount_)
      signalHandler();
  }

  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;
        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 > 0)
                  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(
                    time::milliseconds(trafficPattern_[patternId].interestLifetime));
                else
                  interest.setInterestLifetime(
                    time::milliseconds(getDefaultInterestLifetime()));
                try {
                  totalInterestSent_++;
                  trafficPattern_[patternId].totalInterestSent++;
                  boost::posix_time::ptime sentTime;
                  sentTime = boost::posix_time::microsec_clock::local_time();
                  face_.expressInterest(interest,
                                        bind( &NdnTrafficClient::onData,
                                              this, boost::ref(face_),
                                              _1, _2, totalInterestSent_,
                                              trafficPattern_[patternId].totalInterestSent,
                                              patternId,
                                              sentTime),
                                        bind( &NdnTrafficClient::onTimeout,
                                              this, boost::ref(face_),
                                              _1, totalInterestSent_,
                                              trafficPattern_[patternId].totalInterestSent,
                                              patternId));
                  std::string logLine;
                  logLine = "";
                  logLine += "Sending Interest   - PatternType="+toString(patternId+1);
                  logLine += ", GlobalID="+toString(totalInterestSent_);
                  logLine += ", LocalID="+toString(trafficPattern_[patternId].totalInterestSent);
                  logLine += ", Name="+interest.getName().toUri();
                  m_logger.log(logLine, true, false);
                  deadlineTimer->expires_at(deadlineTimer->expires_at() +
                                            boost::posix_time::millisec(interestInterval_));
                  deadlineTimer->async_wait(bind(&NdnTrafficClient::generateTraffic,
                                                 this,
                                                 boost::asio::placeholders::error,
                                                 deadlineTimer));
                }
                catch (std::exception &e) {
                  m_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(bind(&NdnTrafficClient::generateTraffic,
                                           this,
                                           boost::asio::placeholders::error,
                                           deadlineTimer));
          }
      }
  }

  void
  initialize()
  {
    boost::asio::signal_set signalSet(*ioService_, SIGINT, SIGTERM);
    signalSet.async_wait(bind(&NdnTrafficClient::signalHandler, this));
    m_logger.initializeLog(instanceId_);
    initializeTrafficConfiguration();
    boost::asio::deadline_timer deadlineTimer(*ioService_,
                                              boost::posix_time::millisec(interestInterval_));
    deadlineTimer.async_wait(bind(&NdnTrafficClient::generateTraffic,
                                  this,
                                  boost::asio::placeholders::error,
                                  &deadlineTimer));
    try {
      face_.processEvents();
    }
    catch(std::exception &e) {
      m_logger.log("ERROR: "+(std::string)e.what(), true, true);
      m_logger.shutdownLogger();
    }
  }

private:

  KeyChain keyChain_;
  std::string programName_;
  std::string instanceId_;
  int interestInterval_;
  int interestCount_;
  Logger m_logger;
  std::string configurationFile_;
  ptr_lib::shared_ptr<boost::asio::io_service> ioService_;
  Face face_;
  std::vector<InterestTrafficConfiguration> trafficPattern_;
  std::vector<int> nonceList_;
  int totalInterestSent_;
  int totalInterestReceived_;
  int contentInconsistencies_;
  double minimumInterestRoundTripTime_;
  double maximumInterestRoundTripTime_;
  double totalInterestRoundTripTime_;

};

} // namespace ndn

int main( int argc, char* argv[] )
{
  int option;
  ndn::NdnTrafficClient ndnTrafficClient (argv[0]);
  while ((option = getopt(argc, argv, "hi:c:")) != -1) {
    switch (option) {
    case 'h'  :
      ndnTrafficClient.usage();
      break;
    case 'i'  :
      ndnTrafficClient.setInterestInterval(atoi(optarg));
      break;
    case 'c'  :
      ndnTrafficClient.setInterestCount(atoi(optarg));
      break;
    default   :
      ndnTrafficClient.usage();
      break;
    }
  }

  argc -= optind;
  argv += optind;

  if (argv[0] == NULL)
    ndnTrafficClient.usage();

  ndnTrafficClient.setConfigurationFile(argv[0]);
  ndnTrafficClient.initialize();

  return 0;
}
