A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
ns3::Timer Class Reference

A simple virtual 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 or suspended. 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:
 
 Timer (DestroyPolicy destroyPolicy)
 
 ~Timer ()
 
void Cancel ()
 Cancel the currently-running event if there is one.
 
Time GetDelay () const
 
Time GetDelayLeft () const
 
Timer::State GetState () const
 
bool IsExpired () const
 
bool IsRunning () const
 
bool IsSuspended () const
 
void Remove ()
 Remove from the simulation event-list the currently-running event if there is one.
 
void Resume ()
 Restart the timer to expire within the amount of time left saved during Suspend.
 
void Schedule ()
 Schedule a new event using the currently-configured delay, function, and arguments.
 
void Schedule (Time delay)
 
template<typename... Ts>
void SetArguments (Ts... args)
 
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 ()
 Pause the timer and save the amount of time left until it was set to expire.
 

Private Attributes

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

Static Private Attributes

static constexpr auto TIMER_SUSPENDED {1 << 7}
 Internal bit marking the suspended timer state.
 

Detailed Description

A simple virtual Timer class.

A (virtual time) 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 77 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 or suspended.

In the case of suspension, only CANCEL_ON_DESTROY and REMOVE_ON_DESTROY apply.

These symbols have "Destroy" in their names for historical reasons.

Enumerator
CANCEL_ON_DESTROY 

This policy cancels the event from the destructor of the Timer or from Suspend().

This is typically faster than REMOVE_ON_DESTROY but uses more memory.

REMOVE_ON_DESTROY 

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

This is typically slower than Cancel, but frees memory.

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 90 of file timer.h.

◆ State

The possible states of the Timer.

Enumerator
RUNNING 

Timer is currently running.

EXPIRED 

Timer has already expired.

SUSPENDED 

Timer is suspended.

Definition at line 112 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 36 of file timer.cc.

References NS_LOG_FUNCTION.

◆ Timer() [2/2]

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

Definition at line 45 of file timer.cc.

References NS_LOG_FUNCTION.

◆ ~Timer()

ns3::Timer::~Timer ( )

Definition at line 54 of file timer.cc.

References ns3::EventId::Cancel(), CANCEL_ON_DESTROY, CHECK_ON_DESTROY, ns3::EventId::IsRunning(), m_event, m_flags, m_impl, NS_FATAL_ERROR, NS_LOG_FUNCTION, ns3::EventId::Remove(), and REMOVE_ON_DESTROY.

+ Here is the call graph for this function:

Member Function Documentation

◆ Cancel()

void ns3::Timer::Cancel ( )

Cancel the currently-running event if there is one.

Do nothing otherwise.

Definition at line 108 of file timer.cc.

References ns3::EventId::Cancel(), m_event, and NS_LOG_FUNCTION.

Referenced by ns3::TcpSocketBase::CancelAllTimers(), TimerStateTestCase::DoRun(), ns3::aodv::RoutingProtocol::HelloTimerExpire(), ns3::aodv::RoutingProtocol::NotifyInterfaceDown(), ns3::aodv::RoutingProtocol::NotifyRemoveAddress(), ns3::aodv::Neighbors::Purge(), ns3::dsr::DsrRouteCache::PurgeMac(), ns3::aodv::RoutingProtocol::RecvReplyAck(), ns3::TcpSocketBase::ReTxTimeout(), ns3::aodv::Neighbors::ScheduleTimer(), ns3::dsr::DsrRouteCache::ScheduleTimer(), ns3::dsr::DsrRouting::SendBuffTimerExpire(), ns3::Ipv6AutoconfiguredPrefix::StopPreferredTimer(), and ns3::Ipv6AutoconfiguredPrefix::StopValidTimer().

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

◆ GetDelay()

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

Definition at line 83 of file timer.cc.

References m_delay, and NS_LOG_FUNCTION.

◆ GetDelayLeft()

Time ns3::Timer::GetDelayLeft ( ) 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 90 of file timer.cc.

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

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()

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

Definition at line 143 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 ( ) const
Returns
true if there is no currently-running event, false otherwise.

Definition at line 122 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()

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

Definition at line 129 of file timer.cc.

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

Referenced by TimerStateTestCase::DoRun(), GetState(), ns3::olsr::RoutingProtocol::QueueMessage(), ns3::dsr::DsrRouting::SendBuffTimerExpire(), ns3::TcpSocketBase::SendPendingData(), ns3::aodv::RoutingProtocol::SendRerrMessage(), ns3::aodv::RoutingProtocol::SendRerrWhenNoRouteToForward(), ns3::Ipv6AutoconfiguredPrefix::StopPreferredTimer(), ns3::Ipv6AutoconfiguredPrefix::StopValidTimer(), and Suspend().

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

◆ IsSuspended()

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

Definition at line 136 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 ( )

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::EventId::Remove().

+ Here is the call graph for this function:

◆ Resume()

void ns3::Timer::Resume ( )

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 198 of file timer.cc.

References m_delayLeft, m_event, m_flags, m_impl, NS_ASSERT, NS_LOG_FUNCTION, ns3::internal::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]

void ns3::Timer::Schedule ( )

Schedule a new event using the currently-configured delay, function, and arguments.

Definition at line 162 of file timer.cc.

References m_delay, NS_LOG_FUNCTION, and Schedule().

Referenced by ns3::dsr::DsrRouting::DsrRouting(), ns3::aodv::RoutingProtocol::DoInitialize(), TimerStateTestCase::DoRun(), TimerTemplateTestCase::DoRun(), ns3::aodv::RoutingProtocol::HelloTimerExpire(), ns3::olsr::RoutingProtocol::HelloTimerExpire(), ns3::olsr::RoutingProtocol::HnaTimerExpire(), ns3::olsr::RoutingProtocol::MidTimerExpire(), ns3::aodv::Neighbors::Purge(), ns3::dsr::DsrRouteCache::PurgeMac(), ns3::olsr::RoutingProtocol::QueueMessage(), ns3::aodv::RoutingProtocol::RerrRateLimitTimerExpire(), ns3::aodv::RoutingProtocol::RreqRateLimitTimerExpire(), Schedule(), ns3::aodv::Neighbors::ScheduleTimer(), ns3::dsr::DsrRouteCache::ScheduleTimer(), ns3::dsr::DsrRouting::SendBuffTimerExpire(), ns3::TcpSocketBase::SendDataPacket(), ns3::TcpSocketBase::SendPendingData(), ns3::dsdv::RoutingProtocol::SendPeriodicUpdate(), ns3::aodv::RoutingProtocol::Start(), ns3::dsdv::RoutingProtocol::Start(), ns3::Ipv6AutoconfiguredPrefix::StartPreferredTimer(), ns3::Ipv6AutoconfiguredPrefix::StartValidTimer(), and ns3::olsr::RoutingProtocol::TcTimerExpire().

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

◆ 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 169 of file timer.cc.

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

+ Here is the call graph for this function:

◆ SetArguments()

template<typename... Ts>
void ns3::Timer::SetArguments ( Ts...  args)
Template Parameters
Ts[deduced] Argument types
Parameters
[in]argsarguments

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

Definition at line 295 of file timer.h.

References m_impl, NS_FATAL_ERROR, and ns3::internal::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:

◆ SetDelay()

void ns3::Timer::SetDelay ( const Time delay)
Parameters
[in]delayThe delay

The next call to Schedule will schedule the timer with this delay.

Definition at line 76 of file timer.cc.

References m_delay, and NS_LOG_FUNCTION.

Referenced by ns3::dsr::DsrRouteCache::DsrRouteCache(), ns3::aodv::Neighbors::Neighbors(), TimerStateTestCase::DoRun(), TimerTemplateTestCase::DoRun(), ns3::olsr::RoutingProtocol::QueueMessage(), ns3::aodv::RoutingProtocol::SendReplyByIntermediateNode(), ns3::Ipv6AutoconfiguredPrefix::StartPreferredTimer(), and ns3::Ipv6AutoconfiguredPrefix::StartValidTimer().

+ Here is the caller graph for this function:

◆ SetFunction() [1/2]

template<typename FN >
void ns3::Timer::SetFunction ( FN  fn)
Template Parameters
FN[deduced] The type of the function.
Parameters
[in]fnthe function

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

Definition at line 279 of file timer.h.

References m_impl, and ns3::internal::MakeTimerImpl().

Referenced by ns3::dsr::DsrRouteCache::DsrRouteCache(), ns3::dsr::DsrRouting::DsrRouting(), ns3::aodv::Neighbors::Neighbors(), ns3::TcpSocketBase::TcpSocketBase(), ns3::aodv::RoutingProtocol::DoInitialize(), TimerStateTestCase::DoRun(), TimerTemplateTestCase::DoRun(), ns3::aodv::RoutingProtocol::SendReplyByIntermediateNode(), ns3::olsr::RoutingProtocol::SetIpv4(), ns3::aodv::RoutingProtocol::Start(), ns3::dsdv::RoutingProtocol::Start(), ns3::Ipv6AutoconfiguredPrefix::StartPreferredTimer(), and ns3::Ipv6AutoconfiguredPrefix::StartValidTimer().

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

◆ SetFunction() [2/2]

template<typename MEM_PTR , typename OBJ_PTR >
void ns3::Timer::SetFunction ( MEM_PTR  memPtr,
OBJ_PTR  objPtr 
)
Template Parameters
MEM_PTR[deduced] The type of the class member function.
OBJ_PTR[deduced] The type of the class instance pointer.
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 287 of file timer.h.

References m_impl, and ns3::internal::MakeTimerImpl().

+ Here is the call graph for this function:

◆ Suspend()

void ns3::Timer::Suspend ( )

Pause the timer and save the amount of time left until it was set to expire.

Subsequently calling Resume() will restart the Timer with the remaining time.

The DestroyPolicy set at construction determines whether the underlying Simulator::Event is cancelled or removed.

Calling Suspend on a non-running timer is an error.

Definition at line 181 of file timer.cc.

References ns3::EventId::Cancel(), CANCEL_ON_DESTROY, ns3::Simulator::GetDelayLeft(), IsRunning(), m_delayLeft, m_event, m_flags, NS_ASSERT, NS_LOG_FUNCTION, ns3::EventId::Remove(), REMOVE_ON_DESTROY, 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 254 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 263 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 256 of file timer.h.

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

◆ 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 252 of file timer.h.

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

◆ m_impl

internal::TimerImpl* ns3::Timer::m_impl
private

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

Definition at line 261 of file timer.h.

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

◆ TIMER_SUSPENDED

constexpr auto ns3::Timer::TIMER_SUSPENDED {1 << 7}
staticconstexprprivate

Internal bit marking the suspended timer state.

Definition at line 241 of file timer.h.

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


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