A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
synchronizer.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2008 University of Washington
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 
19 #include "synchronizer.h"
20 #include "log.h"
21 
22 NS_LOG_COMPONENT_DEFINE ("Synchronizer");
23 
24 namespace ns3 {
25 
26 NS_OBJECT_ENSURE_REGISTERED (Synchronizer);
27 
28 TypeId
30 {
31  static TypeId tid = TypeId ("ns3::Synchronizer")
32  .SetParent<Object> ()
33  ;
34  return tid;
35 }
36 
38  : m_realtimeOriginNano (0), m_simOriginNano (0)
39 {
40  NS_LOG_FUNCTION (this);
41 }
42 
44 {
45  NS_LOG_FUNCTION (this);
46 }
47 
48 bool
50 {
51  NS_LOG_FUNCTION (this);
52  return DoRealtime ();
53 }
54 
55 uint64_t
57 {
58  NS_LOG_FUNCTION (this);
60 }
61 
62 void
64 {
65  NS_LOG_FUNCTION (this << ts);
68 }
69 
70 uint64_t
72 {
73  NS_LOG_FUNCTION (this);
75 }
76 
77 int64_t
79 {
80  NS_LOG_FUNCTION (this << ts);
81  int64_t tDrift = DoGetDrift (TimeStepToNanosecond (ts));
82 
83  if (tDrift < 0)
84  {
85  return -NanosecondToTimeStep (-tDrift);
86  } else {
87  return NanosecondToTimeStep (tDrift);
88  }
89 }
90 
91 bool
92 Synchronizer::Synchronize (uint64_t tsCurrent, uint64_t tsDelay)
93 {
94  NS_LOG_FUNCTION (this << tsCurrent << tsDelay);
95  return DoSynchronize (TimeStepToNanosecond (tsCurrent),
96  TimeStepToNanosecond (tsDelay));
97 }
98 
99 void
101 {
102  NS_LOG_FUNCTION (this);
103  DoSignal ();
104 }
105 
106 void
108 {
109  NS_LOG_FUNCTION (this << cond);
110  DoSetCondition (cond);
111 }
112 
113 void
115 {
116  NS_LOG_FUNCTION (this);
117  DoEventStart ();
118 }
119 
120 uint64_t
122 {
123  NS_LOG_FUNCTION (this);
124  return NanosecondToTimeStep (DoEventEnd ());
125 }
126 
127 uint64_t
129 {
130  NS_LOG_FUNCTION (this << ts);
131  return TimeStep (ts).GetNanoSeconds ();
132 }
133 
134 uint64_t
136 {
137  NS_LOG_FUNCTION (this << ns);
138  return NanoSeconds (ns).GetTimeStep ();
139 }
140 
141 } // namespace ns3
142 
143 
virtual int64_t DoGetDrift(uint64_t ns)=0
Declaration of method used to retrieve drift between the real time clock used to synchronize the simu...
virtual void DoSetCondition(bool)=0
Declaration of the method used to set the condition variable that tells a possible simulator thread w...
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by "...
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register the class in the ns-3 factory.
Definition: object-base.h:38
Time TimeStep(uint64_t ts)
Definition: nstime.h:997
uint64_t NanosecondToTimeStep(uint64_t ns)
Convert a normalized nanosecond count into a simulator time step (which can be steps of time in a use...
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:170
uint64_t GetCurrentRealtime(void)
Retrieve the value of the origin of the underlying normalized wall clock time in simulator timestep u...
Definition: synchronizer.cc:56
void SetCondition(bool)
Set the condition variable that tells a possible simulator thread waiting in the Synchronize method t...
void EventStart(void)
Ask the synchronizer to remember what time it is.
uint64_t EventEnd(void)
Ask the synchronizer to return the time step between the instant remembered during EventStart and now...
virtual void DoSetOrigin(uint64_t ns)=0
Establish a correspondence between a simulation time and a wall-clock (real) time.
bool Synchronize(uint64_t tsCurrent, uint64_t tsDelay)
Wait until the real time is in sync with the specified simulation time or until the synchronizer is S...
Definition: synchronizer.cc:92
bool Realtime(void)
Return true if this synchronizer is actually synchronizing to a realtime clock.
Definition: synchronizer.cc:49
uint64_t GetOrigin(void)
Retrieve the value of the origin of the simulation time in simulator timestep units.
Definition: synchronizer.cc:71
virtual uint64_t DoGetCurrentRealtime(void)=0
Retrieve the value of the origin of the underlying normalized wall clock time in simulator timestep u...
virtual void DoEventStart(void)=0
int64_t GetDrift(uint64_t ts)
Retrieve the difference between the real time clock used to synchronize the simulation and the simula...
Definition: synchronizer.cc:78
int64_t GetNanoSeconds(void) const
Definition: nstime.h:297
virtual void DoSignal(void)=0
Declaration of the method used to tell a possible simulator thread waiting in the DoSynchronize metho...
uint64_t m_simOriginNano
Definition: synchronizer.h:304
static TypeId GetTypeId(void)
Definition: synchronizer.cc:29
void SetOrigin(uint64_t ts)
Establish a correspondence between a simulation time and the synchronizer real time.
Definition: synchronizer.cc:63
void Signal(void)
Tell a possible simulator thread waiting in the Synchronize method that an event has happened which d...
uint64_t TimeStepToNanosecond(uint64_t ts)
Convert a simulator time step (which can be steps of time in a user-specified unit) to a normalized t...
a base class which provides memory management and object aggregation
Definition: object.h:64
virtual uint64_t DoEventEnd(void)=0
a unique identifier for an interface.
Definition: type-id.h:49
TypeId SetParent(TypeId tid)
Definition: type-id.cc:610
virtual ~Synchronizer()
Definition: synchronizer.cc:43
virtual bool DoRealtime(void)=0
Return true if this synchronizer is actually synchronizing to a realtime clock.
virtual bool DoSynchronize(uint64_t nsCurrent, uint64_t nsDelay)=0
Wait until the real time is in sync with the specified simulation time.