From e39f1ebe1072fa02735c4d9917a47b17070619bc Mon Sep 17 00:00:00 2001 From: John Abraham Date: Wed, 30 Nov 2011 19:11:19 -0500 Subject: [PATCH] NetAnim: One time initialization, fixes for long duration runs --- src/netanim/model/animation-interface.cc | 142 ++++++++++++++++++++++- src/netanim/model/animation-interface.h | 21 ++++ 2 files changed, 159 insertions(+), 4 deletions(-) diff --git a/src/netanim/model/animation-interface.cc b/src/netanim/model/animation-interface.cc index bcd62a284..090259260 100644 --- a/src/netanim/model/animation-interface.cc +++ b/src/netanim/model/animation-interface.cc @@ -51,14 +51,18 @@ NS_LOG_COMPONENT_DEFINE ("AnimationInterface"); +#define PURGE_INTERVAL 5 +static bool initialized = false; + namespace ns3 { AnimationInterface::AnimationInterface () : m_fHandle (STDOUT_FILENO), m_xml (false), mobilitypollinterval (Seconds(0.25)), usingSockets (false), mport (0), outputfilename (""), OutputFileSet (false), ServerPortSet (false), gAnimUid (0),randomPosition (true), - m_writeCallback (0) + m_writeCallback (0), m_started (false) { + initialized = true; StartAnimation (); } @@ -66,8 +70,9 @@ AnimationInterface::AnimationInterface (const std::string fn, bool usingXML) : m_fHandle (STDOUT_FILENO), m_xml (usingXML), mobilitypollinterval (Seconds(0.25)), usingSockets (false), mport (0), outputfilename (fn), OutputFileSet (false), ServerPortSet (false), gAnimUid (0), randomPosition (true), - m_writeCallback (0) + m_writeCallback (0), m_started (false) { + initialized = true; StartAnimation (); } @@ -75,8 +80,9 @@ AnimationInterface::AnimationInterface (const uint16_t port, bool usingXML) : m_fHandle (STDOUT_FILENO), m_xml (usingXML), mobilitypollinterval (Seconds(0.25)), usingSockets (true), mport (port), outputfilename (""), OutputFileSet (false), ServerPortSet (false), gAnimUid (0), randomPosition (true), - m_writeCallback (0) + m_writeCallback (0), m_started (false) { + initialized = true; StartAnimation (); } @@ -116,6 +122,17 @@ bool AnimationInterface::SetOutputFile (const std::string& fn) return true; } +bool AnimationInterface::IsInitialized () +{ + return initialized; +} + +bool AnimationInterface::IsStarted () +{ + return m_started; + +} + void AnimationInterface::SetAnimWriteCallback (AnimWriteCallback cb) { m_writeCallback = cb; @@ -226,8 +243,90 @@ Vector AnimationInterface::GetPosition (Ptr n) return nodeLocation[n->GetId ()]; } +void AnimationInterface::PurgePendingWifi () +{ + if (pendingWifiPackets.empty ()) + return; + std::vector purgeList; + for (std::map::iterator i = pendingWifiPackets.begin (); + i != pendingWifiPackets.end (); + ++i) + { + + AnimPacketInfo pktInfo = i->second; + double delta = (Simulator::Now ().GetSeconds () - pktInfo.m_fbTx); + if (delta > PURGE_INTERVAL) + { + purgeList.push_back (i->first); + } + } + + for (std::vector ::iterator i = purgeList.begin (); + i != purgeList.end (); + ++i) + { + pendingWifiPackets.erase (*i); + } + +} + +void AnimationInterface::PurgePendingWimax () +{ + if (pendingWimaxPackets.empty ()) + return; + std::vector purgeList; + for (std::map::iterator i = pendingWimaxPackets.begin (); + i != pendingWimaxPackets.end (); + ++i) + { + + AnimPacketInfo pktInfo = i->second; + double delta = (Simulator::Now ().GetSeconds () - pktInfo.m_fbTx); + if (delta > PURGE_INTERVAL) + { + purgeList.push_back (i->first); + } + } + + for (std::vector ::iterator i = purgeList.begin (); + i != purgeList.end (); + ++i) + { + pendingWimaxPackets.erase (*i); + } + +} + +void AnimationInterface::PurgePendingCsma () +{ + if (pendingCsmaPackets.empty ()) + return; + std::vector purgeList; + for (std::map::iterator i = pendingCsmaPackets.begin (); + i != pendingCsmaPackets.end (); + ++i) + { + + AnimPacketInfo pktInfo = i->second; + double delta = (Simulator::Now ().GetSeconds () - pktInfo.m_fbTx); + if (delta > PURGE_INTERVAL) + { + purgeList.push_back (i->first); + } + } + + for (std::vector ::iterator i = purgeList.begin (); + i != purgeList.end (); + ++i) + { + pendingCsmaPackets.erase (*i); + } + +} + void AnimationInterface::StartAnimation () { + m_started = true; if (usingSockets) { SetServerPort (mport); @@ -336,6 +435,11 @@ void AnimationInterface::StartAnimation () Simulator::Schedule (mobilitypollinterval, &AnimationInterface::MobilityAutoCheck, this); } + ConnectCallbacks (); +} + +void AnimationInterface::ConnectCallbacks () +{ // Connect the callbacks Config::Connect ("/ChannelList/*/TxRxPointToPoint", MakeCallback (&AnimationInterface::DevTxTrace, this)); @@ -365,11 +469,12 @@ void AnimationInterface::StartAnimation () MakeCallback (&AnimationInterface::CsmaMacRxTrace, this)); - } + void AnimationInterface::StopAnimation () { + m_started = false; NS_LOG_INFO ("Stopping Animation"); ResetAnimWriteCallback (); if (m_fHandle > 0) @@ -510,6 +615,8 @@ void AnimationInterface::DevTxTrace (std::string context, Ptr p, Ptr tx, Ptr rx, Time txTime, Time rxTime) { + if (!m_started) + return; NS_ASSERT (tx); NS_ASSERT (rx); Time now = Simulator::Now (); @@ -586,6 +693,8 @@ uint64_t AnimationInterface::GetAnimUidFromPacket (Ptr p) void AnimationInterface::WifiPhyTxBeginTrace (std::string context, Ptr p) { + if (!m_started) + return; Ptr ndev = GetNetDeviceFromContext (context); NS_ASSERT (ndev); Ptr n = ndev->GetNode (); @@ -608,6 +717,8 @@ void AnimationInterface::WifiPhyTxEndTrace (std::string context, void AnimationInterface::WifiPhyTxDropTrace (std::string context, Ptr p) { + if (!m_started) + return; Ptr ndev = GetNetDeviceFromContext (context); NS_ASSERT (ndev); // Erase pending wifi @@ -621,6 +732,8 @@ void AnimationInterface::WifiPhyTxDropTrace (std::string context, void AnimationInterface::WifiPhyRxBeginTrace (std::string context, Ptr p) { + if (!m_started) + return; Ptr ndev = GetNetDeviceFromContext (context); NS_ASSERT (ndev); uint64_t AnimUid = GetAnimUidFromPacket (p); @@ -638,6 +751,8 @@ void AnimationInterface::WifiPhyRxBeginTrace (std::string context, void AnimationInterface::WifiPhyRxEndTrace (std::string context, Ptr p) { + if (!m_started) + return; Ptr ndev = GetNetDeviceFromContext (context); NS_ASSERT (ndev); Ptr n = ndev->GetNode (); @@ -656,6 +771,8 @@ void AnimationInterface::WifiPhyRxEndTrace (std::string context, void AnimationInterface::WifiMacRxTrace (std::string context, Ptr p) { + if (!m_started) + return; Ptr ndev = GetNetDeviceFromContext (context); NS_ASSERT (ndev); Ptr n = ndev->GetNode (); @@ -684,6 +801,8 @@ void AnimationInterface::WifiPhyRxDropTrace (std::string context, void AnimationInterface::WimaxTxTrace (std::string context, Ptr p, const Mac48Address & m) { + if (!m_started) + return; Ptr ndev = GetNetDeviceFromContext (context); NS_ASSERT (ndev); Ptr n = ndev->GetNode (); @@ -701,6 +820,8 @@ void AnimationInterface::WimaxTxTrace (std::string context, Ptr p, void AnimationInterface::WimaxRxTrace (std::string context, Ptr p, const Mac48Address & m) { + if (!m_started) + return; Ptr ndev = GetNetDeviceFromContext (context); NS_ASSERT (ndev); Ptr n = ndev->GetNode (); @@ -717,6 +838,8 @@ void AnimationInterface::WimaxRxTrace (std::string context, Ptr p, void AnimationInterface::CsmaPhyTxBeginTrace (std::string context, Ptr p) { + if (!m_started) + return; Ptr ndev = GetNetDeviceFromContext (context); NS_ASSERT (ndev); Ptr n = ndev->GetNode (); @@ -733,6 +856,8 @@ void AnimationInterface::CsmaPhyTxBeginTrace (std::string context, Ptr p) { + if (!m_started) + return; Ptr ndev = GetNetDeviceFromContext (context); NS_ASSERT (ndev); uint64_t AnimUid = GetAnimUidFromPacket (p); @@ -749,6 +874,8 @@ void AnimationInterface::CsmaPhyTxEndTrace (std::string context, Ptr p) { + if (!m_started) + return; Ptr ndev = GetNetDeviceFromContext (context); NS_ASSERT (ndev); Ptr n = ndev->GetNode (); @@ -770,6 +897,8 @@ void AnimationInterface::CsmaPhyRxEndTrace (std::string context, Ptr p) { + if (!m_started) + return; NS_LOG_FUNCTION (this); Ptr ndev = GetNetDeviceFromContext (context); NS_ASSERT (ndev); @@ -796,6 +925,8 @@ void AnimationInterface::CsmaMacRxTrace (std::string context, void AnimationInterface::MobilityCourseChangeTrace (Ptr mobility) { + if (!m_started) + return; Ptr n = mobility->GetObject (); NS_ASSERT (n); Vector v ; @@ -849,6 +980,9 @@ void AnimationInterface::MobilityAutoCheck () WriteDummyPacket (); if (!Simulator::IsFinished ()) { + PurgePendingWifi (); + PurgePendingWimax (); + PurgePendingCsma (); Simulator::Schedule (mobilitypollinterval, &AnimationInterface::MobilityAutoCheck, this); } } diff --git a/src/netanim/model/animation-interface.h b/src/netanim/model/animation-interface.h index 0780e112b..a4e24333a 100644 --- a/src/netanim/model/animation-interface.h +++ b/src/netanim/model/animation-interface.h @@ -98,6 +98,13 @@ public: */ AnimationInterface (uint16_t port, bool usingXML = true); + /** + * \brief Check if AnimationInterface is initialized + * \returns true if AnimationInterface was already initiliazed + * + */ + static bool IsInitialized (void); + /** * \brief Specify that animation commands are to be written * to the specified output file. @@ -203,6 +210,13 @@ public: */ void SetConstantPosition (Ptr n, double x, double y, double z=0); + /** + * \brief Is AnimationInterface started + * \returns true if AnimationInterface was started + * + */ + bool IsStarted (void); + private: #ifndef WIN32 int m_fHandle; // File handle for output (-1 if none) @@ -286,12 +300,19 @@ private: bool NodeHasMoved (Ptr n, Vector newLocation); void AddMargin (); + void PurgePendingWifi (); + void PurgePendingWimax (); + void PurgePendingCsma (); + // Recalculate topology bounds void RecalcTopoBounds (Vector v); std::vector < Ptr > RecalcTopoBounds (); bool randomPosition; AnimWriteCallback m_writeCallback; + void ConnectCallbacks (); + + bool m_started; // Path helper std::vector GetElementsFromContext (std::string context);