A Discrete-Event Network Simulator
Home
Tutorials ▼
English
Portuguese
Docs ▼
Wiki
Manual
Models
Develop ▼
API
Bugs
API
Main Page
Related Pages
Modules
Namespaces
Classes
Files
File List
File Members
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
29
Timer::Timer
()
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
47
Timer::~Timer
()
48
{
49
NS_LOG_FUNCTION
(
this
);
50
if
(
m_flags
&
CHECK_ON_DESTROY
)
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
{
63
Simulator::Remove
(
m_event
);
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
:
87
return
Simulator::GetDelayLeft
(
m_event
);
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
103
Timer::Cancel
(
void
)
104
{
105
NS_LOG_FUNCTION
(
this
);
106
Simulator::Cancel
(
m_event
);
107
}
108
void
109
Timer::Remove
(
void
)
110
{
111
NS_LOG_FUNCTION
(
this
);
112
Simulator::Remove
(
m_event
);
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
);
130
return
(
m_flags
&
TIMER_SUSPENDED
) ==
TIMER_SUSPENDED
;
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
152
Timer::Schedule
(
void
)
153
{
154
NS_LOG_FUNCTION
(
this
);
155
Schedule
(
m_delay
);
156
}
157
158
void
159
Timer::Schedule
(
Time
delay)
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
171
Timer::Suspend
(
void
)
172
{
173
NS_LOG_FUNCTION
(
this
);
174
NS_ASSERT
(
IsRunning
());
175
m_delayLeft
=
Simulator::GetDelayLeft
(
m_event
);
176
Simulator::Remove
(
m_event
);
177
m_flags
|=
TIMER_SUSPENDED
;
178
}
179
180
void
181
Timer::Resume
(
void
)
182
{
183
NS_LOG_FUNCTION
(
this
);
184
NS_ASSERT
(
m_flags
&
TIMER_SUSPENDED
);
185
m_event
=
m_impl
->
Schedule
(
m_delayLeft
);
186
m_flags
&= ~TIMER_SUSPENDED;
187
}
188
189
190
}
// namespace ns3
191
src
core
model
timer.cc
Generated on Tue May 14 2013 11:08:18 for ns-3 by
1.8.1.2