A Discrete-Event Network Simulator
API
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"
22 #include "simulation-singleton.h"
23 #include "simulator.h"
24 
31 namespace 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  {
66  m_event.Cancel();
67  }
68  else if (m_flags & REMOVE_ON_DESTROY)
69  {
70  m_event.Remove();
71  }
72  delete m_impl;
73 }
74 
75 void
76 Timer::SetDelay(const Time& time)
77 {
78  NS_LOG_FUNCTION(this << time);
79  m_delay = time;
80 }
81 
82 Time
84 {
85  NS_LOG_FUNCTION(this);
86  return m_delay;
87 }
88 
89 Time
91 {
92  NS_LOG_FUNCTION(this);
93  switch (GetState())
94  {
95  case Timer::RUNNING:
97  break;
98  case Timer::EXPIRED:
99  return TimeStep(0);
100  break;
101  case Timer::SUSPENDED:
102  return m_delayLeft;
103  break;
104  default:
105  NS_ASSERT(false);
106  return TimeStep(0);
107  break;
108  }
109 }
110 
111 void
113 {
114  NS_LOG_FUNCTION(this);
115  m_event.Cancel();
116 }
117 
118 void
120 {
121  NS_LOG_FUNCTION(this);
122  m_event.Remove();
123 }
124 
125 bool
127 {
128  NS_LOG_FUNCTION(this);
129  return !IsSuspended() && m_event.IsExpired();
130 }
131 
132 bool
134 {
135  NS_LOG_FUNCTION(this);
136  return !IsSuspended() && m_event.IsRunning();
137 }
138 
139 bool
141 {
142  NS_LOG_FUNCTION(this);
144 }
145 
146 enum Timer::State
147 Timer::GetState() const
148 {
149  NS_LOG_FUNCTION(this);
150  if (IsRunning())
151  {
152  return Timer::RUNNING;
153  }
154  else if (IsExpired())
155  {
156  return Timer::EXPIRED;
157  }
158  else
159  {
161  return Timer::SUSPENDED;
162  }
163 }
164 
165 void
167 {
168  NS_LOG_FUNCTION(this);
169  Schedule(m_delay);
170 }
171 
172 void
174 {
175  NS_LOG_FUNCTION(this << delay);
176  NS_ASSERT(m_impl != nullptr);
177  if (m_event.IsRunning())
178  {
179  NS_FATAL_ERROR("Event is still running while re-scheduling.");
180  }
181  m_event = m_impl->Schedule(delay);
182 }
183 
184 void
186 {
187  NS_LOG_FUNCTION(this);
188  NS_ASSERT(IsRunning());
191  {
192  m_event.Cancel();
193  }
194  else if (m_flags & REMOVE_ON_DESTROY)
195  {
196  m_event.Remove();
197  }
199 }
200 
201 void
203 {
204  NS_LOG_FUNCTION(this);
208 }
209 
210 } // 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:260
bool IsExpired() const
Definition: timer.cc:126
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:255
Time GetDelayLeft() const
Definition: timer.cc:90
@ TIMER_SUSPENDED
Definition: timer.h:239
int m_flags
Bitfield for Timer State, DestroyPolicy and InternalSuspended.
Definition: timer.h:251
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
enum Timer::State GetState() const
Definition: timer.cc:147
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
void Cancel()
Cancel the currently-running event if there is one.
Definition: timer.cc:112
Time GetDelay() const
Definition: timer.cc:83
bool IsSuspended() const
Definition: timer.cc:140
Time m_delay
The delay configured for this Timer.
Definition: timer.h:253
void Remove()
Remove from the simulation event-list the currently-running event if there is one.
Definition: timer.cc:119
void Schedule()
Schedule a new event using the currently-configured delay, function, and arguments.
Definition: timer.cc:166
Time m_delayLeft
The amount of time left on the Timer while it is suspended.
Definition: timer.h:262
void Resume()
Restart the timer to expire within the amount of time left saved during Suspend.
Definition: timer.cc:202
bool IsRunning() const
Definition: timer.cc:133
void Suspend()
Pause the timer and save the amount of time left until it was set to expire.
Definition: timer.cc:185
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.