From da44de59b959becdb459d40f81e745965dd1d635 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Thu, 27 Sep 2007 12:51:17 +0200 Subject: [PATCH] Implement a Timer API --- src/simulator/timer.cc | 175 +++++++++++++++++++++++++++++++++++++++++ src/simulator/timer.h | 142 +++++++++++++++++++++++++++++++++ src/simulator/wscript | 3 + 3 files changed, 320 insertions(+) create mode 100644 src/simulator/timer.cc create mode 100644 src/simulator/timer.h diff --git a/src/simulator/timer.cc b/src/simulator/timer.cc new file mode 100644 index 000000000..b8891049d --- /dev/null +++ b/src/simulator/timer.cc @@ -0,0 +1,175 @@ +#include "timer.h" +#include "simulator.h" +#include "simulation-singleton.h" +#include "event-garbage-collector.h" + +namespace ns3 { + +Timer::Timer () + : m_flags (0), + m_delay (FemtoSeconds (0)), + m_event (), + m_impl (0) +{} + +Timer::Timer (int flags) + : m_flags (flags), + m_delay (FemtoSeconds (0)), + m_event (), + m_impl (0) +{} + +Timer::~Timer () +{ + if (m_flags & CHECK_ON_DESTROY) + { + if (m_event.IsRunning ()) + { + NS_FATAL_ERROR ("Event is still running while destroying."); + } + } + else if (m_flags & CANCEL_ON_DESTROY) + { + m_event.Cancel (); + } + else if (m_flags & REMOVE_ON_DESTROY) + { + Simulator::Remove (m_event); + } + delete m_impl; +} + +void +Timer::SetDelay (const Time &time) +{ + m_delay = time; +} +Time +Timer::GetDelay (void) const +{ + return m_delay; +} + + +void +Timer::Cancel (void) +{ + Simulator::Cancel (m_event); +} +void +Timer::Remove (void) +{ + Simulator::Remove (m_event); +} +bool +Timer::IsExpired (void) const +{ + return m_event.IsExpired (); +} +bool +Timer::IsRunning (void) const +{ + return m_event.IsRunning (); +} + + +void +Timer::Schedule (void) +{ + NS_ASSERT (m_impl != 0); + if (m_flags & CHECK_ON_SCHEDULE) + { + if (m_event.IsRunning ()) + { + NS_FATAL_ERROR ("Event is still running while re-scheduling."); + } + } + else if (m_flags & CANCEL_ON_SCHEDULE) + { + m_event.Cancel (); + } + else if (m_flags & REMOVE_ON_SCHEDULE) + { + Simulator::Remove (m_event); + } + m_event = m_impl->Schedule (m_delay); + if (m_flags & GARBAGE_COLLECT) + { + SimulationSingleton::Get ()->Track (m_event); + } +} + + +} // namespace ns3 + + +#ifdef RUN_SELF_TESTS +#include "ns3/test.h" + +namespace { +void bari (int) +{} +void barcir (const int &) +{} +void barir (int &) +{} +void barip (int *) +{} +void barcip (const int *) +{} +} + +namespace ns3 { + +class TimerTests : public Test +{ +public: + TimerTests (); + virtual bool RunTests (void); +}; + +TimerTests::TimerTests () + : Test ("Timer") +{} + +bool +TimerTests::RunTests (void) +{ + bool ok = true; + + int a = 0; + int &b = a; + const int &c = a; + Timer timer; + + timer.SetFunction (&bari, a); + timer.SetArguments (2); + timer.SetArguments (a); + timer.SetArguments (b); + timer.SetArguments (c); + timer.SetFunction (&barir, a); + timer.SetArguments (2); + timer.SetArguments (a); + timer.SetArguments (b); + timer.SetArguments (c); + timer.SetFunction (&barcir, a); + timer.SetArguments (2); + timer.SetArguments (a); + timer.SetArguments (b); + timer.SetArguments (c); + // the following call cannot possibly work and is flagged by + // a runtime error. + //timer.SetArguments (0.0); + timer.SetDelay (Seconds (1.0)); + timer.Schedule (); + + Simulator::Run (); + Simulator::Destroy (); + return ok; +} + +TimerTests g_tests; + +} // namespace ns3 + +#endif /* RUN_SELF_TESTS */ diff --git a/src/simulator/timer.h b/src/simulator/timer.h new file mode 100644 index 000000000..a2623ec5f --- /dev/null +++ b/src/simulator/timer.h @@ -0,0 +1,142 @@ +#ifndef TIMER_H +#define TIMER_H + +#include "ns3/fatal-error.h" +#include "nstime.h" +#include "event-id.h" + +namespace ns3 { + +class TimerImpl; + +class Timer +{ +public: + enum { + CHECK_ON_SCHEDULE = (1<<0), + CHECK_ON_DESTROY = (1<<1), + CANCEL_ON_SCHEDULE = (1<<2), + CANCEL_ON_DESTROY = (1<<3), + REMOVE_ON_SCHEDULE = (1<<4), + REMOVE_ON_DESTROY = (1<<5), + GARBAGE_COLLECT = (1<<6), + }; + Timer (); + Timer (int flags); + ~Timer (); + + void SetFunction (void (*fn) (void)); + template + void SetFunction (void (*fn) (U1), T1 a1); + template + void SetFunction (void (*fn) (U1, U2), T1 a1, T2 a2); + template + void SetFunction (void (*fn) (U1, U2, U3), T1 a1, T2 a2, T3 a3); + + template + void SetArguments (T1 a1); + template + void SetArguments (T1 a1, T2 a2); + template + void SetArguments (T1 a1, T2 a2, T3 a3); + + void SetDelay (const Time &time); + Time GetDelay (void) const; + void Cancel (void); + void Remove (void); + bool IsExpired (void) const; + bool IsRunning (void) const; + + void Schedule (void); + +private: + int m_flags; + Time m_delay; + EventId m_event; + TimerImpl *m_impl; +}; + +} // namespace ns3 + + +// The actual implementation. +#include "simulator.h" +#include "ns3/type-traits.h" + +namespace ns3 { + +template +struct TimerTraits; +template +struct TimerTraits +{ + typedef typename TypeTraits::ReferencedType>::NonConstType StoredType; + typedef const StoredType &ParameterType; +}; + +class TimerImpl +{ +public: + virtual ~TimerImpl () {} + virtual EventId Schedule (const Time &delay) = 0; +}; + + +template +struct TimerImplOne : public TimerImpl +{ + virtual void SetArguments (T1 a1) = 0; +}; + + +template +void +Timer::SetFunction (void (*fn) (U1), T1 a1) +{ + struct FnTimerImplOne : public TimerImplOne::ParameterType> + { + typedef void (*FN) (U1); + FnTimerImplOne (FN fn) + : m_fn (fn) {} + virtual void SetArguments (typename TimerTraits::ParameterType a1) { + m_a1 = a1; + } + virtual EventId Schedule (const Time &delay) { + return Simulator::Schedule (delay, m_fn, m_a1); + } + FN m_fn; + typename TimerTraits::StoredType m_a1; + } *function = new FnTimerImplOne (fn); + function->SetArguments (a1); + delete m_impl; + m_impl = function; + +} + +template +void +Timer::SetArguments (T1 a1) +{ + if (m_impl == 0) + { + NS_FATAL_ERROR ("You cannot set the arguments of a Timer before setting its function."); + return; + } + struct TimerImplOne::ParameterType> *impl = + dynamic_cast::ParameterType> *> (m_impl); + if (impl == 0) + { + NS_FATAL_ERROR ("You tried to set Timer arguments incompatible with its function."); + return; + } + impl->SetArguments (a1); +} + + + + +} // namespace ns3 + +#endif /* TIMER_H */ diff --git a/src/simulator/wscript b/src/simulator/wscript index 970a43aea..68279c9e3 100644 --- a/src/simulator/wscript +++ b/src/simulator/wscript @@ -60,6 +60,8 @@ def build(bld): 'event-impl.cc', 'simulator.cc', 'time-default-value.cc', + 'timer.cc', + 'event-garbage-collector.cc', ] headers = bld.create_obj('ns3header') @@ -73,6 +75,7 @@ def build(bld): 'scheduler-factory.h', 'simulation-singleton.h', 'time-default-value.h', + 'timer.h' ] env = bld.env_of_name('default')