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 2006-2008 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 WindowMsg extends ReplicationMsg
031{
032  private final int numAck;
033
034  /**
035   * Create a new WindowMsg.
036   *
037   * @param numAck The number of acknowledged messages.
038   *               The window will be increase by this credit number.
039   */
040  public WindowMsg(int numAck)
041  {
042    this.numAck = numAck;
043  }
044
045  /**
046   * Creates a new WindowMsg from its encoded form.
047   *
048   * @param in The byte array containing the encoded form of the
049   *           WindowMsg.
050   * @throws DataFormatException If the byte array does not contain a valid
051   *                             encoded form of the WindowMsg.
052   */
053  WindowMsg(byte[] in) throws DataFormatException
054  {
055    final ByteArrayScanner scanner = new ByteArrayScanner(in);
056    final byte msgType = scanner.nextByte();
057    if (msgType != MSG_TYPE_WINDOW)
058    {
059      throw new DataFormatException("input is not a valid Window Message");
060    }
061
062    numAck = scanner.nextIntUTF8();
063  }
064
065  /** {@inheritDoc} */
066  @Override
067  public byte[] getBytes(short protocolVersion)
068  {
069    final ByteArrayBuilder builder = new ByteArrayBuilder();
070    builder.appendByte(MSG_TYPE_WINDOW);
071    builder.appendIntUTF8(numAck);
072    return builder.toByteArray();
073  }
074
075  /**
076   * Get the number of message acknowledged by the Window message.
077   *
078   * @return the number of message acknowledged by the Window message.
079   */
080  public int getNumAck()
081  {
082    return numAck;
083  }
084
085  /** {@inheritDoc} */
086  @Override
087  public String toString()
088  {
089    return "WindowMsg : " + "numAck: " + numAck;
090  }
091}