00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "timer.h"
00021 #include "simulator.h"
00022 #include "simulation-singleton.h"
00023
00024 namespace ns3 {
00025
00026 Timer::Timer ()
00027 : m_flags (0),
00028 m_delay (FemtoSeconds (0)),
00029 m_event (),
00030 m_impl (0)
00031 {}
00032
00033 Timer::Timer (enum DestroyPolicy destroyPolicy)
00034 : m_flags (destroyPolicy),
00035 m_delay (FemtoSeconds (0)),
00036 m_event (),
00037 m_impl (0)
00038 {}
00039
00040 Timer::~Timer ()
00041 {
00042 if (m_flags & CHECK_ON_DESTROY)
00043 {
00044 if (m_event.IsRunning ())
00045 {
00046 NS_FATAL_ERROR ("Event is still running while destroying.");
00047 }
00048 }
00049 else if (m_flags & CANCEL_ON_DESTROY)
00050 {
00051 m_event.Cancel ();
00052 }
00053 else if (m_flags & REMOVE_ON_DESTROY)
00054 {
00055 Simulator::Remove (m_event);
00056 }
00057 delete m_impl;
00058 }
00059
00060 void
00061 Timer::SetDelay (const Time &time)
00062 {
00063 m_delay = time;
00064 }
00065 Time
00066 Timer::GetDelay (void) const
00067 {
00068 return m_delay;
00069 }
00070 Time
00071 Timer::GetDelayLeft (void) const
00072 {
00073 switch (GetState ()) {
00074 case Timer::RUNNING:
00075 return Simulator::GetDelayLeft (m_event);
00076 break;
00077 case Timer::EXPIRED:
00078 return TimeStep (0);
00079 break;
00080 case Timer::SUSPENDED:
00081 return m_delayLeft;
00082 break;
00083 default:
00084 NS_ASSERT (false);
00085 return TimeStep (0);
00086 break;
00087 }
00088 }
00089
00090 void
00091 Timer::Cancel (void)
00092 {
00093 Simulator::Cancel (m_event);
00094 }
00095 void
00096 Timer::Remove (void)
00097 {
00098 Simulator::Remove (m_event);
00099 }
00100 bool
00101 Timer::IsExpired (void) const
00102 {
00103 return !IsSuspended () && m_event.IsExpired ();
00104 }
00105 bool
00106 Timer::IsRunning (void) const
00107 {
00108 return !IsSuspended () && m_event.IsRunning ();
00109 }
00110 bool
00111 Timer::IsSuspended (void) const
00112 {
00113 return (m_flags & TIMER_SUSPENDED) == TIMER_SUSPENDED;
00114 }
00115 enum Timer::State
00116 Timer::GetState (void) const
00117 {
00118 if (IsRunning ())
00119 {
00120 return Timer::RUNNING;
00121 }
00122 else if (IsExpired ())
00123 {
00124 return Timer::EXPIRED;
00125 }
00126 else
00127 {
00128 NS_ASSERT (IsSuspended ());
00129 return Timer::SUSPENDED;
00130 }
00131 }
00132
00133 void
00134 Timer::Schedule (void)
00135 {
00136 Schedule (m_delay);
00137 }
00138
00139 void
00140 Timer::Schedule (Time delay)
00141 {
00142 NS_ASSERT (m_impl != 0);
00143 if (m_event.IsRunning ())
00144 {
00145 NS_FATAL_ERROR ("Event is still running while re-scheduling.");
00146 }
00147 m_event = m_impl->Schedule (delay);
00148 }
00149
00150 void
00151 Timer::Suspend (void)
00152 {
00153 NS_ASSERT (IsRunning ());
00154 m_delayLeft = Simulator::GetDelayLeft (m_event);
00155 Simulator::Remove (m_event);
00156 m_flags |= TIMER_SUSPENDED;
00157 }
00158
00159 void
00160 Timer::Resume (void)
00161 {
00162 NS_ASSERT (m_flags & TIMER_SUSPENDED);
00163 m_event = m_impl->Schedule (m_delayLeft);
00164 m_flags &= ~TIMER_SUSPENDED;
00165 }
00166
00167
00168 }
00169
00170
00171 #ifdef RUN_SELF_TESTS
00172 #include "ns3/test.h"
00173
00174 namespace {
00175 void bari (int)
00176 {}
00177 void bar2i (int, int)
00178 {}
00179 void bar3i (int, int, int)
00180 {}
00181 void bar4i (int, int, int, int)
00182 {}
00183 void bar5i (int, int, int, int, int)
00184 {}
00185 void bar6i (int, int, int, int, int, int)
00186 {}
00187 void barcir (const int &)
00188 {}
00189 void barir (int &)
00190 {}
00191 void barip (int *)
00192 {}
00193 void barcip (const int *)
00194 {}
00195 }
00196
00197 namespace ns3 {
00198
00199 class TimerTests : public Test
00200 {
00201 public:
00202 TimerTests ();
00203 virtual bool RunTests (void);
00204 void bazi (int) {}
00205 void baz2i (int, int) {}
00206 void baz3i (int, int, int) {}
00207 void baz4i (int, int, int, int) {}
00208 void baz5i (int, int, int, int, int) {}
00209 void baz6i (int, int, int, int, int, int) {}
00210 void bazcir (const int&) {}
00211 void bazir (int&) {}
00212 void bazip (int *) {}
00213 void bazcip (const int *) {}
00214 };
00215
00216 TimerTests::TimerTests ()
00217 : Test ("Timer")
00218 {}
00219
00220 bool
00221 TimerTests::RunTests (void)
00222 {
00223 bool result = true;
00224
00225 Timer timer;
00226
00227 timer.SetFunction (&bari);
00228 timer.SetArguments (1);
00229 timer.SetDelay (Seconds (10.0));
00230 NS_TEST_ASSERT (!timer.IsRunning ());
00231 NS_TEST_ASSERT (timer.IsExpired ());
00232 NS_TEST_ASSERT (!timer.IsSuspended ());
00233 NS_TEST_ASSERT_EQUAL (timer.GetState (), Timer::EXPIRED);
00234 timer.Schedule ();
00235 NS_TEST_ASSERT (timer.IsRunning ());
00236 NS_TEST_ASSERT (!timer.IsExpired ());
00237 NS_TEST_ASSERT (!timer.IsSuspended ());
00238 NS_TEST_ASSERT_EQUAL (timer.GetState (), Timer::RUNNING);
00239 timer.Suspend ();
00240 NS_TEST_ASSERT (!timer.IsRunning ());
00241 NS_TEST_ASSERT (!timer.IsExpired ());
00242 NS_TEST_ASSERT (timer.IsSuspended ());
00243 NS_TEST_ASSERT_EQUAL (timer.GetState (), Timer::SUSPENDED);
00244 timer.Resume ();
00245 NS_TEST_ASSERT (timer.IsRunning ());
00246 NS_TEST_ASSERT (!timer.IsExpired ());
00247 NS_TEST_ASSERT (!timer.IsSuspended ());
00248 NS_TEST_ASSERT_EQUAL (timer.GetState (), Timer::RUNNING);
00249 timer.Cancel ();
00250 NS_TEST_ASSERT (!timer.IsRunning ());
00251 NS_TEST_ASSERT (timer.IsExpired ());
00252 NS_TEST_ASSERT (!timer.IsSuspended ());
00253 NS_TEST_ASSERT_EQUAL (timer.GetState (), Timer::EXPIRED);
00254
00255 int a = 0;
00256 int &b = a;
00257 const int &c = a;
00258
00259 timer.SetFunction (&bari);
00260 timer.SetArguments (2);
00261 timer.SetArguments (a);
00262 timer.SetArguments (b);
00263 timer.SetArguments (c);
00264 timer.SetFunction (&barir);
00265 timer.SetArguments (2);
00266 timer.SetArguments (a);
00267 timer.SetArguments (b);
00268 timer.SetArguments (c);
00269 timer.SetFunction (&barcir);
00270 timer.SetArguments (2);
00271 timer.SetArguments (a);
00272 timer.SetArguments (b);
00273 timer.SetArguments (c);
00274
00275
00276
00277 timer.SetDelay (Seconds (1.0));
00278 timer.Schedule ();
00279
00280 timer.SetFunction (&TimerTests::bazi, this);
00281 timer.SetArguments (3);
00282 timer.SetFunction (&TimerTests::bazir, this);
00283 timer.SetArguments (3);
00284 timer.SetFunction (&TimerTests::bazcir, this);
00285 timer.SetArguments (3);
00286
00287 timer.SetFunction (&bar2i);
00288 timer.SetArguments (1, 1);
00289 timer.SetFunction (&bar3i);
00290 timer.SetArguments (1, 1, 1);
00291 timer.SetFunction (&bar4i);
00292 timer.SetArguments (1, 1, 1, 1);
00293 timer.SetFunction (&bar5i);
00294 timer.SetArguments (1, 1, 1, 1, 1);
00295
00296
00297
00298 timer.SetFunction (&TimerTests::baz2i, this);
00299 timer.SetArguments (1, 1);
00300 timer.SetFunction (&TimerTests::baz3i, this);
00301 timer.SetArguments (1, 1, 1);
00302 timer.SetFunction (&TimerTests::baz4i, this);
00303 timer.SetArguments (1, 1, 1, 1);
00304 timer.SetFunction (&TimerTests::baz5i, this);
00305 timer.SetArguments (1, 1, 1, 1, 1);
00306
00307
00308
00309
00310 Simulator::Run ();
00311 Simulator::Destroy ();
00312
00313 return result;
00314 }
00315
00316 TimerTests g_tests;
00317
00318 }
00319
00320 #endif