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