A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 
25 NS_LOG_COMPONENT_DEFINE ("Timer");
26 
27 namespace ns3 {
28 
30  : m_flags (CHECK_ON_DESTROY),
31  m_delay (FemtoSeconds (0)),
32  m_event (),
33  m_impl (0)
34 {
35  NS_LOG_FUNCTION (this);
36 }
37 
38 Timer::Timer (enum DestroyPolicy destroyPolicy)
39  : m_flags (destroyPolicy),
40  m_delay (FemtoSeconds (0)),
41  m_event (),
42  m_impl (0)
43 {
44  NS_LOG_FUNCTION (this << destroyPolicy);
45 }
46 
48 {
49  NS_LOG_FUNCTION (this);
51  {
52  if (m_event.IsRunning ())
53  {
54  NS_FATAL_ERROR ("Event is still running while destroying.");
55  }
56  }
57  else if (m_flags & CANCEL_ON_DESTROY)
58  {
59  m_event.Cancel ();
60  }
61  else if (m_flags & REMOVE_ON_DESTROY)
62  {
64  }
65  delete m_impl;
66 }
67 
68 void
69 Timer::SetDelay (const Time &time)
70 {
71  NS_LOG_FUNCTION (this << time);
72  m_delay = time;
73 }
74 Time
75 Timer::GetDelay (void) const
76 {
77  NS_LOG_FUNCTION (this);
78  return m_delay;
79 }
80 Time
81 Timer::GetDelayLeft (void) const
82 {
83  NS_LOG_FUNCTION (this);
84  switch (GetState ())
85  {
86  case Timer::RUNNING:
88  break;
89  case Timer::EXPIRED:
90  return TimeStep (0);
91  break;
92  case Timer::SUSPENDED:
93  return m_delayLeft;
94  break;
95  default:
96  NS_ASSERT (false);
97  return TimeStep (0);
98  break;
99  }
100 }
101 
102 void
104 {
105  NS_LOG_FUNCTION (this);
107 }
108 void
110 {
111  NS_LOG_FUNCTION (this);
113 }
114 bool
115 Timer::IsExpired (void) const
116 {
117  NS_LOG_FUNCTION (this);
118  return !IsSuspended () && m_event.IsExpired ();
119 }
120 bool
121 Timer::IsRunning (void) const
122 {
123  NS_LOG_FUNCTION (this);
124  return !IsSuspended () && m_event.IsRunning ();
125 }
126 bool
127 Timer::IsSuspended (void) const
128 {
129  NS_LOG_FUNCTION (this);
131 }
132 enum Timer::State
133 Timer::GetState (void) const
134 {
135  NS_LOG_FUNCTION (this);
136  if (IsRunning ())
137  {
138  return Timer::RUNNING;
139  }
140  else if (IsExpired ())
141  {
142  return Timer::EXPIRED;
143  }
144  else
145  {
146  NS_ASSERT (IsSuspended ());
147  return Timer::SUSPENDED;
148  }
149 }
150 
151 void
153 {
154  NS_LOG_FUNCTION (this);
155  Schedule (m_delay);
156 }
157 
158 void
160 {
161  NS_LOG_FUNCTION (this << delay);
162  NS_ASSERT (m_impl != 0);
163  if (m_event.IsRunning ())
164  {
165  NS_FATAL_ERROR ("Event is still running while re-scheduling.");
166  }
167  m_event = m_impl->Schedule (delay);
168 }
169 
170 void
172 {
173  NS_LOG_FUNCTION (this);
174  NS_ASSERT (IsRunning ());
178 }
179 
180 void
182 {
183  NS_LOG_FUNCTION (this);
186  m_flags &= ~TIMER_SUSPENDED;
187 }
188 
189 
190 } // namespace ns3
191 
static Time GetDelayLeft(const EventId &id)
Definition: simulator.cc:189
keep track of time values and allow control of global simulation resolution
Definition: nstime.h:81
#define NS_LOG_FUNCTION(parameters)
Definition: log.h:345
Time TimeStep(uint64_t ts)
Definition: nstime.h:950
Timer()
create a timer with a default event lifetime management policy:
Definition: timer.cc:29
#define NS_ASSERT(condition)
Definition: assert.h:64
static void Cancel(const EventId &id)
Set the cancel bit on this event: the event's associated function will not be invoked when it expires...
Definition: simulator.cc:268
bool IsRunning(void) const
This method is syntactic sugar for the ns3::Simulator::isExpired method.
Definition: event-id.cc:59
#define NS_FATAL_ERROR(msg)
fatal error handling
Definition: fatal-error.h:72
bool IsRunning(void) const
Definition: timer.cc:121
Time m_delay
Definition: timer.h:239
This policy enforces a check from the destructor of the Timer to verify that the timer has already ex...
Definition: timer.h:68
void Schedule(void)
Schedule a new event using the currently-configured delay, function, and arguments.
Definition: timer.cc:152
void Remove(void)
Remove from the simulation event-list the currently-running event if there is one.
Definition: timer.cc:109
virtual EventId Schedule(const Time &delay)=0
Time m_delayLeft
Definition: timer.h:242
void SetDelay(const Time &delay)
Definition: timer.cc:69
int m_flags
Definition: timer.h:238
enum Timer::State GetState(void) const
Definition: timer.cc:133
This policy cancels the event from the destructor of the Timer to verify that the event has already e...
Definition: timer.h:58
This policy removes the event from the simulation event list when the destructor of the Timer is invo...
Definition: timer.h:63
static void Remove(const EventId &id)
Remove an event from the event list.
Definition: simulator.cc:258
void Resume(void)
Restart the timer to expire within the amount of time left saved during Suspend.
Definition: timer.cc:181
NS_LOG_COMPONENT_DEFINE("Timer")
Time GetDelay(void) const
Definition: timer.cc:75
~Timer()
Definition: timer.cc:47
TimerImpl * m_impl
Definition: timer.h:241
EventId m_event
Definition: timer.h:240
void Cancel(void)
Cancel the currently-running event if there is one.
Definition: timer.cc:103
void Cancel(void)
This method is syntactic sugar for the ns3::Simulator::cancel method.
Definition: event-id.cc:47
DestroyPolicy
The policy to use to manager the internal timer when and instance of the Timer class is destroyed...
Definition: timer.h:52
bool IsExpired(void) const
Definition: timer.cc:115
void Suspend(void)
Cancel the timer and save the amount of time left until it was set to expire.
Definition: timer.cc:171
bool IsSuspended(void) const
Definition: timer.cc:127
Time GetDelayLeft(void) const
Definition: timer.cc:81
bool IsExpired(void) const
This method is syntactic sugar for the ns3::Simulator::isExpired method.
Definition: event-id.cc:53