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 2008 Sun Microsystems, Inc.
015 * Portions copyright 2013-2015 ForgeRock AS.
016 */
017package org.opends.server.replication.protocol;
018
019import java.util.zip.DataFormatException;
020
021import org.opends.server.replication.common.ServerStatus;
022
023/**
024 * This message is used by the DS to tell the RS he is changing his status
025 * (new status field used), or by the RS to tell the DS he must change his
026 * status (requested status field used).
027 */
028public class ChangeStatusMsg extends ReplicationMsg
029{
030  /** The status we want the DS to enter (used when from RS to DS). */
031  private final ServerStatus requestedStatus;
032  /** The new status the DS just entered (used when from DS to RS). */
033  private ServerStatus newStatus;
034
035  /**
036   * Create a new ChangeStatusMsg.
037   *
038   * @param requestedStatus The requested status
039   * @param newStatus The new status
040   */
041  public ChangeStatusMsg(ServerStatus requestedStatus, ServerStatus newStatus)
042  {
043    this.requestedStatus = requestedStatus;
044    this.newStatus = newStatus;
045  }
046
047  /**
048   * Creates a new ChangeStatusMsg from its encoded form.
049   *
050   * @param encodedMsg The byte array containing the encoded form of the
051   *           ChangeStatusMsg.
052   * @throws DataFormatException If the byte array does not contain a valid
053   *                             encoded form of the ChangeStatusMsg.
054   */
055  ChangeStatusMsg(byte[] encodedMsg) throws DataFormatException
056  {
057    /*
058     * The message is stored in the form:
059     * <message type><requested status><new status>
060     */
061    try
062    {
063      if (encodedMsg[0] != ReplicationMsg.MSG_TYPE_CHANGE_STATUS)
064      {
065        throw new DataFormatException("byte[] is not a valid msg");
066      }
067      requestedStatus = ServerStatus.valueOf(encodedMsg[1]);
068      newStatus = ServerStatus.valueOf(encodedMsg[2]);
069    } catch (IllegalArgumentException e)
070    {
071      throw new DataFormatException(e.getMessage());
072    }
073  }
074
075  /** {@inheritDoc} */
076  @Override
077  public byte[] getBytes(short protocolVersion)
078  {
079    /*
080     * The message is stored in the form:
081     * <message type><requested status><new status>
082     */
083    return new byte[]
084    {
085      ReplicationMsg.MSG_TYPE_CHANGE_STATUS,
086      requestedStatus.getValue(),
087      newStatus.getValue()
088    };
089  }
090
091  /**
092   * Get the requested status.
093   * @return The requested status
094   */
095  public ServerStatus getRequestedStatus()
096  {
097    return requestedStatus;
098  }
099
100  /**
101   * Get the requested status.
102   * @return The new status
103   */
104  public ServerStatus getNewStatus()
105  {
106    return newStatus;
107  }
108
109  /** {@inheritDoc} */
110  @Override
111  public String toString()
112  {
113    return "ChangeStatusMsg content:" +
114      "\nnewStatus: " + newStatus +
115      "\nrequestedStatus: " + requestedStatus;
116  }
117}