Implementing window-based CCNx consumer.  Adding RTO estimation.

- ContentObject increases window by 1
- NACK decreases window by 1
- TO doesn't change window

If window is zero, next packet is scheduled after 0.1*RTO
diff --git a/apps/ccnx-consumer-cbr.cc b/apps/ccnx-consumer-cbr.cc
index a41753f..5a2a759 100644
--- a/apps/ccnx-consumer-cbr.cc
+++ b/apps/ccnx-consumer-cbr.cc
@@ -34,13 +34,6 @@
 #include "ns3/ccnx-interest-header.h"
 #include "ns3/ccnx-content-object-header.h"
 
-#include <boost/ref.hpp>
-#include <boost/lexical_cast.hpp>
-#include <boost/lambda/lambda.hpp>
-#include <boost/lambda/bind.hpp>
-
-namespace ll = boost::lambda;
-
 NS_LOG_COMPONENT_DEFINE ("CcnxConsumerCbr");
 
 namespace ns3
@@ -77,7 +70,7 @@
 CcnxConsumerCbr::UpdateMean ()
 {
   double mean = 8.0 * m_payloadSize / m_desiredRate.GetBitRate ();
-  m_randExp = ExponentialVariable (mean, 10000 * mean); // set upper limit to inter-arrival time
+  m_randExp = ExponentialVariable (mean, 100 * mean); // set upper limit to inter-arrival time
 }
 
 void
@@ -104,9 +97,12 @@
 void
 CcnxConsumerCbr::ScheduleNextPacket ()
 {
+  // double mean = 8.0 * m_payloadSize / m_desiredRate.GetBitRate ();
+
   if (!m_sendEvent.IsRunning ())
     m_sendEvent = Simulator::Schedule (
                                        Seconds(m_randExp.GetValue ()),
+                                       // Seconds(mean),
                                        &CcnxConsumer::SendPacket, this);
 }
 
diff --git a/apps/ccnx-consumer-window.cc b/apps/ccnx-consumer-window.cc
new file mode 100644
index 0000000..57ca8f5
--- /dev/null
+++ b/apps/ccnx-consumer-window.cc
@@ -0,0 +1,123 @@
+/* -*-  Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
+/*
+ * Copyright (c) 2011 University of California, Los Angeles
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation;
+ *
+ * This program 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
+ */
+
+#include "ccnx-consumer-window.h"
+#include "ns3/ptr.h"
+#include "ns3/log.h"
+#include "ns3/simulator.h"
+#include "ns3/packet.h"
+#include "ns3/callback.h"
+#include "ns3/string.h"
+#include "ns3/uinteger.h"
+#include "ns3/double.h"
+
+NS_LOG_COMPONENT_DEFINE ("CcnxConsumerWindow");
+
+namespace ns3
+{    
+    
+NS_OBJECT_ENSURE_REGISTERED (CcnxConsumerWindow);
+    
+TypeId
+CcnxConsumerWindow::GetTypeId (void)
+{
+  static TypeId tid = TypeId ("ns3::CcnxConsumerWindow")
+    .SetParent<CcnxConsumer> ()
+    .AddConstructor<CcnxConsumerWindow> ()
+
+    .AddAttribute ("Window", "Initial size of the window",
+                   StringValue ("1000"),
+                   MakeUintegerAccessor (&CcnxConsumerWindow::GetWindow, &CcnxConsumerWindow::SetWindow),
+                   MakeUintegerChecker<uint32_t> ())
+    ;
+
+  return tid;
+}
+
+CcnxConsumerWindow::CcnxConsumerWindow ()
+  : m_inFlight (0)
+{
+}
+
+void
+CcnxConsumerWindow::SetWindow (uint32_t window)
+{
+  m_window = window;
+}
+
+uint32_t
+CcnxConsumerWindow::GetWindow () const
+{
+  return m_window;
+}
+
+void
+CcnxConsumerWindow::ScheduleNextPacket ()
+{
+  if (m_window == 0 || m_inFlight >= m_window)
+    {
+      if (!m_sendEvent.IsRunning ())
+        m_sendEvent = Simulator::Schedule (Seconds (m_rtt->RetransmitTimeout ().ToDouble (Time::S) * 0.1), &CcnxConsumer::SendPacket, this);
+      return;
+    }
+  
+  // std::cout << "Window: " << m_window << ", InFlight: " << m_inFlight << "\n";
+  m_inFlight++;
+  if (!m_sendEvent.IsRunning ())
+    m_sendEvent = Simulator::ScheduleNow (&CcnxConsumer::SendPacket, this);
+}
+
+///////////////////////////////////////////////////
+//          Process incoming packets             //
+///////////////////////////////////////////////////
+
+void
+CcnxConsumerWindow::OnContentObject (const Ptr<const CcnxContentObjectHeader> &contentObject,
+                               const Ptr<const Packet> &payload)
+{
+  CcnxConsumer::OnContentObject (contentObject, payload);
+
+  m_window = m_window + 1;
+  
+  if (m_inFlight > 0) m_inFlight--;
+  ScheduleNextPacket ();
+}
+
+void
+CcnxConsumerWindow::OnNack (const Ptr<const CcnxInterestHeader> &interest)
+{
+  CcnxConsumer::OnNack (interest);
+  if (m_inFlight > 0) m_inFlight--;
+
+  if (m_window > 0)
+    {
+      // m_window = 0.5 * m_window;//m_window - 1;
+      m_window = m_window - 1;
+    }
+}
+
+void
+CcnxConsumerWindow::OnTimeout (uint32_t sequenceNumber)
+{
+  if (m_inFlight > 0) m_inFlight--;
+  CcnxConsumer::OnTimeout (sequenceNumber);
+}
+
+} // namespace ns3
diff --git a/apps/ccnx-consumer-window.h b/apps/ccnx-consumer-window.h
new file mode 100644
index 0000000..47099f6
--- /dev/null
+++ b/apps/ccnx-consumer-window.h
@@ -0,0 +1,79 @@
+/* -*-  Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
+/*
+ * Copyright (c) 2011 University of California, Los Angeles
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation;
+ *
+ * This program 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ * Author: Ilya Moiseenko <iliamo@cs.ucla.edu>
+ *         Alexander Afanasyev <alexander.afanasyev@ucla.edu>
+ */
+
+#ifndef CCNX_CONSUMER_WINDOW_H
+#define CCNX_CONSUMER_WINDOW_H
+
+#include "ccnx-consumer.h"
+
+namespace ns3 
+{
+
+/**
+ * @ingroup ccnx
+ * \brief CCNx application for sending out Interest packets (window-based)
+ */
+class CcnxConsumerWindow: public CcnxConsumer
+{
+public: 
+  static TypeId GetTypeId ();
+        
+  /**
+   * \brief Default constructor 
+   */
+  CcnxConsumerWindow ();
+
+  // From CcnxApp
+  // virtual void
+  // OnInterest (const Ptr<const CcnxInterestHeader> &interest);
+
+  virtual void
+  OnNack (const Ptr<const CcnxInterestHeader> &interest);
+
+  virtual void
+  OnContentObject (const Ptr<const CcnxContentObjectHeader> &contentObject,
+                   const Ptr<const Packet> &payload);
+
+  virtual void
+  OnTimeout (uint32_t sequenceNumber);
+ 
+protected:
+  /**
+   * \brief Constructs the Interest packet and sends it using a callback to the underlying CCNx protocol
+   */
+  virtual void
+  ScheduleNextPacket ();
+  
+private:
+  virtual void
+  SetWindow (uint32_t window);
+
+  uint32_t
+  GetWindow () const;
+  
+protected:
+  uint32_t m_window;
+  uint32_t m_inFlight;
+};
+
+} // namespace ns3
+
+#endif
diff --git a/apps/ccnx-consumer.cc b/apps/ccnx-consumer.cc
index fcc1ccf..b5984e7 100644
--- a/apps/ccnx-consumer.cc
+++ b/apps/ccnx-consumer.cc
@@ -92,11 +92,6 @@
                    MakeCcnxNameComponentsAccessor (&CcnxConsumer::m_exclude),
                    MakeCcnxNameComponentsChecker ())
 
-    .AddAttribute ("RTO",
-                   "Initial retransmission timeout",
-                   StringValue ("1s"),
-                   MakeTimeAccessor (&CcnxConsumer::m_rto),
-                   MakeTimeChecker ())
     .AddAttribute ("RetxTimer",
                    "Timeout defining how frequent retransmission timeouts should be checked",
                    StringValue ("1s"),
@@ -116,6 +111,8 @@
   , m_seq (0)
 {
   NS_LOG_FUNCTION_NOARGS ();
+  
+  m_rtt = CreateObject<RttMeanDeviation> (); 
 }
 
 void
@@ -143,25 +140,21 @@
 
   boost::mutex::scoped_lock (m_seqTimeoutsGuard);
 
+  Time rto = m_rtt->RetransmitTimeout ();
+  
   while (!m_seqTimeouts.empty ())
     {
       SeqTimeoutsContainer::index<i_timestamp>::type::iterator entry =
         m_seqTimeouts.get<i_timestamp> ().begin ();
-      if (entry->time + m_rto <= now) // timeout expired?
+      if (entry->time + rto <= now) // timeout expired?
         {
-          m_retxSeqs.insert (entry->seq);
-          m_seqTimeouts.get<i_timestamp> ().modify (entry,
-                                                    ll::bind(&SeqTimeout::time, ll::_1) = now);
+          m_seqTimeouts.get<i_timestamp> ().erase (entry);
+          OnTimeout (entry->seq);
         }
       else
         break; // nothing else to do. All later packets need not be retransmitted
     }
 
-  if (m_retxSeqs.size () > 0)
-    {
-      ScheduleNextPacket ();
-    }
-  
   m_retxEvent = Simulator::Schedule (m_retxTimer,
                                      &CcnxConsumer::CheckRetxTimeout, this); 
 }
@@ -237,6 +230,12 @@
   
   if (m_retxSeqs.size () != 0)
     {
+      // for (RetxSeqsContainer::const_iterator i=m_retxSeqs.begin (); i!=m_retxSeqs.end (); i++)
+      //   {
+      //     std::cout << *i << " ";
+      //   }
+      // std::cout << "\n";
+      
       seq = *m_retxSeqs.begin ();
       NS_LOG_INFO ("Before: " << m_retxSeqs.size ());
       m_retxSeqs.erase (m_retxSeqs.begin ());
@@ -254,6 +253,8 @@
       
       seq = m_seq++;
     }
+
+  // std::cout << Simulator::Now ().ToDouble (Time::S) << "s -> " << seq << "\n";
   
   //
   Ptr<CcnxNameComponents> nameWithSequence = Create<CcnxNameComponents> (m_interestName);
@@ -282,15 +283,10 @@
 
   NS_LOG_DEBUG ("Trying to add " << seq << " with " << Simulator::Now () << ". already " << m_seqTimeouts.size () << " items");  
   
-  std::pair<SeqTimeoutsContainer::iterator, bool>
-    res = m_seqTimeouts.insert (SeqTimeout (seq, Simulator::Now ()));
-  
-  if (!res.second)
-    m_seqTimeouts.modify (res.first,
-                          ll::bind(&SeqTimeout::time, ll::_1) = Simulator::Now ());
-  
+  m_seqTimeouts.insert (SeqTimeout (seq, Simulator::Now ()));
   m_transmittedInterests (&interestHeader, this, m_face);
 
+  m_rtt->SentSeq (SequenceNumber32 (seq), 1);
   ScheduleNextPacket ();
 }
 
@@ -326,6 +322,8 @@
 
   m_seqTimeouts.erase (seq);
   m_retxSeqs.erase (seq);
+
+  m_rtt->AckSeq (SequenceNumber32 (seq));
 }
 
 void
@@ -343,6 +341,7 @@
   // NS_LOG_INFO ("Received NACK: " << boost::cref(*interest));
   uint32_t seq = boost::lexical_cast<uint32_t> (interest->GetName ().GetComponents ().back ());
   NS_LOG_INFO ("< NACK for " << seq);
+  // std::cout << Simulator::Now ().ToDouble (Time::S) << "s -> " << "NACK for " << seq << "\n"; 
 
   // put in the queue of interests to be retransmitted
   NS_LOG_INFO ("Before: " << m_retxSeqs.size ());
@@ -352,4 +351,13 @@
   ScheduleNextPacket ();
 }
 
+void
+CcnxConsumer::OnTimeout (uint32_t sequenceNumber)
+{
+  // std::cout << "TO: " << sequenceNumber << "\n";
+  // m_retxSeqs.insert (sequenceNumber);
+  // std::cout << "Current RTO: " << m_rtt->RetransmitTimeout ().ToDouble (Time::S) << "s\n";
+  ScheduleNextPacket (); 
+}
+
 } // namespace ns3
diff --git a/apps/ccnx-consumer.h b/apps/ccnx-consumer.h
index 31b4eb9..4bffbaf 100644
--- a/apps/ccnx-consumer.h
+++ b/apps/ccnx-consumer.h
@@ -27,6 +27,7 @@
 #include "ns3/ccnx-name-components.h"
 #include "ns3/nstime.h"
 #include "ns3/data-rate.h"
+#include "ns3/rtt-estimator.h"
 
 #include <set>
 
@@ -66,6 +67,8 @@
   OnContentObject (const Ptr<const CcnxContentObjectHeader> &contentObject,
                    const Ptr<const Packet> &payload);
 
+  virtual void
+  OnTimeout (uint32_t sequenceNumber);
 
   // Simulator::Schedule doesn't work with protected members???
   void
@@ -127,9 +130,7 @@
   Time            m_retxTimer;
   EventId         m_retxEvent; // Event to check whether or not retransmission should be performed
 
-  Time            m_rto;        ///< \brief Retransmission timeout
-  Time            m_rttVar;     ///< \brief RTT variance
-  Time            m_sRtt;       ///< \brief smoothed RTT
+  Ptr<RttEstimator> m_rtt;
   
   Time               m_offTime;             ///< \brief Time interval between packets
   CcnxNameComponents m_interestName;        ///< \brief CcnxName of the Interest (use CcnxNameComponents)