4812N/A/*
4812N/A * CDDL HEADER START
4812N/A *
4812N/A * The contents of this file are subject to the terms of the
4812N/A * Common Development and Distribution License, Version 1.0 only
4812N/A * (the "License"). You may not use this file except in compliance
4812N/A * with the License.
4812N/A *
4812N/A * You can obtain a copy of the license at
4812N/A * trunk/opends/resource/legal-notices/OpenDS.LICENSE
4812N/A * or https://OpenDS.dev.java.net/OpenDS.LICENSE.
4812N/A * See the License for the specific language governing permissions
4812N/A * and limitations under the License.
4812N/A *
4812N/A * When distributing Covered Code, include this CDDL HEADER in each
4812N/A * file and include the License file at
4812N/A * trunk/opends/resource/legal-notices/OpenDS.LICENSE. If applicable,
4812N/A * add the following below this CDDL HEADER, with the fields enclosed
4812N/A * by brackets "[]" replaced with your own identifying information:
4812N/A * Portions Copyright [yyyy] [name of copyright owner]
4812N/A *
4812N/A * CDDL HEADER END
4812N/A *
4812N/A *
4812N/A * Copyright 2009 Sun Microsystems, Inc.
6334N/A * Portions copyright 2013 ForgeRock AS.
4812N/A */
4812N/Apackage org.opends.server.replication.protocol;
4812N/A
4812N/Aimport java.io.UnsupportedEncodingException;
4812N/Aimport java.util.zip.DataFormatException;
4812N/A
4812N/Aimport org.opends.server.replication.common.ServerState;
4812N/A
4812N/A/**
4812N/A * Message sent by a replication server to a directory server in reply to the
4812N/A * ServerStartMsg.
4812N/A */
4812N/Apublic class ReplServerStartDSMsg extends StartMsg
4812N/A{
4812N/A private int serverId;
4812N/A private String serverURL;
4812N/A private String baseDn = null;
4812N/A private int windowSize;
4812N/A private ServerState serverState;
4812N/A
4812N/A /**
4812N/A * Whether to continue using SSL to encrypt messages after the start
4812N/A * messages have been exchanged.
4812N/A */
4812N/A private boolean sslEncryption;
4812N/A
4812N/A /**
4812N/A * Threshold value used by the RS to determine if a DS must be put in
4812N/A * degraded status because the number of pending changes for him has crossed
4812N/A * this value. This field is only used by a DS.
4812N/A */
4812N/A private int degradedStatusThreshold = -1;
4812N/A
4812N/A /**
4812N/A * The weight affected to the replication server.
4812N/A */
4812N/A private int weight = -1;
4812N/A
4812N/A /**
4812N/A * Number of currently connected DS to the replication server.
4812N/A */
4812N/A private int connectedDSNumber = -1;
4812N/A
4812N/A /**
4812N/A * Create a ReplServerStartDSMsg.
4812N/A *
4812N/A * @param serverId replication server id
4812N/A * @param serverURL replication server URL
4812N/A * @param baseDn base DN for which the ReplServerStartDSMsg is created.
4812N/A * @param windowSize The window size.
4812N/A * @param serverState our ServerState for this baseDn.
4812N/A * @param generationId The generationId for this server.
4812N/A * @param sslEncryption Whether to continue using SSL to encrypt messages
4812N/A * after the start messages have been exchanged.
4812N/A * @param groupId The group id of the RS
4812N/A * @param degradedStatusThreshold The degraded status threshold
4812N/A * @param weight The weight affected to the replication server.
4812N/A * @param connectedDSNumber Number of currently connected DS to the
4812N/A * replication server.
4812N/A */
4812N/A public ReplServerStartDSMsg(int serverId, String serverURL, String baseDn,
4812N/A int windowSize,
4812N/A ServerState serverState,
4812N/A long generationId,
4812N/A boolean sslEncryption,
4812N/A byte groupId,
4812N/A int degradedStatusThreshold,
4812N/A int weight,
4812N/A int connectedDSNumber)
4812N/A {
6334N/A super((short) -1 /* version set when sending */, generationId);
4812N/A this.serverId = serverId;
4812N/A this.serverURL = serverURL;
4812N/A if (baseDn != null)
4812N/A this.baseDn = baseDn;
4812N/A else
4812N/A this.baseDn = null;
4812N/A this.windowSize = windowSize;
4812N/A this.serverState = serverState;
4812N/A this.sslEncryption = sslEncryption;
4812N/A this.groupId = groupId;
4812N/A this.degradedStatusThreshold = degradedStatusThreshold;
4812N/A this.weight = weight;
4812N/A this.connectedDSNumber = connectedDSNumber;
4812N/A }
4812N/A
4812N/A /**
4812N/A * Creates a new ReplServerStartDSMsg by decoding the provided byte array.
4812N/A * @param in A byte array containing the encoded information for the
4812N/A * ReplServerStartDSMsg
4812N/A * @throws DataFormatException If the in does not contain a properly
4812N/A * encoded ReplServerStartDSMsg.
4812N/A */
4812N/A public ReplServerStartDSMsg(byte[] in) throws DataFormatException
4812N/A {
4812N/A byte[] allowedPduTypes = new byte[1];
4812N/A allowedPduTypes[0] = MSG_TYPE_REPL_SERVER_START_DS;
4812N/A headerLength = decodeHeader(allowedPduTypes, in);
4812N/A
4812N/A try
4812N/A {
4812N/A /* The ReplServerStartDSMsg payload is stored in the form :
4812N/A * <baseDn><serverId><serverURL><windowSize><sslEncryption>
4812N/A * <degradedStatusThreshold><weight><connectedDSNumber>
4812N/A * <serverState>
4812N/A */
4812N/A
4812N/A /* first bytes are the header */
4812N/A int pos = headerLength;
4812N/A
4812N/A /* read the dn
4812N/A * first calculate the length then construct the string
4812N/A */
4812N/A int length = getNextLength(in, pos);
4812N/A baseDn = new String(in, pos, length, "UTF-8");
4812N/A pos += length +1;
4812N/A
4812N/A /*
4812N/A * read the ServerId
4812N/A */
4812N/A length = getNextLength(in, pos);
4812N/A String serverIdString = new String(in, pos, length, "UTF-8");
4812N/A serverId = Integer.valueOf(serverIdString);
4812N/A pos += length +1;
4812N/A
4812N/A /*
4812N/A * read the ServerURL
4812N/A */
4812N/A length = getNextLength(in, pos);
4812N/A serverURL = new String(in, pos, length, "UTF-8");
4812N/A pos += length +1;
4812N/A
4812N/A /*
4812N/A * read the window size
4812N/A */
4812N/A length = getNextLength(in, pos);
4812N/A windowSize = Integer.valueOf(new String(in, pos, length, "UTF-8"));
4812N/A pos += length +1;
4812N/A
4812N/A /*
4812N/A * read the sslEncryption setting
4812N/A */
4812N/A length = getNextLength(in, pos);
4812N/A sslEncryption = Boolean.valueOf(new String(in, pos, length, "UTF-8"));
4812N/A pos += length +1;
4812N/A
4812N/A /**
4812N/A * read the degraded status threshold
4812N/A */
4812N/A length = getNextLength(in, pos);
4812N/A degradedStatusThreshold =
4812N/A Integer.valueOf(new String(in, pos, length, "UTF-8"));
4812N/A pos += length + 1;
4812N/A
4812N/A /*
4812N/A * read the weight
4812N/A */
4812N/A length = getNextLength(in, pos);
4812N/A String weightString = new String(in, pos, length, "UTF-8");
4812N/A weight = Integer.valueOf(weightString);
4812N/A pos += length +1;
4812N/A
4812N/A /*
4812N/A * read the connected DS number
4812N/A */
4812N/A length = getNextLength(in, pos);
4812N/A String connectedDSNumberString = new String(in, pos, length, "UTF-8");
4812N/A connectedDSNumber = Integer.valueOf(connectedDSNumberString);
4812N/A pos += length +1;
4812N/A
4812N/A // Read the ServerState
4812N/A // Caution: ServerState MUST be the last field. Because ServerState can
4812N/A // contain null character (string termination of serverid string ..) it
4812N/A // cannot be decoded using getNextLength() like the other fields. The
4812N/A // only way is to rely on the end of the input buffer : and that forces
4812N/A // the ServerState to be the last. This should be changed and we want to
4812N/A // have more than one ServerState field.
4812N/A serverState = new ServerState(in, pos, in.length - 1);
4812N/A } catch (UnsupportedEncodingException e)
4812N/A {
4812N/A throw new DataFormatException("UTF-8 is not supported by this jvm.");
4812N/A }
4812N/A }
4812N/A
4812N/A /**
4812N/A * Get the Server Id.
4812N/A * @return the server id
4812N/A */
4812N/A public int getServerId()
4812N/A {
4812N/A return this.serverId;
4812N/A }
4812N/A
4812N/A /**
4812N/A * Get the server URL.
4812N/A * @return the server URL
4812N/A */
4812N/A public String getServerURL()
4812N/A {
4812N/A return this.serverURL;
4812N/A }
4812N/A
4812N/A /**
4812N/A * Get the base DN from this ReplServerStartDSMsg.
4812N/A *
4812N/A * @return the base DN from this ReplServerStartDSMsg.
4812N/A */
4812N/A public String getBaseDn()
4812N/A {
4812N/A return baseDn;
4812N/A }
4812N/A
4812N/A /**
4812N/A * Get the serverState.
4812N/A * @return Returns the serverState.
4812N/A */
4812N/A public ServerState getServerState()
4812N/A {
4812N/A return this.serverState;
4812N/A }
4812N/A
4812N/A /**
4812N/A * {@inheritDoc}
4812N/A */
4812N/A @Override
6334N/A public byte[] getBytes(short sessionProtocolVersion)
4812N/A throws UnsupportedEncodingException
4812N/A {
4812N/A /* The ReplServerStartDSMsg is stored in the form :
4812N/A * <operation type><baseDn><serverId><serverURL><windowSize><sslEncryption>
4812N/A * <degradedStatusThreshold><weight><connectedDSNumber>
4812N/A * <serverState>
4812N/A */
4812N/A byte[] byteDn = baseDn.getBytes("UTF-8");
4812N/A byte[] byteServerId = String.valueOf(serverId).getBytes("UTF-8");
4812N/A byte[] byteServerUrl = serverURL.getBytes("UTF-8");
4812N/A byte[] byteServerState = serverState.getBytes();
4812N/A byte[] byteWindowSize = String.valueOf(windowSize).getBytes("UTF-8");
4812N/A byte[] byteSSLEncryption =
4812N/A String.valueOf(sslEncryption).getBytes("UTF-8");
4812N/A byte[] byteDegradedStatusThreshold =
4812N/A String.valueOf(degradedStatusThreshold).getBytes("UTF-8");
4812N/A byte[] byteWeight =
4812N/A String.valueOf(weight).getBytes("UTF-8");
4812N/A byte[] byteConnectedDSNumber =
4812N/A String.valueOf(connectedDSNumber).getBytes("UTF-8");
4812N/A
4812N/A int length = byteDn.length + 1 + byteServerId.length + 1 +
4812N/A byteServerUrl.length + 1 + byteWindowSize.length + 1 +
4812N/A byteSSLEncryption.length + 1 + byteDegradedStatusThreshold.length + 1 +
4812N/A byteWeight.length + 1 + byteConnectedDSNumber.length + 1 +
4812N/A byteServerState.length + 1;
4812N/A
4812N/A /* encode the header in a byte[] large enough */
6334N/A byte resultByteArray[] = encodeHeader(MSG_TYPE_REPL_SERVER_START_DS,
6334N/A length, sessionProtocolVersion);
4812N/A
4812N/A int pos = headerLength;
4812N/A
4812N/A /* put the baseDN and a terminating 0 */
4812N/A pos = addByteArray(byteDn, resultByteArray, pos);
4812N/A
4812N/A /* put the ServerId */
4812N/A pos = addByteArray(byteServerId, resultByteArray, pos);
4812N/A
4812N/A /* put the ServerURL */
4812N/A pos = addByteArray(byteServerUrl, resultByteArray, pos);
4812N/A
4812N/A /* put the window size */
4812N/A pos = addByteArray(byteWindowSize, resultByteArray, pos);
4812N/A
4812N/A /* put the SSL Encryption setting */
4812N/A pos = addByteArray(byteSSLEncryption, resultByteArray, pos);
4812N/A
4812N/A /* put the degraded status threshold */
4812N/A pos = addByteArray(byteDegradedStatusThreshold, resultByteArray, pos);
4812N/A
4812N/A /* put the weight */
4812N/A pos = addByteArray(byteWeight, resultByteArray, pos);
4812N/A
4812N/A /* put the connected DS number */
4812N/A pos = addByteArray(byteConnectedDSNumber, resultByteArray, pos);
4812N/A
4812N/A /* put the ServerState */
4812N/A pos = addByteArray(byteServerState, resultByteArray, pos);
4812N/A
4812N/A return resultByteArray;
4812N/A }
4812N/A
4812N/A /**
4812N/A * get the window size for the server that created this message.
4812N/A *
4812N/A * @return The window size for the server that created this message.
4812N/A */
4812N/A public int getWindowSize()
4812N/A {
4812N/A return windowSize;
4812N/A }
4812N/A
4812N/A /**
4812N/A * Get the SSL encryption value for the server that created the
4812N/A * message.
4812N/A *
4812N/A * @return The SSL encryption value for the server that created the
4812N/A * message.
4812N/A */
4812N/A public boolean getSSLEncryption()
4812N/A {
4812N/A return sslEncryption;
4812N/A }
4812N/A
4812N/A /**
4812N/A * Get the degraded status threshold value.
4812N/A * @return The degraded status threshold value.
4812N/A */
4812N/A public int getDegradedStatusThreshold()
4812N/A {
4812N/A return degradedStatusThreshold;
4812N/A }
4812N/A
4812N/A /**
4812N/A * Set the degraded status threshold (For test purpose).
4812N/A * @param degradedStatusThreshold The degraded status threshold to set.
4812N/A */
4812N/A public void setDegradedStatusThreshold(int degradedStatusThreshold)
4812N/A {
4812N/A this.degradedStatusThreshold = degradedStatusThreshold;
4812N/A }
4812N/A
4812N/A /**
4812N/A * {@inheritDoc}
4812N/A */
4812N/A @Override
4812N/A public String toString()
4812N/A {
4812N/A return "ReplServerStartDSMsg content: " +
4812N/A "\nprotocolVersion: " + protocolVersion +
4812N/A "\ngenerationId: " + generationId +
4812N/A "\nbaseDn: " + baseDn +
4812N/A "\ngroupId: " + groupId +
4812N/A "\nserverId: " + serverId +
4812N/A "\nserverState: " + serverState +
4812N/A "\nserverURL: " + serverURL +
4812N/A "\nsslEncryption: " + sslEncryption +
4812N/A "\ndegradedStatusThreshold: " + degradedStatusThreshold +
4812N/A "\nwindowSize: " + windowSize +
4812N/A "\nweight: " + weight +
4812N/A "\nconnectedDSNumber: " + connectedDSNumber;
4812N/A }
4812N/A
4812N/A /**
4812N/A * Gets the weight of the replication server.
4812N/A * @return The weight of the replication server.
4812N/A */
4812N/A public int getWeight()
4812N/A {
4812N/A return weight;
4812N/A }
4812N/A
4812N/A /**
4812N/A * Gets the number of directory servers connected to the replication server.
4812N/A * @return The number of directory servers connected to the replication
4812N/A * server.
4812N/A */
4812N/A public int getConnectedDSNumber()
4812N/A {
4812N/A return connectedDSNumber;
4812N/A }
4812N/A
4812N/A}