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