chunks+poke+ping: use PendingInterestHandle and RegisteredPrefixHandle

refs #4316, #3919

Change-Id: Ia95362067a9024964cf67543c76375144b8a2782
diff --git a/tools/chunks/catchunks/data-fetcher.cpp b/tools/chunks/catchunks/data-fetcher.cpp
index 313334f..7600016 100644
--- a/tools/chunks/catchunks/data-fetcher.cpp
+++ b/tools/chunks/catchunks/data-fetcher.cpp
@@ -1,8 +1,8 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
-/**
- * Copyright (c) 2016,  Regents of the University of California,
- *                      Colorado State University,
- *                      University Pierre & Marie Curie, Sorbonne University.
+/*
+ * Copyright (c) 2016-2019, Regents of the University of California,
+ *                          Colorado State University,
+ *                          University Pierre & Marie Curie, Sorbonne University.
  *
  * This file is part of ndn-tools (Named Data Networking Essential Tools).
  * See AUTHORS.md for complete list of ndn-tools authors and contributors.
@@ -75,7 +75,7 @@
 {
   if (isRunning()) {
     m_isStopped = true;
-    m_face.removePendingInterest(m_interestId);
+    m_pendingInterest.cancel();
     m_scheduler.cancelAllEvents();
   }
 }
@@ -84,10 +84,10 @@
 DataFetcher::expressInterest(const Interest& interest, const shared_ptr<DataFetcher>& self)
 {
   m_nCongestionRetries = 0;
-  m_interestId = m_face.expressInterest(interest,
-                                        bind(&DataFetcher::handleData, this, _1, _2, self),
-                                        bind(&DataFetcher::handleNack, this, _1, _2, self),
-                                        bind(&DataFetcher::handleTimeout, this, _1, self));
+  m_pendingInterest = m_face.expressInterest(interest,
+                                             bind(&DataFetcher::handleData, this, _1, _2, self),
+                                             bind(&DataFetcher::handleNack, this, _1, _2, self),
+                                             bind(&DataFetcher::handleTimeout, this, _1, self));
 }
 
 void
diff --git a/tools/chunks/catchunks/data-fetcher.hpp b/tools/chunks/catchunks/data-fetcher.hpp
index 5b66450..53e32c0 100644
--- a/tools/chunks/catchunks/data-fetcher.hpp
+++ b/tools/chunks/catchunks/data-fetcher.hpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /*
- * Copyright (c) 2016-2018, Regents of the University of California,
+ * Copyright (c) 2016-2019, Regents of the University of California,
  *                          Colorado State University,
  *                          University Pierre & Marie Curie, Sorbonne University.
  *
@@ -110,7 +110,7 @@
 private:
   Face& m_face;
   Scheduler m_scheduler;
-  const PendingInterestId* m_interestId;
+  PendingInterestHandle m_pendingInterest;
   DataCallback m_onData;
   FailureCallback m_onNack;
   FailureCallback m_onTimeout;
diff --git a/tools/chunks/catchunks/pipeline-interests-aimd.cpp b/tools/chunks/catchunks/pipeline-interests-aimd.cpp
index 73e22c5..eea122e 100644
--- a/tools/chunks/catchunks/pipeline-interests-aimd.cpp
+++ b/tools/chunks/catchunks/pipeline-interests-aimd.cpp
@@ -86,9 +86,6 @@
 void
 PipelineInterestsAimd::doCancel()
 {
-  for (const auto& entry : m_segmentInfo) {
-    m_face.removePendingInterest(entry.second.interestId);
-  }
   m_checkRtoEvent.cancel();
   m_segmentInfo.clear();
 }
@@ -155,8 +152,6 @@
                   << " is " << m_retxCount[segNo] << std::endl;
       }
     }
-
-    m_face.removePendingInterest(m_segmentInfo[segNo].interestId);
   }
 
   Interest interest(Name(m_prefix).appendSegment(segNo));
@@ -164,26 +159,24 @@
   interest.setMustBeFresh(m_options.mustBeFresh);
   interest.setMaxSuffixComponents(1);
 
-  auto interestId = m_face.expressInterest(interest,
-                                           bind(&PipelineInterestsAimd::handleData, this, _1, _2),
-                                           bind(&PipelineInterestsAimd::handleNack, this, _1, _2),
-                                           bind(&PipelineInterestsAimd::handleLifetimeExpiration, this, _1));
+  SegmentInfo& segInfo = m_segmentInfo[segNo];
+  segInfo.interestHdl = m_face.expressInterest(interest,
+                                               bind(&PipelineInterestsAimd::handleData, this, _1, _2),
+                                               bind(&PipelineInterestsAimd::handleNack, this, _1, _2),
+                                               bind(&PipelineInterestsAimd::handleLifetimeExpiration, this, _1));
+  segInfo.timeSent = time::steady_clock::now();
+  segInfo.rto = m_rttEstimator.getEstimatedRto();
+
   m_nInFlight++;
   m_nSent++;
 
   if (isRetransmission) {
-    SegmentInfo& segInfo = m_segmentInfo[segNo];
-    segInfo.timeSent = time::steady_clock::now();
-    segInfo.rto = m_rttEstimator.getEstimatedRto();
     segInfo.state = SegmentState::Retransmitted;
     m_nRetransmitted++;
   }
   else {
     m_highInterest = segNo;
-    m_segmentInfo[segNo] = {interestId,
-                            time::steady_clock::now(),
-                            m_rttEstimator.getEstimatedRto(),
-                            SegmentState::FirstTimeSent};
+    segInfo.state = SegmentState::FirstTimeSent;
   }
 }
 
@@ -427,7 +420,6 @@
   for (auto it = m_segmentInfo.begin(); it != m_segmentInfo.end();) {
     // cancel fetching all segments that follow
     if (it->first > segNo) {
-      m_face.removePendingInterest(it->second.interestId);
       it = m_segmentInfo.erase(it);
       m_nInFlight--;
     }
diff --git a/tools/chunks/catchunks/pipeline-interests-aimd.hpp b/tools/chunks/catchunks/pipeline-interests-aimd.hpp
index aea2d19..12edb95 100644
--- a/tools/chunks/catchunks/pipeline-interests-aimd.hpp
+++ b/tools/chunks/catchunks/pipeline-interests-aimd.hpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /*
- * Copyright (c) 2016-2018, Regents of the University of California,
+ * Copyright (c) 2016-2019, Regents of the University of California,
  *                          Colorado State University,
  *                          University Pierre & Marie Curie, Sorbonne University.
  *
@@ -76,7 +76,7 @@
  */
 struct SegmentInfo
 {
-  const PendingInterestId* interestId; ///< pending interest ID returned by ndn::Face::expressInterest
+  ScopedPendingInterestHandle interestHdl;
   time::steady_clock::TimePoint timeSent;
   Milliseconds rto;
   SegmentState state;
diff --git a/tools/peek/ndnpoke/ndnpoke.cpp b/tools/peek/ndnpoke/ndnpoke.cpp
index 1bda0de..40ec247 100644
--- a/tools/peek/ndnpoke/ndnpoke.cpp
+++ b/tools/peek/ndnpoke/ndnpoke.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-2019,  Regents of the University of California,
  *                           Arizona Board of Regents,
  *                           Colorado State University,
  *                           University Pierre & Marie Curie, Sorbonne University,
@@ -93,7 +93,7 @@
     std::cerr << "Data exceeded maximum packet size" << std::endl;
   }
 
-  m_face.unsetInterestFilter(m_registeredPrefix);
+  m_registeredPrefix.cancel();
 }
 
 void
diff --git a/tools/peek/ndnpoke/ndnpoke.hpp b/tools/peek/ndnpoke/ndnpoke.hpp
index f297f9d..fdad5bb 100644
--- a/tools/peek/ndnpoke/ndnpoke.hpp
+++ b/tools/peek/ndnpoke/ndnpoke.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-2019,  Regents of the University of California,
  *                           Arizona Board of Regents,
  *                           Colorado State University,
  *                           University Pierre & Marie Curie, Sorbonne University,
@@ -75,7 +75,7 @@
   std::istream& m_inStream;
   const PokeOptions& m_options;
 
-  const RegisteredPrefixId* m_registeredPrefix;
+  RegisteredPrefixHandle m_registeredPrefix;
   bool m_wasDataSent;
 };
 
diff --git a/tools/ping/server/ping-server.cpp b/tools/ping/server/ping-server.cpp
index 9b1403d..4162b05 100644
--- a/tools/ping/server/ping-server.cpp
+++ b/tools/ping/server/ping-server.cpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /*
- * Copyright (c) 2015-2018,  Arizona Board of Regents.
+ * Copyright (c) 2015-2019,  Arizona Board of Regents.
  *
  * This file is part of ndn-tools (Named Data Networking Essential Tools).
  * See AUTHORS.md for complete list of ndn-tools authors and contributors.
@@ -33,7 +33,6 @@
   , m_face(face)
   , m_keyChain(keyChain)
   , m_nPings(0)
-  , m_regPrefixId(nullptr)
 {
   auto b = make_shared<Buffer>();
   b->assign(m_options.payloadSize, 'a');
@@ -43,20 +42,18 @@
 void
 PingServer::start()
 {
-  m_regPrefixId = m_face.setInterestFilter(
-                    Name(m_options.prefix).append("ping"),
-                    bind(&PingServer::onInterest, this, _2),
-                    [] (const auto&, const auto& reason) {
-                      BOOST_THROW_EXCEPTION(std::runtime_error("Failed to register prefix: " + reason));
-                    });
+  m_registeredPrefix = m_face.setInterestFilter(
+                       Name(m_options.prefix).append("ping"),
+                       bind(&PingServer::onInterest, this, _2),
+                       [] (const auto&, const auto& reason) {
+                         BOOST_THROW_EXCEPTION(std::runtime_error("Failed to register prefix: " + reason));
+                       });
 }
 
 void
 PingServer::stop()
 {
-  if (m_regPrefixId != nullptr) {
-    m_face.unsetInterestFilter(m_regPrefixId);
-  }
+  m_registeredPrefix.cancel();
 }
 
 size_t
diff --git a/tools/ping/server/ping-server.hpp b/tools/ping/server/ping-server.hpp
index 3a43a6d..46df6f9 100644
--- a/tools/ping/server/ping-server.hpp
+++ b/tools/ping/server/ping-server.hpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /*
- * Copyright (c) 2015-2018,  Arizona Board of Regents.
+ * Copyright (c) 2015-2019,  Arizona Board of Regents.
  *
  * This file is part of ndn-tools (Named Data Networking Essential Tools).
  * See AUTHORS.md for complete list of ndn-tools authors and contributors.
@@ -105,7 +105,7 @@
   KeyChain& m_keyChain;
   size_t m_nPings;
   Block m_payload;
-  const RegisteredPrefixId* m_regPrefixId;
+  RegisteredPrefixHandle m_registeredPrefix;
 };
 
 } // namespace server