A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
timer.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2007 INRIA
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation;
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 *
17 * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
18 */
19#include "timer.h"
20
21#include "log.h"
23#include "simulator.h"
24
31namespace ns3
32{
33
35
37 : m_flags(CHECK_ON_DESTROY),
38 m_delay(FemtoSeconds(0)),
39 m_event(),
40 m_impl(nullptr)
41{
42 NS_LOG_FUNCTION(this);
43}
44
46 : m_flags(destroyPolicy),
47 m_delay(FemtoSeconds(0)),
48 m_event(),
49 m_impl(nullptr)
50{
51 NS_LOG_FUNCTION(this << destroyPolicy);
52}
53
55{
56 NS_LOG_FUNCTION(this);
58 {
59 if (m_event.IsRunning())
60 {
61 NS_FATAL_ERROR("Event is still running while destroying.");
62 }
63 }
64 else if (m_flags & CANCEL_ON_DESTROY)
65 {
67 }
68 else if (m_flags & REMOVE_ON_DESTROY)
69 {
71 }
72 delete m_impl;
73}
74
75void
77{
78 NS_LOG_FUNCTION(this << time);
79 m_delay = time;
80}
81
82Time
84{
85 NS_LOG_FUNCTION(this);
86 return m_delay;
87}
88
89Time
91{
92 NS_LOG_FUNCTION(this);
93 switch (GetState())
94 {
95 case Timer::RUNNING:
97 case Timer::EXPIRED:
98 return TimeStep(0);
100 return m_delayLeft;
101 default:
102 NS_ASSERT(false);
103 return TimeStep(0);
104 }
105}
106
107void
109{
110 NS_LOG_FUNCTION(this);
111 m_event.Cancel();
112}
113
114void
116{
117 NS_LOG_FUNCTION(this);
118 m_event.Remove();
119}
120
121bool
123{
124 NS_LOG_FUNCTION(this);
125 return !IsSuspended() && m_event.IsExpired();
126}
127
128bool
130{
131 NS_LOG_FUNCTION(this);
132 return !IsSuspended() && m_event.IsRunning();
133}
134
135bool
137{
138 NS_LOG_FUNCTION(this);
140}
141
144{
145 NS_LOG_FUNCTION(this);
146 if (IsRunning())
147 {
148 return Timer::RUNNING;
149 }
150 else if (IsExpired())
151 {
152 return Timer::EXPIRED;
153 }
154 else
155 {
157 return Timer::SUSPENDED;
158 }
159}
160
161void
163{
164 NS_LOG_FUNCTION(this);
166}
167
168void
170{
171 NS_LOG_FUNCTION(this << delay);
172 NS_ASSERT(m_impl != nullptr);
173 if (m_event.IsRunning())
174 {
175 NS_FATAL_ERROR("Event is still running while re-scheduling.");
176 }
177 m_event = m_impl->Schedule(delay);
178}
179
180void
182{
183 NS_LOG_FUNCTION(this);
187 {
188 m_event.Cancel();
189 }
190 else if (m_flags & REMOVE_ON_DESTROY)
191 {
192 m_event.Remove();
193 }
195}
196
197void
199{
200 NS_LOG_FUNCTION(this);
203 m_flags &= ~TIMER_SUSPENDED;
204}
205
206} // namespace ns3
void Cancel()
This method is syntactic sugar for the ns3::Simulator::Cancel method.
Definition: event-id.cc:55
bool IsExpired() const
This method is syntactic sugar for the ns3::Simulator::IsExpired method.
Definition: event-id.cc:69
void Remove()
This method is syntactic sugar for the ns3::Simulator::Remove method.
Definition: event-id.cc:62
bool IsRunning() const
This method is syntactic sugar for !IsExpired().
Definition: event-id.cc:76
static Time GetDelayLeft(const EventId &id)
Get the remaining time until this event will execute.
Definition: simulator.cc:208
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:105
void SetDelay(const Time &delay)
Definition: timer.cc:76
~Timer()
Definition: timer.cc:54
TimerImpl * m_impl
The timer implementation, which contains the bound callback function and arguments.
Definition: timer.h:257
bool IsExpired() const
Definition: timer.cc:122
Timer()
Create a timer with a default event lifetime management policy:
Definition: timer.cc:36
EventId m_event
The future event scheduled to expire the timer.
Definition: timer.h:252
Time GetDelayLeft() const
Definition: timer.cc:90
Timer::State GetState() const
Definition: timer.cc:143
int m_flags
Bitfield for Timer State, DestroyPolicy and InternalSuspended.
Definition: timer.h:248
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:109
@ RUNNING
Timer is currently running.
Definition: timer.h:110
@ EXPIRED
Timer has already expired.
Definition: timer.h:111
@ SUSPENDED
Timer is suspended.
Definition: timer.h:112
static constexpr auto TIMER_SUSPENDED
Internal bit marking the suspended timer state.
Definition: timer.h:237
void Cancel()
Cancel the currently-running event if there is one.
Definition: timer.cc:108
Time GetDelay() const
Definition: timer.cc:83
bool IsSuspended() const
Definition: timer.cc:136
Time m_delay
The delay configured for this Timer.
Definition: timer.h:250
void Remove()
Remove from the simulation event-list the currently-running event if there is one.
Definition: timer.cc:115
void Schedule()
Schedule a new event using the currently-configured delay, function, and arguments.
Definition: timer.cc:162
Time m_delayLeft
The amount of time left on the Timer while it is suspended.
Definition: timer.h:259
void Resume()
Restart the timer to expire within the amount of time left saved during Suspend.
Definition: timer.cc:198
bool IsRunning() const
Definition: timer.cc:129
void Suspend()
Pause the timer and save the amount of time left until it was set to expire.
Definition: timer.cc:181
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:66
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
Definition: fatal-error.h:179
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#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:1396
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.