00001 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ 00002 /* 00003 * Copyright (c) 2008 University of Washington 00004 * 00005 * This program is free software; you can redistribute it and/or modify 00006 * it under the terms of the GNU General Public License version 2 as 00007 * published by the Free Software Foundation; 00008 * 00009 * This program is distributed in the hope that it will be useful, 00010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 * GNU General Public License for more details. 00013 * 00014 * You should have received a copy of the GNU General Public License 00015 * along with this program; if not, write to the Free Software 00016 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00017 */ 00018 00019 #include "synchronizer.h" 00020 00021 namespace ns3 { 00022 00023 TypeId 00024 Synchronizer::GetTypeId (void) 00025 { 00026 static TypeId tid = TypeId ("Synchronizer") 00027 .SetParent<Object> () 00028 ; 00029 return tid; 00030 } 00031 00032 Synchronizer::Synchronizer () 00033 : m_realtimeOriginNano (0), m_simOriginNano (0) 00034 { 00035 } 00036 00037 Synchronizer::~Synchronizer () 00038 { 00039 } 00040 00041 bool 00042 Synchronizer::Realtime (void) 00043 { 00044 return DoRealtime (); 00045 } 00046 00047 uint64_t 00048 Synchronizer::GetCurrentRealtime (void) 00049 { 00050 return NanosecondToTimeStep (DoGetCurrentRealtime ()); 00051 } 00052 00053 void 00054 Synchronizer::SetOrigin (uint64_t ts) 00055 { 00056 m_simOriginNano = TimeStepToNanosecond (ts); 00057 DoSetOrigin (m_simOriginNano); 00058 } 00059 00060 uint64_t 00061 Synchronizer::GetOrigin (void) 00062 { 00063 return NanosecondToTimeStep (m_simOriginNano); 00064 } 00065 00066 int64_t 00067 Synchronizer::GetDrift (uint64_t ts) 00068 { 00069 int64_t tDrift = DoGetDrift (TimeStepToNanosecond (ts)); 00070 00071 if (tDrift < 0) 00072 { 00073 return -NanosecondToTimeStep (-tDrift); 00074 } else { 00075 return NanosecondToTimeStep (tDrift); 00076 } 00077 } 00078 00079 bool 00080 Synchronizer::Synchronize (uint64_t tsCurrent, uint64_t tsDelay) 00081 { 00082 return DoSynchronize (TimeStepToNanosecond (tsCurrent), 00083 TimeStepToNanosecond (tsDelay)); 00084 } 00085 00086 void 00087 Synchronizer::Signal (void) 00088 { 00089 DoSignal (); 00090 } 00091 00092 void 00093 Synchronizer::SetCondition (bool cond) 00094 { 00095 DoSetCondition (cond); 00096 } 00097 00098 void 00099 Synchronizer::EventStart (void) 00100 { 00101 DoEventStart (); 00102 } 00103 00104 uint64_t 00105 Synchronizer::EventEnd (void) 00106 { 00107 return NanosecondToTimeStep (DoEventEnd ()); 00108 } 00109 00110 uint64_t 00111 Synchronizer::TimeStepToNanosecond (uint64_t ts) 00112 { 00113 switch (TimeStepPrecision::Get ()) { 00114 case TimeStepPrecision::S: 00115 return ts * 1000000000; 00116 case TimeStepPrecision::MS: 00117 return ts * 1000000; 00118 case TimeStepPrecision::US: 00119 return ts * 1000; 00120 case TimeStepPrecision::NS: 00121 return ts; 00122 case TimeStepPrecision::PS: 00123 return ts / 1000; 00124 case TimeStepPrecision::FS: 00125 return ts / 1000000; 00126 default: 00127 NS_ASSERT_MSG (false, "Synchronizer::TimeStepToNanosecond: " 00128 "Unexpected precision not implemented"); 00129 return 0; 00130 } 00131 } 00132 00133 uint64_t 00134 Synchronizer::NanosecondToTimeStep (uint64_t ns) 00135 { 00136 switch (TimeStepPrecision::Get ()) { 00137 case TimeStepPrecision::S: 00138 return ns / 1000000000; 00139 case TimeStepPrecision::MS: 00140 return ns / 1000000; 00141 case TimeStepPrecision::US: 00142 return ns / 1000; 00143 case TimeStepPrecision::NS: 00144 return ns; 00145 case TimeStepPrecision::PS: 00146 return ns * 1000; 00147 case TimeStepPrecision::FS: 00148 return ns * 1000000; 00149 default: 00150 NS_ASSERT_MSG (false, "Synchronizer::NanosecondToTimeStep: " 00151 "Unexpected precision not implemented"); 00152 return 0; 00153 } 00154 } 00155 00156 }; // namespace ns3 00157 00158