apps+model+utils: Implementing Interest/Data hop counting using new PacketTag
diff --git a/apps/ndn-consumer-zipf-mandelbrot.cc b/apps/ndn-consumer-zipf-mandelbrot.cc
index c0e84d8..9982738 100644
--- a/apps/ndn-consumer-zipf-mandelbrot.cc
+++ b/apps/ndn-consumer-zipf-mandelbrot.cc
@@ -23,6 +23,9 @@
 #include "ns3/ndn-app-face.h"
 #include "ns3/ndn-interest.h"
 #include "ns3/ndn-content-object.h"
+
+#include "ns3/ndnSIM/utils/ndn-fw-hop-count-tag.h"
+
 #include <math.h>
 
 
@@ -176,6 +179,9 @@
 
   m_rtt->SentSeq (SequenceNumber32 (seq), 1);
 
+  FwHopCountTag hopCountTag;
+  packet->AddPacketTag (hopCountTag);
+  
   m_protocolHandler (packet);
 
   ConsumerZipfMandelbrot::ScheduleNextPacket ();
diff --git a/apps/ndn-consumer.cc b/apps/ndn-consumer.cc
index 08d4b2c..60ef55b 100644
--- a/apps/ndn-consumer.cc
+++ b/apps/ndn-consumer.cc
@@ -32,7 +32,7 @@
 #include "ns3/ndn-app-face.h"
 #include "ns3/ndn-interest.h"
 #include "ns3/ndn-content-object.h"
-// #include "ns3/weights-path-stretch-tag.h"
+#include "ns3/ndnSIM/utils/ndn-fw-hop-count-tag.h"
 
 #include <boost/ref.hpp>
 #include <boost/lexical_cast.hpp>
@@ -76,9 +76,6 @@
                    MakeTimeAccessor (&Consumer::GetRetxTimer, &Consumer::SetRetxTimer),
                    MakeTimeChecker ())
 
-    .AddTraceSource ("PathWeightsTrace", "PathWeightsTrace",
-                    MakeTraceSourceAccessor (&Consumer::m_pathWeightsTrace))
-
     .AddTraceSource ("LastRetransmittedInterestDataDelay", "Delay between last retransmitted Interest and received Data",
                      MakeTraceSourceAccessor (&Consumer::m_lastRetransmittedInterestDataDelay))
 
@@ -228,6 +225,9 @@
 
   m_rtt->SentSeq (SequenceNumber32 (seq), 1);
 
+  FwHopCountTag hopCountTag;
+  packet->AddPacketTag (hopCountTag);
+  
   m_protocolHandler (packet);
 
   ScheduleNextPacket ();
@@ -253,16 +253,23 @@
   uint32_t seq = boost::lexical_cast<uint32_t> (contentObject->GetName ().GetComponents ().back ());
   NS_LOG_INFO ("< DATA for " << seq);
 
+  int hopCount = -1;
+  FwHopCountTag hopCountTag;
+  if (payload->RemovePacketTag (hopCountTag))
+    {
+      hopCount = hopCountTag.Get ();
+    }
+  
   SeqTimeoutsContainer::iterator entry = m_seqLastDelay.find (seq);
   if (entry != m_seqLastDelay.end ())
     {
-      m_lastRetransmittedInterestDataDelay (this, seq, Simulator::Now () - entry->time);
+      m_lastRetransmittedInterestDataDelay (this, seq, Simulator::Now () - entry->time, hopCount);
     }
 
   entry = m_seqFullDelay.find (seq);
   if (entry != m_seqFullDelay.end ())
     {
-      m_firstInterestDataDelay (this, seq, Simulator::Now () - entry->time, m_seqRetxCounts[seq]);
+      m_firstInterestDataDelay (this, seq, Simulator::Now () - entry->time, m_seqRetxCounts[seq], hopCount);
     }
 
   m_seqRetxCounts.erase (seq);  
@@ -273,15 +280,6 @@
   m_retxSeqs.erase (seq);
 
   m_rtt->AckSeq (SequenceNumber32 (seq));
-
-  // Ptr<const WeightsPathStretchTag> tag = payload->RemovePacketTag<WeightsPathStretchTag> ();
-  // if (tag != 0)
-  //   {
-  //     // Notify trace about path weights vector (e.g., for path-stretch calculation)
-  //     m_pathWeightsTrace (GetNode (), tag->GetSourceNode (), seq, tag->GetTotalWeight ());
-  //     // if (Names::FindName (GetNode ()) == "36")// || Names::FindName (GetNode ()) == "40"|| Names::FindName (GetNode ()) == "5")
-  //     //   std::cout << Simulator::Now () << "\t" << boost::cref(*tag) << " = " << tag->GetTotalWeight () << "\n";
-  //   }
 }
 
 void
diff --git a/apps/ndn-consumer.h b/apps/ndn-consumer.h
index 03c3dd9..5d09672 100644
--- a/apps/ndn-consumer.h
+++ b/apps/ndn-consumer.h
@@ -181,9 +181,11 @@
   SeqTimeoutsContainer m_seqFullDelay;
   std::map<uint32_t, uint32_t> m_seqRetxCounts;
   
-  TracedCallback<Ptr<Node>, Ptr<Node>, uint32_t, uint32_t > m_pathWeightsTrace;
-  TracedCallback<Ptr<App> /* app */, uint32_t /* seqno */, Time /* delay */> m_lastRetransmittedInterestDataDelay;
-  TracedCallback<Ptr<App> /* app */, uint32_t /* seqno */, Time /* delay */, uint32_t /*retx count*/> m_firstInterestDataDelay;
+  TracedCallback<Ptr<App> /* app */, uint32_t /* seqno */,
+                 Time /* delay */, int32_t /*hop count*/> m_lastRetransmittedInterestDataDelay;
+  TracedCallback<Ptr<App> /* app */, uint32_t /* seqno */,
+                 Time /* delay */, uint32_t /*retx count*/,
+                 int32_t /*hop count*/> m_firstInterestDataDelay;
   
 /// @endcond
 };
diff --git a/apps/ndn-producer.cc b/apps/ndn-producer.cc
index 4f7afc4..f0d464a 100644
--- a/apps/ndn-producer.cc
+++ b/apps/ndn-producer.cc
@@ -30,7 +30,8 @@
 
 #include "ns3/ndn-app-face.h"
 #include "ns3/ndn-fib.h"
-// #include "../model/ndn-fib-impl.h"
+
+#include "ns3/ndnSIM/utils/ndn-fw-hop-count-tag.h"
 
 #include <boost/ref.hpp>
 #include <boost/lambda/lambda.hpp>
@@ -123,22 +124,17 @@
   NS_LOG_INFO ("node("<< GetNode()->GetId() <<") respodning with ContentObject:\n" << boost::cref(*header));
   
   Ptr<Packet> packet = Create<Packet> (m_virtualPayloadSize);
-  // Ptr<const WeightsPathStretchTag> tag = origPacket->RemovePacketTag<WeightsPathStretchTag> ();
-  // if (tag != 0)
-  //   {
-  //     // std::cout << Simulator::Now () << ", " << m_app->GetInstanceTypeId ().GetName () << "\n";
-
-  //     // echo back WeightsPathStretchTag
-  //     packet->AddPacketTag (CreateObject<WeightsPathStretchTag> (*tag));
-
-  //     // \todo
-  //     // packet->AddPacketTag should actually accept Ptr<const WeightsPathStretchTag> instead of
-  //     // Ptr<WeightsPathStretchTag>.  Echoing will be simplified after change is done
-  //   }
   
   packet->AddHeader (*header);
   packet->AddTrailer (tail);
 
+  // Echo back FwHopCountTag if exists
+  FwHopCountTag hopCountTag;
+  if (origPacket->RemovePacketTag (hopCountTag))
+    {
+      packet->AddPacketTag (hopCountTag);
+    }
+
   m_protocolHandler (packet);
   
   m_transmittedContentObjects (header, packet, this, m_face);
diff --git a/docs/source/metric.rst b/docs/source/metric.rst
index 03ee82f..0e821f0 100644
--- a/docs/source/metric.rst
+++ b/docs/source/metric.rst
@@ -247,7 +247,15 @@
     +-----------------+---------------------------------------------------------------------+
     | ``RetxCount``   | number of Interest retransmissions (for LastDelay always equal to 1)|
     +-----------------+---------------------------------------------------------------------+
-
+    | ``HopCount``    | combined number of number of hops for Interest and Data packet.     |
+    |                 | Note that HopCount is increased anytime somebody calls Send method  |
+    |                 | on a face, including delivery of Interest/Data to application via   |
+    |                 | an AppFace (but not from application to ndn::L3Protocol!).          |
+    |                 |                                                                     |
+    |                 | One consequence is that Interests satisfied by an app will have     |
+    |                 | even hop count (min hop count = 2), and Interests satisfied from    |
+    |                 | caches will have off hop count (min hop count = 1)                  |
+    +-----------------+---------------------------------------------------------------------+
 
 .. _app delay trace helper example:
 
diff --git a/examples/ndn-tree-app-delay-tracer.cc b/examples/ndn-tree-app-delay-tracer.cc
index 0b81c06..0fdffd9 100644
--- a/examples/ndn-tree-app-delay-tracer.cc
+++ b/examples/ndn-tree-app-delay-tracer.cc
@@ -84,18 +84,18 @@
   Ptr<Node> consumers[4] = { Names::Find<Node> ("leaf-1"), Names::Find<Node> ("leaf-2"),
                              Names::Find<Node> ("leaf-3"), Names::Find<Node> ("leaf-4") };
   Ptr<Node> producer = Names::Find<Node> ("root");
+  
+  ndn::AppHelper consumerHelper ("ns3::ndn::ConsumerBatches");
+  consumerHelper.SetPrefix ("/root");
+  consumerHelper.SetAttribute ("Batches", StringValue ("1s 1 10s 1"));
+  consumerHelper.Install (consumers[0]);
 
-  for (int i = 0; i < 4; i++)
-    {
-      ndn::AppHelper consumerHelper ("ns3::ndn::ConsumerCbr");
-      consumerHelper.SetAttribute ("Frequency", StringValue ("10")); // 100 interests a second
+  consumerHelper.SetAttribute ("Batches", StringValue ("11s 1")); 
+  consumerHelper.Install (consumers[1]);
+  
+  consumerHelper.SetAttribute ("Batches", StringValue ("11s 1")); 
+  consumerHelper.Install (consumers[2]);
 
-      // Each consumer will express the same data /root/<seq-no>
-      consumerHelper.SetPrefix ("/root");
-      ApplicationContainer app = consumerHelper.Install (consumers[i]);
-      app.Start (Seconds (0.01 * i));
-    }
-    
   ndn::AppHelper producerHelper ("ns3::ndn::Producer");
   producerHelper.SetAttribute ("PayloadSize", StringValue("1024"));  
 
@@ -103,7 +103,8 @@
   // install producer that will satisfy Interests in /root namespace
   ccnxGlobalRoutingHelper.AddOrigins ("/root", producer);
   producerHelper.SetPrefix ("/root");
-  producerHelper.Install (producer);
+  producerHelper.Install (producer)
+    .Start (Seconds (9));
 
   // Calculate and install FIBs
   ccnxGlobalRoutingHelper.CalculateRoutes ();
diff --git a/model/fw/ndn-forwarding-strategy.cc b/model/fw/ndn-forwarding-strategy.cc
index 4bab0a1..0e773f9 100644
--- a/model/fw/ndn-forwarding-strategy.cc
+++ b/model/fw/ndn-forwarding-strategy.cc
@@ -37,6 +37,8 @@
 #include "ns3/boolean.h"
 #include "ns3/string.h"
 
+#include "ns3/ndnSIM/utils/ndn-fw-hop-count-tag.h"
+
 #include <boost/ref.hpp>
 #include <boost/foreach.hpp>
 #include <boost/lambda/lambda.hpp>
@@ -171,7 +173,13 @@
   boost::tie (contentObject, contentObjectHeader, payload) = m_contentStore->Lookup (header);
   if (contentObject != 0)
     {
-      NS_ASSERT (contentObjectHeader != 0);      
+      NS_ASSERT (contentObjectHeader != 0);
+
+      FwHopCountTag hopCountTag;
+      if (origPacket->PeekPacketTag (hopCountTag))
+        {
+          contentObject->AddPacketTag (hopCountTag);
+        }
 
       pitEntry->AddIncoming (inFace/*, Seconds (1.0)*/);
 
@@ -223,8 +231,20 @@
     }
   else
     {
-      // Add or update entry in the content store
-      m_contentStore->Add (header, payload);
+      FwHopCountTag hopCountTag;
+      if (payload->PeekPacketTag (hopCountTag))
+        {
+          Ptr<Packet> payloadCopy = payload->Copy ();
+          payloadCopy->RemovePacketTag (hopCountTag);
+          
+          // Add or update entry in the content store
+          m_contentStore->Add (header, payloadCopy);
+        }
+      else
+        {
+          // Add or update entry in the content store
+          m_contentStore->Add (header, payload); // no need for extra copy
+        }
     }
 
   while (pitEntry != 0)
diff --git a/model/ndn-face.cc b/model/ndn-face.cc
index f91fc19..4ae7ee9 100644
--- a/model/ndn-face.cc
+++ b/model/ndn-face.cc
@@ -32,7 +32,7 @@
 #include "ns3/random-variable.h"
 #include "ns3/pointer.h"
 
-// #include "ns3/weights-path-stretch-tag.h"
+#include "ns3/ndnSIM/utils/ndn-fw-hop-count-tag.h"
 
 #include <boost/ref.hpp>
 
@@ -55,11 +55,6 @@
                    MakeUintegerAccessor (&Face::m_id),
                    MakeUintegerChecker<uint32_t> ())
 
-    // .AddAttribute ("MetricTagging", "Enable metric tagging (path-stretch calculation)",
-    //                BooleanValue (false),
-    //                MakeBooleanAccessor (&Face::m_enableMetricTagging),
-    //                MakeBooleanChecker ())
-
     .AddTraceSource ("NdnTx", "Transmitted packet trace",
                      MakeTraceSourceAccessor (&Face::m_txTrace))
     .AddTraceSource ("NdnRx", "Received packet trace",
@@ -81,7 +76,6 @@
   , m_ifup (false)
   , m_id ((uint32_t)-1)
   , m_metric (0)
-  // , m_enableMetricTagging (false)
 {
   NS_LOG_FUNCTION (this);
 
@@ -128,23 +122,13 @@
       return false;
     }
 
-  // if (m_enableMetricTagging)
-  //   {
-  //     // update path information
-  //     Ptr<const WeightsPathStretchTag> origTag = packet->RemovePacketTag<WeightsPathStretchTag> ();
-  //     Ptr<WeightsPathStretchTag> tag;
-  //     if (origTag == 0)
-  //       {
-  //         tag = CreateObject<WeightsPathStretchTag> (); // create a new tag
-  //       }
-  //     else
-  //       {
-  //         tag = CreateObject<WeightsPathStretchTag> (*origTag); // will update existing tag
-  //       }
-
-  //     tag->AddPathInfo (m_node, GetMetric ());
-  //     packet->AddPacketTag (tag);
-  //   }
+  FwHopCountTag hopCount;
+  bool tagExists = packet->RemovePacketTag (hopCount);
+  if (tagExists)
+    {
+      hopCount.Increment ();
+      packet->AddPacketTag (hopCount);
+    }
 
   bool ok = SendImpl (packet);
   if (ok)
diff --git a/model/ndn-face.h b/model/ndn-face.h
index acb1c90..f5aba0f 100644
--- a/model/ndn-face.h
+++ b/model/ndn-face.h
@@ -215,8 +215,6 @@
   uint32_t m_id; ///< \brief id of the interface in Ndn stack (per-node uniqueness)
   uint32_t m_metric; ///< \brief metric of the face
 
-  // bool m_enableMetricTagging;
-
   TracedCallback<Ptr<const Packet> > m_txTrace;
   TracedCallback<Ptr<const Packet> > m_rxTrace;
   TracedCallback<Ptr<const Packet> > m_dropTrace;
diff --git a/utils/ndn-fw-hop-count-tag.cc b/utils/ndn-fw-hop-count-tag.cc
new file mode 100644
index 0000000..8e4ba4c
--- /dev/null
+++ b/utils/ndn-fw-hop-count-tag.cc
@@ -0,0 +1,67 @@
+/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
+/*
+ * Copyright (c) 2013 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 "ndn-fw-hop-count-tag.h"
+
+namespace ns3 {
+namespace ndn {
+
+TypeId
+FwHopCountTag::GetTypeId ()
+{
+  static TypeId tid = TypeId("ns3::ndn::FwHopCountTag")
+    .SetParent<Tag>()
+    .AddConstructor<FwHopCountTag>()
+    ;
+  return tid;
+}
+
+TypeId
+FwHopCountTag::GetInstanceTypeId () const
+{
+  return FwHopCountTag::GetTypeId ();
+}
+
+uint32_t
+FwHopCountTag::GetSerializedSize () const
+{
+  return sizeof(uint32_t);
+}
+
+void
+FwHopCountTag::Serialize (TagBuffer i) const
+{
+  i.WriteU32 (m_hopCount);
+}
+  
+void
+FwHopCountTag::Deserialize (TagBuffer i)
+{
+  m_hopCount = i.ReadU32 ();
+}
+
+void
+FwHopCountTag::Print (std::ostream &os) const
+{
+  os << m_hopCount;
+}
+
+} // namespace ndn
+} // namespace ns3
diff --git a/utils/ndn-fw-hop-count-tag.h b/utils/ndn-fw-hop-count-tag.h
new file mode 100644
index 0000000..0ed62f0
--- /dev/null
+++ b/utils/ndn-fw-hop-count-tag.h
@@ -0,0 +1,89 @@
+/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
+/*
+ * Copyright (c) 2013 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>
+ */
+
+#ifndef NDN_FW_HOP_COUNT_TAG_H
+#define NDN_FW_HOP_COUNT_TAG_H
+
+#include "ns3/tag.h"
+
+namespace ns3 {
+namespace ndn {
+
+/**
+ * @brief Packet tag that is used to track hop count for Interest-Data pairs
+ */
+class FwHopCountTag : public Tag
+{
+public:
+  static TypeId
+  GetTypeId (void);
+
+  /**
+   * @brief Default constructor
+   */
+  FwHopCountTag () : m_hopCount (0) { };
+
+  /**
+   * @brief Destructor
+   */
+  ~FwHopCountTag () { }
+
+  /**
+   * @brief Increment hop count
+   */
+  void
+  Increment () { m_hopCount ++; }
+
+  /**
+   * @brief Get value of hop count
+   */
+  uint32_t
+  Get () const { return m_hopCount; }
+
+  ////////////////////////////////////////////////////////
+  // from ObjectBase
+  ////////////////////////////////////////////////////////
+  virtual TypeId
+  GetInstanceTypeId () const;
+  
+  ////////////////////////////////////////////////////////
+  // from Tag
+  ////////////////////////////////////////////////////////
+  
+  virtual uint32_t
+  GetSerializedSize () const;
+
+  virtual void
+  Serialize (TagBuffer i) const;
+  
+  virtual void
+  Deserialize (TagBuffer i);
+
+  virtual void
+  Print (std::ostream &os) const;
+  
+private:
+  uint32_t m_hopCount;
+};
+
+} // namespace ndn
+} // namespace ns3
+
+#endif // NDN_FW_HOP_COUNT_TAG_H
diff --git a/utils/tracers/ndn-app-delay-tracer.cc b/utils/tracers/ndn-app-delay-tracer.cc
index b02b3ac..3893d33 100644
--- a/utils/tracers/ndn-app-delay-tracer.cc
+++ b/utils/tracers/ndn-app-delay-tracer.cc
@@ -129,11 +129,12 @@
      << "Type" << "\t"
      << "DelayS" << "\t"
      << "DelayUS" << "\t"
-     << "RetxCount" << "";
+     << "RetxCount" << "\t"
+     << "HopCount"  << "";
 }
 
 void 
-AppDelayTracer::LastRetransmittedInterestDataDelay (Ptr<App> app, uint32_t seqno, Time delay)
+AppDelayTracer::LastRetransmittedInterestDataDelay (Ptr<App> app, uint32_t seqno, Time delay, int32_t hopCount)
 {
   *m_os << m_node << "\t"
         << app->GetId () << "\t"
@@ -141,11 +142,12 @@
         << "LastDelay" << "\t"
         << delay.ToDouble (Time::S) << "\t"
         << delay.ToDouble (Time::US) << "\t"
-        << 1 << "\n";
+        << 1 << "\t"
+        << hopCount << "\n";
 }
   
 void 
-AppDelayTracer::FirstInterestDataDelay (Ptr<App> app, uint32_t seqno, Time delay, uint32_t retxCount)
+AppDelayTracer::FirstInterestDataDelay (Ptr<App> app, uint32_t seqno, Time delay, uint32_t retxCount, int32_t hopCount)
 {
   *m_os << m_node << "\t"
         << app->GetId () << "\t"
@@ -153,7 +155,8 @@
         << "FullDelay" << "\t"
         << delay.ToDouble (Time::S) << "\t"
         << delay.ToDouble (Time::US) << "\t"
-        << retxCount << "\n";
+        << retxCount << "\t"
+        << hopCount << "\n";
 }
 
 
diff --git a/utils/tracers/ndn-app-delay-tracer.h b/utils/tracers/ndn-app-delay-tracer.h
index ac586b2..c7a433c 100644
--- a/utils/tracers/ndn-app-delay-tracer.h
+++ b/utils/tracers/ndn-app-delay-tracer.h
@@ -91,10 +91,10 @@
   Connect ();
 
   void 
-  LastRetransmittedInterestDataDelay (Ptr<App> app, uint32_t seqno, Time delay);
+  LastRetransmittedInterestDataDelay (Ptr<App> app, uint32_t seqno, Time delay, int32_t hopCount);
   
   void 
-  FirstInterestDataDelay (Ptr<App> app, uint32_t seqno, Time delay, uint32_t rextCount);
+  FirstInterestDataDelay (Ptr<App> app, uint32_t seqno, Time delay, uint32_t rextCount, int32_t hopCount);
   
 private:
   std::string m_node;