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-2014 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 one or several servers as the
036 * first message of an export, before sending the entries.
037 */
038public class InitializeTargetMsg extends RoutableMsg
039{
040  private final DN baseDN;
041
042  /** Specifies the number of entries expected to be exported. */
043  private final long entryCount;
044
045  /**
046   * Specifies the serverID of the server that requested this export to happen.
047   * It allows a server that previously sent an InitializeRequestMessage to know
048   * that the current message is related to its own request.
049   */
050  private final int requestorID;
051
052  private int initWindow;
053
054  /**
055   * Creates a InitializeTargetMsg.
056   *
057   * @param baseDN     The base DN for which the InitializeMessage is created.
058   * @param serverID   The serverID of the server that sends this message.
059   * @param destination     The destination of this message.
060   * @param requestorID    The server that initiates this export.
061   * @param entryCount The count of entries that will be sent.
062   * @param initWindow the initialization window.
063   */
064  public InitializeTargetMsg(DN baseDN, int serverID,
065      int destination, int requestorID, long entryCount, int initWindow)
066  {
067    super(serverID, destination);
068    this.requestorID = requestorID;
069    this.baseDN = baseDN;
070    this.entryCount = entryCount;
071    this.initWindow = initWindow; // V4
072  }
073
074  /**
075   * Creates an InitializeTargetMsg by decoding the provided byte array.
076   * @param in A byte array containing the encoded information for the message
077   * @param version The protocol version to use to decode the msg
078   * @throws DataFormatException If the in does not contain a properly
079   *                             encoded InitializeMessage.
080   */
081  InitializeTargetMsg(byte[] in, short version) throws DataFormatException
082  {
083    final ByteArrayScanner scanner = new ByteArrayScanner(in);
084    final byte msgType = scanner.nextByte();
085    if (msgType != MSG_TYPE_INITIALIZE_TARGET)
086    {
087      throw new DataFormatException(
088          "input is not a valid InitializeDestinationMessage");
089    }
090    destination = scanner.nextIntUTF8();
091    baseDN = scanner.nextDN();
092    senderID = scanner.nextIntUTF8();
093    requestorID = scanner.nextIntUTF8();
094    entryCount = scanner.nextLongUTF8();
095
096    if (version >= ProtocolVersion.REPLICATION_PROTOCOL_V4)
097    {
098      initWindow = scanner.nextIntUTF8();
099    }
100  }
101
102  /**
103   * Get the number of entries expected to be sent during the export.
104   * @return the entry count
105   */
106  public long getEntryCount()
107  {
108    return this.entryCount;
109  }
110
111  /**
112   * Get the serverID of the server that initiated the export.
113   * Roughly it is the server running the task,
114   * - the importer for the Initialize task,
115   * - the exporter for the InitializeRemote task.
116   * @return the serverID
117   */
118  public long getInitiatorID()
119  {
120    return this.requestorID;
121  }
122
123  /**
124   * Get the base DN of the domain.
125   *
126   * @return the base DN
127   */
128  public DN getBaseDN()
129  {
130    return this.baseDN;
131  }
132
133  /**
134   * Get the initializationWindow.
135   *
136   * @return the initialization window.
137   */
138  public int getInitWindow()
139  {
140    return this.initWindow;
141  }
142
143  // ============
144  // Msg encoding
145  // ============
146
147  /** {@inheritDoc} */
148  @Override
149  public byte[] getBytes(short version)
150  {
151    final ByteArrayBuilder builder = new ByteArrayBuilder();
152    builder.appendByte(MSG_TYPE_INITIALIZE_TARGET);
153    builder.appendIntUTF8(destination);
154    builder.appendDN(baseDN);
155    builder.appendIntUTF8(senderID);
156    builder.appendIntUTF8(requestorID);
157    builder.appendLongUTF8(entryCount);
158    if (version >= ProtocolVersion.REPLICATION_PROTOCOL_V4)
159    {
160      builder.appendIntUTF8(initWindow);
161    }
162    return builder.toByteArray();
163  }
164
165  /**
166   * Set the initWindow value.
167   * @param initWindow The initialization window.
168   */
169  public void setInitWindow(int initWindow)
170  {
171    this.initWindow = initWindow;
172  }
173}