NetAnim: One time initialization, fixes for long duration runs
authorJohn Abraham <john.abraham@gatech.edu>
Wed, 30 Nov 2011 19:11:19 -0500
changeset 7606 958775025f35
parent 7605 b06595c51eb3
child 7607 58588710f508
NetAnim: One time initialization, fixes for long duration runs
src/netanim/model/animation-interface.cc
src/netanim/model/animation-interface.h
--- a/src/netanim/model/animation-interface.cc	Mon Nov 28 07:40:36 2011 -0800
+++ b/src/netanim/model/animation-interface.cc	Wed Nov 30 19:11:19 2011 -0500
@@ -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 @@
   : 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 @@
   : 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 @@
   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 @@
   return nodeLocation[n->GetId ()];
 }
 
+void AnimationInterface::PurgePendingWifi ()
+{
+  if (pendingWifiPackets.empty ())
+    return;
+  std::vector <uint64_t> purgeList;
+  for (std::map<uint64_t, AnimPacketInfo>::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 <uint64_t>::iterator i = purgeList.begin ();
+       i != purgeList.end ();
+       ++i)
+    {
+      pendingWifiPackets.erase (*i);
+    }
+
+}
+
+void AnimationInterface::PurgePendingWimax ()
+{
+  if (pendingWimaxPackets.empty ())
+    return;
+  std::vector <uint64_t> purgeList;
+  for (std::map<uint64_t, AnimPacketInfo>::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 <uint64_t>::iterator i = purgeList.begin ();
+       i != purgeList.end ();
+       ++i)
+    {
+      pendingWimaxPackets.erase (*i);
+    }
+
+}
+
+void AnimationInterface::PurgePendingCsma ()
+{
+  if (pendingCsmaPackets.empty ())
+    return;
+  std::vector <uint64_t> purgeList;
+  for (std::map<uint64_t, AnimPacketInfo>::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 <uint64_t>::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 @@
       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 @@
                    MakeCallback (&AnimationInterface::CsmaMacRxTrace, this));
 
 
+}
 
-}
 
 void AnimationInterface::StopAnimation ()
 {
+  m_started = false;
   NS_LOG_INFO ("Stopping Animation");
   ResetAnimWriteCallback ();
   if (m_fHandle > 0) 
@@ -510,6 +615,8 @@
                                      Ptr<NetDevice> tx, Ptr<NetDevice> rx,
                                      Time txTime, Time rxTime)
 {
+  if (!m_started)
+    return;
   NS_ASSERT (tx);
   NS_ASSERT (rx);
   Time now = Simulator::Now ();
@@ -586,6 +693,8 @@
 void AnimationInterface::WifiPhyTxBeginTrace (std::string context,
                                           Ptr<const Packet> p)
 {
+  if (!m_started)
+    return;
   Ptr <NetDevice> ndev = GetNetDeviceFromContext (context); 
   NS_ASSERT (ndev);
   Ptr <Node> n = ndev->GetNode ();
@@ -608,6 +717,8 @@
 void AnimationInterface::WifiPhyTxDropTrace (std::string context,
                                              Ptr<const Packet> p)
 {
+  if (!m_started)
+    return;
   Ptr <NetDevice> ndev = GetNetDeviceFromContext (context);
   NS_ASSERT (ndev);
   // Erase pending wifi
@@ -621,6 +732,8 @@
 void AnimationInterface::WifiPhyRxBeginTrace (std::string context,
                                               Ptr<const Packet> p)
 {
+  if (!m_started)
+    return;
   Ptr <NetDevice> ndev = GetNetDeviceFromContext (context);
   NS_ASSERT (ndev);
   uint64_t AnimUid = GetAnimUidFromPacket (p);
@@ -638,6 +751,8 @@
 void AnimationInterface::WifiPhyRxEndTrace (std::string context,
                                             Ptr<const Packet> p)
 {
+  if (!m_started)
+    return;
   Ptr <NetDevice> ndev = GetNetDeviceFromContext (context);
   NS_ASSERT (ndev);
   Ptr <Node> n = ndev->GetNode ();
@@ -656,6 +771,8 @@
 void AnimationInterface::WifiMacRxTrace (std::string context,
                                          Ptr<const Packet> p)
 {
+  if (!m_started)
+    return;
   Ptr <NetDevice> ndev = GetNetDeviceFromContext (context);
   NS_ASSERT (ndev);
   Ptr <Node> n = ndev->GetNode ();
@@ -684,6 +801,8 @@
 
 void AnimationInterface::WimaxTxTrace (std::string context, Ptr<const Packet> p, const Mac48Address & m)
 {
+  if (!m_started)
+    return;
   Ptr <NetDevice> ndev = GetNetDeviceFromContext (context);
   NS_ASSERT (ndev);
   Ptr <Node> n = ndev->GetNode ();
@@ -701,6 +820,8 @@
 
 void AnimationInterface::WimaxRxTrace (std::string context, Ptr<const Packet> p, const Mac48Address & m)
 {
+  if (!m_started)
+    return;
   Ptr <NetDevice> ndev = GetNetDeviceFromContext (context);
   NS_ASSERT (ndev);
   Ptr <Node> n = ndev->GetNode ();
@@ -717,6 +838,8 @@
 
 void AnimationInterface::CsmaPhyTxBeginTrace (std::string context, Ptr<const Packet> p)
 {
+  if (!m_started)
+    return;
   Ptr <NetDevice> ndev = GetNetDeviceFromContext (context);
   NS_ASSERT (ndev);
   Ptr <Node> n = ndev->GetNode ();
@@ -733,6 +856,8 @@
 
 void AnimationInterface::CsmaPhyTxEndTrace (std::string context, Ptr<const Packet> p)
 {
+  if (!m_started)
+    return;
   Ptr <NetDevice> ndev = GetNetDeviceFromContext (context);
   NS_ASSERT (ndev);
   uint64_t AnimUid = GetAnimUidFromPacket (p);
@@ -749,6 +874,8 @@
 
 void AnimationInterface::CsmaPhyRxEndTrace (std::string context, Ptr<const Packet> p)
 {
+  if (!m_started)
+    return;
   Ptr <NetDevice> ndev = GetNetDeviceFromContext (context);
   NS_ASSERT (ndev);
   Ptr <Node> n = ndev->GetNode ();
@@ -770,6 +897,8 @@
 void AnimationInterface::CsmaMacRxTrace (std::string context,
                                          Ptr<const Packet> p)
 {
+  if (!m_started)
+    return;
   NS_LOG_FUNCTION (this);
   Ptr <NetDevice> ndev = GetNetDeviceFromContext (context);
   NS_ASSERT (ndev);
@@ -796,6 +925,8 @@
 void AnimationInterface::MobilityCourseChangeTrace (Ptr <const MobilityModel> mobility)
 
 {
+  if (!m_started)
+    return;
   Ptr <Node> n = mobility->GetObject <Node> ();
   NS_ASSERT (n);
   Vector v ;
@@ -849,6 +980,9 @@
   WriteDummyPacket ();
   if (!Simulator::IsFinished ())
     {
+      PurgePendingWifi ();
+      PurgePendingWimax ();
+      PurgePendingCsma ();
       Simulator::Schedule (mobilitypollinterval, &AnimationInterface::MobilityAutoCheck, this);
     }
 }
--- a/src/netanim/model/animation-interface.h	Mon Nov 28 07:40:36 2011 -0800
+++ b/src/netanim/model/animation-interface.h	Wed Nov 30 19:11:19 2011 -0500
@@ -99,6 +99,13 @@
   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 @@
    */
   void SetConstantPosition (Ptr <Node> 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 @@
   bool NodeHasMoved (Ptr <Node> n, Vector newLocation);
   void AddMargin ();
 
+  void PurgePendingWifi ();
+  void PurgePendingWimax ();
+  void PurgePendingCsma ();
+
   // Recalculate topology bounds
   void RecalcTopoBounds (Vector v);
   std::vector < Ptr <Node> > RecalcTopoBounds ();
 
   bool randomPosition;
   AnimWriteCallback m_writeCallback;
+  void ConnectCallbacks ();
+
+  bool m_started;
 
   // Path helper
   std::vector<std::string> GetElementsFromContext (std::string context);