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
All
Classes
Namespaces
Files
Functions
Variables
Typedefs
Enumerations
Enumerator
Friends
Groups
Pages
timer.cc
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
ns3::Simulator::GetDelayLeft
static Time GetDelayLeft(const EventId &id)
Definition:
simulator.cc:188
ns3::Time
keep track of time unit.
Definition:
nstime.h:149
NS_LOG_FUNCTION
#define NS_LOG_FUNCTION(parameters)
Definition:
log.h:311
ns3::Timer::Timer
Timer()
Definition:
timer.cc:29
NS_ASSERT
#define NS_ASSERT(condition)
Definition:
assert.h:64
NS_LOG_COMPONENT_DEFINE
#define NS_LOG_COMPONENT_DEFINE(name)
Definition:
log.h:122
ns3::Simulator::Cancel
static void Cancel(const EventId &id)
Definition:
simulator.cc:267
ns3::EventId::IsRunning
bool IsRunning(void) const
Definition:
event-id.cc:59
NS_FATAL_ERROR
#define NS_FATAL_ERROR(msg)
fatal error handling
Definition:
fatal-error.h:72
ns3::Timer::IsRunning
bool IsRunning(void) const
Definition:
timer.cc:121
ns3::Timer::CHECK_ON_DESTROY
Definition:
timer.h:68
ns3::Timer::Schedule
void Schedule(void)
Definition:
timer.cc:152
ns3::Timer::Remove
void Remove(void)
Definition:
timer.cc:109
ns3::Timer::SetDelay
void SetDelay(const Time &delay)
Definition:
timer.cc:69
ns3::Timer::GetState
enum Timer::State GetState(void) const
Definition:
timer.cc:133
ns3::Timer::CANCEL_ON_DESTROY
Definition:
timer.h:58
ns3::Timer::REMOVE_ON_DESTROY
Definition:
timer.h:63
ns3::Simulator::Remove
static void Remove(const EventId &id)
Definition:
simulator.cc:257
ns3::Timer::Resume
void Resume(void)
Definition:
timer.cc:181
ns3::FemtoSeconds
Time FemtoSeconds(uint64_t fs)
create ns3::Time instances in units of femtoseconds.
Definition:
nstime.h:657
ns3::Timer::GetDelay
Time GetDelay(void) const
Definition:
timer.cc:75
ns3::Timer::Cancel
void Cancel(void)
Definition:
timer.cc:103
ns3::EventId::Cancel
void Cancel(void)
Definition:
event-id.cc:47
ns3::Timer::DestroyPolicy
DestroyPolicy
Definition:
timer.h:52
ns3::Timer::IsExpired
bool IsExpired(void) const
Definition:
timer.cc:115
ns3::Timer::Suspend
void Suspend(void)
Definition:
timer.cc:171
ns3::Timer::IsSuspended
bool IsSuspended(void) const
Definition:
timer.cc:127
ns3::Timer::GetDelayLeft
Time GetDelayLeft(void) const
Definition:
timer.cc:81
ns3::EventId::IsExpired
bool IsExpired(void) const
Definition:
event-id.cc:53
src
core
model
timer.cc
Generated on Sun Mar 16 2014 11:25:02 for ns-3 by
1.8.6