More bug fixes to complete basic operations of the application. Additionally implemented simple dump
option in application.

Change-Id: I3a47580af916482b0d901456455a9395d89048c7
diff --git a/src/sequential-data-fetcher.cpp b/src/sequential-data-fetcher.cpp
index b994384..9a5ae5f 100644
--- a/src/sequential-data-fetcher.cpp
+++ b/src/sequential-data-fetcher.cpp
@@ -39,11 +39,12 @@
 }
 
 void
-SequentialDataFetcher::start()
+SequentialDataFetcher::start(const time::milliseconds& timeout)
 {
   m_manager->Initialize();
   // downloading logic
   this->implementSequentialLogic();
+  m_manager->processEvents(timeout);
 }
 
 void
@@ -62,28 +63,24 @@
   throw(Error("Not implemented yet"));
 }
 
-std::vector<ndn::Name>
+void
 SequentialDataFetcher::downloadTorrentFile()
 {
-  std::vector<ndn::Name> returnedNames;
-  returnedNames = m_manager->downloadTorrentFile(".appdata/torrent_files/");
-  if (!returnedNames.empty() && IoUtil::NAME_TYPE::FILE_MANIFEST == IoUtil::findType(returnedNames[0])) {
-    LOG_INFO  << "Torrent File Received: "
-              << m_torrentFileName.getSubName(0, m_torrentFileName.size() - 1) << std::endl;
-  }
-  return returnedNames;
+  auto torrentPath = ".appdata/" + m_torrentFileName.get(-3).toUri() + "/torrent_files/";
+  m_manager->downloadTorrentFile(torrentPath,
+                                 bind(&SequentialDataFetcher::onTorrentFileSegmentReceived, this, _1),
+                                 bind(&SequentialDataFetcher::onDataRetrievalFailure, this, _1, _2));
 }
 
 void
-SequentialDataFetcher::downloadManifestFiles(const std::vector<ndn::Name>& manifestsName)
+SequentialDataFetcher::downloadManifestFiles(const std::vector<ndn::Name>& manifestNames)
 {
-  std::vector<ndn::Name> packetsName;
-  for (auto i = manifestsName.begin(); i != manifestsName.end(); i++) {
+  auto manifestPath = ".appdata/" + m_torrentFileName.get(-3).toUri() + "/manifests/";
+  for (auto i = manifestNames.begin(); i != manifestNames.end(); i++) {
     m_manager->download_file_manifest(*i,
-                              ".appdata/manifests/",
+                              manifestPath,
                               bind(&SequentialDataFetcher::onManifestReceived, this, _1),
                               bind(&SequentialDataFetcher::onDataRetrievalFailure, this, _1, _2));
-    m_manager->processEvents();
   }
 }
 
@@ -95,26 +92,37 @@
                               bind(&SequentialDataFetcher::onDataPacketReceived, this, _1),
                               bind(&SequentialDataFetcher::onDataRetrievalFailure, this, _1, _2));
   }
-  m_manager->processEvents();
 }
 
 void
 SequentialDataFetcher::implementSequentialLogic() {
-  std::vector<ndn::Name> returnedNames;
-  returnedNames = this->downloadTorrentFile();
-  if (returnedNames.empty()) {
-    // we have downloaded the entire torrent (including manifests, data packets, etc..)
-    return;
-  }
-  // check the first returned name whether it is the name of a file manifest or a data packet
-  const Name& nameToCheck = returnedNames[0];
-  if (IoUtil::findType(nameToCheck) == IoUtil::DATA_PACKET) {
-    // In this case, the returned names correspond to data packets
-    this->downloadPackets(returnedNames);
+  // TODO(?) Fix seeding, and implement windowing:
+  /*
+  Alex says look at ndn-cxx:
+  * fetcher with queue (with window)
+  * segment fetcher ?
+  * catchunks (pipeline?)
+  */
+  if (!m_manager->hasAllTorrentSegments()) {
+    this->downloadTorrentFile();
   }
   else {
-    // In this case, the returned names correspond to file manifests
-    this->downloadManifestFiles(returnedNames);
+    LOG_INFO <<  m_torrentFileName << " complete" <<  std::endl;
+    std::vector<ndn::Name> namesToFetch;
+    m_manager->findFileManifestsToDownload(namesToFetch);
+    if (!namesToFetch.empty()) {
+      this->downloadManifestFiles(namesToFetch);
+    }
+    else {
+      LOG_INFO << "All manifests complete" <<  std::endl;
+      m_manager->findAllMissingDataPackets(namesToFetch);
+      if (!namesToFetch.empty()) {
+        this->downloadPackets(namesToFetch);
+      }
+      else {
+        LOG_INFO << "All data complete" <<  std::endl;
+      }
+    }
   }
 }
 
@@ -126,6 +134,12 @@
 }
 
 void
+SequentialDataFetcher::onTorrentFileSegmentReceived(const std::vector<Name>& manifestNames)
+{
+  this->downloadManifestFiles(manifestNames);
+}
+
+void
 SequentialDataFetcher::onManifestReceived(const std::vector<Name>& packetNames)
 {
   LOG_INFO << "Manifest File Received: "