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-2009 Sun Microsystems, Inc. 025 * Portions Copyright 2013-2014 ForgeRock AS. 026 */ 027package org.opends.server.replication.protocol; 028 029import java.util.zip.DataFormatException; 030 031import org.opends.server.replication.common.CSN; 032 033import static org.opends.server.replication.protocol.ByteArrayBuilder.*; 034import static org.opends.server.replication.protocol.ProtocolVersion.*; 035 036/** 037 * Class that define messages sent by a replication domain (DS) to the 038 * replication server to let the RS know the DS current change time. 039 */ 040public class ChangeTimeHeartbeatMsg extends ReplicationMsg 041{ 042 043 /** 044 * The CSN containing the change time. 045 */ 046 private final CSN csn; 047 048 /** 049 * Constructor of a Change Time Heartbeat message providing the change time 050 * value in a CSN. 051 * 052 * @param csn 053 * The provided CSN. 054 */ 055 public ChangeTimeHeartbeatMsg(CSN csn) 056 { 057 this.csn = csn; 058 } 059 060 /** 061 * Get a CSN with the transmitted change time. 062 * 063 * @return the CSN 064 */ 065 public CSN getCSN() 066 { 067 return csn; 068 } 069 070 /** 071 * Creates a message from a provided byte array. 072 * 073 * @param in 074 * The provided byte array. 075 * @param version 076 * The version of the protocol to use to decode the msg. 077 * @throws DataFormatException 078 * When an error occurs. 079 */ 080 public ChangeTimeHeartbeatMsg(byte[] in, short version) 081 throws DataFormatException 082 { 083 try 084 { 085 final ByteArrayScanner scanner = new ByteArrayScanner(in); 086 final byte msgType = scanner.nextByte(); 087 if (msgType != MSG_TYPE_CT_HEARTBEAT) 088 { 089 throw new DataFormatException("input is not a valid " 090 + getClass().getSimpleName() + " message: " + msgType); 091 } 092 093 csn = version >= REPLICATION_PROTOCOL_V7 094 ? scanner.nextCSN() 095 : scanner.nextCSNUTF8(); 096 097 if (!scanner.isEmpty()) 098 { 099 throw new DataFormatException( 100 "Did not expect to find more bytes to read for " 101 + getClass().getSimpleName() + " message."); 102 } 103 } 104 catch (RuntimeException e) 105 { 106 // Index out of bounds, bad format, etc. 107 throw new DataFormatException("byte[] is not a valid CT_HEARTBEAT msg"); 108 } 109 } 110 111 /** {@inheritDoc} */ 112 @Override 113 public byte[] getBytes(short protocolVersion) 114 { 115 if (protocolVersion < ProtocolVersion.REPLICATION_PROTOCOL_V7) 116 { 117 ByteArrayBuilder builder = new ByteArrayBuilder(bytes(1) + csnsUTF8(1)); 118 builder.appendByte(MSG_TYPE_CT_HEARTBEAT); 119 builder.appendCSNUTF8(csn); 120 return builder.toByteArray(); 121 } 122 123 final ByteArrayBuilder builder = new ByteArrayBuilder(bytes(1) + csns(1)); 124 builder.appendByte(MSG_TYPE_CT_HEARTBEAT); 125 builder.appendCSN(csn); 126 return builder.toByteArray(); 127 } 128 129 /** {@inheritDoc} */ 130 @Override 131 public String toString() 132 { 133 return getClass().getSimpleName() + ", csn=" + csn.toStringUI(); 134 } 135 136}