add ascii/pcap trace helpers
authorMathieu Lacage <mathieu.lacage@sophia.inria.fr>
Fri, 28 Mar 2008 13:05:44 -0700
changeset 2786 f261e50396ac
parent 2785 8d6957b68b8b
child 2787 83b3e68557e1
add ascii/pcap trace helpers
src/helper/point-to-point-helper.cc
src/helper/point-to-point-helper.h
--- a/src/helper/point-to-point-helper.cc	Fri Mar 28 12:49:39 2008 -0700
+++ b/src/helper/point-to-point-helper.cc	Fri Mar 28 13:05:44 2008 -0700
@@ -2,6 +2,10 @@
 #include "ns3/point-to-point-net-device.h"
 #include "ns3/point-to-point-channel.h"
 #include "ns3/queue.h"
+#include "ns3/pcap-writer.h"
+#include "ns3/config.h"
+#include "ns3/packet.h"
+
 
 namespace ns3 {
 
@@ -40,6 +44,58 @@
 }
 
 
+void 
+PointToPointHelper::EnablePcap (std::string filename)
+{
+  m_pcap = true;
+  m_pcapFilename = filename;
+}
+void 
+PointToPointHelper::DisablePcap (void)
+{
+  m_pcap = false;
+}
+
+void 
+PointToPointHelper::EnableAscii (std::ostream &os)
+{
+  m_ascii = true;
+  m_asciiOs = &os;
+}
+void 
+PointToPointHelper::DisableAscii (void)
+{
+  m_ascii = false;
+}
+
+void
+PointToPointHelper::EnablePcap (Ptr<Node> node, Ptr<NetDevice> device, Ptr<Queue> queue)
+{
+  std::ostringstream oss;
+  oss << m_pcapFilename << "-" << node->GetId () << "-" << device->GetIfIndex ();
+  std::string filename = oss.str ();
+  Ptr<PcapWriter> pcap = Create<PcapWriter> ();
+  pcap->Open (filename);
+  pcap->WriteEthernetHeader ();
+  device->TraceConnectWithoutContext ("Rx", MakeBoundCallback (&PointToPointHelper::RxEvent, pcap));
+  queue->TraceConnectWithoutContext ("Enqueue", MakeBoundCallback (&PointToPointHelper::EnqueueEvent, pcap));
+}
+
+void
+PointToPointHelper::EnableAscii (Ptr<Node> node, Ptr<NetDevice> device)
+{
+  Packet::EnableMetadata ();
+  std::ostringstream oss;
+  oss << "/NodeList/" << node->GetId () << "/DeviceList/" << device->GetIfIndex () << "/Rx";
+  Config::Connect (oss.str (), MakeBoundCallback (&PointToPointHelper::AsciiEvent, m_asciiOs));
+  oss.str ("");
+  oss << "/NodeList/" << node->GetId () << "/DeviceList/" << device->GetIfIndex () << "/TxQueue/Enqueue";
+  Config::Connect (oss.str (), MakeBoundCallback (&PointToPointHelper::AsciiEvent, m_asciiOs));
+  oss.str ("");
+  oss << "/NodeList/" << node->GetId () << "/DeviceList/" << device->GetIfIndex () << "/TxQueue/Dequeue";
+  Config::Connect (oss.str (), MakeBoundCallback (&PointToPointHelper::AsciiEvent, m_asciiOs));
+}
+
 NetDeviceContainer 
 PointToPointHelper::Build (NodeContainer c)
 {
@@ -64,11 +120,38 @@
   Ptr<PointToPointChannel> channel = m_channelFactory.Create<PointToPointChannel> ();
   devA->Attach (channel);
   devB->Attach (channel);
+  if (m_pcap)
+    {
+      EnablePcap (a, devA, queueA);
+      EnablePcap (b, devB, queueB);
+    }
+  if (m_ascii)
+    {
+      EnableAscii (a, devA);
+      EnableAscii (b, devB);
+    }
+
   container.Add (devA);
   container.Add (devB);
 
   return container;
 }
 
+void 
+PointToPointHelper::EnqueueEvent (Ptr<PcapWriter> writer, Ptr<const Packet> packet)
+{
+  writer->WritePacket (packet);
+}
+void 
+PointToPointHelper::RxEvent (Ptr<PcapWriter> writer, Ptr<const Packet> packet)
+{
+  writer->WritePacket (packet);
+}
+void 
+PointToPointHelper::AsciiEvent (std::ostream *os, std::string path, Ptr<const Packet> packet)
+{
+  *os << path << " " << *packet << std::endl;
+}
+
 
 } // namespace ns3
--- a/src/helper/point-to-point-helper.h	Fri Mar 28 12:49:39 2008 -0700
+++ b/src/helper/point-to-point-helper.h	Fri Mar 28 13:05:44 2008 -0700
@@ -8,6 +8,11 @@
 
 namespace ns3 {
 
+class Queue;
+class NetDevice;
+class Node;
+class PcapWriter;
+
 /**
  * \brief build a set of PointToPointNetDevice objects
  */
@@ -55,6 +60,36 @@
   void SetChannelParameter (std::string name, Attribute value);
 
   /**
+   * \param filename file template to dump pcap traces in.
+   *
+   * Every ns3::PointToPointNetDevice created through subsequent calls
+   * to PointToPointHelper::Build will be configured to dump
+   * pcap output in a file named filename-nodeid-deviceid.
+   */
+  void EnablePcap (std::string filename);
+  /**
+   * Every ns3::PointToPointNetDevice created through subsequent calls
+   * to PointToPointHelper::Build will be configured to not dump any pcap
+   * output.
+   */
+  void DisablePcap (void);
+
+  /**
+   * \param os an output stream where ascii trace should be sent.
+   *
+   * Every ns3::PointToPointNetDevice created through subsequent calls
+   * to PointToPointHelper::Build will be configured to dump Rx, EnQueue
+   * and Dequeue events as ascii data in the specified output stream.
+   */
+  void EnableAscii (std::ostream &os);
+  /**
+   * Every ns3::PointToPointNetDevice created through subsequent calls
+   * to PointToPointHelper::Build will be configured to not dump any
+   * ascii output.
+   */
+  void DisableAscii (void);
+
+  /**
    * \param c a set of nodes
    *
    * This method creates a ns3::PointToPointChannel with the
@@ -74,9 +109,18 @@
   NetDeviceContainer Build (Ptr<Node> a, Ptr<Node> b);
 
 private:
+  void EnablePcap (Ptr<Node> node, Ptr<NetDevice> device, Ptr<Queue> queue);
+  void EnableAscii (Ptr<Node> node, Ptr<NetDevice> device);
+  static void RxEvent (Ptr<PcapWriter> writer, Ptr<const Packet> packet);
+  static void EnqueueEvent (Ptr<PcapWriter> writer, Ptr<const Packet> packet);
+  static void AsciiEvent (std::ostream *os, std::string path, Ptr<const Packet> packet);
   ObjectFactory m_queueFactory;
   ObjectFactory m_channelFactory;
   ObjectFactory m_deviceFactory;
+  bool m_pcap;
+  std::string m_pcapFilename;
+  bool m_ascii;
+  std::ostream *m_asciiOs;
 };