use of org.agrona.DirectBuffer in project simple-binary-encoding by real-logic.
the class CompositeElementsGenerationTest method shouldOtfDecodeCorrectly.
@Test
public void shouldOtfDecodeCorrectly() throws Exception {
final ByteBuffer encodedSchemaBuffer = ByteBuffer.allocate(SCHEMA_BUFFER_CAPACITY);
encodeSchema(encodedSchemaBuffer);
final ByteBuffer encodedMsgBuffer = ByteBuffer.allocate(MSG_BUFFER_CAPACITY);
encodeTestMessage(encodedMsgBuffer);
encodedSchemaBuffer.flip();
final Ir ir = decodeIr(encodedSchemaBuffer);
final DirectBuffer decodeBuffer = new UnsafeBuffer(encodedMsgBuffer);
final OtfHeaderDecoder otfHeaderDecoder = new OtfHeaderDecoder(ir.headerStructure());
assertThat(otfHeaderDecoder.getBlockLength(decodeBuffer, 0), is(22));
assertThat(otfHeaderDecoder.getSchemaId(decodeBuffer, 0), is(3));
assertThat(otfHeaderDecoder.getTemplateId(decodeBuffer, 0), is(1));
assertThat(otfHeaderDecoder.getSchemaVersion(decodeBuffer, 0), is(0));
final TokenListener mockTokenListener = mock(TokenListener.class);
OtfMessageDecoder.decode(decodeBuffer, otfHeaderDecoder.encodedLength(), MSG_ENCODER.sbeSchemaVersion(), MSG_ENCODER.sbeBlockLength(), ir.getMessage(MSG_ENCODER.sbeTemplateId()), mockTokenListener);
final InOrder inOrder = inOrder(mockTokenListener);
inOrder.verify(mockTokenListener).onBeginComposite(any(), any(), eq(2), eq(17));
inOrder.verify(mockTokenListener).onEnum(any(), eq(decodeBuffer), eq(8), any(), eq(3), eq(6), eq(0));
inOrder.verify(mockTokenListener).onEncoding(any(), eq(decodeBuffer), eq(9), any(), eq(0));
inOrder.verify(mockTokenListener).onBitSet(any(), eq(decodeBuffer), eq(10), any(), eq(8), eq(12), eq(0));
inOrder.verify(mockTokenListener).onBeginComposite(any(), any(), eq(13), eq(16));
inOrder.verify(mockTokenListener).onEncoding(any(), eq(decodeBuffer), eq(14), any(), eq(0));
inOrder.verify(mockTokenListener).onEncoding(any(), eq(decodeBuffer), eq(22), any(), eq(0));
inOrder.verify(mockTokenListener).onEndComposite(any(), any(), eq(13), eq(16));
inOrder.verify(mockTokenListener).onEndComposite(any(), any(), eq(2), eq(17));
}
use of org.agrona.DirectBuffer in project Aeron by real-logic.
the class RecordingPos method isActive.
/**
* Is the recording counter still active.
*
* @param counters to search within.
* @param counterId to search for.
* @param recordingId to confirm it is still the same value.
* @return true if the counter is still active otherwise false.
*/
public static boolean isActive(final CountersReader counters, final int counterId, final long recordingId) {
final DirectBuffer buffer = counters.metaDataBuffer();
final int recordOffset = CountersReader.metaDataOffset(counterId);
return counters.getCounterTypeId(counterId) == RECORDING_POSITION_TYPE_ID && buffer.getLong(recordOffset + KEY_OFFSET + RECORDING_ID_OFFSET) == recordingId && counters.getCounterState(counterId) == RECORD_ALLOCATED;
}
use of org.agrona.DirectBuffer in project Aeron by real-logic.
the class LocalSocketAddressStatus method findAddress.
/**
* Find the currently bound socket address for the channel. There is an expectation that only one exists when
* searching.
*
* @param countersReader for the connected driver.
* @param channelStatus value for the channel which aggregates the transports.
* @param channelStatusId identity of the counter for the channel which aggregates the transports.
* @return the endpoint representing the bound socket address or null if not found.
*/
public static String findAddress(final CountersReader countersReader, final long channelStatus, final int channelStatusId) {
String endpoint = null;
if (channelStatus == ChannelEndpointStatus.ACTIVE) {
final DirectBuffer buffer = countersReader.metaDataBuffer();
for (int i = 0, size = countersReader.maxCounterId(); i < size; i++) {
final int counterState = countersReader.getCounterState(i);
if (RECORD_ALLOCATED == counterState) {
if (countersReader.getCounterTypeId(i) == LOCAL_SOCKET_ADDRESS_STATUS_TYPE_ID) {
final int recordOffset = CountersReader.metaDataOffset(i);
final int keyIndex = recordOffset + CountersReader.KEY_OFFSET;
if (channelStatusId == buffer.getInt(keyIndex + CHANNEL_STATUS_ID_OFFSET) && ChannelEndpointStatus.ACTIVE == countersReader.getCounterValue(i)) {
final int length = buffer.getInt(keyIndex + LOCAL_SOCKET_ADDRESS_LENGTH_OFFSET);
if (length > 0) {
endpoint = buffer.getStringWithoutLengthAscii(keyIndex + LOCAL_SOCKET_ADDRESS_STRING_OFFSET, length);
}
break;
}
}
} else if (RECORD_UNUSED == counterState) {
break;
}
}
}
return endpoint;
}
use of org.agrona.DirectBuffer in project Aeron by real-logic.
the class SamplesUtil method mapCounters.
/**
* Map an existing CnC file.
*
* @param cncFileVersion to set as value of file.
* @return the {@link CountersReader} over the CnC file.
*/
public static CountersReader mapCounters(final MutableInteger cncFileVersion) {
final File cncFile = CommonContext.newDefaultCncFile();
System.out.println("Command `n Control file " + cncFile);
final MappedByteBuffer cncByteBuffer = mapExistingFileReadOnly(cncFile);
final DirectBuffer cncMetaData = createMetaDataBuffer(cncByteBuffer);
final int cncVersion = cncMetaData.getInt(cncVersionOffset(0));
cncFileVersion.set(cncVersion);
checkVersion(cncVersion);
return new CountersReader(createCountersMetaDataBuffer(cncByteBuffer, cncMetaData), createCountersValuesBuffer(cncByteBuffer, cncMetaData));
}
use of org.agrona.DirectBuffer in project Aeron by real-logic.
the class AuthenticationTest method launchService.
private void launchService(final MutableLong sessionId, final MutableReference<byte[]> encodedPrincipal, final AtomicLong msgCounter) {
final ClusteredService service = new StubClusteredService() {
private int counterValue = 0;
public void onSessionOpen(final ClientSession session, final long timestamp) {
sessionId.value = session.id();
encodedPrincipal.set(session.encodedPrincipal());
}
public void onSessionMessage(final ClientSession session, final long timestamp, final DirectBuffer buffer, final int offset, final int length, final Header header) {
assertEquals(counterValue, buffer.getInt(offset));
msgCounter.getAndIncrement();
counterValue++;
}
};
container = ClusteredServiceContainer.launch(new ClusteredServiceContainer.Context().clusteredService(service).terminationHook(ClusterTests.NOOP_TERMINATION_HOOK).errorHandler(ClusterTests.errorHandler(0)));
}
Aggregations