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-2009 Sun Microsystems, Inc.
015 * Portions copyright 2013-2014 ForgeRock AS.
016 */
017package org.opends.server.replication.protocol;
018
019import java.util.zip.DataFormatException;
020
021/**
022 * This message is part of the replication protocol. RS1 sends a
023 * MonitorRequestMsg to RS2 to request its monitoring information. When RS2
024 * receives a MonitorRequestMsg from RS1, RS2 responds with a MonitorMessage.
025 */
026public class MonitorRequestMsg extends ReplicationMsg
027{
028  /**
029   * The destination server or servers of this message.
030   */
031  private final int destination;
032
033  /**
034   * The serverID of the server that sends this message.
035   */
036  private final int senderID;
037
038  /**
039   * Creates a message.
040   *
041   * @param serverID
042   *          The sender server of this message.
043   * @param destination
044   *          The server or servers targeted by this message.
045   */
046  public MonitorRequestMsg(int serverID, int destination)
047  {
048    this.senderID = serverID;
049    this.destination = destination;
050  }
051
052
053
054  /**
055   * Creates a new message by decoding the provided byte array.
056   *
057   * @param in
058   *          A byte array containing the encoded information for the message,
059   * @throws DataFormatException
060   *           If the in does not contain a properly, encoded message.
061   */
062  MonitorRequestMsg(byte[] in) throws DataFormatException
063  {
064    final ByteArrayScanner scanner = new ByteArrayScanner(in);
065    final byte msgType = scanner.nextByte();
066    if (msgType != MSG_TYPE_REPL_SERVER_MONITOR_REQUEST)
067    {
068      throw new DataFormatException("input is not a valid "
069          + getClass().getCanonicalName());
070    }
071    this.senderID = scanner.nextIntUTF8();
072    this.destination = scanner.nextIntUTF8();
073  }
074
075  /** {@inheritDoc} */
076  @Override
077  public byte[] getBytes(short protocolVersion)
078  {
079    final ByteArrayBuilder builder = new ByteArrayBuilder();
080    builder.appendByte(MSG_TYPE_REPL_SERVER_MONITOR_REQUEST);
081    builder.appendIntUTF8(senderID);
082    builder.appendIntUTF8(destination);
083    return builder.toByteArray();
084  }
085
086  /**
087   * Get the destination.
088   *
089   * @return the destination
090   */
091  public int getDestination()
092  {
093    return destination;
094  }
095
096  /**
097   * Get the server ID of the server that sent this message.
098   *
099   * @return the server id
100   */
101  public int getSenderID()
102  {
103    return senderID;
104  }
105
106  /**
107   * Returns a string representation of the message.
108   *
109   * @return the string representation of this message.
110   */
111  @Override
112  public String toString()
113  {
114    return "[" + getClass().getCanonicalName() + " sender=" + senderID
115        + " destination=" + destination + "]";
116  }
117}