directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From erodrig...@apache.org
Subject svn commit: r124622 - in incubator/directory/ntp/trunk/core/src: java/org/apache/ntp/io java/org/apache/ntp/message java/org/apache/ntp/service test/org/apache/ntp
Date Sat, 08 Jan 2005 04:48:35 GMT
Author: erodriguez
Date: Fri Jan  7 20:48:33 2005
New Revision: 124622

URL: http://svn.apache.org/viewcvs?view=rev&rev=124622
Log:
Working NTP PDU encoding and decoding.
Added:
   incubator/directory/ntp/trunk/core/src/java/org/apache/ntp/io/NtpMessageEncoder.java
Modified:
   incubator/directory/ntp/trunk/core/src/java/org/apache/ntp/io/NtpMessageDecoder.java
   incubator/directory/ntp/trunk/core/src/java/org/apache/ntp/message/NtpMessage.java
   incubator/directory/ntp/trunk/core/src/java/org/apache/ntp/message/NtpMessageModifier.java
   incubator/directory/ntp/trunk/core/src/java/org/apache/ntp/service/NtpServiceImpl.java
   incubator/directory/ntp/trunk/core/src/test/org/apache/ntp/NtpMessageEncoderTest.java

Modified: incubator/directory/ntp/trunk/core/src/java/org/apache/ntp/io/NtpMessageDecoder.java
Url: http://svn.apache.org/viewcvs/incubator/directory/ntp/trunk/core/src/java/org/apache/ntp/io/NtpMessageDecoder.java?view=diff&rev=124622&p1=incubator/directory/ntp/trunk/core/src/java/org/apache/ntp/io/NtpMessageDecoder.java&r1=124621&p2=incubator/directory/ntp/trunk/core/src/java/org/apache/ntp/io/NtpMessageDecoder.java&r2=124622
==============================================================================
--- incubator/directory/ntp/trunk/core/src/java/org/apache/ntp/io/NtpMessageDecoder.java	(original)
+++ incubator/directory/ntp/trunk/core/src/java/org/apache/ntp/io/NtpMessageDecoder.java	Fri
Jan  7 20:48:33 2005
@@ -39,34 +39,28 @@
 		modifier.setVersionNumber( parseVersionNumber( header ) );
 		modifier.setMode( parseMode( header ) );
 		
-		modifier.setStratum( StratumType.getTypeByOrdinal( request.get() ) );
+		modifier.setStratum( parseStratum( request ) );
 		
-		int pollInterval = (int)Math.round( Math.pow( 2, request.get() ) );
-		modifier.setPollInterval( pollInterval );
-		
-		double precision = 1000 * Math.pow( 2, request.get() );
-		modifier.setPrecision( precision );
+		modifier.setPollInterval( parsePollInterval( request ) );
 		
+		modifier.setPrecision( parsePrecision( request ) );
 		
 		modifier.setRootDelay( parseRootDelay( request ) );
 		
 		modifier.setRootDispersion( parseRootDispersion( request ) );
 		
-		byte[] nextFourBytes = new byte[ 4 ];
-		request.get( nextFourBytes );
-		modifier.setReferenceIdentifier( parseReferenceIdentifier( nextFourBytes ) );
-		
+		modifier.setReferenceIdentifier( parseReferenceIdentifier( request ) );
 		
 		modifier.setReferenceTimestamp( new NtpTimeStamp( request ) );
 		modifier.setOriginateTimestamp( new NtpTimeStamp( request ) );
 		
 		byte[] unneededBytes = new byte[ 8 ];
 		request.get( unneededBytes );
+		
 		modifier.setReceiveTimestamp( new NtpTimeStamp() );
 		
 		modifier.setTransmitTimestamp( new NtpTimeStamp( request ) );
 		
-		
 		return modifier.getNtpMessage();
 	}
 	
@@ -85,21 +79,38 @@
 	    return ModeType.getTypeByOrdinal( header & 0x07 );
 	}
 	
-	private ReferenceIdentifier parseReferenceIdentifier( byte[] bytes )
+	private StratumType parseStratum( ByteBuffer request )
+	{
+		return StratumType.getTypeByOrdinal( request.get() );
+	}
+	
+	private byte parsePollInterval( ByteBuffer bytes )
 	{
-		return ReferenceIdentifier.getTypeByName( new String( bytes ) );
+		return (byte)Math.round( Math.pow( 2, bytes.get() ) );
 	}
 	
-    private double parseRootDelay( ByteBuffer bytes )
+	private byte parsePrecision( ByteBuffer bytes )
+	{
+		return (byte)( 1000 * Math.pow( 2, bytes.get() ) );
+	}
+	
+	private ReferenceIdentifier parseReferenceIdentifier( ByteBuffer request )
+	{
+		byte[] nextFourBytes = new byte[ 4 ];
+		request.get( nextFourBytes );
+		return ReferenceIdentifier.getTypeByName( new String( nextFourBytes ) );
+	}
+	
+    private int parseRootDelay( ByteBuffer bytes )
     {
         int temp = 256 * ( 256 * ( 256 * bytes.get() + bytes.get() ) + bytes.get() ) + bytes.get();
-        return 1000 * ( ( (double)temp ) / 0x10000 );
+        return 1000 * ( temp / 0x10000 );
     }
 
-    private double parseRootDispersion( ByteBuffer bytes )
+    private int parseRootDispersion( ByteBuffer bytes )
     {
-        long temp = 256 * ( 256 * ( 256 * bytes.get() + bytes.get() ) + bytes.get() ) + bytes.get();
-        return 1000 * ( ( (double)temp ) / 0x10000 );
+        int temp = 256 * ( 256 * ( 256 * bytes.get() + bytes.get() ) + bytes.get() ) + bytes.get();
+        return 1000 * ( temp / 0x10000 );
     }
 }
 

Added: incubator/directory/ntp/trunk/core/src/java/org/apache/ntp/io/NtpMessageEncoder.java
Url: http://svn.apache.org/viewcvs/incubator/directory/ntp/trunk/core/src/java/org/apache/ntp/io/NtpMessageEncoder.java?view=auto&rev=124622
==============================================================================
--- (empty file)
+++ incubator/directory/ntp/trunk/core/src/java/org/apache/ntp/io/NtpMessageEncoder.java	Fri
Jan  7 20:48:33 2005
@@ -0,0 +1,70 @@
+/*
+ *   Copyright 2005 The Apache Software Foundation
+ *
+ *   Licensed 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.
+ *
+ */
+
+package org.apache.ntp.io;
+
+import java.nio.ByteBuffer;
+
+import org.apache.ntp.message.LeapIndicatorType;
+import org.apache.ntp.message.ModeType;
+import org.apache.ntp.message.NtpMessage;
+
+
+public class NtpMessageEncoder
+{
+	public ByteBuffer encode( NtpMessage message )
+	{
+		ByteBuffer buffer = ByteBuffer.allocate( 576 );
+		
+		byte header = 0x00;
+		header = encodeLeapIndicator( message.getLeapIndicator(), header );
+		header = encodeVersionNumber( message.getVersionNumber(), header );
+		header = encodeMode( message.getMode(), header );
+		buffer.put( header );
+		
+		buffer.put( (byte)( message.getStratum().getOrdinal() & 0xFF ) );
+		buffer.put( (byte)( message.getPollInterval() & 0xFF ) );
+		buffer.put( (byte)( message.getPrecision() & 0xFF ) );
+		
+		buffer.putInt( message.getRootDelay() );
+		buffer.putInt( message.getRootDispersion() );
+		buffer.putInt( message.getReferenceIdentifier().getOrdinal() );
+		
+		buffer.rewind();
+		
+		return buffer;
+	}
+	
+	private byte encodeLeapIndicator( LeapIndicatorType leapIndicator, byte header )
+	{
+		byte twoBits = (byte)(leapIndicator.getOrdinal() & 0x03);
+		return (byte)( ( twoBits << 6 ) | header);
+	}
+	
+	private byte encodeVersionNumber( int versionNumber, byte header )
+	{
+		byte threeBits = (byte)( versionNumber & 0x07 );
+		return (byte)( ( threeBits << 3 ) | header );
+	}
+	
+	private byte encodeMode( ModeType mode, byte header )
+	{
+		byte threeBits = (byte)( mode.getOrdinal() & 0x07 );
+		return (byte)( threeBits | header );
+	}
+}
+

Modified: incubator/directory/ntp/trunk/core/src/java/org/apache/ntp/message/NtpMessage.java
Url: http://svn.apache.org/viewcvs/incubator/directory/ntp/trunk/core/src/java/org/apache/ntp/message/NtpMessage.java?view=diff&rev=124622&p1=incubator/directory/ntp/trunk/core/src/java/org/apache/ntp/message/NtpMessage.java&r1=124621&p2=incubator/directory/ntp/trunk/core/src/java/org/apache/ntp/message/NtpMessage.java&r2=124622
==============================================================================
--- incubator/directory/ntp/trunk/core/src/java/org/apache/ntp/message/NtpMessage.java	(original)
+++ incubator/directory/ntp/trunk/core/src/java/org/apache/ntp/message/NtpMessage.java	Fri
Jan  7 20:48:33 2005
@@ -24,11 +24,10 @@
 	private int               versionNumber;
 	private ModeType          mode;
 	private StratumType       stratumType;
-	private int               pollInterval;
-	private double            precision;
-	
-	private double rootDelay;
-	private double rootDispersion;
+	private byte              pollInterval;
+	private byte              precision;
+	private int               rootDelay;
+	private int               rootDispersion;
 	
 	private ReferenceIdentifier referenceIdentifier;
 	
@@ -39,8 +38,8 @@
 	
 	
 	public NtpMessage( LeapIndicatorType leapIndicator, int versionNumber,
-			ModeType mode, StratumType stratumType, int pollInterval,
-			double precision, double rootDelay, double rootDispersion,
+			ModeType mode, StratumType stratumType, byte pollInterval,
+			byte precision, int rootDelay, int rootDispersion,
 			ReferenceIdentifier referenceIdentifier, NtpTimeStamp referenceTimestamp,
 			NtpTimeStamp originateTimestamp, NtpTimeStamp receiveTimestamp,
 			NtpTimeStamp transmitTimestamp )
@@ -88,7 +87,7 @@
 	/**
 	 * @return Returns the Poll Interval.
 	 */
-	public int getPollInterval()
+	public byte getPollInterval()
 	{
 		return pollInterval;
 	}
@@ -96,7 +95,7 @@
 	/**
 	 * @return Returns the Precision.
 	 */
-	public double getPrecision()
+	public byte getPrecision()
 	{
 		return precision;
 	}
@@ -128,7 +127,7 @@
 	/**
 	 * @return Returns the Root Delay.
 	 */
-	public double getRootDelay()
+	public int getRootDelay()
 	{
 		return rootDelay;
 	}
@@ -136,7 +135,7 @@
 	/**
 	 * @return Returns the Root Dispersion.
 	 */
-	public double getRootDispersion()
+	public int getRootDispersion()
 	{
 		return rootDispersion;
 	}

Modified: incubator/directory/ntp/trunk/core/src/java/org/apache/ntp/message/NtpMessageModifier.java
Url: http://svn.apache.org/viewcvs/incubator/directory/ntp/trunk/core/src/java/org/apache/ntp/message/NtpMessageModifier.java?view=diff&rev=124622&p1=incubator/directory/ntp/trunk/core/src/java/org/apache/ntp/message/NtpMessageModifier.java&r1=124621&p2=incubator/directory/ntp/trunk/core/src/java/org/apache/ntp/message/NtpMessageModifier.java&r2=124622
==============================================================================
--- incubator/directory/ntp/trunk/core/src/java/org/apache/ntp/message/NtpMessageModifier.java
(original)
+++ incubator/directory/ntp/trunk/core/src/java/org/apache/ntp/message/NtpMessageModifier.java
Fri Jan  7 20:48:33 2005
@@ -24,11 +24,10 @@
 	private int               versionNumber;
 	private ModeType          mode;
 	private StratumType       stratumType;
-	private int               pollInterval;
-	private double            precision;
-	
-	private double rootDelay;
-	private double rootDispersion;
+	private byte              pollInterval;
+	private byte              precision;
+	private int               rootDelay;
+	private int               rootDispersion;
 	
 	private ReferenceIdentifier referenceIdentifier;
 	
@@ -74,7 +73,7 @@
 	/**
 	 * @param pollInterval The Poll Interval to set.
 	 */
-	public void setPollInterval(int pollInterval)
+	public void setPollInterval(byte pollInterval)
 	{
 		this.pollInterval = pollInterval;
 	}
@@ -82,7 +81,7 @@
 	/**
 	 * @param precision The Precision to set.
 	 */
-	public void setPrecision(double precision)
+	public void setPrecision(byte precision)
 	{
 		this.precision = precision;
 	}
@@ -114,7 +113,7 @@
 	/**
 	 * @param rootDelay The Root Delay to set.
 	 */
-	public void setRootDelay(double rootDelay)
+	public void setRootDelay(int rootDelay)
 	{
 		this.rootDelay = rootDelay;
 	}
@@ -122,7 +121,7 @@
 	/**
 	 * @param rootDispersion The Root Dispersion to set.
 	 */
-	public void setRootDispersion(double rootDispersion)
+	public void setRootDispersion(int rootDispersion)
 	{
 		this.rootDispersion = rootDispersion;
 	}

Modified: incubator/directory/ntp/trunk/core/src/java/org/apache/ntp/service/NtpServiceImpl.java
Url: http://svn.apache.org/viewcvs/incubator/directory/ntp/trunk/core/src/java/org/apache/ntp/service/NtpServiceImpl.java?view=diff&rev=124622&p1=incubator/directory/ntp/trunk/core/src/java/org/apache/ntp/service/NtpServiceImpl.java&r1=124621&p2=incubator/directory/ntp/trunk/core/src/java/org/apache/ntp/service/NtpServiceImpl.java&r2=124622
==============================================================================
--- incubator/directory/ntp/trunk/core/src/java/org/apache/ntp/service/NtpServiceImpl.java
(original)
+++ incubator/directory/ntp/trunk/core/src/java/org/apache/ntp/service/NtpServiceImpl.java
Fri Jan  7 20:48:33 2005
@@ -37,8 +37,8 @@
 		modifier.setVersionNumber( 4 );
 		modifier.setMode( ModeType.SERVER );
 		modifier.setStratum( StratumType.SECONDARY_REFERENCE );
-		modifier.setPollInterval( 64 );
-		modifier.setPrecision( -20 );
+		modifier.setPollInterval( (byte)0x40 );
+		modifier.setPrecision( (byte)0xEC );
 		modifier.setRootDelay( 0 );
 		modifier.setRootDispersion( 0 );
 		modifier.setReferenceIdentifier( ReferenceIdentifier.LOCL );

Modified: incubator/directory/ntp/trunk/core/src/test/org/apache/ntp/NtpMessageEncoderTest.java
Url: http://svn.apache.org/viewcvs/incubator/directory/ntp/trunk/core/src/test/org/apache/ntp/NtpMessageEncoderTest.java?view=diff&rev=124622&p1=incubator/directory/ntp/trunk/core/src/test/org/apache/ntp/NtpMessageEncoderTest.java&r1=124621&p2=incubator/directory/ntp/trunk/core/src/test/org/apache/ntp/NtpMessageEncoderTest.java&r2=124622
==============================================================================
--- incubator/directory/ntp/trunk/core/src/test/org/apache/ntp/NtpMessageEncoderTest.java
(original)
+++ incubator/directory/ntp/trunk/core/src/test/org/apache/ntp/NtpMessageEncoderTest.java
Fri Jan  7 20:48:33 2005
@@ -19,9 +19,11 @@
 
 import java.nio.ByteBuffer;
 
+import org.apache.ntp.io.NtpMessageDecoder;
 import org.apache.ntp.io.NtpMessageEncoder;
 import org.apache.ntp.message.LeapIndicatorType;
 import org.apache.ntp.message.ModeType;
+import org.apache.ntp.message.NtpMessage;
 import org.apache.ntp.message.NtpMessageModifier;
 import org.apache.ntp.message.NtpTimeStamp;
 import org.apache.ntp.message.ReferenceIdentifier;
@@ -39,10 +41,10 @@
 		modifier.setVersionNumber( 4 );
 		modifier.setMode( ModeType.SERVER );
 		modifier.setStratum( StratumType.PRIMARY_REFERENCE );
-		modifier.setPollInterval( 6 );
-		modifier.setPrecision( 0.000015 );
-		modifier.setRootDelay( 0.0 );
-		modifier.setRootDispersion( 0.0006 );
+		modifier.setPollInterval( (byte)0x06 ); // 6
+		modifier.setPrecision( (byte)0xFA );    // -6
+		modifier.setRootDelay( 0 );
+		modifier.setRootDispersion( 0 );
 		modifier.setReferenceIdentifier( ReferenceIdentifier.LOCL );
 		
 		NtpTimeStamp now = new NtpTimeStamp();
@@ -52,11 +54,17 @@
 		modifier.setReceiveTimestamp( now );
 		modifier.setTransmitTimestamp( now );
 		
+		NtpMessage message = modifier.getNtpMessage();
+		
 		NtpMessageEncoder encoder = new NtpMessageEncoder();
-		replyByteBuffer = encoder.encode( modifier.getNtpMessage() );
-		replyByteBuffer.flip();
+		replyByteBuffer = encoder.encode( message );
+		
+		print( message );
+		
+		NtpMessageDecoder decoder = new NtpMessageDecoder();
+		NtpMessage reply          = decoder.decode( replyByteBuffer );
 		
-		print( modifier.getNtpMessage() );
+		print( reply );
 	}
 }
 

Mime
View raw message