Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package org.apache.commons.net.ntp;
  /*
   * Licensed to the Apache Software Foundation (ASF) under one or more
   * contributor license agreements.  See the NOTICE file distributed with
   * this work for additional information regarding copyright ownership.
   * The ASF licenses this file to You under the Apache License, Version 2.0
   * (the "License"); you may not use this file except in compliance with
   * the License.  You may obtain a copy of the License at
   *
  *      http://www.apache.org/licenses/LICENSE-2.0
  *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 
 
 import java.util.List;

Wrapper class to network time packet messages (NTP, etc) that computes related timing info and stats.

Author(s):
Jason Mathews, MITRE Corp
Version:
$Revision: 1299238 $
 
 public class TimeInfo {
 
     private final NtpV3Packet _message;
     private List<String_comments;
     private Long _delay;
     private Long _offset;

    
time at which time message packet was received by local machine
 
     private final long _returnTime;

    
flag indicating that the TimeInfo details was processed and delay/offset were computed
 
     private boolean _detailsComputed;

    
Create TimeInfo object with raw packet message and destination time received.

Parameters:
message NTP message packet
returnTime destination receive time
Throws:
java.lang.IllegalArgumentException if message is null
 
     public TimeInfo(NtpV3Packet messagelong returnTime) {
         this(messagereturnTimenulltrue);
     }

    
Create TimeInfo object with raw packet message and destination time received.

Parameters:
message NTP message packet
returnTime destination receive time
comments List of errors/warnings identified during processing
Throws:
java.lang.IllegalArgumentException if message is null
 
     public TimeInfo(NtpV3Packet messagelong returnTimeList<Stringcomments)
     {
             this(messagereturnTimecommentstrue);
     }

    
Create TimeInfo object with raw packet message and destination time received. Auto-computes details if computeDetails flag set otherwise this is delayed until computeDetails() is called. Delayed computation is for fast intialization when sub-millisecond timing is needed.

Parameters:
msgPacket NTP message packet
returnTime destination receive time
doComputeDetails flag to pre-compute delay/offset values
Throws:
java.lang.IllegalArgumentException if message is null
 
     public TimeInfo(NtpV3Packet msgPacketlong returnTimeboolean doComputeDetails)
     {
             this(msgPacketreturnTimenulldoComputeDetails);
     }

    
Create TimeInfo object with raw packet message and destination time received. Auto-computes details if computeDetails flag set otherwise this is delayed until computeDetails() is called. Delayed computation is for fast intialization when sub-millisecond timing is needed.

Parameters:
message NTP message packet
returnTime destination receive time
comments list of comments used to store errors/warnings with message
doComputeDetails flag to pre-compute delay/offset values
Throws:
java.lang.IllegalArgumentException if message is null
 
    public TimeInfo(NtpV3Packet messagelong returnTimeList<Stringcomments,
                   boolean doComputeDetails)
    {
        if (message == null) {
            throw new IllegalArgumentException("message cannot be null");
        }
        this. = returnTime;
        this. = message;
        this. = comments;
        if (doComputeDetails) {
            computeDetails();
        }
    }

    
Add comment (error/warning) to list of comments associated with processing of NTP parameters. If comment list not create then one will be created.

Parameters:
comment
    public void addComment(String comment)
    {
        if ( == null) {
             = new ArrayList<String>();
        }
        .add(comment);
    }

    
Compute and validate details of the NTP message packet. Computed fields include the offset and delay.
    public void computeDetails()
    {
        if () {
            return// details already computed - do nothing
        }
         = true;
        if ( == null) {
             = new ArrayList<String>();
        }
        TimeStamp origNtpTime = .getOriginateTimeStamp();
        long origTime = origNtpTime.getTime();
        // Receive Time is time request received by server (t2)
        TimeStamp rcvNtpTime = .getReceiveTimeStamp();
        long rcvTime = rcvNtpTime.getTime();
        // Transmit time is time reply sent by server (t3)
        TimeStamp xmitNtpTime = .getTransmitTimeStamp();
        long xmitTime = xmitNtpTime.getTime();
        /*
         * Round-trip network delay and local clock offset (or time drift) is calculated
         * according to this standard NTP equation:
         *
         * LocalClockOffset = ((ReceiveTimestamp - OriginateTimestamp) +
         *                     (TransmitTimestamp - DestinationTimestamp)) / 2
         *
         * equations from RFC-1305 (NTPv3)
         *      roundtrip delay = (t4 - t1) - (t3 - t2)
         *      local clock offset = ((t2 - t1) + (t3 - t4)) / 2
         *
         * It takes into account network delays and assumes that they are symmetrical.
         *
         * Note the typo in SNTP RFCs 1769/2030 which state that the delay
         * is (T4 - T1) - (T2 - T3) with the "T2" and "T3" switched.
         */
        if (origNtpTime.ntpValue() == 0)
        {
            // without originate time cannot determine when packet went out
            // might be via a broadcast NTP packet...
            if (xmitNtpTime.ntpValue() != 0)
            {
                 = Long.valueOf(xmitTime - );
                .add("Error: zero orig time -- cannot compute delay");
            } else {
                .add("Error: zero orig time -- cannot compute delay/offset");
            }
        } else if (rcvNtpTime.ntpValue() == 0 || xmitNtpTime.ntpValue() == 0) {
            .add("Warning: zero rcvNtpTime or xmitNtpTime");
            // assert destTime >= origTime since network delay cannot be negative
            if (origTime > ) {
                .add("Error: OrigTime > DestRcvTime");
            } else {
                // without receive or xmit time cannot figure out processing time
                // so delay is simply the network travel time
                 = Long.valueOf( - origTime);
            }
            // TODO: is offset still valid if rcvNtpTime=0 || xmitNtpTime=0 ???
            // Could always hash origNtpTime (sendTime) but if host doesn't set it
            // then it's an malformed ntp host anyway and we don't care?
            // If server is in broadcast mode then we never send out a query in first place...
            if (rcvNtpTime.ntpValue() != 0)
            {
                // xmitTime is 0 just use rcv time
                 = Long.valueOf(rcvTime - origTime);
            } else if (xmitNtpTime.ntpValue() != 0)
            {
                // rcvTime is 0 just use xmitTime time
                 = Long.valueOf(xmitTime - );
            }
        } else
        {
             long delayValue =  - origTime;
             // assert xmitTime >= rcvTime: difference typically < 1ms
             if (xmitTime < rcvTime)
             {
                 // server cannot send out a packet before receiving it...
                 .add("Error: xmitTime < rcvTime"); // time-travel not allowed
             } else
             {
                 // subtract processing time from round-trip network delay
                 long delta = xmitTime - rcvTime;
                 // in normal cases the processing delta is less than
                 // the total roundtrip network travel time.
                 if (delta <= delayValue)
                 {
                     delayValue -= delta// delay = (t4 - t1) - (t3 - t2)
                 } else
                 {
                     // if delta - delayValue == 1 ms then it's a round-off error
                     // e.g. delay=3ms, processing=4ms
                     if (delta - delayValue == 1)
                     {
                         // delayValue == 0 -> local clock saw no tick change but destination clock did
                         if (delayValue != 0)
                         {
                             .add("Info: processing time > total network time by 1 ms -> assume zero delay");
                             delayValue = 0;
                         }
                     } else {
                        .add("Warning: processing time > total network time");
                    }
                 }
             }
              = Long.valueOf(delayValue);
            if (origTime > ) {
                .add("Error: OrigTime > DestRcvTime");
            }
             = Long.valueOf(((rcvTime - origTime) + (xmitTime - )) / 2);
        }
    }

    
Return list of comments (if any) during processing of NTP packet.

Returns:
List or null if not yet computed
    public List<StringgetComments()
    {
        return ;
    }

    
Get round-trip network delay. If null then could not compute the delay.

Returns:
Long or null if delay not available.
    public Long getDelay()
    {
        return ;
    }

    
Get clock offset needed to adjust local clock to match remote clock. If null then could not compute the offset.

Returns:
Long or null if offset not available.
    public Long getOffset()
    {
        return ;
    }

    
Returns NTP message packet.

Returns:
NTP message packet.
    public NtpV3Packet getMessage()
    {
        return ;
    }

    
Returns time at which time message packet was received by local machine.

Returns:
packet return time.
    public long getReturnTime()
    {
        return ;
    }
New to GrepCode? Check out our FAQ X