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 
24 namespace ns3 {
25 
27  : m_flags (CHECK_ON_DESTROY),
28  m_delay (FemtoSeconds (0)),
29  m_event (),
30  m_impl (0)
31 {
32 }
33 
34 Timer::Timer (enum DestroyPolicy destroyPolicy)
35  : m_flags (destroyPolicy),
36  m_delay (FemtoSeconds (0)),
37  m_event (),
38  m_impl (0)
39 {
40 }
41 
43 {
45  {
46  if (m_event.IsRunning ())
47  {
48  NS_FATAL_ERROR ("Event is still running while destroying.");
49  }
50  }
51  else if (m_flags & CANCEL_ON_DESTROY)
52  {
53  m_event.Cancel ();
54  }
55  else if (m_flags & REMOVE_ON_DESTROY)
56  {
58  }
59  delete m_impl;
60 }
61 
62 void
63 Timer::SetDelay (const Time &time)
64 {
65  m_delay = time;
66 }
67 Time
68 Timer::GetDelay (void) const
69 {
70  return m_delay;
71 }
72 Time
73 Timer::GetDelayLeft (void) const
74 {
75  switch (GetState ())
76  {
77  case Timer::RUNNING:
79  break;
80  case Timer::EXPIRED:
81  return TimeStep (0);
82  break;
83  case Timer::SUSPENDED:
84  return m_delayLeft;
85  break;
86  default:
87  NS_ASSERT (false);
88  return TimeStep (0);
89  break;
90  }
91 }
92 
93 void
95 {
97 }
98 void
100 {
102 }
103 bool
104 Timer::IsExpired (void) const
105 {
106  return !IsSuspended () && m_event.IsExpired ();
107 }
108 bool
109 Timer::IsRunning (void) const
110 {
111  return !IsSuspended () && m_event.IsRunning ();
112 }
113 bool
114 Timer::IsSuspended (void) const
115 {
117 }
118 enum Timer::State
119 Timer::GetState (void) const
120 {
121  if (IsRunning ())
122  {
123  return Timer::RUNNING;
124  }
125  else if (IsExpired ())
126  {
127  return Timer::EXPIRED;
128  }
129  else
130  {
131  NS_ASSERT (IsSuspended ());
132  return Timer::SUSPENDED;
133  }
134 }
135 
136 void
138 {
139  Schedule (m_delay);
140 }
141 
142 void
144 {
145  NS_ASSERT (m_impl != 0);
146  if (m_event.IsRunning ())
147  {
148  NS_FATAL_ERROR ("Event is still running while re-scheduling.");
149  }
150  m_event = m_impl->Schedule (delay);
151 }
152 
153 void
155 {
156  NS_ASSERT (IsRunning ());
160 }
161 
162 void
164 {
167  m_flags &= ~TIMER_SUSPENDED;
168 }
169 
170 
171 } // namespace ns3
172