util: Implement helper class to fetch multi-segmented data

Change-Id: I03137869a21a194fa88ae7cae03ef402294771db
Refs: #1879
diff --git a/src/util/segment-fetcher.cpp b/src/util/segment-fetcher.cpp
new file mode 100644
index 0000000..e7703c5
--- /dev/null
+++ b/src/util/segment-fetcher.cpp
@@ -0,0 +1,119 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2013-2014 Regents of the University of California.
+ *
+ * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
+ *
+ * ndn-cxx library is free software: you can redistribute it and/or modify it under the
+ * terms of the GNU Lesser General Public License as published by the Free Software
+ * Foundation, either version 3 of the License, or (at your option) any later version.
+ *
+ * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+ * PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more details.
+ *
+ * You should have received copies of the GNU General Public License and GNU Lesser
+ * General Public License along with ndn-cxx, e.g., in COPYING.md file.  If not, see
+ * <http://www.gnu.org/licenses/>.
+ *
+ * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
+ */
+
+#include "segment-fetcher.hpp"
+
+#include "../encoding/buffer-stream.hpp"
+
+namespace ndn {
+namespace util {
+
+SegmentFetcher::SegmentFetcher(Face& face,
+                               const VerifySegment& verifySegment,
+                               const CompleteCallback& completeCallback,
+                               const ErrorCallback& errorCallback)
+  : m_face(face)
+  , m_verifySegment(verifySegment)
+  , m_completeCallback(completeCallback)
+  , m_errorCallback(errorCallback)
+  , m_buffer(make_shared<OBufferStream>())
+{
+}
+
+void
+SegmentFetcher::fetch(Face& face,
+                      const Interest& baseInterest,
+                      const VerifySegment& verifySegment,
+                      const CompleteCallback& completeCallback,
+                      const ErrorCallback& errorCallback)
+{
+  shared_ptr<SegmentFetcher> fetcher =
+    shared_ptr<SegmentFetcher>(new SegmentFetcher(face, verifySegment,
+                                                  completeCallback, errorCallback));
+
+  fetcher->fetchFirstSegment(baseInterest, fetcher);
+}
+
+void
+SegmentFetcher::fetchFirstSegment(const Interest& baseInterest,
+                                  const shared_ptr<SegmentFetcher>& self)
+{
+  Interest interest(baseInterest);
+  interest.setChildSelector(1);
+  interest.setMustBeFresh(true);
+
+  m_face.expressInterest(interest,
+                         bind(&SegmentFetcher::onSegmentReceived, this, _1, _2, true, self),
+                         bind(m_errorCallback, INTEREST_TIMEOUT, "Timeout"));
+}
+
+void
+SegmentFetcher::fetchNextSegment(const Interest& origInterest, const Name& dataName,
+                                 uint64_t segmentNo,
+                                 const shared_ptr<SegmentFetcher>& self)
+{
+  Interest interest(origInterest); // to preserve any special selectors
+  interest.refreshNonce();
+  interest.setChildSelector(0);
+  interest.setMustBeFresh(false);
+  interest.setName(dataName.getPrefix(-1).appendSegment(segmentNo));
+  m_face.expressInterest(interest,
+                         bind(&SegmentFetcher::onSegmentReceived, this, _1, _2, false, self),
+                         bind(m_errorCallback, INTEREST_TIMEOUT, "Timeout"));
+}
+
+void
+SegmentFetcher::onSegmentReceived(const Interest& origInterest,
+                                  const Data& data, bool isSegmentZeroExpected,
+                                  const shared_ptr<SegmentFetcher>& self)
+{
+  if (!m_verifySegment(data)) {
+    return m_errorCallback(SEGMENT_VERIFICATION_FAIL, "Segment validation fail");
+  }
+
+  try {
+    uint64_t currentSegment = data.getName().get(-1).toSegment();
+
+    if (isSegmentZeroExpected && currentSegment != 0) {
+      fetchNextSegment(origInterest, data.getName(), 0, self);
+    }
+    else {
+      m_buffer->write(reinterpret_cast<const char*>(data.getContent().value()),
+                      data.getContent().value_size());
+
+      const name::Component& finalBlockId = data.getMetaInfo().getFinalBlockId();
+      if (finalBlockId.empty() ||
+          finalBlockId.toSegment() > currentSegment)
+        {
+          fetchNextSegment(origInterest, data.getName(), currentSegment + 1, self);
+        }
+      else {
+        return m_completeCallback(m_buffer->buf());
+      }
+    }
+  }
+  catch (const tlv::Error& e) {
+    m_errorCallback(DATA_HAS_NO_SEGMENT, std::string("Error while decoding segment: ") + e.what());
+  }
+}
+
+} // util
+} // ndn