utils: Adding content store trace helper class (thanks to Xiaoyan Hu)
diff --git a/utils/tracers/ndn-cs-imp-tracer.cc b/utils/tracers/ndn-cs-imp-tracer.cc
new file mode 100644
index 0000000..3016bf4
--- /dev/null
+++ b/utils/tracers/ndn-cs-imp-tracer.cc
@@ -0,0 +1,164 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
+/*
+ * Copyright (c) 2011 UCLA
+ *
+ * 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-cs-imp-tracer.h"
+
+#include "ns3/node.h"
+#include "ns3/packet.h"
+#include "ns3/config.h"
+#include "ns3/callback.h"
+#include "ns3/ndn-app.h"
+#include "ns3/ndn-interest.h"
+#include "ns3/ndn-content-object.h"
+#include "ns3/simulator.h"
+#include "ns3/node-list.h"
+#include "ns3/log.h"
+
+#include <fstream>
+
+NS_LOG_COMPONENT_DEFINE ("ndn.CsImpTracer");
+
+namespace ns3 {
+namespace ndn {
+
+boost::tuple< boost::shared_ptr<std::ostream>, std::list<Ptr<CsImpTracer> > >
+CsImpTracer::InstallAll (const std::string &file, Time averagingPeriod/* = Seconds (0.5)*/)
+{
+ using namespace boost;
+ using namespace std;
+
+ std::list<Ptr<CsImpTracer> > 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: " << (*node)->GetId ());
+
+ Ptr<CsImpTracer> trace = Create<CsImpTracer> (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);
+}
+
+
+
+CsImpTracer::CsImpTracer (boost::shared_ptr<std::ostream> os, Ptr<Node> node)
+ : CsTracer (node)
+ , m_os (os)
+{
+ Reset ();
+}
+
+CsImpTracer::CsImpTracer (boost::shared_ptr<std::ostream> os, const std::string &node)
+ : CsTracer (node)
+ , m_os (os)
+{
+ Reset ();
+}
+
+CsImpTracer::~CsImpTracer ()
+{
+};
+
+void
+CsImpTracer::SetAveragingPeriod (const Time &period)
+{
+ m_period = period;
+ m_printEvent.Cancel ();
+ m_printEvent = Simulator::Schedule (m_period, &CsImpTracer::PeriodicPrinter, this);
+}
+
+void
+CsImpTracer::PeriodicPrinter ()
+{
+ Print (*m_os);
+ Reset ();
+
+ m_printEvent = Simulator::Schedule (m_period, &CsImpTracer::PeriodicPrinter, this);
+}
+
+void
+CsImpTracer::PrintHeader (std::ostream &os) const
+{
+ os << "Time" << "\t"
+
+ << "Node" << "\t"
+// << "FaceId" << "\t"
+ // << "FaceDescr" << "\t"
+
+ << "Type" << "\t"
+ << "Packets" << "\t";
+// << "Kilobytes";
+}
+
+void
+CsImpTracer::Reset ()
+{
+ m_stats.Reset();
+}
+
+#define PRINTER(printName, fieldName) \
+ os << time.ToDouble (Time::S) << "\t" \
+ << m_node << "\t" \
+ << printName << "\t" \
+ << m_stats.fieldName << "\n";
+
+
+void
+CsImpTracer::Print (std::ostream &os) const
+{
+ Time time = Simulator::Now ();
+
+ PRINTER ("CacheHits", m_cacheHits);
+ PRINTER ("CacheMisses", m_cacheMisses);
+}
+
+void
+CsImpTracer::CacheHits (std::string context,
+ Ptr<const InterestHeader>, Ptr<const ContentObjectHeader>)
+{
+ m_stats.m_cacheHits ++;
+}
+
+void
+CsImpTracer::CacheMisses (std::string context,
+ Ptr<const InterestHeader>)
+{
+ m_stats.m_cacheMisses ++;
+}
+
+
+} // namespace ndn
+} // namespace ns3
diff --git a/utils/tracers/ndn-cs-imp-tracer.h b/utils/tracers/ndn-cs-imp-tracer.h
new file mode 100644
index 0000000..bc9c48d
--- /dev/null
+++ b/utils/tracers/ndn-cs-imp-tracer.h
@@ -0,0 +1,115 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
+/*
+ * Copyright (c) 2011 UCLA
+ *
+ * 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_CS_IMP_TRACER_H
+#define NDN_CS_IMP_TRACER_H
+
+#include "ndn-cs-tracer.h"
+
+#include <ns3/nstime.h>
+#include <ns3/event-id.h>
+
+#include <boost/tuple/tuple.hpp>
+#include <boost/shared_ptr.hpp>
+#include <map>
+#include <list>
+
+namespace ns3 {
+namespace ndn {
+
+/**
+ * @ingroup ndn
+ * @brief CCNx network-layer tracer for aggregate packet counts
+ */
+class CsImpTracer : public CsTracer
+{
+public:
+ /**
+ * @brief Trace constructor that attaches to the node using node pointer
+ * @param os reference to the output stream
+ * @param node pointer to the node
+ */
+ CsImpTracer (boost::shared_ptr<std::ostream> os, Ptr<Node> node);
+
+ /**
+ * @brief Trace constructor that attaches to the node using node name
+ * @param os reference to the output stream
+ * @param nodeName name of the node registered using Names::Add
+ */
+ CsImpTracer (boost::shared_ptr<std::ostream> os, const std::string &nodeName);
+
+ /**
+ * @brief Destructor
+ */
+ virtual ~CsImpTracer ();
+
+ /**
+ * @brief Helper method to install tracers on all simulation nodes
+ *
+ * @param file File to which traces will be written
+ * @param averagingPeriod 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<CsImpTracer> > >
+ InstallAll (const std::string &file, Time averagingPeriod = Seconds (0.5));
+
+protected:
+ // from CsTracer
+ virtual void
+ PrintHeader (std::ostream &os) const;
+
+ virtual void
+ Print (std::ostream &os) const;
+
+ virtual void
+ CacheHits (std::string context,
+ Ptr<const InterestHeader>, Ptr<const ContentObjectHeader>);
+
+ virtual void
+ CacheMisses (std::string context,
+ Ptr<const InterestHeader>);
+
+protected:
+ void
+ SetAveragingPeriod (const Time &period);
+
+ void
+ Reset ();
+
+ void
+ PeriodicPrinter ();
+
+protected:
+ boost::shared_ptr<std::ostream> m_os;
+
+ Time m_period;
+ EventId m_printEvent;
+ Stats m_stats;
+
+// mutable std::map<Ptr<const Face>, boost::tuple<Stats, Stats> > m_stats;
+};
+
+} // namespace ndn
+} // namespace ns3
+
+#endif // NDN_CS_IMP_TRACER_H
diff --git a/utils/tracers/ndn-cs-tracer.cc b/utils/tracers/ndn-cs-tracer.cc
new file mode 100644
index 0000000..742961f
--- /dev/null
+++ b/utils/tracers/ndn-cs-tracer.cc
@@ -0,0 +1,73 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
+/*
+ * Copyright (c) 2011 UCLA
+ *
+ * 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-cs-tracer.h"
+#include "ns3/node.h"
+#include "ns3/packet.h"
+#include "ns3/config.h"
+#include "ns3/names.h"
+#include "ns3/callback.h"
+
+#include <boost/lexical_cast.hpp>
+
+#include "ns3/ndn-interest.h"
+#include "ns3/ndn-content-object.h"
+
+using namespace std;
+
+namespace ns3 {
+namespace ndn {
+
+CsTracer::CsTracer (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;
+ }
+}
+
+CsTracer::CsTracer (const std::string &node)
+: m_node (node)
+{
+ Connect ();
+}
+
+CsTracer::~CsTracer ()
+{
+};
+
+
+void
+CsTracer::Connect ()
+{
+ Config::Connect ("/NodeList/"+m_node+"/$ns3::ndn::ContentStore/CacheHits",
+ MakeCallback (&CsTracer::CacheHits, this));
+ Config::Connect ("/NodeList/"+m_node+"/$ns3::ndn::ContentStore/CacheMisses",
+ MakeCallback (&CsTracer::CacheMisses, this));
+}
+
+} // namespace ndn
+} // namespace ns3
diff --git a/utils/tracers/ndn-cs-tracer.h b/utils/tracers/ndn-cs-tracer.h
new file mode 100644
index 0000000..5b057f7
--- /dev/null
+++ b/utils/tracers/ndn-cs-tracer.h
@@ -0,0 +1,121 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
+/*
+ * Copyright (c) 2011-2012 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: Xiaoyan Hu <x......u@gmail.com>
+ * Alexander Afanasyev <alexander.afanasyev@ucla.edu>
+ */
+
+#ifndef CCNX_CS_TRACER_H
+#define CCNX_CS_TRACER_H
+
+#include "ns3/ptr.h"
+#include "ns3/simple-ref-count.h"
+
+namespace ns3 {
+
+class Node;
+class Packet;
+
+namespace ndn {
+
+class InterestHeader;
+class ContentObjectHeader;
+
+/**
+ * @brief Base class for content store tracers (CacheHits and CacheMisses)
+ */
+class CsTracer : public SimpleRefCount<CsTracer>
+{
+public:
+ /**
+ * @brief Trace constructor that attaches to the node using node pointer
+ * @param node pointer to the node
+ */
+ CsTracer (Ptr<Node> node);
+
+ /**
+ * @brief Trace constructor that attaches to the node using node name
+ * @param nodeName name of the node registered using Names::Add
+ */
+ CsTracer (const std::string &node);
+
+ /**
+ * @brief Destructor
+ */
+ virtual ~CsTracer ();
+
+ /**
+ * @brief Print head of the trace (e.g., for post-processing)
+ *
+ * @param os reference to output stream
+ */
+ virtual void
+ PrintHeader (std::ostream &os) const = 0;
+
+ /**
+ * @brief Print current trace data
+ *
+ * @param os reference to output stream
+ */
+ virtual void
+ Print (std::ostream &os) const = 0;
+
+protected:
+ void
+ Connect ();
+
+ virtual void
+ CacheHits (std::string context,
+ Ptr<const InterestHeader>, Ptr<const ContentObjectHeader>) = 0;
+
+ virtual void
+ CacheMisses (std::string context,
+ Ptr<const InterestHeader>) = 0;
+
+protected:
+ std::string m_node;
+ Ptr<Node> m_nodePtr;
+
+ struct Stats
+ {
+ inline void Reset ()
+ {
+ m_cacheHits = 0;
+ m_cacheMisses = 0;
+ }
+ double m_cacheHits;
+ double m_cacheMisses;
+ };
+};
+
+/**
+ * @brief Helper to dump the trace to an output stream
+ */
+inline std::ostream&
+operator << (std::ostream &os, const CsTracer &tracer)
+{
+ os << "# ";
+ tracer.PrintHeader (os);
+ os << "\n";
+ tracer.Print (os);
+ return os;
+}
+
+} // namespace ndn
+} // namespace ns3
+
+#endif // CCNX_CS_TRACER_H