001 /*
002 * CDDL HEADER START
003 *
004 * The contents of this file are subject to the terms of the
005 * Common Development and Distribution License, Version 1.0 only
006 * (the "License"). You may not use this file except in compliance
007 * with the License.
008 *
009 * You can obtain a copy of the license at
010 * trunk/opends/resource/legal-notices/OpenDS.LICENSE
011 * or https://OpenDS.dev.java.net/OpenDS.LICENSE.
012 * See the License for the specific language governing permissions
013 * and limitations under the License.
014 *
015 * When distributing Covered Code, include this CDDL HEADER in each
016 * file and include the License file at
017 * trunk/opends/resource/legal-notices/OpenDS.LICENSE. If applicable,
018 * add the following below this CDDL HEADER, with the fields enclosed
019 * by brackets "[]" replaced with your own identifying information:
020 * Portions Copyright [yyyy] [name of copyright owner]
021 *
022 * CDDL HEADER END
023 *
024 *
025 * Copyright 2006-2008 Sun Microsystems, Inc.
026 */
027 package org.opends.server.replication.protocol;
028
029 import java.io.UnsupportedEncodingException;
030 import java.util.zip.DataFormatException;
031
032 import org.opends.server.replication.common.ChangeNumber;
033
034 /**
035 * Used to send acks between LDAP and replication servers.
036 */
037 public class AckMessage extends ReplicationMessage
038 {
039 // ChangeNumber of the update that was acked.
040 private ChangeNumber changeNumber;
041
042 /**
043 * Creates a new AckMessage from a ChangeNumber.
044 *
045 * @param changeNumber The ChangeNumber used to build the AckMessage.
046 */
047 public AckMessage(ChangeNumber changeNumber)
048 {
049 this.changeNumber = changeNumber;
050 }
051
052 /**
053 * Creates a new AckMessage by decoding the provided byte array.
054 *
055 * @param in The byte array containing the encoded form of the AckMessage.
056 * @throws DataFormatException If in does not contain a properly encoded
057 * AckMessage.
058 */
059 public AckMessage(byte[] in) throws DataFormatException
060 {
061 try
062 {
063 /* first byte is the type */
064 if (in[0] != MSG_TYPE_ACK)
065 throw new DataFormatException("byte[] is not a valid modify msg");
066 int pos = 1;
067
068 /* read the changeNumber */
069 int length = getNextLength(in, pos);
070 String changenumberStr = new String(in, pos, length, "UTF-8");
071 changeNumber = new ChangeNumber(changenumberStr);
072 pos +=24;
073 } catch (UnsupportedEncodingException e)
074 {
075 throw new DataFormatException("UTF-8 is not supported by this jvm.");
076 }
077 }
078
079 /**
080 * Get the ChangeNumber from the message.
081 *
082 * @return the ChangeNumber
083 */
084 public ChangeNumber getChangeNumber()
085 {
086 return changeNumber;
087 }
088
089 /**
090 * {@inheritDoc}
091 */
092 @Override
093 public byte[] getBytes()
094 {
095 try
096 {
097 byte[] changeNumberByte = changeNumber.toString().getBytes("UTF-8");
098 int length = 1 + changeNumberByte.length + 1;
099 byte[] resultByteArray = new byte[length];
100 int pos = 1;
101
102 /* put the type of the operation */
103 resultByteArray[0] = MSG_TYPE_ACK;
104
105 /* put the ChangeNumber */
106 pos = addByteArray(changeNumberByte, resultByteArray, pos);
107
108 return resultByteArray;
109 } catch (UnsupportedEncodingException e)
110 {
111 return null;
112 }
113 }
114 }