Migrate to C++17 and misc code cleanups

Change-Id: I6b63385c92361a7ef5803d2bfd00f39c77e88d34
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;
 }