utils: draft implementation for queue drop tracer
diff --git a/utils/tracers/l2-rate-tracer.cc b/utils/tracers/l2-rate-tracer.cc
new file mode 100644
index 0000000..823977b
--- /dev/null
+++ b/utils/tracers/l2-rate-tracer.cc
@@ -0,0 +1,155 @@
+/* -*- 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 "l2-rate-tracer.h"
+#include "ns3/node.h"
+#include "ns3/packet.h"
+#include "ns3/config.h"
+#include "ns3/callback.h"
+#include "ns3/simulator.h"
+#include "ns3/node-list.h"
+#include "ns3/node.h"
+#include "ns3/log.h"
+
+#include <boost/lexical_cast.hpp>
+#include <fstream>
+
+using namespace boost;
+using namespace std;
+
+NS_LOG_COMPONENT_DEFINE ("L2RateTracer");
+
+namespace ns3 {
+
+boost::tuple< boost::shared_ptr<std::ostream>, std::list<Ptr<L2RateTracer> > >
+L2RateTracer::InstallAll (const std::string &file, Time averagingPeriod/* = Seconds (0.5)*/)
+{
+  std::list<Ptr<L2RateTracer> > tracers;
+  boost::shared_ptr<std::ofstream> outputStream (new std::ofstream ());
+  outputStream->open (file.c_str (), std::ios_base::out | std::ios_base::trunc);
+
+  if (!outputStream->is_open ())
+    return boost::make_tuple (outputStream, tracers);
+
+  for (NodeList::Iterator node = NodeList::Begin ();
+       node != NodeList::End ();
+       node++)
+    {
+      NS_LOG_DEBUG ("Node: " << lexical_cast<string> ((*node)->GetId ()));
+
+      Ptr<L2RateTracer> trace = Create<L2RateTracer> (outputStream, *node);
+      trace->SetAveragingPeriod (averagingPeriod);
+      tracers.push_back (trace);
+    }
+
+  if (tracers.size () > 0)
+    {
+      // *m_l3RateTrace << "# "; // not necessary for R's read.table
+      tracers.front ()->PrintHeader (*outputStream);
+      *outputStream << "\n";
+    }
+
+  return boost::make_tuple (outputStream, tracers);
+}
+
+
+L2RateTracer::L2RateTracer (boost::shared_ptr<std::ostream> os, Ptr<Node> node)
+  : L2Tracer (node)
+  , m_os (os)
+{
+  SetAveragingPeriod (Seconds (1.0));
+}
+
+L2RateTracer::~L2RateTracer ()
+{
+  m_printEvent.Cancel ();
+}
+
+void
+L2RateTracer::SetAveragingPeriod (const Time &period)
+{
+  m_period = period;
+  m_printEvent.Cancel ();
+  m_printEvent = Simulator::Schedule (m_period, &L2RateTracer::PeriodicPrinter, this);
+}
+
+void
+L2RateTracer::PeriodicPrinter ()
+{
+  Print (*m_os);
+  Reset ();
+
+  m_printEvent = Simulator::Schedule (m_period, &L2RateTracer::PeriodicPrinter, this);
+}
+
+void
+L2RateTracer::PrintHeader (std::ostream &os) const
+{
+  os << "Time" << "\t"
+
+     << "Node" << "\t"
+     << "Interface" << "\t"
+
+     << "Type" << "\t"
+     << "Packets" << "\t"
+     << "Kilobytes";
+}
+
+void
+L2RateTracer::Reset ()
+{
+  m_stats.get<0> ().Reset ();
+  m_stats.get<1> ().Reset ();
+}
+
+const double alpha = 0.8;
+
+#define STATS(INDEX) m_stats.get<INDEX> ()
+#define RATE(INDEX, fieldName) STATS(INDEX).fieldName / m_period.ToDouble (Time::S)
+
+#define PRINTER(printName, fieldName, interface)                        \
+STATS(2).fieldName = /*new value*/alpha * RATE(0, fieldName) + /*old value*/(1-alpha) * STATS(2).fieldName; \
+STATS(3).fieldName = /*new value*/alpha * RATE(1, fieldName) / 1024.0 + /*old value*/(1-alpha) * STATS(3).fieldName; \
+                                                                        \
+ os << time.ToDouble (Time::S) << "\t"                                  \
+ << m_node << "\t"                                                      \
+ << interface << "\t"                                                  \
+ << printName << "\t"                                                   \
+ << STATS(2).fieldName  << "\t"                                         \
+ << STATS(3).fieldName << "\n";
+
+void
+L2RateTracer::Print (std::ostream &os) const
+{
+  Time time = Simulator::Now ();
+
+  PRINTER ("Drop", m_drop, "combined");
+}
+
+void
+L2RateTracer::Drop (Ptr<const Packet> packet)
+{
+  // no interface information... this should be part of this L2Tracer object data
+
+  m_stats.get<0> ().m_drop ++;
+  m_stats.get<1> ().m_drop += packet->GetSize ();
+}
+
+} // namespace ns3
diff --git a/utils/tracers/l2-rate-tracer.h b/utils/tracers/l2-rate-tracer.h
new file mode 100644
index 0000000..48105ac
--- /dev/null
+++ b/utils/tracers/l2-rate-tracer.h
@@ -0,0 +1,90 @@
+/* -*- 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 L2_RATE_TRACER_H
+#define L2_RATE_TRACER_H
+
+#include "l2-tracer.h"
+
+#include "ns3/nstime.h"
+#include "ns3/event-id.h"
+
+#include <boost/tuple/tuple.hpp>
+#include <boost/shared_ptr.hpp>
+#include <map>
+
+namespace ns3 {
+
+/**
+ * @ingroup ndn
+ */
+class L2RateTracer : public L2Tracer
+{
+public:
+  /**
+   * @brief Network layer tracer constructor
+   */
+  L2RateTracer (boost::shared_ptr<std::ostream> os, Ptr<Node> node);
+  virtual ~L2RateTracer ();
+
+  /**
+   * @brief Helper method to install tracers on all simulation nodes
+   *
+   * @param file File to which traces will be written
+   * @param averagingPeriod Defines averaging period for the rate calculation,
+   *        as well as how often data will be written into the trace file (default, every half second)
+   *
+   * @returns a tuple of reference to output stream and list of tracers. !!! Attention !!! This tuple needs to be preserved
+   *          for the lifetime of simulation, otherwise SEGFAULTs are inevitable
+   *
+   */
+  static boost::tuple< boost::shared_ptr<std::ostream>, std::list<Ptr<L2RateTracer> > >
+  InstallAll (const std::string &file, Time averagingPeriod = Seconds (0.5));
+
+  void
+  SetAveragingPeriod (const Time &period);
+
+  virtual void
+  PrintHeader (std::ostream &os) const;
+
+  virtual void
+  Print (std::ostream &os) const;
+
+  virtual void
+  Drop (Ptr<const Packet>);
+
+private:
+  void
+  PeriodicPrinter ();
+
+  void
+  Reset ();
+
+private:
+  boost::shared_ptr<std::ostream> m_os;
+  Time m_period;
+  EventId m_printEvent;
+
+  mutable boost::tuple<Stats, Stats, Stats, Stats> m_stats;
+};
+
+} // namespace ns3
+
+#endif // L2_RATE_TRACER_H
diff --git a/utils/tracers/l2-tracer.cc b/utils/tracers/l2-tracer.cc
new file mode 100644
index 0000000..a3dc05b
--- /dev/null
+++ b/utils/tracers/l2-tracer.cc
@@ -0,0 +1,54 @@
+/* -*- 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 "l2-tracer.h"
+#include "ns3/node.h"
+#include "ns3/config.h"
+#include "ns3/names.h"
+#include "ns3/callback.h"
+
+#include <boost/lexical_cast.hpp>
+
+using namespace std;
+
+namespace ns3 {
+
+L2Tracer::L2Tracer (Ptr<Node> node)
+: m_nodePtr (node)
+{
+  m_node = boost::lexical_cast<string> (m_nodePtr->GetId ());
+
+  Connect ();
+
+  string name = Names::FindName (node);
+  if (!name.empty ())
+    {
+      m_node = name;
+    }
+}
+
+void
+L2Tracer::Connect ()
+{
+  Config::ConnectWithoutContext ("/NodeList/"+m_node+"/DeviceList/*/TxQueue/Drop",
+                                 MakeCallback (&L2Tracer::Drop, this));
+}
+
+} // namespace ns3
diff --git a/utils/tracers/l2-tracer.h b/utils/tracers/l2-tracer.h
new file mode 100644
index 0000000..a937d66
--- /dev/null
+++ b/utils/tracers/l2-tracer.h
@@ -0,0 +1,84 @@
+/* -*- 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 L2_TRACER_H
+#define L2_TRACER_H
+
+#include "ns3/ptr.h"
+#include "ns3/simple-ref-count.h"
+#include "ns3/packet.h"
+
+namespace ns3 {
+
+class Node;
+
+class L2Tracer : public SimpleRefCount<L2Tracer>
+{
+public:
+  L2Tracer (Ptr<Node> node);
+  virtual ~L2Tracer () { };
+
+  void
+  Connect ();
+
+  virtual void
+  PrintHeader (std::ostream &os) const = 0;
+
+  virtual void
+  Print (std::ostream &os) const = 0;
+
+  virtual void
+  Drop (Ptr<const Packet>) = 0;
+
+  // Rx/Tx is NetDevice specific
+  // please refer to pyviz.cc in order to extend this tracer
+
+protected:
+  std::string m_node;
+  Ptr<Node> m_nodePtr;
+
+  struct Stats
+  {
+    void Reset ()
+    {
+      m_in = 0;
+      m_out = 0;
+      m_drop = 0;
+    }
+
+    uint64_t m_in;
+    uint64_t m_out;
+    uint64_t m_drop;
+  };
+};
+
+inline std::ostream&
+operator << (std::ostream &os, const L2Tracer &tracer)
+{
+  os << "# ";
+  tracer.PrintHeader (os);
+  os << "\n";
+  tracer.Print (os);
+  return os;
+}
+
+} // namespace ns3
+
+#endif // L2_TRACER_H