00001 00002 #include "delay-jitter-estimation.h" 00003 #include "ns3/tag.h" 00004 #include "ns3/simulator.h" 00005 #include "ns3/string.h" 00006 00007 namespace ns3 { 00008 00009 class DelayJitterEstimationTimestampTag : public Tag 00010 { 00011 public: 00012 DelayJitterEstimationTimestampTag (); 00013 static TypeId GetTypeId (void); 00014 virtual TypeId GetInstanceTypeId (void) const; 00015 00016 virtual uint32_t GetSerializedSize (void) const; 00017 virtual void Serialize (TagBuffer i) const; 00018 virtual void Deserialize (TagBuffer i); 00019 virtual void Print (std::ostream &os) const; 00020 00021 Time GetTxTime (void) const; 00022 private: 00023 uint64_t m_creationTime; 00024 }; 00025 00026 DelayJitterEstimationTimestampTag::DelayJitterEstimationTimestampTag () 00027 : m_creationTime (Simulator::Now ().GetTimeStep ()) 00028 {} 00029 00030 TypeId 00031 DelayJitterEstimationTimestampTag::GetTypeId (void) 00032 { 00033 static TypeId tid = TypeId ("anon::DelayJitterEstimationTimestampTag") 00034 .SetParent<Tag> () 00035 .AddConstructor<DelayJitterEstimationTimestampTag> () 00036 .AddAttribute ("CreationTime", 00037 "The time at which the timestamp was created", 00038 StringValue ("0.0s"), 00039 MakeTimeAccessor (&DelayJitterEstimationTimestampTag::GetTxTime), 00040 MakeTimeChecker ()) 00041 ; 00042 return tid; 00043 } 00044 TypeId 00045 DelayJitterEstimationTimestampTag::GetInstanceTypeId (void) const 00046 { 00047 return GetTypeId (); 00048 } 00049 00050 uint32_t 00051 DelayJitterEstimationTimestampTag::GetSerializedSize (void) const 00052 { 00053 return 8; 00054 } 00055 void 00056 DelayJitterEstimationTimestampTag::Serialize (TagBuffer i) const 00057 { 00058 i.WriteU64 (m_creationTime); 00059 } 00060 void 00061 DelayJitterEstimationTimestampTag::Deserialize (TagBuffer i) 00062 { 00063 m_creationTime = i.ReadU64 (); 00064 } 00065 void 00066 DelayJitterEstimationTimestampTag::Print (std::ostream &os) const 00067 { 00068 os << "CreationTime=" << m_creationTime; 00069 } 00070 Time 00071 DelayJitterEstimationTimestampTag::GetTxTime (void) const 00072 { 00073 return TimeStep (m_creationTime); 00074 } 00075 00076 DelayJitterEstimation::DelayJitterEstimation () 00077 : m_previousRx (Simulator::Now ()), 00078 m_previousRxTx (Simulator::Now ()), 00079 m_jitter (Seconds (0.0)), 00080 m_delay (Seconds (0.0)) 00081 {} 00082 void 00083 DelayJitterEstimation::PrepareTx (Ptr<const Packet> packet) 00084 { 00085 DelayJitterEstimationTimestampTag tag; 00086 packet->AddTag (tag); 00087 } 00088 void 00089 DelayJitterEstimation::RecordRx (Ptr<const Packet> packet) 00090 { 00091 DelayJitterEstimationTimestampTag tag; 00092 bool found; 00093 found = packet->FindFirstMatchingTag (tag); 00094 if (!found) 00095 { 00096 return; 00097 } 00098 tag.GetTxTime (); 00099 00100 Time delta = (Simulator::Now () - m_previousRx) - (tag.GetTxTime () - m_previousRxTx); 00101 m_jitter += (Abs (delta) - m_jitter ) / Scalar (16.0); 00102 m_previousRx = Simulator::Now (); 00103 m_previousRxTx = tag.GetTxTime (); 00104 m_delay = Simulator::Now () - tag.GetTxTime (); 00105 } 00106 00107 Time 00108 DelayJitterEstimation::GetLastDelay (void) const 00109 { 00110 return m_delay; 00111 } 00112 Time 00113 DelayJitterEstimation::GetLastJitter (void) const 00114 { 00115 return m_jitter; 00116 } 00117 00118 } // namespace ns3