Java Code Examples for io.aeron.Publication#offer()

The following examples show how to use io.aeron.Publication#offer() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FileSender.java    From aeron with Apache License 2.0 6 votes vote down vote up
private static void sendFileCreate(
    final Publication publication, final long correlationId, final int length, final String filename)
{
    final ExpandableArrayBuffer buffer = new ExpandableArrayBuffer();

    buffer.putInt(VERSION_OFFSET, VERSION, LITTLE_ENDIAN);
    buffer.putInt(TYPE_OFFSET, FILE_CREATE_TYPE, LITTLE_ENDIAN);
    buffer.putLong(CORRELATION_ID_OFFSET, correlationId, LITTLE_ENDIAN);
    buffer.putLong(FILE_LENGTH_OFFSET, length, LITTLE_ENDIAN);

    final int msgLength = FILE_NAME_OFFSET + buffer.putStringUtf8(FILE_NAME_OFFSET, filename);

    long result;
    while ((result = publication.offer(buffer, 0, msgLength)) < 0)
    {
        checkResult(result);
        Thread.yield();
    }
}
 
Example 2
Source File: Common.java    From aeron with Apache License 2.0 5 votes vote down vote up
static void offer(final Publication publication, final int count, final String prefix)
{
    final ExpandableArrayBuffer buffer = new ExpandableArrayBuffer();

    for (int i = 0; i < count; i++)
    {
        final int length = buffer.putStringWithoutLengthAscii(0, prefix + i);

        while (publication.offer(buffer, 0, length) <= 0)
        {
            Tests.yield();
        }
    }
}
 
Example 3
Source File: Common.java    From aeron with Apache License 2.0 5 votes vote down vote up
static void offerToPosition(final Publication publication, final String prefix, final long minimumPosition)
{
    final ExpandableArrayBuffer buffer = new ExpandableArrayBuffer();

    for (int i = 0; publication.position() < minimumPosition; i++)
    {
        final int length = buffer.putStringWithoutLengthAscii(0, prefix + i);

        while (publication.offer(buffer, 0, length) <= 0)
        {
            Tests.yield();
        }
    }
}
 
Example 4
Source File: ArchiveCreator.java    From aeron with Apache License 2.0 5 votes vote down vote up
private static void offerToPosition(final Publication publication, final long minimumPosition)
{
    final ExpandableArrayBuffer buffer = new ExpandableArrayBuffer();

    for (int i = 0; publication.position() < minimumPosition; i++)
    {
        final int length = buffer.putStringWithoutLengthAscii(0, MESSAGE_PREFIX + i);

        while (publication.offer(buffer, 0, length) <= 0)
        {
            Thread.yield();
            checkInterruptStatus();
        }
    }
}
 
Example 5
Source File: EmbeddedPingPong.java    From aeron with Apache License 2.0 5 votes vote down vote up
private static void roundTripMessages(
    final FragmentHandler fragmentHandler,
    final Publication pingPublication,
    final Subscription pongSubscription,
    final long numMessages)
{
    while (!pongSubscription.isConnected())
    {
        Thread.yield();
    }

    final Image image = pongSubscription.imageAtIndex(0);

    for (long i = 0; i < numMessages; i++)
    {
        long offeredPosition;

        do
        {
            OFFER_BUFFER.putLong(0, System.nanoTime());
        }
        while ((offeredPosition = pingPublication.offer(OFFER_BUFFER, 0, MESSAGE_LENGTH, null)) < 0L);

        PONG_HANDLER_IDLE_STRATEGY.reset();
        do
        {
            while (image.poll(fragmentHandler, FRAGMENT_COUNT_LIMIT) <= 0)
            {
                PONG_HANDLER_IDLE_STRATEGY.idle();
            }
        }
        while (image.position() < offeredPosition);
    }
}
 
Example 6
Source File: ArchiveTest.java    From aeron with Apache License 2.0 4 votes vote down vote up
@Test
public void shouldRecoverRecordingWithNonZeroStartPosition()
{
    final MediaDriver.Context driverCtx = new MediaDriver.Context()
        .dirDeleteOnStart(true)
        .threadingMode(ThreadingMode.SHARED);
    final Context archiveCtx = new Context().threadingMode(SHARED);

    long resultingPosition;
    final int initialPosition = DataHeaderFlyweight.HEADER_LENGTH * 9;
    final long recordingId;

    try (ArchivingMediaDriver ignore = ArchivingMediaDriver.launch(driverCtx.clone(), archiveCtx.clone());
        AeronArchive archive = AeronArchive.connect())
    {
        final int termLength = 128 * 1024;
        final int initialTermId = 29;

        final String channel = new ChannelUriStringBuilder()
            .media(CommonContext.IPC_MEDIA)
            .initialPosition(initialPosition, initialTermId, termLength)
            .build();

        final Publication publication = archive.addRecordedExclusivePublication(channel, 1);
        final DirectBuffer buffer = new UnsafeBuffer("Hello World".getBytes(StandardCharsets.US_ASCII));

        while ((resultingPosition = publication.offer(buffer)) <= 0)
        {
            Tests.yield();
        }

        final Aeron aeron = archive.context().aeron();

        int counterId;
        final int sessionId = publication.sessionId();
        final CountersReader countersReader = aeron.countersReader();
        while (Aeron.NULL_VALUE == (counterId = RecordingPos.findCounterIdBySession(countersReader, sessionId)))
        {
            Tests.yield();
        }

        recordingId = RecordingPos.getRecordingId(countersReader, counterId);

        while (countersReader.getCounterValue(counterId) < resultingPosition)
        {
            Tests.yield();
        }
    }

    try (Catalog catalog = openCatalog(archiveCtx))
    {
        final Catalog.CatalogEntryProcessor catalogEntryProcessor =
            (headerEncoder, headerDecoder, descriptorEncoder, descriptorDecoder) ->
            descriptorEncoder.stopPosition(Aeron.NULL_VALUE);

        assertTrue(catalog.forEntry(recordingId, catalogEntryProcessor));
    }

    final Context archiveCtxClone = archiveCtx.clone();
    final MediaDriver.Context driverCtxClone = driverCtx.clone();
    try (ArchivingMediaDriver ignore = ArchivingMediaDriver.launch(driverCtxClone, archiveCtxClone);
        AeronArchive archive = AeronArchive.connect())
    {
        assertEquals(initialPosition, archive.getStartPosition(recordingId));
        assertEquals(resultingPosition, archive.getStopPosition(recordingId));
    }
    finally
    {
        archiveCtxClone.deleteDirectory();
        driverCtxClone.deleteDirectory();
    }
}
 
Example 7
Source File: IngressSessionDecorator.java    From aeron with Apache License 2.0 2 votes vote down vote up
/**
 * Non-blocking publish of a partial buffer containing a message plus session header to a cluster.
 * <p>
 * This version of the method will set the timestamp value in the header to {@link Aeron#NULL_VALUE}.
 *
 * @param publication to be offer to.
 * @param buffer      containing message.
 * @param offset      offset in the buffer at which the encoded message begins.
 * @param length      in bytes of the encoded message.
 * @return the same as {@link Publication#offer(DirectBuffer, int, int)}.
 */
public long offer(final Publication publication, final DirectBuffer buffer, final int offset, final int length)
{
    return publication.offer(headerBuffer, 0, HEADER_LENGTH, buffer, offset, length, null);
}