NetAnim: One time initialization, fixes for long duration runs

This commit is contained in:
John Abraham
2011-11-30 19:11:19 -05:00
parent 0f1c425737
commit e39f1ebe10
2 changed files with 159 additions and 4 deletions

View File

@@ -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 <Node> n)
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 @@ 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<const Packet> p,
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 @@ uint64_t AnimationInterface::GetAnimUidFromPacket (Ptr <const Packet> p)
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::WifiPhyTxEndTrace (std::string context,
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::WifiPhyTxDropTrace (std::string context,
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::WifiPhyRxBeginTrace (std::string context,
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::WifiPhyRxEndTrace (std::string context,
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::WifiPhyRxDropTrace (std::string context,
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::WimaxTxTrace (std::string context, Ptr<const Packet> p,
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::WimaxRxTrace (std::string context, Ptr<const Packet> p,
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::CsmaPhyTxBeginTrace (std::string context, Ptr<const Pac
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::CsmaPhyTxEndTrace (std::string context, Ptr<const Packe
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::CsmaPhyRxEndTrace (std::string context, Ptr<const Packe
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::CsmaMacRxTrace (std::string context,
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 @@ void AnimationInterface::MobilityAutoCheck ()
WriteDummyPacket ();
if (!Simulator::IsFinished ())
{
PurgePendingWifi ();
PurgePendingWimax ();
PurgePendingCsma ();
Simulator::Schedule (mobilitypollinterval, &AnimationInterface::MobilityAutoCheck, this);
}
}

View File

@@ -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 <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 @@ private:
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);