| | |
| | | * |
| | | * |
| | | * Copyright 2006-2008 Sun Microsystems, Inc. |
| | | * Portions copyright 2013 ForgeRock AS. |
| | | * Portions copyright 2013-2014 ForgeRock AS. |
| | | */ |
| | | package org.opends.server.replication.protocol; |
| | | |
| | | import java.io.UnsupportedEncodingException; |
| | | import java.util.zip.DataFormatException; |
| | | |
| | | |
| | | /** |
| | | * This message is used by LDAP server or by Replication Servers to |
| | | * update the send window of the remote entities. |
| | |
| | | { |
| | | private final int numAck; |
| | | |
| | | |
| | | /** |
| | | * Create a new WindowMsg. |
| | | * |
| | |
| | | * @throws DataFormatException If the byte array does not contain a valid |
| | | * encoded form of the WindowMsg. |
| | | */ |
| | | public WindowMsg(byte[] in) throws DataFormatException |
| | | WindowMsg(byte[] in) throws DataFormatException |
| | | { |
| | | /* The WindowMsg is encoded in the form : |
| | | * <numAck> |
| | | */ |
| | | try |
| | | final ByteArrayScanner scanner = new ByteArrayScanner(in); |
| | | final byte msgType = scanner.nextByte(); |
| | | if (msgType != MSG_TYPE_WINDOW) |
| | | { |
| | | /* first byte is the type */ |
| | | if (in[0] != MSG_TYPE_WINDOW) |
| | | throw new DataFormatException("input is not a valid Window Message"); |
| | | int pos = 1; |
| | | |
| | | /* |
| | | * read the number of acks contained in this message. |
| | | * first calculate the length then construct the string |
| | | */ |
| | | int length = getNextLength(in, pos); |
| | | String numAckStr = new String(in, pos, length, "UTF-8"); |
| | | pos += length +1; |
| | | numAck = Integer.parseInt(numAckStr); |
| | | } catch (UnsupportedEncodingException e) |
| | | { |
| | | throw new DataFormatException("UTF-8 is not supported by this jvm."); |
| | | throw new DataFormatException("input is not a valid Window Message"); |
| | | } |
| | | |
| | | numAck = scanner.nextIntUTF8(); |
| | | } |
| | | |
| | | /** |
| | | * {@inheritDoc} |
| | | */ |
| | | /** {@inheritDoc} */ |
| | | @Override |
| | | public byte[] getBytes(short protocolVersion) |
| | | { |
| | | /* |
| | | * WindowMsg contains. |
| | | * <numAck> |
| | | */ |
| | | try { |
| | | byte[] byteNumAck = String.valueOf(numAck).getBytes("UTF-8"); |
| | | |
| | | int length = 1 + byteNumAck.length + 1; |
| | | |
| | | byte[] resultByteArray = new byte[length]; |
| | | |
| | | /* put the type of the operation */ |
| | | resultByteArray[0] = MSG_TYPE_WINDOW; |
| | | int pos = 1; |
| | | |
| | | pos = addByteArray(byteNumAck, resultByteArray, pos); |
| | | |
| | | return resultByteArray; |
| | | } |
| | | catch (UnsupportedEncodingException e) |
| | | { |
| | | return null; |
| | | } |
| | | final ByteArrayBuilder builder = new ByteArrayBuilder(); |
| | | builder.append(MSG_TYPE_WINDOW); |
| | | builder.appendUTF8(numAck); |
| | | return builder.toByteArray(); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * Get the number of message acknowledged by the Window Message. |
| | | * |
| | |
| | | return numAck; |
| | | } |
| | | |
| | | /** |
| | | * {@inheritDoc} |
| | | */ |
| | | /** {@inheritDoc} */ |
| | | @Override |
| | | public String toString() |
| | | { |