Search in sources :

Example 41 with UnsafeBuffer

use of org.agrona.concurrent.UnsafeBuffer in project Aeron by real-logic.

the class SimplePublisher method main.

public static void main(final String[] args) throws Exception {
    // Allocate enough buffer size to hold maximum message length
    // The UnsafeBuffer class is part of the Agrona library and is used for efficient buffer management
    final UnsafeBuffer buffer = new UnsafeBuffer(BufferUtil.allocateDirectAligned(512, BitUtil.CACHE_LINE_LENGTH));
    // The channel (an endpoint identifier) to send the message to
    final String channel = "aeron:udp?endpoint=localhost:40123";
    // A unique identifier for a stream within a channel. Stream ID 0 is reserved
    // for internal use and should not be used by applications.
    final int streamId = 10;
    System.out.println("Publishing to " + channel + " on stream Id " + streamId);
    // Create a context, needed for client connection to media driver
    // A separate media driver process needs to be running prior to starting this application
    final Aeron.Context ctx = new Aeron.Context();
    // AutoCloseable, and will automatically clean up resources when this try block is finished.
    try (Aeron aeron = Aeron.connect(ctx);
        Publication publication = aeron.addPublication(channel, streamId)) {
        final String message = "Hello World! ";
        final byte[] messageBytes = message.getBytes();
        buffer.putBytes(0, messageBytes);
        // Try to publish the buffer. 'offer' is a non-blocking call.
        // If it returns less than 0, the message was not sent, and the offer should be retried.
        final long result = publication.offer(buffer, 0, messageBytes.length);
        if (result < 0L) {
            if (result == Publication.BACK_PRESSURED) {
                System.out.println(" Offer failed due to back pressure");
            } else if (result == Publication.NOT_CONNECTED) {
                System.out.println(" Offer failed because publisher is not connected to subscriber");
            } else if (result == Publication.ADMIN_ACTION) {
                System.out.println("Offer failed because of an administration action in the system");
            } else if (result == Publication.CLOSED) {
                System.out.println("Offer failed publication is closed");
            } else {
                System.out.println(" Offer failed due to unknown reason");
            }
        } else {
            System.out.println(" yay !!");
        }
        System.out.println("Done sending.");
    }
}
Also used : Publication(io.aeron.Publication) UnsafeBuffer(org.agrona.concurrent.UnsafeBuffer) Aeron(io.aeron.Aeron)

Example 42 with UnsafeBuffer

use of org.agrona.concurrent.UnsafeBuffer in project deeplearning4j by deeplearning4j.

the class SbeStatsInitializationReport method decode.

@Override
public void decode(byte[] decode) {
    MutableDirectBuffer buffer = new UnsafeBuffer(decode);
    decode(buffer);
}
Also used : MutableDirectBuffer(org.agrona.MutableDirectBuffer) UnsafeBuffer(org.agrona.concurrent.UnsafeBuffer)

Example 43 with UnsafeBuffer

use of org.agrona.concurrent.UnsafeBuffer in project deeplearning4j by deeplearning4j.

the class SbeStorageMetaData method decode.

@Override
public void decode(byte[] decode) {
    MutableDirectBuffer buffer = new UnsafeBuffer(decode);
    decode(buffer);
}
Also used : MutableDirectBuffer(org.agrona.MutableDirectBuffer) UnsafeBuffer(org.agrona.concurrent.UnsafeBuffer)

Example 44 with UnsafeBuffer

use of org.agrona.concurrent.UnsafeBuffer in project deeplearning4j by deeplearning4j.

the class SbeStorageMetaData method encode.

@Override
public byte[] encode() {
    byte[] bytes = new byte[encodingLengthBytes()];
    MutableDirectBuffer buffer = new UnsafeBuffer(bytes);
    encode(buffer);
    return bytes;
}
Also used : MutableDirectBuffer(org.agrona.MutableDirectBuffer) UnsafeBuffer(org.agrona.concurrent.UnsafeBuffer)

Example 45 with UnsafeBuffer

use of org.agrona.concurrent.UnsafeBuffer in project Aeron by real-logic.

the class StreamInstanceArchiveFragmentReader method poll.

int poll(final FragmentHandler fragmentHandler, final int fromTermId, final int fromTermOffset, final long replayLength) throws IOException {
    int polled = 0;
    long transmitted = 0;
    int archiveFileIndex = ArchiveFileUtil.archiveDataFileIndex(initialTermId, termBufferLength, fromTermId);
    final int archiveOffset = ArchiveFileUtil.archiveOffset(fromTermOffset, fromTermId, initialTermId, termBufferLength);
    final String archiveDataFileName = ArchiveFileUtil.archiveDataFileName(streamInstanceId, archiveFileIndex);
    final File archiveDataFile = new File(archiveFolder, archiveDataFileName);
    if (!archiveDataFile.exists()) {
        throw new IllegalStateException(archiveDataFile.getAbsolutePath() + " not found");
    }
    RandomAccessFile currentDataFile = null;
    FileChannel currentDataChannel = null;
    UnsafeBuffer termMappedUnsafeBuffer = null;
    try {
        currentDataFile = new RandomAccessFile(archiveDataFile, "r");
        currentDataChannel = currentDataFile.getChannel();
        int archiveTermStartOffset = archiveOffset - fromTermOffset;
        termMappedUnsafeBuffer = new UnsafeBuffer(currentDataChannel.map(FileChannel.MapMode.READ_ONLY, archiveTermStartOffset, termBufferLength));
        int fragmentOffset = archiveOffset & (termBufferLength - 1);
        while (true) {
            final Header fragmentHeader = new Header(initialTermId, Integer.numberOfLeadingZeros(termBufferLength));
            fragmentHeader.buffer(termMappedUnsafeBuffer);
            // read to end of term or requested data
            while (fragmentOffset < termBufferLength && transmitted < replayLength) {
                fragmentHeader.offset(fragmentOffset);
                final int frameLength = fragmentHeader.frameLength();
                polled += readFragment(fragmentHandler, termMappedUnsafeBuffer, fragmentOffset, frameLength, fragmentHeader);
                final int alignedLength = BitUtil.align(frameLength, FRAME_ALIGNMENT);
                transmitted += alignedLength;
                fragmentOffset += alignedLength;
            }
            if (transmitted >= replayLength) {
                return polled;
            }
            fragmentOffset = 0;
            archiveTermStartOffset += termBufferLength;
            if (archiveTermStartOffset == ArchiveFileUtil.ARCHIVE_FILE_SIZE) {
                // roll file
                archiveTermStartOffset = 0;
                archiveFileIndex++;
                final String archiveDataFileNameN = ArchiveFileUtil.archiveDataFileName(streamInstanceId, archiveFileIndex);
                final File archiveDataFileN = new File(archiveFolder, archiveDataFileNameN);
                if (!archiveDataFileN.exists()) {
                    throw new IllegalStateException(archiveDataFileN.getAbsolutePath() + " not found");
                }
                IoUtil.unmap(termMappedUnsafeBuffer.byteBuffer());
                CloseHelper.quietClose(currentDataChannel);
                CloseHelper.quietClose(currentDataFile);
                currentDataFile = new RandomAccessFile(archiveDataFileN, "r");
                currentDataChannel = currentDataFile.getChannel();
            } else {
                IoUtil.unmap(termMappedUnsafeBuffer.byteBuffer());
            }
            // roll term
            termMappedUnsafeBuffer.wrap(currentDataChannel.map(FileChannel.MapMode.READ_ONLY, archiveTermStartOffset, termBufferLength));
        }
    } finally {
        if (termMappedUnsafeBuffer != null) {
            IoUtil.unmap(termMappedUnsafeBuffer.byteBuffer());
        }
        CloseHelper.quietClose(currentDataChannel);
        CloseHelper.quietClose(currentDataFile);
    }
}
Also used : FileChannel(java.nio.channels.FileChannel) UnsafeBuffer(org.agrona.concurrent.UnsafeBuffer)

Aggregations

UnsafeBuffer (org.agrona.concurrent.UnsafeBuffer)78 Test (org.junit.Test)42 StatusMessageFlyweight (io.aeron.protocol.StatusMessageFlyweight)8 MediaDriver (io.aeron.driver.MediaDriver)7 MutableDirectBuffer (org.agrona.MutableDirectBuffer)6 Header (io.aeron.logbuffer.Header)5 InOrder (org.mockito.InOrder)5 MappedByteBuffer (java.nio.MappedByteBuffer)4 Theory (org.junit.experimental.theories.Theory)4 ReceiveChannelEndpoint (io.aeron.driver.media.ReceiveChannelEndpoint)3 File (java.io.File)3 Date (java.util.Date)3 DirectBuffer (org.agrona.DirectBuffer)3 Before (org.junit.Before)3 DataPoint (org.junit.experimental.theories.DataPoint)3 SendChannelEndpoint (io.aeron.driver.media.SendChannelEndpoint)2 BufferClaim (io.aeron.logbuffer.BufferClaim)2 FragmentHandler (io.aeron.logbuffer.FragmentHandler)2 PrintStream (java.io.PrintStream)2 InetSocketAddress (java.net.InetSocketAddress)2