View Javadoc

1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *  
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *  
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License. 
18   *  
19   */
20  
21  package org.apache.directory.server.ntp.io;
22  
23  
24  import java.nio.ByteBuffer;
25  
26  import org.apache.directory.server.ntp.messages.LeapIndicatorType;
27  import org.apache.directory.server.ntp.messages.ModeType;
28  import org.apache.directory.server.ntp.messages.NtpMessage;
29  import org.apache.directory.server.ntp.messages.ReferenceIdentifier;
30  
31  
32  /**
33   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
34   * @version $Rev: 547539 $, $Date: 2007-06-15 08:08:06 +0200 (Fr, 15 Jun 2007) $
35   */
36  public class NtpMessageEncoder
37  {
38      /**
39       * Encodes the {@link NtpMessage} into the {@link ByteBuffer}.
40       *
41       * @param byteBuffer
42       * @param message
43       */
44      public void encode( ByteBuffer byteBuffer, NtpMessage message )
45      {
46          byte header = 0x00;
47          header = encodeLeapIndicator( message.getLeapIndicator(), header );
48          header = encodeVersionNumber( message.getVersionNumber(), header );
49          header = encodeMode( message.getMode(), header );
50          byteBuffer.put( header );
51  
52          byteBuffer.put( ( byte ) ( message.getStratum().getOrdinal() & 0xFF ) );
53          byteBuffer.put( ( byte ) ( message.getPollInterval() & 0xFF ) );
54          byteBuffer.put( ( byte ) ( message.getPrecision() & 0xFF ) );
55  
56          byteBuffer.putInt( message.getRootDelay() );
57          byteBuffer.putInt( message.getRootDispersion() );
58  
59          encodeReferenceIdentifier( message.getReferenceIdentifier(), byteBuffer );
60  
61          message.getReferenceTimestamp().writeTo( byteBuffer );
62          message.getOriginateTimestamp().writeTo( byteBuffer );
63          message.getReceiveTimestamp().writeTo( byteBuffer );
64          message.getTransmitTimestamp().writeTo( byteBuffer );
65      }
66  
67  
68      private byte encodeLeapIndicator( LeapIndicatorType leapIndicator, byte header )
69      {
70          byte twoBits = ( byte ) ( leapIndicator.getOrdinal() & 0x03 );
71          return ( byte ) ( ( twoBits << 6 ) | header );
72      }
73  
74  
75      private byte encodeVersionNumber( int versionNumber, byte header )
76      {
77          byte threeBits = ( byte ) ( versionNumber & 0x07 );
78          return ( byte ) ( ( threeBits << 3 ) | header );
79      }
80  
81  
82      private byte encodeMode( ModeType mode, byte header )
83      {
84          byte threeBits = ( byte ) ( mode.getOrdinal() & 0x07 );
85          return ( byte ) ( threeBits | header );
86      }
87  
88  
89      private void encodeReferenceIdentifier( ReferenceIdentifier identifier, ByteBuffer byteBuffer )
90      {
91          char[] characters = identifier.getCode().toCharArray();
92  
93          for ( int ii = 0; ii < characters.length; ii++ )
94          {
95              byteBuffer.put( ( byte ) characters[ii] );
96          }
97      }
98  }