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"
22 #include "simulation-singleton.h"
23 #include "log.h"
24 
31 namespace ns3 {
32 
33 NS_LOG_COMPONENT_DEFINE ("Timer");
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 
44 Timer::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  {
70  }
71  delete m_impl;
72 }
73 
74 void
75 Timer::SetDelay (const Time &time)
76 {
77  NS_LOG_FUNCTION (this << time);
78  m_delay = time;
79 }
80 Time
81 Timer::GetDelay (void) const
82 {
83  NS_LOG_FUNCTION (this);
84  return m_delay;
85 }
86 Time
87 Timer::GetDelayLeft (void) const
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;
98  case Timer::SUSPENDED:
99  return m_delayLeft;
100  break;
101  default:
102  NS_ASSERT (false);
103  return TimeStep (0);
104  break;
105  }
106 }
107 
108 void
110 {
111  NS_LOG_FUNCTION (this);
113 }
114 void
116 {
117  NS_LOG_FUNCTION (this);
119 }
120 bool
121 Timer::IsExpired (void) const
122 {
123  NS_LOG_FUNCTION (this);
124  return !IsSuspended () && m_event.IsExpired ();
125 }
126 bool
127 Timer::IsRunning (void) const
128 {
129  NS_LOG_FUNCTION (this);
130  return !IsSuspended () && m_event.IsRunning ();
131 }
132 bool
133 Timer::IsSuspended (void) const
134 {
135  NS_LOG_FUNCTION (this);
137 }
138 enum Timer::State
139 Timer::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  {
152  NS_ASSERT (IsSuspended ());
153  return Timer::SUSPENDED;
154  }
155 }
156 
157 void
159 {
160  NS_LOG_FUNCTION (this);
161  Schedule (m_delay);
162 }
163 
164 void
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 
176 void
178 {
179  NS_LOG_FUNCTION (this);
180  NS_ASSERT (IsRunning ());
184 }
185 
186 void
188 {
189  NS_LOG_FUNCTION (this);
193 }
194 
195 
196 } // namespace ns3
197 
static Time GetDelayLeft(const EventId &id)
Get the remaining time until this event will execute.
Definition: simulator.cc:258
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:102
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by "...
Timer()
Create a timer with a default event lifetime management policy:
Definition: timer.cc:35
Timer is currently running.
Definition: timer.h:102
#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
Time GetDelayLeft(void) const
Definition: timer.cc:87
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
bool IsSuspended(void) const
Definition: timer.cc:133
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
Definition: fatal-error.h:162
static void Cancel(const EventId &id)
Set the cancel bit on this event: the event&#39;s associated function will not be invoked when it expires...
Definition: simulator.cc:346
ns3::SimulationSingleton declaration and template implementation.
ns3::Simulator declaration.
bool IsExpired(void) const
This method is syntactic sugar for the ns3::Simulator::IsExpired method.
Definition: event-id.cc:59
Time m_delay
The delay configured for this Timer.
Definition: timer.h:283
ns3::Timer class declaration.
This policy enforces a check from the destructor of the Timer to verify that the timer has already ex...
Definition: timer.h:96
void Schedule(void)
Schedule a new event using the currently-configured delay, function, and arguments.
Definition: timer.cc:158
void Remove(void)
Remove from the simulation event-list the currently-running event if there is one.
Definition: timer.cc:115
virtual EventId Schedule(const Time &delay)=0
Schedule the callback for a future time.
Time m_delayLeft
The amount of time left on the Timer while it is suspended.
Definition: timer.h:292
void SetDelay(const Time &delay)
Definition: timer.cc:75
int m_flags
Bitfield for Timer State, DestroyPolicy and InternalSuspended.
Definition: timer.h:281
This policy cancels the event from the destructor of the Timer to verify that the event has already e...
Definition: timer.h:86
This policy removes the event from the simulation event list when the destructor of the Timer is invo...
Definition: timer.h:91
static void Remove(const EventId &id)
Remove an event from the event list.
Definition: simulator.cc:336
Every class exported by the ns3 library is enclosed in the ns3 namespace.
void Resume(void)
Restart the timer to expire within the amount of time left saved during Suspend.
Definition: timer.cc:187
Time TimeStep(uint64_t ts)
Definition: nstime.h:1071
State
The possible states of the Timer.
Definition: timer.h:99
bool IsRunning(void) const
Definition: timer.cc:127
Timer has already expired.
Definition: timer.h:103
~Timer()
Definition: timer.cc:53
enum Timer::State GetState(void) const
Definition: timer.cc:139
TimerImpl * m_impl
The timer implementation, which contains the bound callback function and arguments.
Definition: timer.h:290
EventId m_event
The future event scheduled to expire the timer.
Definition: timer.h:285
void Cancel(void)
Cancel the currently-running event if there is one.
Definition: timer.cc:109
bool IsRunning(void) const
This method is syntactic sugar for !IsExpired().
Definition: event-id.cc:65
void Cancel(void)
This method is syntactic sugar for the ns3::Simulator::Cancel method.
Definition: event-id.cc:53
DestroyPolicy
The policy to use to manager the internal timer when an instance of the Timer class is destroyed...
Definition: timer.h:80
void Suspend(void)
Cancel the timer and save the amount of time left until it was set to expire.
Definition: timer.cc:177
Time FemtoSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:1054
Time GetDelay(void) const
Definition: timer.cc:81
Debug message logging.
bool IsExpired(void) const
Definition: timer.cc:121