A Discrete-Event Network Simulator
API
ns3::Timer Class Reference

A simple Timer class. More...

#include "timer.h"

+ Collaboration diagram for ns3::Timer:

Public Types

enum  DestroyPolicy { CANCEL_ON_DESTROY = (1 << 3), REMOVE_ON_DESTROY = (1 << 4), CHECK_ON_DESTROY = (1 << 5) }
 The policy to use to manager the internal timer when an instance of the Timer class is destroyed. More...
 
enum  State { RUNNING, EXPIRED, SUSPENDED }
 The possible states of the Timer. More...
 

Public Member Functions

 Timer ()
 Create a timer with a default event lifetime management policy: More...
 
 Timer (enum DestroyPolicy destroyPolicy)
 
 ~Timer ()
 
void Cancel (void)
 Cancel the currently-running event if there is one. More...
 
Time GetDelay (void) const
 
Time GetDelayLeft (void) const
 
enum Timer::State GetState (void) const
 
bool IsExpired (void) const
 
bool IsRunning (void) const
 
bool IsSuspended (void) const
 
void Remove (void)
 Remove from the simulation event-list the currently-running event if there is one. More...
 
void Resume (void)
 Restart the timer to expire within the amount of time left saved during Suspend. More...
 
void Schedule (void)
 Schedule a new event using the currently-configured delay, function, and arguments. More...
 
void Schedule (Time delay)
 
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)
 
template<typename T1 , typename T2 , typename T3 , typename T4 >
void SetArguments (T1 a1, T2 a2, T3 a3, T4 a4)
 
template<typename T1 , typename T2 , typename T3 , typename T4 , typename T5 >
void SetArguments (T1 a1, T2 a2, T3 a3, T4 a4, T5 a5)
 
template<typename T1 , typename T2 , typename T3 , typename T4 , typename T5 , typename T6 >
void SetArguments (T1 a1, T2 a2, T3 a3, T4 a4, T5 a5, T6 a6)
 
void SetDelay (const Time &delay)
 
template<typename FN >
void SetFunction (FN fn)
 
template<typename MEM_PTR , typename OBJ_PTR >
void SetFunction (MEM_PTR memPtr, OBJ_PTR objPtr)
 
void Suspend (void)
 Cancel the timer and save the amount of time left until it was set to expire. More...
 

Private Types

enum  InternalSuspended { TIMER_SUSPENDED = (1 << 7) }
 Internal bit marking the suspended state. More...
 

Private Attributes

Time m_delay
 The delay configured for this Timer. More...
 
Time m_delayLeft
 The amount of time left on the Timer while it is suspended. More...
 
EventId m_event
 The future event scheduled to expire the timer. More...
 
int m_flags
 Bitfield for Timer State, DestroyPolicy and InternalSuspended. More...
 
TimerImplm_impl
 The timer implementation, which contains the bound callback function and arguments. More...
 

Detailed Description

A simple Timer class.

A timer is used to hold together a delay, a function to invoke when the delay expires, and a set of arguments to pass to the function when the delay expires.

A Timer can be suspended, resumed, cancelled and queried for the time left, but it can't be extended (except by suspending and resuming.)

A timer can also be used to enforce a set of predefined event lifetime management policies. These policies are specified at construction time and cannot be changed after.

See also
Watchdog for a simpler interface for a watchdog Virtual Time Timer and Watchdog.

Definition at line 73 of file timer.h.

Member Enumeration Documentation

◆ DestroyPolicy

The policy to use to manager the internal timer when an instance of the Timer class is destroyed.

Enumerator
CANCEL_ON_DESTROY 

This policy cancels the event from the destructor of the Timer to verify that the event has already expired.

REMOVE_ON_DESTROY 

This policy removes the event from the simulation event list when the destructor of the Timer is invoked.

CHECK_ON_DESTROY 

This policy enforces a check from the destructor of the Timer to verify that the timer has already expired.

Definition at line 80 of file timer.h.

◆ InternalSuspended

Internal bit marking the suspended state.

Enumerator
TIMER_SUSPENDED 

Definition at line 267 of file timer.h.

◆ State

The possible states of the Timer.

Enumerator
RUNNING 
EXPIRED 

Timer is currently running.

SUSPENDED 

Timer has already expired.

Definition at line 99 of file timer.h.

Constructor & Destructor Documentation

◆ Timer() [1/2]

ns3::Timer::Timer ( )

Create a timer with a default event lifetime management policy:

  • CHECK_ON_DESTROY

Definition at line 35 of file timer.cc.

References NS_LOG_FUNCTION.

◆ Timer() [2/2]

ns3::Timer::Timer ( enum DestroyPolicy  destroyPolicy)
Parameters
[in]destroyPolicythe event lifetime management policies to use for destroy events

Definition at line 44 of file timer.cc.

References NS_LOG_FUNCTION.

◆ ~Timer()

ns3::Timer::~Timer ( )

Member Function Documentation

◆ Cancel()

◆ GetDelay()

Time ns3::Timer::GetDelay ( void  ) const
Returns
The currently-configured delay for the next Schedule.

Definition at line 81 of file timer.cc.

References m_delay, and NS_LOG_FUNCTION.

◆ GetDelayLeft()

Time ns3::Timer::GetDelayLeft ( void  ) const
Returns
The amount of time left until this timer expires.

This method returns zero if the timer is in EXPIRED state.

Definition at line 87 of file timer.cc.

References EXPIRED, ns3::Simulator::GetDelayLeft(), GetState(), m_delayLeft, m_event, NS_ASSERT, NS_LOG_FUNCTION, RUNNING, SUSPENDED, and ns3::TimeStep().

Referenced by ns3::dsdv::RoutingProtocol::RecvDsdv(), ns3::TcpSocketBase::SendPendingData(), ns3::aodv::RoutingProtocol::SendRequest(), ns3::aodv::RoutingProtocol::SendRerrMessage(), and ns3::aodv::RoutingProtocol::SendRerrWhenNoRouteToForward().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ GetState()

enum Timer::State ns3::Timer::GetState ( void  ) const
Returns
The current state of the timer.

Definition at line 139 of file timer.cc.

References EXPIRED, IsExpired(), IsRunning(), IsSuspended(), NS_ASSERT, NS_LOG_FUNCTION, RUNNING, and SUSPENDED.

Referenced by TimerStateTestCase::DoRun(), and GetDelayLeft().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ IsExpired()

bool ns3::Timer::IsExpired ( void  ) const
Returns
true if there is no currently-running event, false otherwise.

Definition at line 121 of file timer.cc.

References ns3::EventId::IsExpired(), IsSuspended(), m_event, and NS_LOG_FUNCTION.

Referenced by TimerStateTestCase::DoRun(), GetState(), ns3::TcpSocketBase::SendDataPacket(), and ns3::TcpSocketBase::SendPendingData().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ IsRunning()

◆ IsSuspended()

bool ns3::Timer::IsSuspended ( void  ) const
Returns
true if this timer was suspended and not yet resumed, false otherwise.

Definition at line 133 of file timer.cc.

References m_flags, NS_LOG_FUNCTION, and TIMER_SUSPENDED.

Referenced by ns3::dsr::DsrRouting::CheckSendBuffer(), TimerStateTestCase::DoRun(), GetState(), IsExpired(), and IsRunning().

+ Here is the caller graph for this function:

◆ Remove()

void ns3::Timer::Remove ( void  )

Remove from the simulation event-list the currently-running event if there is one.

Do nothing otherwise.

Definition at line 115 of file timer.cc.

References m_event, NS_LOG_FUNCTION, and ns3::Simulator::Remove().

+ Here is the call graph for this function:

◆ Resume()

void ns3::Timer::Resume ( 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.

Definition at line 187 of file timer.cc.

References m_delayLeft, m_event, m_flags, m_impl, NS_ASSERT, NS_LOG_FUNCTION, ns3::TimerImpl::Schedule(), and TIMER_SUSPENDED.

Referenced by ns3::dsr::DsrRouting::CheckSendBuffer(), and TimerStateTestCase::DoRun().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ Schedule() [1/2]

◆ Schedule() [2/2]

void ns3::Timer::Schedule ( Time  delay)
Parameters
[in]delaythe delay to use

Schedule a new event using the specified delay (ignore the delay set by Timer::SetDelay), function, and arguments.

Definition at line 165 of file timer.cc.

References ns3::EventId::IsRunning(), m_event, m_impl, NS_ASSERT, NS_FATAL_ERROR, NS_LOG_FUNCTION, and ns3::TimerImpl::Schedule().

+ Here is the call graph for this function:

◆ SetArguments() [1/6]

template<typename T1 >
void ns3::Timer::SetArguments ( T1  a1)
Parameters
[in]a1the first argument

Store this argument in this Timer for later use by Timer::Schedule.

Definition at line 324 of file timer.h.

References m_impl, NS_FATAL_ERROR, and ns3::TimerImpl::SetArgs().

Referenced by TimerStateTestCase::DoRun(), TimerTemplateTestCase::DoRun(), and ns3::aodv::RoutingProtocol::SendReplyByIntermediateNode().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ SetArguments() [2/6]

template<typename T1 , typename T2 >
void ns3::Timer::SetArguments ( T1  a1,
T2  a2 
)
Parameters
[in]a1the first argument
[in]a2the second argument

Store these arguments in this Timer for later use by Timer::Schedule.

Definition at line 335 of file timer.h.

References m_impl, NS_FATAL_ERROR, and ns3::TimerImpl::SetArgs().

+ Here is the call graph for this function:

◆ SetArguments() [3/6]

template<typename T1 , typename T2 , typename T3 >
void ns3::Timer::SetArguments ( T1  a1,
T2  a2,
T3  a3 
)
Parameters
[in]a1the first argument
[in]a2the second argument
[in]a3the third argument

Store these arguments in this Timer for later use by Timer::Schedule.

Definition at line 347 of file timer.h.

References m_impl, NS_FATAL_ERROR, and ns3::TimerImpl::SetArgs().

+ Here is the call graph for this function:

◆ SetArguments() [4/6]

template<typename T1 , typename T2 , typename T3 , typename T4 >
void ns3::Timer::SetArguments ( T1  a1,
T2  a2,
T3  a3,
T4  a4 
)
Parameters
[in]a1the first argument
[in]a2the second argument
[in]a3the third argument
[in]a4the fourth argument

Store these arguments in this Timer for later use by Timer::Schedule.

Definition at line 359 of file timer.h.

References m_impl, NS_FATAL_ERROR, and ns3::TimerImpl::SetArgs().

+ Here is the call graph for this function:

◆ SetArguments() [5/6]

template<typename T1 , typename T2 , typename T3 , typename T4 , typename T5 >
void ns3::Timer::SetArguments ( T1  a1,
T2  a2,
T3  a3,
T4  a4,
T5  a5 
)
Parameters
[in]a1the first argument
[in]a2the second argument
[in]a3the third argument
[in]a4the fourth argument
[in]a5the fifth argument

Store these arguments in this Timer for later use by Timer::Schedule.

Definition at line 371 of file timer.h.

References m_impl, NS_FATAL_ERROR, and ns3::TimerImpl::SetArgs().

+ Here is the call graph for this function:

◆ SetArguments() [6/6]

template<typename T1 , typename T2 , typename T3 , typename T4 , typename T5 , typename T6 >
void ns3::Timer::SetArguments ( T1  a1,
T2  a2,
T3  a3,
T4  a4,
T5  a5,
T6  a6 
)
Parameters
[in]a1the first argument
[in]a2the second argument
[in]a3the third argument
[in]a4the fourth argument
[in]a5the fifth argument
[in]a6the sixth argument

Store these arguments in this Timer for later use by Timer::Schedule.

Definition at line 383 of file timer.h.

References m_impl, NS_FATAL_ERROR, and ns3::TimerImpl::SetArgs().

+ Here is the call graph for this function:

◆ SetDelay()

void ns3::Timer::SetDelay ( const Time delay)

◆ SetFunction() [1/2]

◆ SetFunction() [2/2]

template<typename MEM_PTR , typename OBJ_PTR >
void ns3::Timer::SetFunction ( MEM_PTR  memPtr,
OBJ_PTR  objPtr 
)
Parameters
[in]memPtrthe member function pointer
[in]objPtrthe pointer to object

Store this function and object in this Timer for later use by Timer::Schedule.

Definition at line 316 of file timer.h.

References m_impl, and ns3::MakeTimerImpl().

+ Here is the call graph for this function:

◆ Suspend()

void ns3::Timer::Suspend ( void  )

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.

Definition at line 177 of file timer.cc.

References ns3::Simulator::GetDelayLeft(), IsRunning(), m_delayLeft, m_event, m_flags, NS_ASSERT, NS_LOG_FUNCTION, ns3::Simulator::Remove(), and TIMER_SUSPENDED.

Referenced by ns3::dsr::DsrRouting::CheckSendBuffer(), and TimerStateTestCase::DoRun().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Member Data Documentation

◆ m_delay

Time ns3::Timer::m_delay
private

The delay configured for this Timer.

Definition at line 283 of file timer.h.

Referenced by GetDelay(), Schedule(), and SetDelay().

◆ m_delayLeft

Time ns3::Timer::m_delayLeft
private

The amount of time left on the Timer while it is suspended.

Definition at line 292 of file timer.h.

Referenced by GetDelayLeft(), Resume(), and Suspend().

◆ m_event

EventId ns3::Timer::m_event
private

The future event scheduled to expire the timer.

Definition at line 285 of file timer.h.

Referenced by Cancel(), GetDelayLeft(), IsExpired(), IsRunning(), Remove(), Resume(), Schedule(), Suspend(), and ~Timer().

◆ m_flags

int ns3::Timer::m_flags
private

Bitfield for Timer State, DestroyPolicy and InternalSuspended.

Internal:
The DestroyPolicy, State and InternalSuspended state are stored in this single bitfield. The State uses the low-order bits, so the other users of the bitfield have to be careful in defining their bits to avoid the State.

Definition at line 281 of file timer.h.

Referenced by IsSuspended(), Resume(), Suspend(), and ~Timer().

◆ m_impl

TimerImpl* ns3::Timer::m_impl
private

The timer implementation, which contains the bound callback function and arguments.

Definition at line 290 of file timer.h.

Referenced by Resume(), Schedule(), SetArguments(), SetFunction(), and ~Timer().


The documentation for this class was generated from the following files: