Search in sources :

Example 96 with DirectBuffer

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));
}
Also used : DirectBuffer(org.agrona.DirectBuffer) InOrder(org.mockito.InOrder) OtfHeaderDecoder(uk.co.real_logic.sbe.otf.OtfHeaderDecoder) Ir(uk.co.real_logic.sbe.ir.Ir) TokenListener(uk.co.real_logic.sbe.otf.TokenListener) UnsafeBuffer(org.agrona.concurrent.UnsafeBuffer) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 97 with DirectBuffer

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;
}
Also used : DirectBuffer(org.agrona.DirectBuffer)

Example 98 with DirectBuffer

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;
}
Also used : MutableDirectBuffer(org.agrona.MutableDirectBuffer) DirectBuffer(org.agrona.DirectBuffer)

Example 99 with DirectBuffer

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));
}
Also used : DirectBuffer(org.agrona.DirectBuffer) MappedByteBuffer(java.nio.MappedByteBuffer) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) CountersReader(org.agrona.concurrent.status.CountersReader)

Example 100 with DirectBuffer

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)));
}
Also used : DirectBuffer(org.agrona.DirectBuffer) Header(io.aeron.logbuffer.Header) ClientSession(io.aeron.cluster.service.ClientSession) StubClusteredService(io.aeron.test.cluster.StubClusteredService) ClusteredService(io.aeron.cluster.service.ClusteredService) StubClusteredService(io.aeron.test.cluster.StubClusteredService) ClusteredServiceContainer(io.aeron.cluster.service.ClusteredServiceContainer)

Aggregations

DirectBuffer (org.agrona.DirectBuffer)116 Header (io.aeron.logbuffer.Header)21 UnsafeBuffer (org.agrona.concurrent.UnsafeBuffer)21 MutableDirectBuffer (org.agrona.MutableDirectBuffer)20 File (java.io.File)19 MediaDriver (io.aeron.driver.MediaDriver)18 CountersReader (org.agrona.concurrent.status.CountersReader)15 ThreadingMode (io.aeron.driver.ThreadingMode)12 FragmentHandler (io.aeron.logbuffer.FragmentHandler)12 MappedByteBuffer (java.nio.MappedByteBuffer)12 ClusteredService (io.aeron.cluster.service.ClusteredService)10 InterruptAfter (io.aeron.test.InterruptAfter)10 Test (org.junit.jupiter.api.Test)10 ExtendWith (org.junit.jupiter.api.extension.ExtendWith)10 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)10 Publication (io.aeron.Publication)9 ClientSession (io.aeron.cluster.service.ClientSession)9 ClusteredServiceContainer (io.aeron.cluster.service.ClusteredServiceContainer)8 Assertions (org.junit.jupiter.api.Assertions)8 Mockito.mock (org.mockito.Mockito.mock)8