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 021package org.apache.directory.server.ntp.io; 022 023 024import java.nio.ByteBuffer; 025 026import org.apache.directory.server.ntp.messages.LeapIndicatorType; 027import org.apache.directory.server.ntp.messages.ModeType; 028import org.apache.directory.server.ntp.messages.NtpMessage; 029import org.apache.directory.server.ntp.messages.ReferenceIdentifier; 030 031 032/** 033 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a> 034 */ 035public class NtpMessageEncoder 036{ 037 /** 038 * Encodes the {@link NtpMessage} into the {@link ByteBuffer}. 039 * 040 * @param byteBuffer 041 * @param message 042 */ 043 public void encode( ByteBuffer byteBuffer, NtpMessage message ) 044 { 045 byte header = 0x00; 046 header = encodeLeapIndicator( message.getLeapIndicator(), header ); 047 header = encodeVersionNumber( message.getVersionNumber(), header ); 048 header = encodeMode( message.getMode(), header ); 049 byteBuffer.put( header ); 050 051 byteBuffer.put( ( byte ) ( message.getStratum().getOrdinal() & 0xFF ) ); 052 byteBuffer.put( ( byte ) ( message.getPollInterval() & 0xFF ) ); 053 byteBuffer.put( ( byte ) ( message.getPrecision() & 0xFF ) ); 054 055 byteBuffer.putInt( message.getRootDelay() ); 056 byteBuffer.putInt( message.getRootDispersion() ); 057 058 encodeReferenceIdentifier( message.getReferenceIdentifier(), byteBuffer ); 059 060 message.getReferenceTimestamp().writeTo( byteBuffer ); 061 message.getOriginateTimestamp().writeTo( byteBuffer ); 062 message.getReceiveTimestamp().writeTo( byteBuffer ); 063 message.getTransmitTimestamp().writeTo( byteBuffer ); 064 } 065 066 067 private byte encodeLeapIndicator( LeapIndicatorType leapIndicator, byte header ) 068 { 069 byte twoBits = ( byte ) ( leapIndicator.getOrdinal() & 0x03 ); 070 return ( byte ) ( ( twoBits << 6 ) | header ); 071 } 072 073 074 private byte encodeVersionNumber( int versionNumber, byte header ) 075 { 076 byte threeBits = ( byte ) ( versionNumber & 0x07 ); 077 return ( byte ) ( ( threeBits << 3 ) | header ); 078 } 079 080 081 private byte encodeMode( ModeType mode, byte header ) 082 { 083 byte threeBits = ( byte ) ( mode.getOrdinal() & 0x07 ); 084 return ( byte ) ( threeBits | header ); 085 } 086 087 088 private void encodeReferenceIdentifier( ReferenceIdentifier identifier, ByteBuffer byteBuffer ) 089 { 090 char[] characters = identifier.getCode().toCharArray(); 091 092 for ( int ii = 0; ii < characters.length; ii++ ) 093 { 094 byteBuffer.put( ( byte ) characters[ii] ); 095 } 096 } 097}