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 legal-notices/CDDLv1_0.txt
010 * or http://forgerock.org/license/CDDLv1.0.html.
011 * See the License for the specific language governing permissions
012 * and limitations under the License.
013 *
014 * When distributing Covered Code, include this CDDL HEADER in each
015 * file and include the License file at legal-notices/CDDLv1_0.txt.
016 * If applicable, add the following below this CDDL HEADER, with the
017 * fields enclosed by brackets "[]" replaced with your own identifying
018 * information:
019 *      Portions Copyright [yyyy] [name of copyright owner]
020 *
021 * CDDL HEADER END
022 *
023 *
024 *      Copyright 2010 Sun Microsystems, Inc.
025 *      Portions copyright 2013-2014 ForgeRock AS.
026 */
027package org.opends.server.replication.protocol;
028
029import java.util.zip.DataFormatException;
030
031/**
032 * This message is used by LDAP server or by Replication Servers to
033 * update the send window of the remote entities.
034 *
035 * A receiving entity should create such a message with a given credit
036 * when it wants to open the send window of the remote entity.
037 * A LDAP or Replication Server should increase its send window when receiving
038 * such a message.
039 */
040public class InitializeRcvAckMsg extends RoutableMsg
041{
042  private final int numAck;
043
044  /**
045   * Create a new message..
046   *
047   * @param sender The server ID of the server that send this message.
048   * @param destination The destination server or servers of this message.
049   * @param numAck The number of acknowledged messages.
050   *               The window will be increase by this credit number.
051   */
052  public InitializeRcvAckMsg(int sender, int destination, int numAck)
053  {
054    super(sender, destination);
055    this.numAck = numAck;
056  }
057
058  /**
059   * Creates a new message from its encoded form.
060   *
061   * @param in The byte array containing the encoded form of the message.
062   * @throws DataFormatException If the byte array does not contain a valid
063   *                             encoded form of the message.
064   */
065  InitializeRcvAckMsg(byte[] in) throws DataFormatException
066  {
067    final ByteArrayScanner scanner = new ByteArrayScanner(in);
068    if (scanner.nextByte() != MSG_TYPE_INITIALIZE_RCV_ACK)
069    {
070      throw new DataFormatException("input is not a valid "
071          + getClass().getCanonicalName());
072    }
073
074    senderID = scanner.nextIntUTF8();
075    destination = scanner.nextIntUTF8();
076    numAck = scanner.nextIntUTF8();
077  }
078
079  /** {@inheritDoc} */
080  @Override
081  public byte[] getBytes(short protocolVersion)
082  {
083    final ByteArrayBuilder builder = new ByteArrayBuilder();
084    builder.appendByte(MSG_TYPE_INITIALIZE_RCV_ACK);
085    builder.appendIntUTF8(senderID);
086    builder.appendIntUTF8(destination);
087    builder.appendIntUTF8(numAck);
088    return builder.toByteArray();
089  }
090
091  /** {@inheritDoc} */
092  @Override
093  public String toString()
094  {
095    return getClass().getSimpleName() + "=[" +
096      " sender=" + this.senderID +
097      " destination=" + this.destination +
098      " msgID=" + this.numAck + "]";
099  }
100
101  /**
102   * Get the number of message acknowledged by this message.
103   *
104   * @return the number of message acknowledged by this message.
105   */
106  public int getNumAck()
107  {
108    return numAck;
109  }
110}