tools: Simplify nfd-status using SegmentFetcher

refs #2456

Change-Id: I5beb402b211717816ae11d3474b41ade15c7418c
diff --git a/tools/nfd-status.cpp b/tools/nfd-status.cpp
index 9824d64..a4b6e1a 100644
--- a/tools/nfd-status.cpp
+++ b/tools/nfd-status.cpp
@@ -38,12 +38,15 @@
 #include <ndn-cxx/management/nfd-fib-entry.hpp>
 #include <ndn-cxx/management/nfd-rib-entry.hpp>
 #include <ndn-cxx/management/nfd-strategy-choice.hpp>
+#include <ndn-cxx/util/segment-fetcher.hpp>
 
 #include <boost/algorithm/string/replace.hpp>
 #include <list>
 
 namespace ndn {
 
+using util::SegmentFetcher;
+
 class NfdStatus
 {
 public:
@@ -132,26 +135,13 @@
     runNextStep();
   }
 
+
   void
-  fetchSegments(const Data& data, void (NfdStatus::*onDone)())
+  onErrorFetch(uint32_t errorCode, const std::string& errorMsg)
   {
-    m_buffer->write((const char*)data.getContent().value(),
-                    data.getContent().value_size());
+    std::cerr << "Error code:" << errorCode << ", message:" << errorMsg << std::endl;
 
-    uint64_t currentSegment = data.getName().get(-1).toSegment();
-
-    const name::Component& finalBlockId = data.getMetaInfo().getFinalBlockId();
-    if (finalBlockId.empty() ||
-        finalBlockId.toSegment() > currentSegment)
-      {
-        m_face.expressInterest(data.getName().getPrefix(-1).appendSegment(currentSegment+1),
-                               bind(&NfdStatus::fetchSegments, this, _2, onDone),
-                               bind(&NfdStatus::onTimeout, this));
-      }
-    else
-      {
-        return (this->*onDone)();
-      }
+    runNextStep();
   }
 
   void
@@ -277,24 +267,23 @@
     interest.setChildSelector(1);
     interest.setMustBeFresh(true);
 
-    m_face.expressInterest(interest,
-                           bind(&NfdStatus::fetchSegments, this, _2,
-                                &NfdStatus::afterFetchedChannelStatusInformation),
-                           bind(&NfdStatus::onTimeout, this));
+    SegmentFetcher::fetch(m_face, interest,
+                          util::DontVerifySegment(),
+                          bind(&NfdStatus::afterFetchedChannelStatusInformation, this, _1),
+                          bind(&NfdStatus::onErrorFetch, this, _1, _2));
   }
 
   void
-  afterFetchedChannelStatusInformation()
+  afterFetchedChannelStatusInformation(const ConstBufferPtr& dataset)
   {
-    ConstBufferPtr buf = m_buffer->buf();
     if (m_isOutputXml) {
       std::cout << "<channels>";
 
       size_t offset = 0;
-      while (offset < buf->size()) {
+      while (offset < dataset->size()) {
         bool isOk = false;
         Block block;
-        std::tie(isOk, block) = Block::fromBuffer(buf, offset);
+        std::tie(isOk, block) = Block::fromBuffer(dataset, offset);
         if (!isOk) {
           std::cerr << "ERROR: cannot decode ChannelStatus TLV" << std::endl;
           break;
@@ -317,10 +306,10 @@
       std::cout << "Channels:" << std::endl;
 
       size_t offset = 0;
-      while (offset < buf->size()) {
+      while (offset < dataset->size()) {
         bool isOk = false;
         Block block;
-        std::tie(isOk, block) = Block::fromBuffer(buf, offset);
+        std::tie(isOk, block) = Block::fromBuffer(dataset, offset);
         if (!isOk) {
           std::cerr << "ERROR: cannot decode ChannelStatus TLV" << std::endl;
           break;
@@ -348,24 +337,23 @@
     interest.setChildSelector(1);
     interest.setMustBeFresh(true);
 
-    m_face.expressInterest(interest,
-                           bind(&NfdStatus::fetchSegments, this, _2,
-                                &NfdStatus::afterFetchedFaceStatusInformation),
-                           bind(&NfdStatus::onTimeout, this));
+    SegmentFetcher::fetch(m_face, interest,
+                          util::DontVerifySegment(),
+                          bind(&NfdStatus::afterFetchedFaceStatusInformation, this, _1),
+                          bind(&NfdStatus::onErrorFetch, this, _1, _2));
   }
 
   void
-  afterFetchedFaceStatusInformation()
+  afterFetchedFaceStatusInformation(const ConstBufferPtr& dataset)
   {
-    ConstBufferPtr buf = m_buffer->buf();
     if (m_isOutputXml) {
       std::cout << "<faces>";
 
       size_t offset = 0;
-      while (offset < buf->size()) {
+      while (offset < dataset->size()) {
         bool isOk = false;
         Block block;
-        std::tie(isOk, block) = Block::fromBuffer(buf, offset);
+        std::tie(isOk, block) = Block::fromBuffer(dataset, offset);
         if (!isOk) {
           std::cerr << "ERROR: cannot decode FaceStatus TLV" << std::endl;
           break;
@@ -430,10 +418,10 @@
       std::cout << "Faces:" << std::endl;
 
       size_t offset = 0;
-      while (offset < buf->size()) {
+      while (offset < dataset->size()) {
         bool isOk = false;
         Block block;
-        std::tie(isOk, block) = Block::fromBuffer(buf, offset);
+        std::tie(isOk, block) = Block::fromBuffer(dataset, offset);
         if (!isOk) {
           std::cerr << "ERROR: cannot decode FaceStatus TLV" << std::endl;
           break;
@@ -480,24 +468,24 @@
     Interest interest("/localhost/nfd/fib/list");
     interest.setChildSelector(1);
     interest.setMustBeFresh(true);
-    m_face.expressInterest(interest,
-                           bind(&NfdStatus::fetchSegments, this, _2,
-                                &NfdStatus::afterFetchedFibEnumerationInformation),
-                           bind(&NfdStatus::onTimeout, this));
+
+    SegmentFetcher::fetch(m_face, interest,
+                          util::DontVerifySegment(),
+                          bind(&NfdStatus::afterFetchedFibEnumerationInformation, this, _1),
+                          bind(&NfdStatus::onErrorFetch, this, _1, _2));
   }
 
   void
-  afterFetchedFibEnumerationInformation()
+  afterFetchedFibEnumerationInformation(const ConstBufferPtr& dataset)
   {
-    ConstBufferPtr buf = m_buffer->buf();
     if (m_isOutputXml) {
       std::cout << "<fib>";
 
       size_t offset = 0;
-      while (offset < buf->size()) {
+      while (offset < dataset->size()) {
         bool isOk = false;
         Block block;
-        std::tie(isOk, block) = Block::fromBuffer(buf, offset);
+        std::tie(isOk, block) = Block::fromBuffer(dataset, offset);
         if (!isOk) {
           std::cerr << "ERROR: cannot decode FibEntry TLV";
           break;
@@ -527,10 +515,10 @@
       std::cout << "FIB:" << std::endl;
 
       size_t offset = 0;
-      while (offset < buf->size()) {
+      while (offset < dataset->size()) {
         bool isOk = false;
         Block block;
-        std::tie(isOk, block) = Block::fromBuffer(buf, offset);
+        std::tie(isOk, block) = Block::fromBuffer(dataset, offset);
         if (!isOk) {
           std::cerr << "ERROR: cannot decode FibEntry TLV" << std::endl;
           break;
@@ -568,24 +556,24 @@
     Interest interest("/localhost/nfd/strategy-choice/list");
     interest.setChildSelector(1);
     interest.setMustBeFresh(true);
-    m_face.expressInterest(interest,
-                           bind(&NfdStatus::fetchSegments, this, _2,
-                                &NfdStatus::afterFetchedStrategyChoiceInformationInformation),
-                           bind(&NfdStatus::onTimeout, this));
+
+    SegmentFetcher::fetch(m_face, interest,
+                          util::DontVerifySegment(),
+                          bind(&NfdStatus::afterFetchedStrategyChoiceInformationInformation, this, _1),
+                          bind(&NfdStatus::onErrorFetch, this, _1, _2));
   }
 
   void
-  afterFetchedStrategyChoiceInformationInformation()
+  afterFetchedStrategyChoiceInformationInformation(const ConstBufferPtr& dataset)
   {
-    ConstBufferPtr buf = m_buffer->buf();
     if (m_isOutputXml) {
       std::cout << "<strategyChoices>";
 
       size_t offset = 0;
-      while (offset < buf->size()) {
+      while (offset < dataset->size()) {
         bool isOk = false;
         Block block;
-        std::tie(isOk, block) = Block::fromBuffer(buf, offset);
+        std::tie(isOk, block) = Block::fromBuffer(dataset, offset);
         if (!isOk) {
           std::cerr << "ERROR: cannot decode StrategyChoice TLV";
           break;
@@ -615,10 +603,10 @@
       std::cout << "Strategy choices:" << std::endl;
 
       size_t offset = 0;
-      while (offset < buf->size()) {
+      while (offset < dataset->size()) {
         bool isOk = false;
         Block block;
-        std::tie(isOk, block) = Block::fromBuffer(buf, offset);
+        std::tie(isOk, block) = Block::fromBuffer(dataset, offset);
         if (!isOk) {
           std::cerr << "ERROR: cannot decode StrategyChoice TLV" << std::endl;
           break;
@@ -644,24 +632,23 @@
     interest.setChildSelector(1);
     interest.setMustBeFresh(true);
 
-    m_face.expressInterest(interest,
-                           bind(&NfdStatus::fetchSegments, this, _2,
-                                &NfdStatus::afterFetchedRibStatusInformation),
-                           bind(&NfdStatus::onTimeout, this));
+    SegmentFetcher::fetch(m_face, interest,
+                          util::DontVerifySegment(),
+                          bind(&NfdStatus::afterFetchedRibStatusInformation, this, _1),
+                          bind(&NfdStatus::onErrorFetch, this, _1, _2));
   }
 
   void
-  afterFetchedRibStatusInformation()
+  afterFetchedRibStatusInformation(const ConstBufferPtr& dataset)
   {
-    ConstBufferPtr buf = m_buffer->buf();
     if (m_isOutputXml) {
       std::cout << "<rib>";
 
       size_t offset = 0;
-      while (offset < buf->size()) {
+      while (offset < dataset->size()) {
         bool isOk = false;
         Block block;
-        std::tie(isOk, block) = Block::fromBuffer(buf, offset);
+        std::tie(isOk, block) = Block::fromBuffer(dataset, offset);
         if (!isOk) {
           std::cerr << "ERROR: cannot decode RibEntry TLV";
           break;
@@ -706,10 +693,10 @@
       std::cout << "RIB:" << std::endl;
 
       size_t offset = 0;
-      while (offset < buf->size()) {
+      while (offset < dataset->size()) {
         bool isOk = false;
         Block block;
-        std::tie(isOk, block) = Block::fromBuffer(buf, offset);
+        std::tie(isOk, block) = Block::fromBuffer(dataset, offset);
         if (!isOk) {
           std::cerr << "ERROR: cannot decode RibEntry TLV" << std::endl;
           break;