001    /*
002     *  Licensed to the Apache Software Foundation (ASF) under one
003     *  or more contributor license agreements.  See the NOTICE file
004     *  distributed with this work for additional information
005     *  regarding copyright ownership.  The ASF licenses this file
006     *  to you under the Apache License, Version 2.0 (the
007     *  "License"); you may not use this file except in compliance
008     *  with the License.  You may obtain a copy of the License at
009     *  
010     *    http://www.apache.org/licenses/LICENSE-2.0
011     *  
012     *  Unless required by applicable law or agreed to in writing,
013     *  software distributed under the License is distributed on an
014     *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015     *  KIND, either express or implied.  See the License for the
016     *  specific language governing permissions and limitations
017     *  under the License. 
018     *  
019     */
020    
021    package org.apache.directory.server.ntp.io;
022    
023    
024    import java.nio.ByteBuffer;
025    
026    import org.apache.directory.server.ntp.messages.LeapIndicatorType;
027    import org.apache.directory.server.ntp.messages.ModeType;
028    import org.apache.directory.server.ntp.messages.NtpMessage;
029    import org.apache.directory.server.ntp.messages.ReferenceIdentifier;
030    
031    
032    /**
033     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
034     * @version $Rev: 547539 $, $Date: 2007-06-15 08:08:06 +0200 (Fri, 15 Jun 2007) $
035     */
036    public class NtpMessageEncoder
037    {
038        /**
039         * Encodes the {@link NtpMessage} into the {@link ByteBuffer}.
040         *
041         * @param byteBuffer
042         * @param message
043         */
044        public void encode( ByteBuffer byteBuffer, NtpMessage message )
045        {
046            byte header = 0x00;
047            header = encodeLeapIndicator( message.getLeapIndicator(), header );
048            header = encodeVersionNumber( message.getVersionNumber(), header );
049            header = encodeMode( message.getMode(), header );
050            byteBuffer.put( header );
051    
052            byteBuffer.put( ( byte ) ( message.getStratum().getOrdinal() & 0xFF ) );
053            byteBuffer.put( ( byte ) ( message.getPollInterval() & 0xFF ) );
054            byteBuffer.put( ( byte ) ( message.getPrecision() & 0xFF ) );
055    
056            byteBuffer.putInt( message.getRootDelay() );
057            byteBuffer.putInt( message.getRootDispersion() );
058    
059            encodeReferenceIdentifier( message.getReferenceIdentifier(), byteBuffer );
060    
061            message.getReferenceTimestamp().writeTo( byteBuffer );
062            message.getOriginateTimestamp().writeTo( byteBuffer );
063            message.getReceiveTimestamp().writeTo( byteBuffer );
064            message.getTransmitTimestamp().writeTo( byteBuffer );
065        }
066    
067    
068        private byte encodeLeapIndicator( LeapIndicatorType leapIndicator, byte header )
069        {
070            byte twoBits = ( byte ) ( leapIndicator.getOrdinal() & 0x03 );
071            return ( byte ) ( ( twoBits << 6 ) | header );
072        }
073    
074    
075        private byte encodeVersionNumber( int versionNumber, byte header )
076        {
077            byte threeBits = ( byte ) ( versionNumber & 0x07 );
078            return ( byte ) ( ( threeBits << 3 ) | header );
079        }
080    
081    
082        private byte encodeMode( ModeType mode, byte header )
083        {
084            byte threeBits = ( byte ) ( mode.getOrdinal() & 0x07 );
085            return ( byte ) ( threeBits | header );
086        }
087    
088    
089        private void encodeReferenceIdentifier( ReferenceIdentifier identifier, ByteBuffer byteBuffer )
090        {
091            char[] characters = identifier.getCode().toCharArray();
092    
093            for ( int ii = 0; ii < characters.length; ii++ )
094            {
095                byteBuffer.put( ( byte ) characters[ii] );
096            }
097        }
098    }