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
010     * trunk/opends/resource/legal-notices/OpenDS.LICENSE
011     * or https://OpenDS.dev.java.net/OpenDS.LICENSE.
012     * See the License for the specific language governing permissions
013     * and limitations under the License.
014     *
015     * When distributing Covered Code, include this CDDL HEADER in each
016     * file and include the License file at
017     * trunk/opends/resource/legal-notices/OpenDS.LICENSE.  If applicable,
018     * add the following below this CDDL HEADER, with the fields enclosed
019     * by brackets "[]" replaced with your own identifying information:
020     *      Portions Copyright [yyyy] [name of copyright owner]
021     *
022     * CDDL HEADER END
023     *
024     *
025     *      Copyright 2008 Sun Microsystems, Inc.
026     */
027    package org.opends.server.replication.protocol;
028    
029    import java.io.Serializable;
030    import java.io.UnsupportedEncodingException;
031    import java.util.zip.DataFormatException;
032    
033    /**
034     * This message is part of the replication protocol.
035     * RS1 sends a MonitorRequestMessage to RS2 to requests its monitoring
036     * informations.
037     * When RS2 receives a MonitorRequestMessage from RS1, RS2 responds with a
038     * MonitorMessage.
039     */
040    public class MonitorRequestMessage extends RoutableMessage implements
041        Serializable
042    {
043    
044      private static final long serialVersionUID = -2407640479423633234L;
045    
046      /**
047       * Creates a message.
048       *
049       * @param sender The sender server of this message.
050       * @param destination The server or servers targetted by this message.
051       */
052      public MonitorRequestMessage(short sender, short destination)
053      {
054        super(sender, destination);
055      }
056    
057      /**
058       * Creates a new message by decoding the provided byte array.
059       * @param in A byte array containing the encoded information for the message,
060       * @throws DataFormatException If the in does not contain a properly,
061       *                             encoded message.
062       */
063      public MonitorRequestMessage(byte[] in) throws DataFormatException
064      {
065        super();
066        try
067        {
068          // First byte is the type
069          if (in[0] != MSG_TYPE_REPL_SERVER_MONITOR_REQUEST)
070            throw new DataFormatException("input is not a valid " +
071                this.getClass().getCanonicalName());
072          int pos = 1;
073    
074          // sender
075          int length = getNextLength(in, pos);
076          String senderString = new String(in, pos, length, "UTF-8");
077          this.senderID = Short.valueOf(senderString);
078          pos += length +1;
079    
080          // destination
081          length = getNextLength(in, pos);
082          String destinationString = new String(in, pos, length, "UTF-8");
083          this.destination = Short.valueOf(destinationString);
084          pos += length +1;
085    
086        } catch (UnsupportedEncodingException e)
087        {
088          throw new DataFormatException("UTF-8 is not supported by this jvm.");
089        }
090      }
091    
092      /**
093       * {@inheritDoc}
094       */
095      @Override
096      public byte[] getBytes()
097      {
098        try
099        {
100          byte[] senderBytes = String.valueOf(senderID).getBytes("UTF-8");
101          byte[] destinationBytes = String.valueOf(destination).getBytes("UTF-8");
102    
103          int length = 1 + senderBytes.length + 1
104                         + destinationBytes.length + 1;
105    
106          byte[] resultByteArray = new byte[length];
107    
108          /* put the type of the operation */
109          resultByteArray[0] = MSG_TYPE_REPL_SERVER_MONITOR_REQUEST;
110          int pos = 1;
111    
112          /* put the sender */
113          pos = addByteArray(senderBytes, resultByteArray, pos);
114    
115          /* put the destination */
116          pos = addByteArray(destinationBytes, resultByteArray, pos);
117    
118          return resultByteArray;
119        }
120        catch (UnsupportedEncodingException e)
121        {
122          return null;
123        }
124      }
125    }