diff --git a/src/helper/point-to-point-helper.cc b/src/helper/point-to-point-helper.cc index b7f9d21bf..31c846a0c 100644 --- a/src/helper/point-to-point-helper.cc +++ b/src/helper/point-to-point-helper.cc @@ -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 @@ PointToPointHelper::SetChannelParameter (std::string n1, Attribute v1) } +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, Ptr device, Ptr queue) +{ + std::ostringstream oss; + oss << m_pcapFilename << "-" << node->GetId () << "-" << device->GetIfIndex (); + std::string filename = oss.str (); + Ptr pcap = Create (); + pcap->Open (filename); + pcap->WriteEthernetHeader (); + device->TraceConnectWithoutContext ("Rx", MakeBoundCallback (&PointToPointHelper::RxEvent, pcap)); + queue->TraceConnectWithoutContext ("Enqueue", MakeBoundCallback (&PointToPointHelper::EnqueueEvent, pcap)); +} + +void +PointToPointHelper::EnableAscii (Ptr node, Ptr 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 @@ PointToPointHelper::Build (Ptr a, Ptr b) Ptr channel = m_channelFactory.Create (); 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 writer, Ptr packet) +{ + writer->WritePacket (packet); +} +void +PointToPointHelper::RxEvent (Ptr writer, Ptr packet) +{ + writer->WritePacket (packet); +} +void +PointToPointHelper::AsciiEvent (std::ostream *os, std::string path, Ptr packet) +{ + *os << path << " " << *packet << std::endl; +} + } // namespace ns3 diff --git a/src/helper/point-to-point-helper.h b/src/helper/point-to-point-helper.h index 0b48c683f..50b6164d8 100644 --- a/src/helper/point-to-point-helper.h +++ b/src/helper/point-to-point-helper.h @@ -8,6 +8,11 @@ namespace ns3 { +class Queue; +class NetDevice; +class Node; +class PcapWriter; + /** * \brief build a set of PointToPointNetDevice objects */ @@ -54,6 +59,36 @@ public: */ 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 * @@ -74,9 +109,18 @@ public: NetDeviceContainer Build (Ptr a, Ptr b); private: + void EnablePcap (Ptr node, Ptr device, Ptr queue); + void EnableAscii (Ptr node, Ptr device); + static void RxEvent (Ptr writer, Ptr packet); + static void EnqueueEvent (Ptr writer, Ptr packet); + static void AsciiEvent (std::ostream *os, std::string path, Ptr packet); ObjectFactory m_queueFactory; ObjectFactory m_channelFactory; ObjectFactory m_deviceFactory; + bool m_pcap; + std::string m_pcapFilename; + bool m_ascii; + std::ostream *m_asciiOs; };