chunks: add --naming-convention option

Refs: #5109
Change-Id: I98170dd362663900900e9f792ffb5340ad96fe3a
diff --git a/tools/chunks/catchunks/consumer.cpp b/tools/chunks/catchunks/consumer.cpp
index 12de675..c1be8e8 100644
--- a/tools/chunks/catchunks/consumer.cpp
+++ b/tools/chunks/catchunks/consumer.cpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /*
- * Copyright (c) 2016-2020, Regents of the University of California,
+ * Copyright (c) 2016-2021, Regents of the University of California,
  *                          Colorado State University,
  *                          University Pierre & Marie Curie, Sorbonne University.
  *
@@ -33,7 +33,6 @@
 Consumer::Consumer(security::Validator& validator, std::ostream& os)
   : m_validator(validator)
   , m_outputStream(os)
-  , m_nextToPrint(0)
 {
 }
 
@@ -47,8 +46,8 @@
 
   m_discover->onDiscoverySuccess.connect([this] (const Name& versionedName) {
     m_pipeline->run(versionedName,
-      [this] (const Data& data) { handleData(data); },
-      [] (const std::string& msg) { NDN_THROW(std::runtime_error(msg)); });
+                    [this] (const Data& data) { handleData(data); },
+                    [] (const std::string& msg) { NDN_THROW(std::runtime_error(msg)); });
   });
   m_discover->onDiscoveryFailure.connect([] (const std::string& msg) {
     NDN_THROW(std::runtime_error(msg));
diff --git a/tools/chunks/catchunks/consumer.hpp b/tools/chunks/catchunks/consumer.hpp
index 3f6f77e..a2151a9 100644
--- a/tools/chunks/catchunks/consumer.hpp
+++ b/tools/chunks/catchunks/consumer.hpp
@@ -94,7 +94,7 @@
   std::ostream& m_outputStream;
   unique_ptr<DiscoverVersion> m_discover;
   unique_ptr<PipelineInterests> m_pipeline;
-  uint64_t m_nextToPrint;
+  uint64_t m_nextToPrint = 0;
 
 PUBLIC_WITH_TESTS_ELSE_PRIVATE:
   std::map<uint64_t, shared_ptr<const Data>> m_bufferedData;
diff --git a/tools/chunks/catchunks/main.cpp b/tools/chunks/catchunks/main.cpp
index faa7ed6..59ea8d8 100644
--- a/tools/chunks/catchunks/main.cpp
+++ b/tools/chunks/catchunks/main.cpp
@@ -39,6 +39,7 @@
 
 #include <fstream>
 #include <ndn-cxx/security/validator-null.hpp>
+#include <ndn-cxx/util/rtt-estimator.hpp>
 
 #include <boost/program_options/options_description.hpp>
 #include <boost/program_options/parsers.hpp>
@@ -47,31 +48,34 @@
 namespace ndn {
 namespace chunks {
 
+namespace po = boost::program_options;
+
 static int
 main(int argc, char* argv[])
 {
-  std::string programName(argv[0]);
+  const std::string programName(argv[0]);
 
   Options options;
-  std::string uri, pipelineType("cubic"), cwndPath, rttPath;
-  time::milliseconds::rep minRto(200), maxRto(60000);
-  double rtoAlpha(0.125), rtoBeta(0.25);
-  int rtoK(8);
+  std::string prefix, nameConv, pipelineType("cubic");
+  std::string cwndPath, rttPath;
+  auto rttEstOptions = make_shared<util::RttEstimator::Options>();
+  rttEstOptions->k = 8; // increased from the ndn-cxx default of 4
 
-  namespace po = boost::program_options;
   po::options_description basicDesc("Basic Options");
   basicDesc.add_options()
     ("help,h",      "print this help message and exit")
-    ("pipeline-type,p", po::value<std::string>(&pipelineType)->default_value(pipelineType),
-                        "type of Interest pipeline to use; valid values are: 'fixed', 'aimd', 'cubic'")
     ("fresh,f",     po::bool_switch(&options.mustBeFresh),
                     "only return fresh content (set MustBeFresh on all outgoing Interests)")
     ("lifetime,l",  po::value<time::milliseconds::rep>()->default_value(options.interestLifetime.count()),
                     "lifetime of expressed Interests, in milliseconds")
     ("retries,r",   po::value<int>(&options.maxRetriesOnTimeoutOrNack)->default_value(options.maxRetriesOnTimeoutOrNack),
                     "maximum number of retries in case of Nack or timeout (-1 = no limit)")
+    ("pipeline-type,p", po::value<std::string>(&pipelineType)->default_value(pipelineType),
+                        "type of Interest pipeline to use; valid values are: 'fixed', 'aimd', 'cubic'")
     ("no-version-discovery,D", po::bool_switch(&options.disableVersionDiscovery),
-                    "skip version discovery, even if the supplied name does not end with a version component")
+                               "skip version discovery even if the name does not end with a version component")
+    ("naming-convention,N", po::value<std::string>(&nameConv),
+                            "encoding convention to use for name components, either 'marker' or 'typed'")
     ("quiet,q",     po::bool_switch(&options.isQuiet), "suppress all diagnostic output, except fatal errors")
     ("verbose,v",   po::bool_switch(&options.isVerbose), "turn on verbose output (per segment information")
     ("version,V",   "print program version and exit")
@@ -85,36 +89,42 @@
 
   po::options_description adaptivePipeDesc("Adaptive pipeline options (AIMD & CUBIC)");
   adaptivePipeDesc.add_options()
-    ("ignore-marks", po::bool_switch(&options.ignoreCongMarks),
-                     "do not reduce the window after receiving a congestion mark")
-    ("disable-cwa",  po::bool_switch(&options.disableCwa),
-                     "disable Conservative Window Adaptation, i.e., reduce the window on "
-                     "each timeout or congestion mark instead of at most once per RTT")
-    ("reset-cwnd-to-init", po::bool_switch(&options.resetCwndToInit),
-                           "after a timeout or congestion mark, reset the window "
-                           "to the initial value instead of resetting to ssthresh")
+    ("ignore-marks",  po::bool_switch(&options.ignoreCongMarks),
+                      "do not reduce the window after receiving a congestion mark")
+    ("disable-cwa",   po::bool_switch(&options.disableCwa),
+                      "disable Conservative Window Adaptation (reduce the window "
+                      "on each congestion event instead of at most once per RTT)")
     ("init-cwnd",     po::value<double>(&options.initCwnd)->default_value(options.initCwnd),
                       "initial congestion window in segments")
     ("init-ssthresh", po::value<double>(&options.initSsthresh),
                       "initial slow start threshold in segments (defaults to infinity)")
-    ("aimd-step", po::value<double>(&options.aiStep)->default_value(options.aiStep),
-                  "additive-increase step")
-    ("aimd-beta", po::value<double>(&options.mdCoef)->default_value(options.mdCoef),
-                  "multiplicative decrease factor (AIMD)")
-    ("rto-alpha", po::value<double>(&rtoAlpha)->default_value(rtoAlpha),
+    ("rto-alpha", po::value<double>(&rttEstOptions->alpha)->default_value(rttEstOptions->alpha),
                   "alpha value for RTO calculation")
-    ("rto-beta",  po::value<double>(&rtoBeta)->default_value(rtoBeta),
+    ("rto-beta",  po::value<double>(&rttEstOptions->beta)->default_value(rttEstOptions->beta),
                   "beta value for RTO calculation")
-    ("rto-k",     po::value<int>(&rtoK)->default_value(rtoK),
+    ("rto-k",     po::value<int>(&rttEstOptions->k)->default_value(rttEstOptions->k),
                   "k value for RTO calculation")
-    ("min-rto",   po::value<time::milliseconds::rep>(&minRto)->default_value(minRto),
+    ("min-rto",   po::value<time::milliseconds::rep>()->default_value(
+                    time::duration_cast<time::milliseconds>(rttEstOptions->minRto).count()),
                   "minimum RTO value, in milliseconds")
-    ("max-rto",   po::value<time::milliseconds::rep>(&maxRto)->default_value(maxRto),
+    ("max-rto",   po::value<time::milliseconds::rep>()->default_value(
+                    time::duration_cast<time::milliseconds>(rttEstOptions->maxRto).count()),
                   "maximum RTO value, in milliseconds")
     ("log-cwnd",  po::value<std::string>(&cwndPath), "log file for congestion window stats")
     ("log-rtt",   po::value<std::string>(&rttPath), "log file for round-trip time stats")
     ;
 
+  po::options_description aimdPipeDesc("AIMD pipeline options");
+  aimdPipeDesc.add_options()
+    ("aimd-step", po::value<double>(&options.aiStep)->default_value(options.aiStep),
+                  "additive increase step")
+    ("aimd-beta", po::value<double>(&options.mdCoef)->default_value(options.mdCoef),
+                  "multiplicative decrease factor")
+    ("reset-cwnd-to-init", po::bool_switch(&options.resetCwndToInit),
+                           "after a congestion event, reset the window to the "
+                           "initial value instead of resetting to ssthresh")
+    ;
+
   po::options_description cubicPipeDesc("CUBIC pipeline options");
   cubicPipeDesc.add_options()
     ("cubic-beta", po::value<double>(&options.cubicBeta), "window decrease factor (defaults to 0.7)")
@@ -125,17 +135,18 @@
   visibleDesc.add(basicDesc)
              .add(fixedPipeDesc)
              .add(adaptivePipeDesc)
+             .add(aimdPipeDesc)
              .add(cubicPipeDesc);
 
   po::options_description hiddenDesc;
   hiddenDesc.add_options()
-    ("ndn-name,n", po::value<std::string>(&uri), "NDN name of the requested content");
+    ("name", po::value<std::string>(&prefix), "NDN name of the requested content");
 
   po::options_description optDesc;
   optDesc.add(visibleDesc).add(hiddenDesc);
 
   po::positional_options_description p;
-  p.add("ndn-name", -1);
+  p.add("name", -1);
 
   po::variables_map vm;
   try {
@@ -143,55 +154,83 @@
     po::notify(vm);
   }
   catch (const po::error& e) {
-    std::cerr << "ERROR: " << e.what() << std::endl;
+    std::cerr << "ERROR: " << e.what() << "\n";
     return 2;
   }
   catch (const boost::bad_any_cast& e) {
-    std::cerr << "ERROR: " << e.what() << std::endl;
+    std::cerr << "ERROR: " << e.what() << "\n";
     return 2;
   }
 
   if (vm.count("help") > 0) {
-    std::cout << "Usage: " << programName << " [options] ndn:/name" << std::endl;
+    std::cout << "Usage: " << programName << " [options] ndn:/name\n";
     std::cout << visibleDesc;
     return 0;
   }
 
   if (vm.count("version") > 0) {
-    std::cout << "ndncatchunks " << tools::VERSION << std::endl;
+    std::cout << "ndncatchunks " << tools::VERSION << "\n";
     return 0;
   }
 
-  if (vm.count("ndn-name") == 0) {
-    std::cerr << "Usage: " << programName << " [options] ndn:/name" << std::endl;
+  if (prefix.empty()) {
+    std::cerr << "Usage: " << programName << " [options] ndn:/name\n";
     std::cerr << visibleDesc;
     return 2;
   }
 
-  if (options.maxPipelineSize < 1 || options.maxPipelineSize > 1024) {
-    std::cerr << "ERROR: pipeline size must be between 1 and 1024" << std::endl;
-    return 2;
+  if (nameConv == "marker" || nameConv == "m" || nameConv == "1") {
+    name::setConventionEncoding(name::Convention::MARKER);
   }
-
-  if (options.maxRetriesOnTimeoutOrNack < -1 || options.maxRetriesOnTimeoutOrNack > 1024) {
-    std::cerr << "ERROR: retries value must be between -1 and 1024" << std::endl;
+  else if (nameConv == "typed" || nameConv == "t" || nameConv == "2") {
+    name::setConventionEncoding(name::Convention::TYPED);
+  }
+  else if (!nameConv.empty()) {
+    std::cerr << "ERROR: '" << nameConv << "' is not a valid naming convention\n";
     return 2;
   }
 
   options.interestLifetime = time::milliseconds(vm["lifetime"].as<time::milliseconds::rep>());
   if (options.interestLifetime < 0_ms) {
-    std::cerr << "ERROR: lifetime cannot be negative" << std::endl;
+    std::cerr << "ERROR: --lifetime cannot be negative\n";
+    return 2;
+  }
+
+  if (options.maxRetriesOnTimeoutOrNack < -1 || options.maxRetriesOnTimeoutOrNack > 1024) {
+    std::cerr << "ERROR: --retries must be between -1 and 1024\n";
     return 2;
   }
 
   if (options.isQuiet && options.isVerbose) {
-    std::cerr << "ERROR: cannot be quiet and verbose at the same time" << std::endl;
+    std::cerr << "ERROR: cannot be quiet and verbose at the same time\n";
+    return 2;
+  }
+
+  if (options.maxPipelineSize < 1 || options.maxPipelineSize > 1024) {
+    std::cerr << "ERROR: --pipeline-size must be between 1 and 1024\n";
+    return 2;
+  }
+
+  if (rttEstOptions->k < 0) {
+    std::cerr << "ERROR: --rto-k cannot be negative\n";
+    return 2;
+  }
+
+  rttEstOptions->minRto = time::milliseconds(vm["min-rto"].as<time::milliseconds::rep>());
+  if (rttEstOptions->minRto < 0_ms) {
+    std::cerr << "ERROR: --min-rto cannot be negative\n";
+    return 2;
+  }
+
+  rttEstOptions->maxRto = time::milliseconds(vm["max-rto"].as<time::milliseconds::rep>());
+  if (rttEstOptions->maxRto < rttEstOptions->minRto) {
+    std::cerr << "ERROR: --max-rto cannot be smaller than --min-rto\n";
     return 2;
   }
 
   try {
     Face face;
-    auto discover = make_unique<DiscoverVersion>(face, Name(uri), options);
+    auto discover = make_unique<DiscoverVersion>(face, Name(prefix), options);
     unique_ptr<PipelineInterests> pipeline;
     unique_ptr<StatisticsCollector> statsCollector;
     unique_ptr<RttEstimatorWithStats> rttEstimator;
@@ -202,26 +241,18 @@
       pipeline = make_unique<PipelineInterestsFixed>(face, options);
     }
     else if (pipelineType == "aimd" || pipelineType == "cubic") {
-      auto optionsRttEst = make_shared<RttEstimatorWithStats::Options>();
-      optionsRttEst->alpha = rtoAlpha;
-      optionsRttEst->beta = rtoBeta;
-      optionsRttEst->k = rtoK;
-      optionsRttEst->initialRto = 1_s;
-      optionsRttEst->minRto = time::milliseconds(minRto);
-      optionsRttEst->maxRto = time::milliseconds(maxRto);
-      optionsRttEst->rtoBackoffMultiplier = 2;
       if (options.isVerbose) {
         using namespace ndn::time;
         std::cerr << "RTT estimator parameters:\n"
-                  << "\tAlpha = " << optionsRttEst->alpha << "\n"
-                  << "\tBeta = " << optionsRttEst->beta << "\n"
-                  << "\tK = " << optionsRttEst->k << "\n"
-                  << "\tInitial RTO = " << duration_cast<milliseconds>(optionsRttEst->initialRto) << "\n"
-                  << "\tMin RTO = " << duration_cast<milliseconds>(optionsRttEst->minRto) << "\n"
-                  << "\tMax RTO = " << duration_cast<milliseconds>(optionsRttEst->maxRto) << "\n"
-                  << "\tBackoff multiplier = " << optionsRttEst->rtoBackoffMultiplier << "\n";
+                  << "\tAlpha = " << rttEstOptions->alpha << "\n"
+                  << "\tBeta = " << rttEstOptions->beta << "\n"
+                  << "\tK = " << rttEstOptions->k << "\n"
+                  << "\tInitial RTO = " << duration_cast<milliseconds>(rttEstOptions->initialRto) << "\n"
+                  << "\tMin RTO = " << duration_cast<milliseconds>(rttEstOptions->minRto) << "\n"
+                  << "\tMax RTO = " << duration_cast<milliseconds>(rttEstOptions->maxRto) << "\n"
+                  << "\tBackoff multiplier = " << rttEstOptions->rtoBackoffMultiplier << "\n";
       }
-      rttEstimator = make_unique<RttEstimatorWithStats>(std::move(optionsRttEst));
+      rttEstimator = make_unique<RttEstimatorWithStats>(std::move(rttEstOptions));
 
       unique_ptr<PipelineInterestsAdaptive> adaptivePipeline;
       if (pipelineType == "aimd") {
@@ -235,14 +266,14 @@
         if (!cwndPath.empty()) {
           statsFileCwnd.open(cwndPath);
           if (statsFileCwnd.fail()) {
-            std::cerr << "ERROR: failed to open " << cwndPath << std::endl;
+            std::cerr << "ERROR: failed to open '" << cwndPath << "'\n";
             return 4;
           }
         }
         if (!rttPath.empty()) {
           statsFileRtt.open(rttPath);
           if (statsFileRtt.fail()) {
-            std::cerr << "ERROR: failed to open " << rttPath << std::endl;
+            std::cerr << "ERROR: failed to open '" << rttPath << "'\n";
             return 4;
           }
         }
@@ -252,7 +283,7 @@
       pipeline = std::move(adaptivePipeline);
     }
     else {
-      std::cerr << "ERROR: Interest pipeline type not valid" << std::endl;
+      std::cerr << "ERROR: '" << pipelineType << "' is not a valid pipeline type\n";
       return 2;
     }
 
@@ -263,15 +294,15 @@
     face.processEvents();
   }
   catch (const Consumer::ApplicationNackError& e) {
-    std::cerr << "ERROR: " << e.what() << std::endl;
+    std::cerr << "ERROR: " << e.what() << "\n";
     return 3;
   }
   catch (const Consumer::DataValidationError& e) {
-    std::cerr << "ERROR: " << e.what() << std::endl;
+    std::cerr << "ERROR: " << e.what() << "\n";
     return 5;
   }
   catch (const std::exception& e) {
-    std::cerr << "ERROR: " << e.what() << std::endl;
+    std::cerr << "ERROR: " << e.what() << "\n";
     return 1;
   }