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   */
35  public class NtpMessageEncoder
36  {
37      /**
38       * Encodes the {@link NtpMessage} into the {@link ByteBuffer}.
39       *
40       * @param byteBuffer
41       * @param message
42       */
43      public void encode( ByteBuffer byteBuffer, NtpMessage message )
44      {
45          byte header = 0x00;
46          header = encodeLeapIndicator( message.getLeapIndicator(), header );
47          header = encodeVersionNumber( message.getVersionNumber(), header );
48          header = encodeMode( message.getMode(), header );
49          byteBuffer.put( header );
50  
51          byteBuffer.put( ( byte ) ( message.getStratum().getOrdinal() & 0xFF ) );
52          byteBuffer.put( ( byte ) ( message.getPollInterval() & 0xFF ) );
53          byteBuffer.put( ( byte ) ( message.getPrecision() & 0xFF ) );
54  
55          byteBuffer.putInt( message.getRootDelay() );
56          byteBuffer.putInt( message.getRootDispersion() );
57  
58          encodeReferenceIdentifier( message.getReferenceIdentifier(), byteBuffer );
59  
60          message.getReferenceTimestamp().writeTo( byteBuffer );
61          message.getOriginateTimestamp().writeTo( byteBuffer );
62          message.getReceiveTimestamp().writeTo( byteBuffer );
63          message.getTransmitTimestamp().writeTo( byteBuffer );
64      }
65  
66  
67      private byte encodeLeapIndicator( LeapIndicatorType leapIndicator, byte header )
68      {
69          byte twoBits = ( byte ) ( leapIndicator.getOrdinal() & 0x03 );
70          return ( byte ) ( ( twoBits << 6 ) | header );
71      }
72  
73  
74      private byte encodeVersionNumber( int versionNumber, byte header )
75      {
76          byte threeBits = ( byte ) ( versionNumber & 0x07 );
77          return ( byte ) ( ( threeBits << 3 ) | header );
78      }
79  
80  
81      private byte encodeMode( ModeType mode, byte header )
82      {
83          byte threeBits = ( byte ) ( mode.getOrdinal() & 0x07 );
84          return ( byte ) ( threeBits | header );
85      }
86  
87  
88      private void encodeReferenceIdentifier( ReferenceIdentifier identifier, ByteBuffer byteBuffer )
89      {
90          char[] characters = identifier.getCode().toCharArray();
91  
92          for ( int ii = 0; ii < characters.length; ii++ )
93          {
94              byteBuffer.put( ( byte ) characters[ii] );
95          }
96      }
97  }