chunks: fix aimd hanging with files less than chunk size

refs: #4439

Change-Id: Ie3ef5d81e6baf154ade97beda183bc6b16764085
diff --git a/tools/chunks/catchunks/pipeline-interests-aimd.cpp b/tools/chunks/catchunks/pipeline-interests-aimd.cpp
index eac9dfd..961a064 100644
--- a/tools/chunks/catchunks/pipeline-interests-aimd.cpp
+++ b/tools/chunks/catchunks/pipeline-interests-aimd.cpp
@@ -70,6 +70,14 @@
 void
 PipelineInterestsAimd::doRun()
 {
+  if (allSegmentsReceived()) {
+    cancel();
+    if (!m_options.isQuiet) {
+      printSummary();
+    }
+    return;
+  }
+
   // schedule the event to check retransmission timer
   m_checkRtoEvent = m_scheduler.scheduleEvent(m_options.rtoCheckInterval, [this] { checkRto(); });
 
@@ -121,7 +129,7 @@
   if (isStopping())
     return;
 
-  if (m_hasFinalBlockId && segNo > m_lastSegmentNo && !isRetransmission)
+  if (m_hasFinalBlockId && segNo > m_lastSegmentNo)
     return;
 
   if (!isRetransmission && m_hasFailure)
@@ -291,8 +299,7 @@
   }
 
   BOOST_ASSERT(m_nReceived > 0);
-  if (m_hasFinalBlockId &&
-      static_cast<uint64_t>(m_nReceived - 1) >= m_lastSegmentNo) { // all segments have been received
+  if (allSegmentsReceived()) {
     cancel();
     if (!m_options.isQuiet) {
       printSummary();
@@ -435,6 +442,12 @@
   }
 }
 
+bool
+PipelineInterestsAimd::allSegmentsReceived() const
+{
+  return m_hasFinalBlockId && static_cast<uint64_t>(m_nReceived - 1) >= m_lastSegmentNo;
+}
+
 void
 PipelineInterestsAimd::printSummary() const
 {
@@ -442,7 +455,7 @@
   std::cerr << "Total # of lost/retransmitted segments: " << m_nRetransmitted
             << " (caused " << m_nLossEvents << " window decreases)\n"
             << "Packet loss rate: "
-            << (static_cast<double>(m_nRetransmitted) / static_cast<double>(m_nSent)) * 100 << "%\n"
+            << (m_nSent == 0 ? 0 : (static_cast<double>(m_nRetransmitted) / static_cast<double>(m_nSent) * 100))  << "%\n"
             << "Total # of received congestion marks: " << m_nCongMarks << "\n"
             << "RTT ";
 
diff --git a/tools/chunks/catchunks/pipeline-interests-aimd.hpp b/tools/chunks/catchunks/pipeline-interests-aimd.hpp
index 0257e7b..c2e5166 100644
--- a/tools/chunks/catchunks/pipeline-interests-aimd.hpp
+++ b/tools/chunks/catchunks/pipeline-interests-aimd.hpp
@@ -184,6 +184,9 @@
   void
   cancelInFlightSegmentsGreaterThan(uint64_t segNo);
 
+  bool
+  allSegmentsReceived() const;
+
 PUBLIC_WITH_TESTS_ELSE_PRIVATE:
   void
   printSummary() const final;