src/tools/model/delay-jitter-estimation.cc
changeset 10099 dbc507428c0f
parent 10098 6eae0980db76
parent 10097 b7d9a6de6e94
child 10100 169a01a864de
equal deleted inserted replaced
10098:6eae0980db76 10099:dbc507428c0f
     1 
       
     2 #include "delay-jitter-estimation.h"
       
     3 #include "ns3/tag.h"
       
     4 #include "ns3/simulator.h"
       
     5 #include "ns3/string.h"
       
     6 
       
     7 namespace ns3 {
       
     8 
       
     9 class DelayJitterEstimationTimestampTag : public Tag
       
    10 {
       
    11 public:
       
    12   DelayJitterEstimationTimestampTag ();
       
    13   static TypeId GetTypeId (void);
       
    14   virtual TypeId GetInstanceTypeId (void) const;
       
    15 
       
    16   virtual uint32_t GetSerializedSize (void) const;
       
    17   virtual void Serialize (TagBuffer i) const;
       
    18   virtual void Deserialize (TagBuffer i);
       
    19   virtual void Print (std::ostream &os) const;
       
    20 
       
    21   Time GetTxTime (void) const;
       
    22 private:
       
    23   uint64_t m_creationTime;
       
    24 };
       
    25 
       
    26 DelayJitterEstimationTimestampTag::DelayJitterEstimationTimestampTag ()
       
    27   : m_creationTime (Simulator::Now ().GetTimeStep ())
       
    28 {
       
    29 }
       
    30 
       
    31 TypeId
       
    32 DelayJitterEstimationTimestampTag::GetTypeId (void)
       
    33 {
       
    34   static TypeId tid = TypeId ("anon::DelayJitterEstimationTimestampTag")
       
    35     .SetParent<Tag> ()
       
    36     .AddConstructor<DelayJitterEstimationTimestampTag> ()
       
    37     .AddAttribute ("CreationTime",
       
    38                    "The time at which the timestamp was created",
       
    39                    StringValue ("0.0s"),
       
    40                    MakeTimeAccessor (&DelayJitterEstimationTimestampTag::GetTxTime),
       
    41                    MakeTimeChecker ())
       
    42   ;
       
    43   return tid;
       
    44 }
       
    45 TypeId
       
    46 DelayJitterEstimationTimestampTag::GetInstanceTypeId (void) const
       
    47 {
       
    48   return GetTypeId ();
       
    49 }
       
    50 
       
    51 uint32_t
       
    52 DelayJitterEstimationTimestampTag::GetSerializedSize (void) const
       
    53 {
       
    54   return 8;
       
    55 }
       
    56 void
       
    57 DelayJitterEstimationTimestampTag::Serialize (TagBuffer i) const
       
    58 {
       
    59   i.WriteU64 (m_creationTime);
       
    60 }
       
    61 void
       
    62 DelayJitterEstimationTimestampTag::Deserialize (TagBuffer i)
       
    63 {
       
    64   m_creationTime = i.ReadU64 ();
       
    65 }
       
    66 void
       
    67 DelayJitterEstimationTimestampTag::Print (std::ostream &os) const
       
    68 {
       
    69   os << "CreationTime=" << m_creationTime;
       
    70 }
       
    71 Time
       
    72 DelayJitterEstimationTimestampTag::GetTxTime (void) const
       
    73 {
       
    74   return TimeStep (m_creationTime);
       
    75 }
       
    76 
       
    77 DelayJitterEstimation::DelayJitterEstimation ()
       
    78   : m_previousRx (Simulator::Now ()),
       
    79     m_previousRxTx (Simulator::Now ()),
       
    80     m_jitter (0),
       
    81     m_delay (Seconds (0.0))
       
    82 {
       
    83 }
       
    84 void
       
    85 DelayJitterEstimation::PrepareTx (Ptr<const Packet> packet)
       
    86 {
       
    87   DelayJitterEstimationTimestampTag tag;
       
    88   packet->AddByteTag (tag);
       
    89 }
       
    90 void
       
    91 DelayJitterEstimation::RecordRx (Ptr<const Packet> packet)
       
    92 {
       
    93   DelayJitterEstimationTimestampTag tag;
       
    94   bool found;
       
    95   found = packet->FindFirstMatchingByteTag (tag);
       
    96   if (!found)
       
    97     {
       
    98       return;
       
    99     }
       
   100   tag.GetTxTime ();
       
   101 
       
   102   Time delta = (Simulator::Now () - m_previousRx) - (tag.GetTxTime () - m_previousRxTx);
       
   103   m_jitter += (Abs (delta) - m_jitter) / 16;
       
   104   m_previousRx = Simulator::Now ();
       
   105   m_previousRxTx = tag.GetTxTime ();
       
   106   m_delay = Simulator::Now () - tag.GetTxTime ();
       
   107 }
       
   108 
       
   109 Time 
       
   110 DelayJitterEstimation::GetLastDelay (void) const
       
   111 {
       
   112   return m_delay;
       
   113 }
       
   114 uint64_t
       
   115 DelayJitterEstimation::GetLastJitter (void) const
       
   116 {
       
   117   return m_jitter.GetHigh ();
       
   118 }
       
   119 
       
   120 } // namespace ns3