A Discrete-Event Network Simulator
API
timer.cc
Go to the documentation of this file.
1/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
3 * Copyright (c) 2007 INRIA
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation;
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 *
18 * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
19 */
20#include "timer.h"
21#include "simulator.h"
23#include "log.h"
24
31namespace ns3 {
32
34
36 : m_flags (CHECK_ON_DESTROY),
37 m_delay (FemtoSeconds (0)),
38 m_event (),
39 m_impl (0)
40{
41 NS_LOG_FUNCTION (this);
42}
43
44Timer::Timer (enum DestroyPolicy destroyPolicy)
45 : m_flags (destroyPolicy),
46 m_delay (FemtoSeconds (0)),
47 m_event (),
48 m_impl (0)
49{
50 NS_LOG_FUNCTION (this << destroyPolicy);
51}
52
54{
55 NS_LOG_FUNCTION (this);
57 {
58 if (m_event.IsRunning ())
59 {
60 NS_FATAL_ERROR ("Event is still running while destroying.");
61 }
62 }
63 else if (m_flags & CANCEL_ON_DESTROY)
64 {
65 m_event.Cancel ();
66 }
67 else if (m_flags & REMOVE_ON_DESTROY)
68 {
69 m_event.Remove ();
70 }
71 delete m_impl;
72}
73
74void
75Timer::SetDelay (const Time &time)
76{
77 NS_LOG_FUNCTION (this << time);
78 m_delay = time;
79}
80Time
81Timer::GetDelay (void) const
82{
83 NS_LOG_FUNCTION (this);
84 return m_delay;
85}
86Time
88{
89 NS_LOG_FUNCTION (this);
90 switch (GetState ())
91 {
92 case Timer::RUNNING:
94 break;
95 case Timer::EXPIRED:
96 return TimeStep (0);
97 break;
99 return m_delayLeft;
100 break;
101 default:
102 NS_ASSERT (false);
103 return TimeStep (0);
104 break;
105 }
106}
107
108void
110{
111 NS_LOG_FUNCTION (this);
112 m_event.Cancel ();
113}
114void
116{
117 NS_LOG_FUNCTION (this);
118 m_event.Remove ();
119}
120bool
122{
123 NS_LOG_FUNCTION (this);
124 return !IsSuspended () && m_event.IsExpired ();
125}
126bool
128{
129 NS_LOG_FUNCTION (this);
130 return !IsSuspended () && m_event.IsRunning ();
131}
132bool
134{
135 NS_LOG_FUNCTION (this);
137}
138enum Timer::State
139Timer::GetState (void) const
140{
141 NS_LOG_FUNCTION (this);
142 if (IsRunning ())
143 {
144 return Timer::RUNNING;
145 }
146 else if (IsExpired ())
147 {
148 return Timer::EXPIRED;
149 }
150 else
151 {
153 return Timer::SUSPENDED;
154 }
155}
156
157void
159{
160 NS_LOG_FUNCTION (this);
162}
163
164void
166{
167 NS_LOG_FUNCTION (this << delay);
168 NS_ASSERT (m_impl != 0);
169 if (m_event.IsRunning ())
170 {
171 NS_FATAL_ERROR ("Event is still running while re-scheduling.");
172 }
173 m_event = m_impl->Schedule (delay);
174}
175
176void
178{
179 NS_LOG_FUNCTION (this);
180 NS_ASSERT (IsRunning ());
183 {
184 m_event.Cancel ();
185 }
186 else if (m_flags & REMOVE_ON_DESTROY)
187 {
188 m_event.Remove ();
189 }
191}
192
193void
195{
196 NS_LOG_FUNCTION (this);
199 m_flags &= ~TIMER_SUSPENDED;
200}
201
202
203} // namespace ns3
204
bool IsRunning(void) const
This method is syntactic sugar for !IsExpired().
Definition: event-id.cc:71
void Cancel(void)
This method is syntactic sugar for the ns3::Simulator::Cancel method.
Definition: event-id.cc:53
void Remove(void)
This method is syntactic sugar for the ns3::Simulator::Remove method.
Definition: event-id.cc:59
bool IsExpired(void) const
This method is syntactic sugar for the ns3::Simulator::IsExpired method.
Definition: event-id.cc:65
static Time GetDelayLeft(const EventId &id)
Get the remaining time until this event will execute.
Definition: simulator.cc:204
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:103
void SetDelay(const Time &delay)
Definition: timer.cc:75
void Suspend(void)
Pause the timer and save the amount of time left until it was set to expire.
Definition: timer.cc:177
bool IsExpired(void) const
Definition: timer.cc:121
void Remove(void)
Remove from the simulation event-list the currently-running event if there is one.
Definition: timer.cc:115
~Timer()
Definition: timer.cc:53
TimerImpl * m_impl
The timer implementation, which contains the bound callback function and arguments.
Definition: timer.h:259
Timer()
Create a timer with a default event lifetime management policy:
Definition: timer.cc:35
EventId m_event
The future event scheduled to expire the timer.
Definition: timer.h:254
bool IsRunning(void) const
Definition: timer.cc:127
@ TIMER_SUSPENDED
Definition: timer.h:238
int m_flags
Bitfield for Timer State, DestroyPolicy and InternalSuspended.
Definition: timer.h:250
DestroyPolicy
The policy to use to manager the internal timer when an instance of the Timer class is destroyed or s...
Definition: timer.h:87
@ CANCEL_ON_DESTROY
This policy cancels the event from the destructor of the Timer or from Suspend().
Definition: timer.h:93
@ CHECK_ON_DESTROY
This policy enforces a check from the destructor of the Timer to verify that the timer has already ex...
Definition: timer.h:104
@ REMOVE_ON_DESTROY
This policy removes the event from the simulation event list when the destructor of the Timer is invo...
Definition: timer.h:99
State
The possible states of the Timer.
Definition: timer.h:108
@ RUNNING
Timer is currently running.
Definition: timer.h:109
@ EXPIRED
Timer has already expired.
Definition: timer.h:110
@ SUSPENDED
Timer is suspended.
Definition: timer.h:111
enum Timer::State GetState(void) const
Definition: timer.cc:139
Time GetDelayLeft(void) const
Definition: timer.cc:87
void Resume(void)
Restart the timer to expire within the amount of time left saved during Suspend.
Definition: timer.cc:194
void Schedule(void)
Schedule a new event using the currently-configured delay, function, and arguments.
Definition: timer.cc:158
void Cancel(void)
Cancel the currently-running event if there is one.
Definition: timer.cc:109
Time GetDelay(void) const
Definition: timer.cc:81
Time m_delay
The delay configured for this Timer.
Definition: timer.h:252
Time m_delayLeft
The amount of time left on the Timer while it is suspended.
Definition: timer.h:261
bool IsSuspended(void) const
Definition: timer.cc:133
virtual EventId Schedule(const Time &delay)=0
Schedule the callback for a future time.
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file,...
Definition: assert.h:67
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
Definition: fatal-error.h:165
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
Time FemtoSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:1284
Debug message logging.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
ns3::SimulationSingleton declaration and template implementation.
ns3::Simulator declaration.
ns3::Timer class declaration.