Refactor handling of pauses in StaticSpeedHelper to fix bug (must return null speed when paused)
authorGustavo J. A. M. Carneiro <gjc@inescporto.pt>
Mon, 01 Oct 2007 17:10:15 +0100
changeset 1659 4df279b828d3
parent 1658 52f6bf2a7caa
child 1660 4a4621b4e3ab
Refactor handling of pauses in StaticSpeedHelper to fix bug (must return null speed when paused)
src/mobility/static-speed-helper.cc
src/mobility/static-speed-helper.h
--- a/src/mobility/static-speed-helper.cc	Mon Oct 01 15:11:16 2007 +0100
+++ b/src/mobility/static-speed-helper.cc	Mon Oct 01 17:10:15 2007 +0100
@@ -31,7 +31,8 @@
 StaticSpeedHelper::StaticSpeedHelper (const Position &position,
 				      const Speed &speed)
   : m_position (position),
-    m_speed (speed)
+    m_speed (speed),
+    m_paused (true)
 {}
 void 
 StaticSpeedHelper::InitializePosition (const Position &position)
@@ -41,14 +42,7 @@
   m_speed.dy = 0.0;
   m_speed.dz = 0.0;
   m_lastUpdate = Simulator::Now ();
-  m_pauseEnd = Simulator::Now ();
-}
-
-void
-StaticSpeedHelper::Reset (const Speed &speed, const Time &pauseDelay)
-{
-  Reset (speed);
-  m_pauseEnd = Simulator::Now () + pauseDelay;
+  m_paused = true;
 }
 
 Position 
@@ -61,7 +55,7 @@
 Speed 
 StaticSpeedHelper::GetSpeed (void) const
 {
-  return m_speed;
+  return m_paused? Speed (0, 0, 0) : m_speed;
 }
 void 
 StaticSpeedHelper::SetSpeed (const Speed &speed)
@@ -73,17 +67,13 @@
 void
 StaticSpeedHelper::Update (void) const
 {
-  Time now = Simulator::Now ();
-  if (m_pauseEnd > now)
+  if (m_paused)
     {
       return;
     }
-  Time last = std::max (now, m_pauseEnd);
-  if (m_lastUpdate >= last)
-    {
-      return;
-    }
-  Time deltaTime = now - last;
+  Time now = Simulator::Now ();
+  NS_ASSERT (m_lastUpdate <= now);
+  Time deltaTime = now - m_lastUpdate;
   m_lastUpdate = now;
   double deltaS = deltaTime.GetSeconds ();
   m_position.x += m_speed.dx * deltaS;
@@ -96,7 +86,7 @@
 {
   Update ();
   m_speed = speed;
-  m_pauseEnd = Simulator::Now ();
+  Unpause ();
 }
 void
 StaticSpeedHelper::UpdateFull (const Rectangle &bounds) const
@@ -115,5 +105,21 @@
   return m_position;
 }
 
+void 
+StaticSpeedHelper::Pause (void)
+{
+  Update ();
+  m_paused = true;
+}
+
+void 
+StaticSpeedHelper::Unpause (void)
+{
+  if (m_paused)
+    {
+      m_lastUpdate = Simulator::Now ();
+      m_paused = false;
+    }
+}
 
 } // namespace ns3
--- a/src/mobility/static-speed-helper.h	Mon Oct 01 15:11:16 2007 +0100
+++ b/src/mobility/static-speed-helper.h	Mon Oct 01 17:10:15 2007 +0100
@@ -37,12 +37,13 @@
 		     const Speed &speed);
   void InitializePosition (const Position &position);
 
-  void Reset (const Speed &speed, const Time &pauseDelay);
   void Reset (const Speed &speed);
   Position GetCurrentPosition (const Rectangle &bounds) const;
   Position GetCurrentPosition (void) const;
   Speed GetSpeed (void) const;
   void SetSpeed (const Speed &speed);
+  void Pause (void);
+  void Unpause (void);
 
  private:
   void Update (void) const;
@@ -50,7 +51,7 @@
   mutable Time m_lastUpdate;
   mutable Position m_position;
   Speed m_speed;
-  Time m_pauseEnd;
+  bool m_paused;
 };
 
 } // namespace ns3