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