Minor bug fixes to allow basic function function of the application

Change-Id: I15e1e093451f199c569457058ed17389c32d12cf
diff --git a/src/sequential-data-fetcher.cpp b/src/sequential-data-fetcher.cpp
index 5873374..06b3f02 100644
--- a/src/sequential-data-fetcher.cpp
+++ b/src/sequential-data-fetcher.cpp
@@ -66,8 +66,10 @@
 {
   std::vector<ndn::Name> returnedNames;
   returnedNames = m_manager->downloadTorrentFile(".appdata/torrent_files/");
-  std::cout << "Torrent File Received: "
-            << m_torrentFileName.getSubName(0, m_torrentFileName.size() - 1) << std::endl;
+  if (!returnedNames.empty() && IoUtil::NAME_TYPE::FILE_MANIFEST == IoUtil::findType(returnedNames[0])) {
+    std::cout << "Torrent File Received: "
+              << m_torrentFileName.getSubName(0, m_torrentFileName.size() - 1) << std::endl;
+  }
   return returnedNames;
 }
 
@@ -91,8 +93,8 @@
     m_manager->download_data_packet(*i,
                               bind(&SequentialDataFetcher::onDataPacketReceived, this, _1),
                               bind(&SequentialDataFetcher::onDataRetrievalFailure, this, _1, _2));
-    m_manager->processEvents();
   }
+  m_manager->processEvents();
 }
 
 void
@@ -134,26 +136,26 @@
 SequentialDataFetcher::onDataRetrievalFailure(const ndn::Interest& interest,
                                               const std::string& errorCode)
 {
-  std::cerr << "Data Retrieval Failed: " << interest.getName() << std::endl;
+  // std::cerr << "Data Retrieval Failed: " << interest.getName() << std::endl;
 
   // Data retrieval failure
   uint32_t nameType = IoUtil::findType(interest.getName());
   if (nameType == IoUtil::TORRENT_FILE) {
     // this should never happen
-    std::cerr << "Torrent File Segment Downloading Failed: " << interest.getName();
+    // std::cerr << "Torrent File Segment Downloading Failed: " << interest.getName();
     this->downloadTorrentFile();
   }
   else if (nameType == IoUtil::FILE_MANIFEST) {
-    std::cerr << "Manifest File Segment Downloading Failed: " << interest.getName();
+    // std::cerr << "Manifest File Segment Downloading Failed: " << interest.getName();
     this->downloadManifestFiles({ interest.getName() });
   }
   else if (nameType == IoUtil::DATA_PACKET) {
-    std::cerr << "Data Packet Downloading Failed: " << interest.getName();
+    // std::cerr << "Data Packet Downloading Failed: " << interest.getName();
     this->downloadPackets({ interest.getName() });
   }
   else {
     // This should never happen
-    std::cerr << "Unknown Packet Type Downloading Failed: " << interest.getName();
+    // std::cerr << "Unknown Packet Type Downloading Failed: " << interest.getName();
   }
 }
 
diff --git a/src/torrent-manager.cpp b/src/torrent-manager.cpp
index 75e09b5..790e166 100644
--- a/src/torrent-manager.cpp
+++ b/src/torrent-manager.cpp
@@ -174,8 +174,8 @@
     torrentName = m_torrentFileName.getSubName(1, m_torrentFileName.size() - 3);
   }
 
-  m_updateHandler = make_shared<UpdateHandler>(torrentName, m_keyChain,
-                                               make_shared<StatsTable>(m_statsTable), m_face);
+  // m_updateHandler = make_shared<UpdateHandler>(torrentName, m_keyChain,
+  //                                              make_shared<StatsTable>(m_statsTable), m_face);
 
   // .../<torrent_name>/torrent-file/<implicit_digest>
   string dataPath = ".appdata/" + m_torrentFileName.get(-3).toUri();
@@ -246,9 +246,9 @@
 TorrentManager::downloadTorrentFile(const std::string& path)
 {
   // check whether we should send out an "ALIVE" Interest
-  if (m_updateHandler->needsUpdate()) {
-    m_updateHandler->sendAliveInterest(m_stats_table_iter);
-  }
+  // if (m_updateHandler->needsUpdate()) {
+  //   m_updateHandler->sendAliveInterest(m_stats_table_iter);
+  // }
   shared_ptr<Name> searchRes = this->findTorrentFileSegmentToDownload();
   auto manifestNames = make_shared<std::vector<Name>>();
   if (searchRes == nullptr) {
@@ -564,7 +564,6 @@
   const auto& interestName = interest.getName();
   std::shared_ptr<Data> data = nullptr;
   auto cmp = [&interestName](const Data& t){return t.getFullName() == interestName;};
-
   // determine if it is torrent file (that we have)
   auto torrent_it =  std::find_if(m_torrentSegments.begin(), m_torrentSegments.end(), cmp);
   if (m_torrentSegments.end() != torrent_it) {
@@ -698,12 +697,14 @@
 
   // find the pair of (std::shared_ptr<fs::fstream>, std::vector<bool>)
   // that corresponds to the specific submanifest
-  auto& fileState = m_fileStates[manifest_it->getFullName()];
-
-  auto dataNum = dataName.get(dataName.size() - 2).toSequenceNumber();
-
-  // find whether we have the requested packet from the bitmap
-  return fileState.second[dataNum];
+  auto fileState_it = m_fileStates.find(manifest_it->getFullName());
+  if (m_fileStates.end() != fileState_it) {
+    const auto& fileState = fileState_it->second;
+    auto dataNum = dataName.get(dataName.size() - 2).toSequenceNumber();
+    // find whether we have the requested packet from the bitmap
+    return fileState.second[dataNum];
+  }
+  return false;
 }
 
 void
@@ -733,12 +734,21 @@
 void
 TorrentManager::findAllMissingDataPackets(std::vector<Name>& packetNames)
 {
-  for (auto j = m_fileManifests.begin(); j != m_fileManifests.end(); j++) {
-    auto& fileState = m_fileStates[j->getFullName()];
-    for (auto i = j->catalog().begin(); i != j->catalog().end(); i++) {
-      auto dataNum = i->get(i->size() - 2).toSequenceNumber();
-      if (!fileState.second[dataNum]) {
-        packetNames.push_back(*i);
+  for (auto j = m_fileManifests.begin(); j != m_fileManifests.end(); ++j) {
+    auto fileState_it = m_fileStates.find(j->getFullName());
+    // if we have no packets from this file
+    if (m_fileStates.end() == fileState_it) {
+      packetNames.reserve(packetNames.size() + j->catalog().size());
+      packetNames.insert(packetNames.end(), j->catalog().begin(), j->catalog().end());
+    }
+    // find the packets that we are missing
+    else {
+      const auto &fileState =  fileState_it->second;
+      for (auto i = j->catalog().begin(); i != j->catalog().end(); i++) {
+        auto dataNum = i->get(i->size() - 2).toSequenceNumber();
+        if (!fileState.second[dataNum]) {
+          packetNames.push_back(*i);
+        }
       }
     }
   }
@@ -763,9 +773,9 @@
   if (m_sortingCounter >= SORTING_INTERVAL) {
     // Use the sorting interval to send out "ALIVE" Interests as well
     // check whether we should send out an "ALIVE" Interest
-    if (m_updateHandler->needsUpdate()) {
-      m_updateHandler->sendAliveInterest(m_stats_table_iter);
-    }
+    // if (m_updateHandler->needsUpdate()) {
+    //   m_updateHandler->sendAliveInterest(m_stats_table_iter);
+    // }
     // Do the actual sorting related stuff
     m_sortingCounter = 0;
     m_statsTable.sort();