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-2010 Sun Microsystems, Inc.
025 *      Portions Copyright 2013-2015 ForgeRock AS.
026 */
027package org.opends.server.replication.protocol;
028
029import java.util.zip.DataFormatException;
030
031import org.opends.server.types.DN;
032
033/**
034 * This message is part of the replication protocol.
035 * This message is sent by a server to another server in order to
036 * request this other server to do an export to the server sender
037 * of this message.
038 */
039public class InitializeRequestMsg extends RoutableMsg
040{
041  private final DN baseDN;
042  private int initWindow;
043
044  /**
045   * Creates a InitializeRequestMsg message.
046   *
047   * @param baseDN      the base DN of the replication domain.
048   * @param destination destination of this message
049   * @param serverID    serverID of the server that will send this message
050   * @param initWindow  initialization window for flow control
051   */
052  public InitializeRequestMsg(DN baseDN, int serverID, int destination,
053      int initWindow)
054  {
055    super(serverID, destination);
056    this.baseDN = baseDN;
057    this.initWindow = initWindow; // V4
058  }
059
060  /**
061   * Creates a new InitializeRequestMsg by decoding the provided byte array.
062   * @param in A byte array containing the encoded information for the message
063   * @param version The protocol version to use to decode the msg
064   * @throws DataFormatException If the in does not contain a properly
065   *                             encoded InitializeMessage.
066   */
067  InitializeRequestMsg(byte[] in, short version) throws DataFormatException
068  {
069    final ByteArrayScanner scanner = new ByteArrayScanner(in);
070    final byte msgType = scanner.nextByte();
071    if (msgType != MSG_TYPE_INITIALIZE_REQUEST)
072    {
073      throw new DataFormatException(
074          "input is not a valid InitializeRequestMessage");
075    }
076    baseDN = scanner.nextDN();
077    senderID = scanner.nextIntUTF8();
078    destination = scanner.nextIntUTF8();
079
080    if (version >= ProtocolVersion.REPLICATION_PROTOCOL_V4)
081    {
082      initWindow = scanner.nextIntUTF8();
083    }
084  }
085
086  /**
087   * Get the base DN from this InitializeRequestMsg.
088   *
089   * @return the base DN from this InitializeRequestMsg.
090   */
091  public DN getBaseDN()
092  {
093    return baseDN;
094  }
095
096  // ============
097  // Msg encoding
098  // ============
099
100  /** {@inheritDoc} */
101  @Override
102  public byte[] getBytes(short version)
103  {
104    final ByteArrayBuilder builder = new ByteArrayBuilder();
105    builder.appendByte(MSG_TYPE_INITIALIZE_REQUEST);
106    builder.appendDN(baseDN);
107    builder.appendIntUTF8(senderID);
108    builder.appendIntUTF8(destination);
109    if (version >= ProtocolVersion.REPLICATION_PROTOCOL_V4)
110    {
111      builder.appendIntUTF8(initWindow);
112    }
113    return builder.toByteArray();
114  }
115
116  /**
117   * Get a string representation of this object.
118   * @return A string representation of this object.
119   */
120  @Override
121  public String toString()
122  {
123    return "InitializeRequestMessage: baseDN=" + baseDN + " senderId="
124       + senderID + " destination=" + destination + " initWindow=" + initWindow;
125  }
126
127  /**
128   * Return the initWindow value.
129   * @return the initWindow.
130   */
131  public int getInitWindow()
132  {
133    return this.initWindow;
134  }
135
136  /**
137   * Set the initWindow value.
138   * @param initWindow The initialization window.
139   */
140  public void setInitWindow(int initWindow)
141  {
142    this.initWindow = initWindow;
143  }
144}