peek: code modernization and cleanup

Change-Id: I42d706aa02ab03557d708c729f65716c1c2d3494
diff --git a/tools/peek/ndnpeek/ndnpeek.cpp b/tools/peek/ndnpeek/ndnpeek.cpp
index c371882..8ce23f6 100644
--- a/tools/peek/ndnpeek/ndnpeek.cpp
+++ b/tools/peek/ndnpeek/ndnpeek.cpp
@@ -32,37 +32,28 @@
 namespace peek {
 
 NdnPeek::NdnPeek(Face& face, const PeekOptions& options)
-  : m_face(face)
-  , m_options(options)
-  , m_timeout(options.timeout)
-  , m_resultCode(ResultCode::TIMEOUT)
+  : m_options(options)
+  , m_face(face)
+  , m_scheduler(m_face.getIoService())
 {
-  if (m_timeout < 0_ms) {
-    m_timeout = m_options.interestLifetime < 0_ms ?
-                DEFAULT_INTEREST_LIFETIME : m_options.interestLifetime;
-  }
-}
-
-time::milliseconds
-NdnPeek::getTimeout() const
-{
-  return m_timeout;
-}
-
-ResultCode
-NdnPeek::getResultCode() const
-{
-  return m_resultCode;
 }
 
 void
 NdnPeek::start()
 {
-  m_face.expressInterest(createInterest(),
-                         bind(&NdnPeek::onData, this, _2),
-                         bind(&NdnPeek::onNack, this, _2),
-                         nullptr);
-  m_expressInterestTime = time::steady_clock::now();
+  m_pendingInterest = m_face.expressInterest(createInterest(),
+                                             [this] (auto&&, const auto& data) { this->onData(data); },
+                                             [this] (auto&&, const auto& nack) { this->onNack(nack); },
+                                             [this] (auto&&) { this->onTimeout(); });
+
+  if (m_options.timeout) {
+    m_timeoutEvent = m_scheduler.schedule(*m_options.timeout, [this] {
+      m_pendingInterest.cancel();
+      onTimeout();
+    });
+  }
+
+  m_sendTime = time::steady_clock::now();
 }
 
 Interest
@@ -71,11 +62,11 @@
   Interest interest(m_options.name);
   interest.setCanBePrefix(m_options.canBePrefix);
   interest.setMustBeFresh(m_options.mustBeFresh);
-  if (m_options.link != nullptr) {
+  if (m_options.link) {
     interest.setForwardingHint(m_options.link->getDelegationList());
   }
-  if (m_options.interestLifetime >= 0_ms) {
-    interest.setInterestLifetime(m_options.interestLifetime);
+  if (m_options.interestLifetime) {
+    interest.setInterestLifetime(*m_options.interestLifetime);
   }
 
   if (m_options.isVerbose) {
@@ -89,10 +80,11 @@
 NdnPeek::onData(const Data& data)
 {
   m_resultCode = ResultCode::DATA;
+  m_timeoutEvent.cancel();
 
   if (m_options.isVerbose) {
     std::cerr << "DATA: " << data.getName() << "\nRTT: "
-              << time::duration_cast<time::milliseconds>(time::steady_clock::now() - m_expressInterestTime).count()
+              << time::duration_cast<time::milliseconds>(time::steady_clock::now() - m_sendTime).count()
               << " ms" << std::endl;
   }
 
@@ -110,11 +102,12 @@
 NdnPeek::onNack(const lp::Nack& nack)
 {
   m_resultCode = ResultCode::NACK;
-  lp::NackHeader header = nack.getHeader();
+  m_timeoutEvent.cancel();
 
+  lp::NackHeader header = nack.getHeader();
   if (m_options.isVerbose) {
     std::cerr << "NACK: " << header.getReason() << "\nRTT: "
-              << time::duration_cast<time::milliseconds>(time::steady_clock::now() - m_expressInterestTime).count()
+              << time::duration_cast<time::milliseconds>(time::steady_clock::now() - m_sendTime).count()
               << " ms" << std::endl;
   }
 
@@ -127,5 +120,16 @@
   }
 }
 
+void
+NdnPeek::onTimeout()
+{
+  m_resultCode = ResultCode::TIMEOUT;
+  m_timeoutEvent.cancel();
+
+  if (m_options.isVerbose) {
+    std::cerr << "TIMEOUT" << std::endl;
+  }
+}
+
 } // namespace peek
 } // namespace ndn