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