# HG changeset patch # User Tom Henderson # Date 1227141779 28800 # Node ID 7e06b2dedca19304fddbab38506b687c4142c01e # Parent 9c1f3687b0c282a47d334c1e8f49996997a68813 close pcap files at end of simulation diff -r 9c1f3687b0c2 -r 7e06b2dedca1 src/common/pcap-writer.cc --- a/src/common/pcap-writer.cc Wed Nov 19 16:41:17 2008 -0800 +++ b/src/common/pcap-writer.cc Wed Nov 19 16:42:59 2008 -0800 @@ -46,10 +46,7 @@ PcapWriter::PcapWriter () PcapWriter::~PcapWriter () { - if (m_writer != 0) - { - m_writer->close (); - } + Close(); delete m_writer; m_writer = 0; } @@ -59,6 +56,15 @@ PcapWriter::Open (std::string const &nam { m_writer = new std::ofstream (); m_writer->open (name.c_str ()); +} + +void +PcapWriter::Close () +{ + if (m_writer != 0 && m_writer->is_open ()) + { + m_writer->close (); + } } void diff -r 9c1f3687b0c2 -r 7e06b2dedca1 src/common/pcap-writer.h --- a/src/common/pcap-writer.h Wed Nov 19 16:41:17 2008 -0800 +++ b/src/common/pcap-writer.h Wed Nov 19 16:42:59 2008 -0800 @@ -50,6 +50,11 @@ public: void Open (std::string const &name); /** + * Close file. + */ + void Close (void); + + /** * Write a pcap header in the output file which specifies * that the content of the file will be Packets with * Ethernet/LLC/SNAP encapsulation. This method should diff -r 9c1f3687b0c2 -r 7e06b2dedca1 src/helper/csma-helper.cc --- a/src/helper/csma-helper.cc Wed Nov 19 16:41:17 2008 -0800 +++ b/src/helper/csma-helper.cc Wed Nov 19 16:42:59 2008 -0800 @@ -30,11 +30,29 @@ namespace ns3 { +std::vector CsmaHelper::m_traces; + CsmaHelper::CsmaHelper () { m_queueFactory.SetTypeId ("ns3::DropTailQueue"); m_deviceFactory.SetTypeId ("ns3::CsmaNetDevice"); m_channelFactory.SetTypeId ("ns3::CsmaChannel"); +} + +void +CsmaHelper::Cleanup (void) +{ + uint32_t illegal = std::numeric_limits::max(); + + for (std::vector::iterator i = m_traces.begin (); + i != m_traces.end (); i++) + { + i->nodeId = illegal; + i->deviceId = illegal; + i->writer->Close(); + i->writer = 0; + } + m_traces.clear (); } void @@ -95,6 +113,12 @@ CsmaHelper::EnablePcap (std::string file oss.str (""); oss << "/NodeList/" << nodeid << "/DeviceList/" << deviceid << "/$ns3::CsmaNetDevice/TxQueue/Enqueue"; Config::ConnectWithoutContext (oss.str (), MakeBoundCallback (&CsmaHelper::EnqueueEvent, pcap)); + // Store pcap pointer for later destruction + CsmaHelper::Trace trace; + trace.nodeId = nodeid; + trace.deviceId = deviceid; + trace.writer = pcap; + m_traces.push_back (trace); } void CsmaHelper::EnablePcap (std::string filename, NetDeviceContainer d) @@ -123,6 +147,8 @@ void void CsmaHelper::EnablePcapAll (std::string filename) { + Simulator::ScheduleDestroy (&CsmaHelper::Cleanup); + EnablePcap (filename, NodeContainer::GetGlobal ()); } @@ -271,4 +297,19 @@ CsmaHelper::AsciiRxEvent (std::ostream * *os << path << " " << *packet << std::endl; } +Ptr +CsmaHelper::GetStream (uint32_t nodeId, uint32_t deviceId) +{ + for (std::vector::iterator i = m_traces.begin (); + i != m_traces.end (); i++) + { + if (i->nodeId == nodeId && + i->deviceId == deviceId) + { + return i->writer; + } + } + return 0; +} + } // namespace ns3 diff -r 9c1f3687b0c2 -r 7e06b2dedca1 src/helper/csma-helper.h --- a/src/helper/csma-helper.h Wed Nov 19 16:41:17 2008 -0800 +++ b/src/helper/csma-helper.h Wed Nov 19 16:42:59 2008 -0800 @@ -240,6 +240,8 @@ public: NetDeviceContainer& hubDevices, NetDeviceContainer& spokeDevices); private: + static void Cleanup (void); + Ptr InstallPriv (Ptr node, Ptr channel) const; static void RxEvent (Ptr writer, Ptr packet); @@ -251,6 +253,14 @@ private: ObjectFactory m_queueFactory; ObjectFactory m_deviceFactory; ObjectFactory m_channelFactory; + + static Ptr GetStream (uint32_t nodeId, uint32_t deviceId); + struct Trace { + uint32_t nodeId; + uint32_t deviceId; + Ptr writer; + }; + static std::vector m_traces; }; diff -r 9c1f3687b0c2 -r 7e06b2dedca1 src/helper/point-to-point-helper.cc --- a/src/helper/point-to-point-helper.cc Wed Nov 19 16:41:17 2008 -0800 +++ b/src/helper/point-to-point-helper.cc Wed Nov 19 16:42:59 2008 -0800 @@ -26,15 +26,31 @@ #include "ns3/config.h" #include "ns3/packet.h" - namespace ns3 { +std::vector PointToPointHelper::m_traces; PointToPointHelper::PointToPointHelper () { m_queueFactory.SetTypeId ("ns3::DropTailQueue"); m_deviceFactory.SetTypeId ("ns3::PointToPointNetDevice"); m_channelFactory.SetTypeId ("ns3::PointToPointChannel"); +} + +void +PointToPointHelper::Cleanup (void) +{ + uint32_t illegal = std::numeric_limits::max(); + + for (std::vector::iterator i = m_traces.begin (); + i != m_traces.end (); i++) + { + i->nodeId = illegal; + i->deviceId = illegal; + i->writer->Close(); + i->writer = 0; + } + m_traces.clear (); } void @@ -95,6 +111,12 @@ PointToPointHelper::EnablePcap (std::str oss.str (""); oss << "/NodeList/" << nodeid << "/DeviceList/" << deviceid << "/$ns3::PointToPointNetDevice/TxQueue/Enqueue"; Config::ConnectWithoutContext (oss.str (), MakeBoundCallback (&PointToPointHelper::EnqueueEvent, pcap)); + // Store pcap pointer for later destruction + PointToPointHelper::Trace trace; + trace.nodeId = nodeid; + trace.deviceId = deviceid; + trace.writer = pcap; + m_traces.push_back (trace); } void PointToPointHelper::EnablePcap (std::string filename, NetDeviceContainer d) @@ -123,6 +145,8 @@ void void PointToPointHelper::EnablePcapAll (std::string filename) { + Simulator::ScheduleDestroy (&PointToPointHelper::Cleanup); + EnablePcap (filename, NodeContainer::GetGlobal ()); } @@ -251,5 +275,19 @@ PointToPointHelper::AsciiRxEvent (std::o *os << path << " " << *packet << std::endl; } +Ptr +PointToPointHelper::GetStream (uint32_t nodeId, uint32_t deviceId) +{ + for (std::vector::iterator i = m_traces.begin (); + i != m_traces.end (); i++) + { + if (i->nodeId == nodeId && + i->deviceId == deviceId) + { + return i->writer; + } + } + return 0; +} } // namespace ns3 diff -r 9c1f3687b0c2 -r 7e06b2dedca1 src/helper/point-to-point-helper.h --- a/src/helper/point-to-point-helper.h Wed Nov 19 16:41:17 2008 -0800 +++ b/src/helper/point-to-point-helper.h Wed Nov 19 16:42:59 2008 -0800 @@ -208,6 +208,8 @@ public: NetDeviceContainer& hubDevices, NetDeviceContainer& spokeDevices); private: + static void Cleanup (void); + void EnablePcap (Ptr node, Ptr device, Ptr queue); void EnableAscii (Ptr node, Ptr device); static void RxEvent (Ptr writer, Ptr packet); @@ -219,6 +221,14 @@ private: ObjectFactory m_queueFactory; ObjectFactory m_channelFactory; ObjectFactory m_deviceFactory; + + static Ptr GetStream (uint32_t nodeId, uint32_t deviceId); + struct Trace { + uint32_t nodeId; + uint32_t deviceId; + Ptr writer; + }; + static std::vector m_traces; };