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 legal-notices/CDDLv1_0.txt 010 * or http://forgerock.org/license/CDDLv1.0.html. 011 * See the License for the specific language governing permissions 012 * and limitations under the License. 013 * 014 * When distributing Covered Code, include this CDDL HEADER in each 015 * file and include the License file at legal-notices/CDDLv1_0.txt. 016 * If applicable, add the following below this CDDL HEADER, with the 017 * fields enclosed by brackets "[]" replaced with your own identifying 018 * information: 019 * Portions Copyright [yyyy] [name of copyright owner] 020 * 021 * CDDL HEADER END 022 * 023 * 024 * Copyright 2006-2010 Sun Microsystems, Inc. 025 * Portions Copyright 2013-2015 ForgeRock AS. 026 */ 027package org.opends.server.replication.protocol; 028 029import java.util.zip.DataFormatException; 030 031/** 032 * This message is part of the replication protocol. 033 * This message is sent by a server to one or several other servers and 034 * contain one entry to be sent over the protocol in the context of 035 * an import/export over the protocol. 036 */ 037public class EntryMsg extends RoutableMsg 038{ 039 /** The byte array containing the bytes of the entry transported. */ 040 private final byte[] entryByteArray; 041 /** From V4. */ 042 private int msgId = -1; 043 044 /** 045 * Creates a new EntryMsg. 046 * 047 * @param serverID The sender of this message. 048 * @param destination The destination of this message. 049 * @param entryBytes The bytes of the entry. 050 * @param msgId Message counter. 051 */ 052 public EntryMsg(int serverID, int destination, byte[] entryBytes, int msgId) 053 { 054 this(serverID, destination, entryBytes, 0, entryBytes.length, msgId); 055 } 056 057 /** 058 * Creates a new EntryMsg. 059 * 060 * @param serverID The sender of this message. 061 * @param destination The destination of this message. 062 * @param entryBytes The bytes of the entry. 063 * @param startPos The starting Position in the array. 064 * @param length Number of array elements to be copied. 065 * @param msgId Message counter. 066 */ 067 public EntryMsg(int serverID, int destination, byte[] entryBytes, int startPos, 068 int length, int msgId) 069 { 070 super(serverID, destination); 071 this.entryByteArray = new byte[length]; 072 System.arraycopy(entryBytes, startPos, this.entryByteArray, 0, length); 073 this.msgId = msgId; 074 } 075 076 /** 077 * Creates a new EntryMsg from its encoded form. 078 * 079 * @param in The byte array containing the encoded form of the message. 080 * @param version The protocol version to use to decode the msg 081 * @throws DataFormatException If the byte array does not contain a valid 082 * encoded form of the ServerStartMessage. 083 */ 084 EntryMsg(byte[] in, short version) throws DataFormatException 085 { 086 final ByteArrayScanner scanner = new ByteArrayScanner(in); 087 final byte msgType = scanner.nextByte(); 088 if (msgType != MSG_TYPE_ENTRY) 089 { 090 throw new DataFormatException("input is not a valid " 091 + getClass().getCanonicalName()); 092 } 093 this.senderID = scanner.nextIntUTF8(); 094 this.destination = scanner.nextIntUTF8(); 095 if (version >= ProtocolVersion.REPLICATION_PROTOCOL_V4) 096 { 097 this.msgId = scanner.nextIntUTF8(); 098 } 099 this.entryByteArray = scanner.remainingBytesZeroTerminated(); 100 } 101 102 /** 103 * Returns the entry bytes. 104 * @return The entry bytes. 105 */ 106 public byte[] getEntryBytes() 107 { 108 return entryByteArray; 109 } 110 111 /** {@inheritDoc} */ 112 @Override 113 public byte[] getBytes(short version) 114 { 115 final ByteArrayBuilder builder = new ByteArrayBuilder(); 116 builder.appendByte(MSG_TYPE_ENTRY); 117 builder.appendIntUTF8(senderID); 118 builder.appendIntUTF8(destination); 119 if (version >= ProtocolVersion.REPLICATION_PROTOCOL_V4) 120 { 121 builder.appendIntUTF8(msgId); 122 } 123 builder.appendZeroTerminatedByteArray(entryByteArray); 124 return builder.toByteArray(); 125 } 126 127 /** 128 * Return the msg id. 129 * @return The msg id. 130 */ 131 public int getMsgId() 132 { 133 return this.msgId; 134 } 135 136 /** 137 * Set the msg id. 138 * @param msgId The msg id. 139 */ 140 public void setMsgId(int msgId) 141 { 142 this.msgId = msgId; 143 } 144}