Implement a Timer API
This commit is contained in:
1 parent
8180ca7a9d
commit
da44de59b9
3 files changed
+320
No files matched your search
@@ -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<EventGarbageCollector>::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 */
|
||||
@@ -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 <typename U1, typename T1>
|
||||
void SetFunction (void (*fn) (U1), T1 a1);
|
||||
template <typename U1, typename U2,
|
||||
typename T1, typename T2>
|
||||
void SetFunction (void (*fn) (U1, U2), T1 a1, T2 a2);
|
||||
template <typename U1, typename U2, typename U3,
|
||||
typename T1, typename T2, typename T3>
|
||||
void SetFunction (void (*fn) (U1, U2, U3), T1 a1, T2 a2, T3 a3);
|
||||
|
||||
template <typename T1>
|
||||
void SetArguments (T1 a1);
|
||||
template <typename T1, typename T2>
|
||||
void SetArguments (T1 a1, T2 a2);
|
||||
template <typename T1, typename T2, typename T3>
|
||||
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 <typename T>
|
||||
struct TimerTraits;
|
||||
template <typename T>
|
||||
struct TimerTraits
|
||||
{
|
||||
typedef typename TypeTraits<typename TypeTraits<T>::ReferencedType>::NonConstType StoredType;
|
||||
typedef const StoredType &ParameterType;
|
||||
};
|
||||
|
||||
class TimerImpl
|
||||
{
|
||||
public:
|
||||
virtual ~TimerImpl () {}
|
||||
virtual EventId Schedule (const Time &delay) = 0;
|
||||
};
|
||||
|
||||
|
||||
template <typename T1>
|
||||
struct TimerImplOne : public TimerImpl
|
||||
{
|
||||
virtual void SetArguments (T1 a1) = 0;
|
||||
};
|
||||
|
||||
|
||||
template <typename U1, typename T1>
|
||||
void
|
||||
Timer::SetFunction (void (*fn) (U1), T1 a1)
|
||||
{
|
||||
struct FnTimerImplOne : public TimerImplOne<typename TimerTraits<U1>::ParameterType>
|
||||
{
|
||||
typedef void (*FN) (U1);
|
||||
FnTimerImplOne (FN fn)
|
||||
: m_fn (fn) {}
|
||||
virtual void SetArguments (typename TimerTraits<U1>::ParameterType a1) {
|
||||
m_a1 = a1;
|
||||
}
|
||||
virtual EventId Schedule (const Time &delay) {
|
||||
return Simulator::Schedule (delay, m_fn, m_a1);
|
||||
}
|
||||
FN m_fn;
|
||||
typename TimerTraits<T1>::StoredType m_a1;
|
||||
} *function = new FnTimerImplOne (fn);
|
||||
function->SetArguments (a1);
|
||||
delete m_impl;
|
||||
m_impl = function;
|
||||
|
||||
}
|
||||
|
||||
template <typename T1>
|
||||
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<typename TimerTraits<T1>::ParameterType> *impl =
|
||||
dynamic_cast<struct TimerImplOne<typename TimerTraits<T1>::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 */
|
||||
@@ -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')
|
||||
|
||||
Reference in new issue
Block a user