@Override public void wrap(DirectBuffer responseBuffer, int offset, int length) { messageHeaderDecoder.wrap(responseBuffer, 0); if (messageHeaderDecoder.templateId() != bodyDecoder.sbeTemplateId()) { throw new RuntimeException("Unexpected response from broker."); } bodyDecoder.wrap( responseBuffer, messageHeaderDecoder.encodedLength(), messageHeaderDecoder.blockLength(), messageHeaderDecoder.version()); final int errorDataLength = bodyDecoder.errorDataLength(); final int errorDataOffset = messageHeaderDecoder.encodedLength() + messageHeaderDecoder.blockLength() + ErrorResponseDecoder.errorDataHeaderLength(); errorData = responseBuffer.getStringWithoutLengthUtf8(errorDataOffset, errorDataLength); bodyDecoder.limit(errorDataOffset + errorDataLength); } }
@Override public void wrap(DirectBuffer responseBuffer, int offset, int length) { messageHeaderDecoder.wrap(responseBuffer, 0); if (messageHeaderDecoder.templateId() != bodyDecoder.sbeTemplateId()) { throw new RuntimeException("Unexpected response from broker."); } bodyDecoder.wrap( responseBuffer, messageHeaderDecoder.encodedLength(), messageHeaderDecoder.blockLength(), messageHeaderDecoder.version()); final int errorDataLength = bodyDecoder.errorDataLength(); final int errorDataOffset = messageHeaderDecoder.encodedLength() + messageHeaderDecoder.blockLength() + ErrorResponseDecoder.errorDataHeaderLength(); errorData = responseBuffer.getStringWithoutLengthUtf8(errorDataOffset, errorDataLength); bodyDecoder.limit(errorDataOffset + errorDataLength); } }
@Test public void shouldWriteResponse() { responseWriter.errorCode(ErrorCode.PARTITION_LEADER_MISMATCH).errorMessage("error message"); final UnsafeBuffer buf = new UnsafeBuffer(new byte[responseWriter.getLength()]); // when responseWriter.write(buf, 0); // then int offset = 0; messageHeaderDecoder.wrap(buf, offset); assertThat(messageHeaderDecoder.schemaId()).isEqualTo(responseDecoder.sbeSchemaId()); assertThat(messageHeaderDecoder.version()).isEqualTo(responseDecoder.sbeSchemaVersion()); assertThat(messageHeaderDecoder.templateId()).isEqualTo(responseDecoder.sbeTemplateId()); assertThat(messageHeaderDecoder.blockLength()).isEqualTo(responseDecoder.sbeBlockLength()); offset += messageHeaderDecoder.encodedLength(); responseDecoder.wrap( buf, offset, responseDecoder.sbeBlockLength(), responseDecoder.sbeSchemaVersion()); assertThat(responseDecoder.errorCode()).isEqualTo(ErrorCode.PARTITION_LEADER_MISMATCH); assertThat(responseDecoder.errorData()).isEqualTo("error message"); } }
@Test public void shouldWriteResponse() { responseWriter.errorCode(ErrorCode.PARTITION_LEADER_MISMATCH).errorMessage("error message"); final UnsafeBuffer buf = new UnsafeBuffer(new byte[responseWriter.getLength()]); // when responseWriter.write(buf, 0); // then int offset = 0; messageHeaderDecoder.wrap(buf, offset); assertThat(messageHeaderDecoder.schemaId()).isEqualTo(responseDecoder.sbeSchemaId()); assertThat(messageHeaderDecoder.version()).isEqualTo(responseDecoder.sbeSchemaVersion()); assertThat(messageHeaderDecoder.templateId()).isEqualTo(responseDecoder.sbeTemplateId()); assertThat(messageHeaderDecoder.blockLength()).isEqualTo(responseDecoder.sbeBlockLength()); offset += messageHeaderDecoder.encodedLength(); responseDecoder.wrap( buf, offset, responseDecoder.sbeBlockLength(), responseDecoder.sbeSchemaVersion()); assertThat(responseDecoder.errorCode()).isEqualTo(ErrorCode.PARTITION_LEADER_MISMATCH); assertThat(responseDecoder.errorData()).isEqualTo("error message"); } }