00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "watchdog.h"
00021
00022 namespace ns3 {
00023
00024 Watchdog::Watchdog ()
00025 : m_impl (0),
00026 m_event (),
00027 m_end (MicroSeconds (0))
00028 {}
00029
00030 Watchdog::~Watchdog ()
00031 {
00032 delete m_impl;
00033 }
00034
00035 void
00036 Watchdog::Ping (Time delay)
00037 {
00038 Time end = Simulator::Now () + delay;
00039 m_end = std::max (m_end, end);
00040 if (m_event.IsRunning ())
00041 {
00042 return;
00043 }
00044 m_event = Simulator::Schedule (m_end - Now (), &Watchdog::Expire, this);
00045 }
00046
00047 void
00048 Watchdog::Expire (void)
00049 {
00050 if (m_end == Simulator::Now ())
00051 {
00052 m_impl->Invoke ();
00053 }
00054 else
00055 {
00056 m_event = Simulator::Schedule (m_end - Now (), &Watchdog::Expire, this);
00057 }
00058 }
00059
00060 }
00061
00062 #ifdef RUN_SELF_TESTS
00063
00064 #include "ns3/test.h"
00065
00066 namespace ns3 {
00067
00068 class WatchdogTests : public Test
00069 {
00070 public:
00071 WatchdogTests ();
00072 virtual bool RunTests (void);
00073 private:
00074 void Expire (Time expected);
00075 bool m_error;
00076 };
00077
00078 WatchdogTests::WatchdogTests ()
00079 : Test ("Watchdog")
00080 {}
00081
00082 void
00083 WatchdogTests::Expire (Time expected)
00084 {
00085 bool result = true;
00086 NS_TEST_ASSERT_EQUAL (Simulator::Now (), expected);
00087 m_error = !result;
00088 }
00089
00090 bool
00091 WatchdogTests::RunTests (void)
00092 {
00093 bool result = true;
00094
00095 m_error = false;
00096 Watchdog watchdog;
00097 watchdog.SetFunction (&WatchdogTests::Expire, this);
00098 watchdog.SetArguments (MicroSeconds (40));
00099 watchdog.Ping (MicroSeconds (10));
00100 Simulator::Schedule (MicroSeconds (5), &Watchdog::Ping, &watchdog, MicroSeconds (20));
00101 Simulator::Schedule (MicroSeconds (20), &Watchdog::Ping, &watchdog, MicroSeconds (2));
00102 Simulator::Schedule (MicroSeconds (23), &Watchdog::Ping, &watchdog, MicroSeconds (17));
00103 Simulator::Run ();
00104 NS_TEST_ASSERT (!m_error);
00105 Simulator::Destroy ();
00106
00107 return result;
00108 }
00109
00110 static WatchdogTests g_watchdogTests;
00111
00112 }
00113
00114 #endif