Migrate to C++17 and misc code cleanups

Change-Id: I6b63385c92361a7ef5803d2bfd00f39c77e88d34
diff --git a/src/handles/command-base-handle.cpp b/src/handles/command-base-handle.cpp
index a820e3b..ba126c0 100644
--- a/src/handles/command-base-handle.cpp
+++ b/src/handles/command-base-handle.cpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /*
- * Copyright (c) 2014-2020, Regents of the University of California.
+ * Copyright (c) 2014-2022, Regents of the University of California.
  *
  * This file is part of NDN repo-ng (Next generation of NDN repository).
  * See AUTHORS.md for complete list of repo-ng authors and contributors.
@@ -19,7 +19,7 @@
 
 #include "command-base-handle.hpp"
 
-#include <ndn-cxx/util/random.hpp>
+#include <optional>
 
 namespace repo {
 
@@ -29,23 +29,23 @@
 
 /** \brief obtain signer from SignerTag attached to Interest, if available
  */
-static ndn::optional<std::string>
+static std::optional<std::string>
 getSignerFromTag(const ndn::Interest& interest)
 {
-  std::shared_ptr<SignerTag> signerTag = interest.getTag<SignerTag>();
+  auto signerTag = interest.getTag<SignerTag>();
   if (signerTag == nullptr) {
-    return ndn::nullopt;
+    return std::nullopt;
   }
   else {
     return signerTag->get().toUri();
   }
 }
 
-CommandBaseHandle::CommandBaseHandle(Face& face, RepoStorage& storageHandle,
-                  Scheduler& scheduler, Validator& validator)
+CommandBaseHandle::CommandBaseHandle(Face& face, RepoStorage& storage,
+                                     Scheduler& sched, ndn::security::Validator& validator)
   : face(face)
-  , storageHandle(storageHandle)
-  , scheduler(scheduler)
+  , storageHandle(storage)
+  , scheduler(sched)
   , m_validator(validator)
 {
 }
@@ -53,19 +53,16 @@
 ndn::mgmt::Authorization
 CommandBaseHandle::makeAuthorization()
 {
-  return [=] (const ndn::Name& prefix, const ndn::Interest& interest,
-            const ndn::mgmt::ControlParameters* params,
-            const ndn::mgmt::AcceptContinuation& accept,
-            const ndn::mgmt::RejectContinuation& reject) {
+  return [=] (const ndn::Name&, const auto& interest,
+              const ndn::mgmt::ControlParameters*,
+              const ndn::mgmt::AcceptContinuation& accept,
+              const ndn::mgmt::RejectContinuation& reject) {
   m_validator.validate(interest,
-    [accept] (const ndn::Interest& request) {
-
-      auto signer1 = getSignerFromTag(request);
-      std::string signer = signer1.value_or("*");
+    [accept] (const auto& request) {
+      auto signer = getSignerFromTag(request).value_or("*");
       accept(signer);
     },
-    [reject] (const ndn::Interest& request,
-              const ndn::security::ValidationError& error) {
+    [reject] (auto&&...) {
       reject(ndn::mgmt::RejectReply::STATUS403);
     });
   };
diff --git a/src/handles/command-base-handle.hpp b/src/handles/command-base-handle.hpp
index 205b884..520b7d0 100644
--- a/src/handles/command-base-handle.hpp
+++ b/src/handles/command-base-handle.hpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /*
- * Copyright (c) 2014-2018, Regents of the University of California.
+ * Copyright (c) 2014-2022, Regents of the University of California.
  *
  * This file is part of NDN repo-ng (Next generation of NDN repository).
  * See AUTHORS.md for complete list of repo-ng authors and contributors.
@@ -28,6 +28,7 @@
 #include "repo-command.hpp"
 
 #include <ndn-cxx/mgmt/dispatcher.hpp>
+#include <ndn-cxx/security/validator.hpp>
 
 namespace repo {
 
@@ -37,16 +38,12 @@
   class Error : public std::runtime_error
   {
   public:
-    explicit
-    Error(const std::string& what)
-      : std::runtime_error(what)
-    {
-    }
+    using std::runtime_error::runtime_error;
   };
 
 public:
   CommandBaseHandle(Face& face, RepoStorage& storageHandle,
-                    Scheduler& scheduler, Validator& validator);
+                    Scheduler& scheduler, ndn::security::Validator& validator);
 
   virtual
   ~CommandBaseHandle() = default;
@@ -58,14 +55,14 @@
   bool
   validateParameters(const ndn::mgmt::ControlParameters& parameters)
   {
-    const RepoCommandParameter* castParams =
-      dynamic_cast<const RepoCommandParameter*>(&parameters);
+    const auto* castParams = dynamic_cast<const RepoCommandParameter*>(&parameters);
     BOOST_ASSERT(castParams != nullptr);
+
     T command;
     try {
       command.validateRequest(*castParams);
     }
-    catch (const RepoCommand::ArgumentError& ae) {
+    catch (const RepoCommand::ArgumentError&) {
       return false;
     }
     return true;
@@ -77,8 +74,9 @@
   Scheduler& scheduler;
 
 private:
-  Validator& m_validator;
+  ndn::security::Validator& m_validator;
 };
+
 } // namespace repo
 
-#endif // REPO_HANDLES_COMMAND_BASE_HANDLE_HPP
\ No newline at end of file
+#endif // REPO_HANDLES_COMMAND_BASE_HANDLE_HPP
diff --git a/src/handles/delete-handle.cpp b/src/handles/delete-handle.cpp
index c892583..d9efe76 100644
--- a/src/handles/delete-handle.cpp
+++ b/src/handles/delete-handle.cpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /*
- * Copyright (c) 2014-2018, Regents of the University of California.
+ * Copyright (c) 2014-2022, Regents of the University of California.
  *
  * This file is part of NDN repo-ng (Next generation of NDN repository).
  * See AUTHORS.md for complete list of repo-ng authors and contributors.
@@ -27,7 +27,7 @@
 
 DeleteHandle::DeleteHandle(Face& face, RepoStorage& storageHandle,
                            ndn::mgmt::Dispatcher& dispatcher, Scheduler& scheduler,
-                           Validator& validator)
+                           ndn::security::Validator& validator)
   : CommandBaseHandle(face, storageHandle, scheduler, validator)
 {
   dispatcher.addControlCommand<RepoCommandParameter>(ndn::PartialName("delete"),
@@ -72,7 +72,8 @@
 }
 
 RepoCommandResponse
-DeleteHandle::negativeReply(const Interest& interest, uint64_t statusCode, std::string text) const
+DeleteHandle::negativeReply(const Interest& interest, uint64_t statusCode,
+                            const std::string& text) const
 {
   RepoCommandResponse response(statusCode, text);
   response.setBody(response.wireEncode());
@@ -108,9 +109,9 @@
       nDeletedData++;
     }
   }
+
   //All the data deleted, return 200
   done(positiveReply(interest, parameter, 200, nDeletedData));
-
 }
 
 } // namespace repo
diff --git a/src/handles/delete-handle.hpp b/src/handles/delete-handle.hpp
index ca32709..6bce2c1 100644
--- a/src/handles/delete-handle.hpp
+++ b/src/handles/delete-handle.hpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /*
- * Copyright (c) 2014-2018, Regents of the University of California.
+ * Copyright (c) 2014-2022, Regents of the University of California.
  *
  * This file is part of NDN repo-ng (Next generation of NDN repository).
  * See AUTHORS.md for complete list of repo-ng authors and contributors.
@@ -22,27 +22,20 @@
 
 #include "command-base-handle.hpp"
 
-#include <ndn-cxx/mgmt/dispatcher.hpp>
-
 namespace repo {
 
 class DeleteHandle : public CommandBaseHandle
 {
-
 public:
   class Error : public CommandBaseHandle::Error
   {
   public:
-    explicit
-    Error(const std::string& what)
-      : CommandBaseHandle::Error(what)
-    {
-    }
+    using CommandBaseHandle::Error::Error;
   };
 
-public:
   DeleteHandle(Face& face, RepoStorage& storageHandle,
-               ndn::mgmt::Dispatcher& dispatcher, Scheduler& scheduler, Validator& validator);
+               ndn::mgmt::Dispatcher& dispatcher, Scheduler& scheduler,
+               ndn::security::Validator& validator);
 
 private:
   void
@@ -55,7 +48,7 @@
                 uint64_t statusCode, uint64_t nDeletedData) const;
 
   RepoCommandResponse
-  negativeReply(const Interest& interest, uint64_t statusCode, const std::string text) const;
+  negativeReply(const Interest& interest, uint64_t statusCode, const std::string& text) const;
 
   void
   processSingleDeleteCommand(const Interest& interest, const RepoCommandParameter& parameter,
diff --git a/src/handles/tcp-bulk-insert-handle.hpp b/src/handles/tcp-bulk-insert-handle.hpp
index 49ee8e0..2930985 100644
--- a/src/handles/tcp-bulk-insert-handle.hpp
+++ b/src/handles/tcp-bulk-insert-handle.hpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /*
- * Copyright (c) 2014-2019, Regents of the University of California.
+ * Copyright (c) 2014-2022, Regents of the University of California.
  *
  * This file is part of NDN repo-ng (Next generation of NDN repository).
  * See AUTHORS.md for complete list of repo-ng authors and contributors.
@@ -33,11 +33,7 @@
   class Error : public std::runtime_error
   {
   public:
-    explicit
-    Error(const std::string& what)
-      : std::runtime_error(what)
-    {
-    }
+    using std::runtime_error::runtime_error;
   };
 
 public:
diff --git a/src/handles/write-handle.cpp b/src/handles/write-handle.cpp
index d971878..1e30b5e 100644
--- a/src/handles/write-handle.cpp
+++ b/src/handles/write-handle.cpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /*
- * Copyright (c) 2014-2019, Regents of the University of California.
+ * Copyright (c) 2014-2022, Regents of the University of California.
  *
  * This file is part of NDN repo-ng (Next generation of NDN repository).
  * See AUTHORS.md for complete list of repo-ng authors and contributors.
@@ -26,22 +26,14 @@
 
 NDN_LOG_INIT(repo.WriteHandle);
 
-static const int DEFAULT_CREDIT = 12;
-static const bool DEFAULT_CANBE_PREFIX = false;
-static const milliseconds MAX_TIMEOUT(60_s);
-static const milliseconds NOEND_TIMEOUT(10000_ms);
-static const milliseconds PROCESS_DELETE_TIME(10000_ms);
-static const milliseconds DEFAULT_INTEREST_LIFETIME(4000_ms);
+const int DEFAULT_CREDIT = 12;
+const time::milliseconds NOEND_TIMEOUT = 10_s;
+const time::milliseconds PROCESS_DELETE_TIME = 10_s;
 
 WriteHandle::WriteHandle(Face& face, RepoStorage& storageHandle, ndn::mgmt::Dispatcher& dispatcher,
-                         Scheduler& scheduler, Validator& validator)
+                         Scheduler& scheduler, ndn::security::Validator& validator)
   : CommandBaseHandle(face, storageHandle, scheduler, validator)
   , m_validator(validator)
-  , m_credit(DEFAULT_CREDIT)
-  , m_canBePrefix(DEFAULT_CANBE_PREFIX)
-  , m_maxTimeout(MAX_TIMEOUT)
-  , m_noEndTimeout(NOEND_TIMEOUT)
-  , m_interestLifetime(DEFAULT_INTEREST_LIFETIME)
 {
   dispatcher.addControlCommand<RepoCommandParameter>(ndn::PartialName("insert"),
     makeAuthorization(),
@@ -62,20 +54,20 @@
 
 void
 WriteHandle::handleInsertCommand(const Name& prefix, const Interest& interest,
-                                 const ndn::mgmt::ControlParameters& parameter,
+                                 const ndn::mgmt::ControlParameters& params,
                                  const ndn::mgmt::CommandContinuation& done)
 {
-  RepoCommandParameter* repoParameter =
-    dynamic_cast<RepoCommandParameter*>(const_cast<ndn::mgmt::ControlParameters*>(&parameter));
+  auto& repoParam = dynamic_cast<RepoCommandParameter&>(const_cast<ndn::mgmt::ControlParameters&>(params));
 
-  if (repoParameter->hasStartBlockId() || repoParameter->hasEndBlockId()) {
-    processSegmentedInsertCommand(interest, *repoParameter, done);
+  if (repoParam.hasStartBlockId() || repoParam.hasEndBlockId()) {
+    processSegmentedInsertCommand(interest, repoParam, done);
   }
   else {
-    processSingleInsertCommand(interest, *repoParameter, done);
+    processSingleInsertCommand(interest, repoParam, done);
   }
-  if (repoParameter->hasInterestLifetime())
-    m_interestLifetime = repoParameter->getInterestLifetime();
+  if (repoParam.hasInterestLifetime()) {
+    m_interestLifetime = repoParam.getInterestLifetime();
+  }
 }
 
 void
@@ -83,7 +75,7 @@
 {
   m_validator.validate(data,
                        std::bind(&WriteHandle::onDataValidated, this, interest, _1, processId),
-                       [](const Data& data, const ValidationError& error){NDN_LOG_ERROR("Error: " << error);});
+                       [] (auto&&, const auto& error) { NDN_LOG_ERROR("Error: " << error); });
 }
 
 void
@@ -128,7 +120,6 @@
 
   response.setCode(300);
   Interest fetchInterest(parameter.getName());
-  fetchInterest.setCanBePrefix(m_canBePrefix);
   fetchInterest.setInterestLifetime(m_interestLifetime);
   face.expressInterest(fetchInterest,
                        std::bind(&WriteHandle::onData, this, _1, _2, processId),
@@ -144,34 +135,32 @@
   Name name = parameter.getName();
   SegmentNo startBlockId = parameter.getStartBlockId();
 
-  uint64_t initialCredit = m_credit;
-
+  int initialCredit = DEFAULT_CREDIT;
   if (parameter.hasEndBlockId()) {
-    initialCredit =
-      std::min(initialCredit, parameter.getEndBlockId() - parameter.getStartBlockId() + 1);
+    initialCredit = std::min<int>(initialCredit, parameter.getEndBlockId() - parameter.getStartBlockId() + 1);
   }
   else {
     // set noEndTimeout timer
-    process.noEndTime = ndn::time::steady_clock::now() +
-                        m_noEndTimeout;
+    process.noEndTime = time::steady_clock::now() + NOEND_TIMEOUT;
   }
 
   Name fetchName = name;
-  SegmentNo segment = startBlockId;
-  fetchName.appendSegment(segment);
+  fetchName.appendSegment(startBlockId);
   Interest interest(fetchName);
 
   ndn::util::SegmentFetcher::Options options;
   options.initCwnd = initialCredit;
   options.interestLifetime = m_interestLifetime;
-  options.maxTimeout = m_maxTimeout;
   auto fetcher = ndn::util::SegmentFetcher::start(face, interest, m_validator, options);
-  fetcher->onError.connect([] (uint32_t errorCode, const std::string& errorMsg)
-                           {NDN_LOG_ERROR("Error: " << errorMsg);});
-  fetcher->afterSegmentValidated.connect([this, &fetcher, &processId] (const Data& data)
-                                         {onSegmentData(*fetcher, data, processId);});
-  fetcher->afterSegmentTimedOut.connect([this, &fetcher, &processId] ()
-                                        {onSegmentTimeout(*fetcher, processId);});
+  fetcher->onError.connect([] (uint32_t, const auto& errorMsg) {
+    NDN_LOG_ERROR("Error: " << errorMsg);
+  });
+  fetcher->afterSegmentValidated.connect([this, &fetcher, &processId] (const Data& data) {
+    onSegmentData(*fetcher, data, processId);
+  });
+  fetcher->afterSegmentTimedOut.connect([this, &fetcher, &processId] {
+    onSegmentTimeout(*fetcher, processId);
+  });
 }
 
 void
@@ -193,9 +182,8 @@
   ProcessInfo& process = m_processes[processId];
   //read whether notime timeout
   if (!response.hasEndBlockId()) {
-
-    ndn::time::steady_clock::TimePoint& noEndTime = process.noEndTime;
-    ndn::time::steady_clock::TimePoint now = ndn::time::steady_clock::now();
+    auto noEndTime = process.noEndTime;
+    auto now = time::steady_clock::now();
 
     if (now > noEndTime) {
       NDN_LOG_DEBUG("noEndtimeout: " << processId);
@@ -281,10 +269,10 @@
 
 void
 WriteHandle::handleCheckCommand(const Name& prefix, const Interest& interest,
-                                const ndn::mgmt::ControlParameters& parameter,
+                                const ndn::mgmt::ControlParameters& params,
                                 const ndn::mgmt::CommandContinuation& done)
 {
-  const RepoCommandParameter& repoParameter = dynamic_cast<const RepoCommandParameter&>(parameter);
+  const auto& repoParameter = dynamic_cast<const RepoCommandParameter&>(params);
 
   //check whether this process exists
   ProcessId processId = repoParameter.getProcessId();
@@ -324,8 +312,8 @@
 void
 WriteHandle::extendNoEndTime(ProcessInfo& process)
 {
-  auto& noEndTime = process.noEndTime;
-  auto now = ndn::time::steady_clock::now();
+  auto noEndTime = process.noEndTime;
+  auto now = time::steady_clock::now();
   RepoCommandResponse& response = process.response;
   if (now > noEndTime) {
     response.setCode(405);
@@ -333,7 +321,7 @@
   }
 
   //extends noEndTime
-  process.noEndTime = ndn::time::steady_clock::now() + m_noEndTimeout;
+  process.noEndTime = time::steady_clock::now() + NOEND_TIMEOUT;
 }
 
 RepoCommandResponse
@@ -341,7 +329,6 @@
 {
   RepoCommandResponse response(statusCode, text);
   response.setBody(response.wireEncode());
-
   return response;
 }
 
diff --git a/src/handles/write-handle.hpp b/src/handles/write-handle.hpp
index 115801e..04ed3f5 100644
--- a/src/handles/write-handle.hpp
+++ b/src/handles/write-handle.hpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /*
- * Copyright (c) 2014-2018, Regents of the University of California.
+ * Copyright (c) 2014-2022, Regents of the University of California.
  *
  * This file is part of NDN repo-ng (Next generation of NDN repository).
  * See AUTHORS.md for complete list of repo-ng authors and contributors.
@@ -22,7 +22,6 @@
 
 #include "command-base-handle.hpp"
 
-#include <ndn-cxx/mgmt/dispatcher.hpp>
 #include <ndn-cxx/util/segment-fetcher.hpp>
 
 #include <queue>
@@ -52,23 +51,16 @@
  */
 class WriteHandle : public CommandBaseHandle
 {
-
 public:
   class Error : public CommandBaseHandle::Error
   {
   public:
-    explicit
-    Error(const std::string& what)
-      : CommandBaseHandle::Error(what)
-    {
-    }
+    using CommandBaseHandle::Error::Error;
   };
 
-
-public:
   WriteHandle(Face& face, RepoStorage& storageHandle,
               ndn::mgmt::Dispatcher& dispatcher, Scheduler& scheduler,
-              Validator& validator);
+              ndn::security::Validator& validator);
 
 private:
   /**
@@ -92,7 +84,7 @@
      * It is initialized to now()+noEndTimeout when segmented fetch process begins,
      * and reset to now()+noEndTimeout each time an insert status check command is processed.
      */
-    ndn::time::steady_clock::TimePoint noEndTime;
+    time::steady_clock::time_point noEndTime;
   };
 
 private: // insert command
@@ -105,7 +97,7 @@
                       const ndn::mgmt::CommandContinuation& done);
 
   void
-  onValidationFailed(const Interest& interest, const ValidationError& error);
+  onValidationFailed(const Interest& interest, const ndn::security::ValidationError& error);
 
 private: // single data fetching
   /**
@@ -171,7 +163,7 @@
                      const ndn::mgmt::CommandContinuation& done);
 
   void
-  onCheckValidationFailed(const Interest& interest, const ValidationError& error);
+  onCheckValidationFailed(const Interest& interest, const ndn::security::ValidationError& error);
 
 private:
   void
@@ -187,15 +179,9 @@
   negativeReply(std::string text, int statusCode);
 
 private:
-  Validator& m_validator;
-
+  ndn::security::Validator& m_validator;
   std::map<ProcessId, ProcessInfo> m_processes;
-
-  int m_credit;
-  bool m_canBePrefix;
-  ndn::time::milliseconds m_maxTimeout;
-  ndn::time::milliseconds m_noEndTimeout;
-  ndn::time::milliseconds m_interestLifetime;
+  time::milliseconds m_interestLifetime = ndn::DEFAULT_INTEREST_LIFETIME;
 };
 
 } // namespace repo