add Timer::Suspend/Resume/IsSuspended
authorMathieu Lacage <mathieu.lacage@sophia.inria.fr>
Wed, 03 Oct 2007 13:01:29 +0200
changeset 1691 d717c61ae738
parent 1690 16b198d63c1e
child 1692 b35ba80cafef
add Timer::Suspend/Resume/IsSuspended
src/simulator/timer.cc
src/simulator/timer.h
--- a/src/simulator/timer.cc	Wed Oct 03 13:01:12 2007 +0200
+++ b/src/simulator/timer.cc	Wed Oct 03 13:01:29 2007 +0200
@@ -79,7 +79,11 @@
 {
   return m_event.IsRunning ();
 }
-
+bool
+Timer::IsSuspended (void) const
+{
+  return (m_flags & TIMER_SUSPENDED) == TIMER_SUSPENDED;
+}
 
 void 
 Timer::Schedule (void)
@@ -113,6 +117,23 @@
     }
 }
 
+void
+Timer::Suspend (void)
+{
+  NS_ASSERT (IsRunning ());
+  m_delayLeft = Simulator::GetDelayLeft (m_event);
+  Simulator::Remove (m_event);
+  m_flags |= TIMER_SUSPENDED;
+}
+
+void
+Timer::Resume (void)
+{
+  NS_ASSERT (m_flags & TIMER_SUSPENDED);
+  m_event = m_impl->Schedule (m_delayLeft);
+  m_flags &= ~TIMER_SUSPENDED;
+}
+
 
 } // namespace ns3
 
@@ -171,10 +192,30 @@
 {
   bool result = true;
 
+  Timer timer;
+
+  timer.SetFunction (&bari);
+  timer.SetArguments (1);
+  timer.SetDelay (Seconds (10.0));
+  NS_TEST_ASSERT (!timer.IsRunning ());
+  NS_TEST_ASSERT (timer.IsExpired ());
+  NS_TEST_ASSERT (!timer.IsSuspended ());
+  timer.Schedule ();
+  NS_TEST_ASSERT (timer.IsRunning ());
+  NS_TEST_ASSERT (!timer.IsExpired ());
+  NS_TEST_ASSERT (!timer.IsSuspended ());
+  timer.Suspend ();
+  NS_TEST_ASSERT (!timer.IsRunning ());
+  NS_TEST_ASSERT (timer.IsExpired ());
+  NS_TEST_ASSERT (timer.IsSuspended ());
+  timer.Resume ();
+  NS_TEST_ASSERT (timer.IsRunning ());
+  NS_TEST_ASSERT (!timer.IsExpired ());
+  NS_TEST_ASSERT (!timer.IsSuspended ());
+
   int a = 0;
   int &b = a;
   const int &c = a;
-  Timer timer;
 
   timer.SetFunction (&bari);
   timer.SetArguments (2);
@@ -229,6 +270,7 @@
 
   Simulator::Run ();
   Simulator::Destroy ();
+		  
   return result;
 }
 
--- a/src/simulator/timer.h	Wed Oct 03 13:01:12 2007 +0200
+++ b/src/simulator/timer.h	Wed Oct 03 13:01:29 2007 +0200
@@ -191,6 +191,11 @@
    */
   bool IsRunning (void) const;
   /**
+   * \returns true if this timer was suspended and not yet resumed, false
+   *          otherwise.
+   */
+  bool IsSuspended (void) const;
+  /**
    * Schedule a new event using the currently-configured delay, function, 
    * and arguments.
    */
@@ -203,6 +208,19 @@
    */
   void Schedule (Time delay);
 
+  /**
+   * Cancel the timer and save the amount of time left until it was
+   * set to expire.
+   * Calling Suspend on a non-running timer is an error.
+   */
+  void Suspend (void);
+  /**
+   * Restart the timer to expire within the amount of time left saved
+   * during Suspend.
+   * Calling Resume without a prior call to Suspend is an error.
+   */
+  void Resume (void);
+
 private:
   template <typename FN>
   void DoSetFunction (IntToType<0>, FN fn);
@@ -234,10 +252,15 @@
   template <typename MEM_PTR, typename OBJ_PTR>
   void DoSetFunction (IntToType<6>, MEM_PTR memPtr, OBJ_PTR objPtr);
 
+  enum {
+    TIMER_SUSPENDED = (1<<7)
+  };
+
   int m_flags;
   Time m_delay;
   EventId m_event;
   TimerImpl *m_impl;
+  Time m_delayLeft;
 };
 
 } // namespace ns3