Java Code Examples for io.aeron.protocol.DataHeaderFlyweight#wrap()

The following examples show how to use io.aeron.protocol.DataHeaderFlyweight#wrap() . 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: SegmentInspector.java    From aeron with Apache License 2.0 6 votes vote down vote up
/**
 * Dump the contents of a segment file to a {@link PrintStream}.
 *
 * @param out              for the dumped contents.
 * @param messageDumpLimit for the number of bytes per message fragment to dump.
 * @param buffer           the wraps the segment file.
 */
public static void dumpSegment(final PrintStream out, final int messageDumpLimit, final UnsafeBuffer buffer)
{
    final DataHeaderFlyweight dataHeaderFlyweight = new DataHeaderFlyweight();
    final int length = buffer.capacity();
    int offset = 0;

    while (offset < length)
    {
        dataHeaderFlyweight.wrap(buffer, offset, length - offset);
        out.println(offset + ": " + dataHeaderFlyweight.toString());

        final int frameLength = dataHeaderFlyweight.frameLength();
        if (frameLength < DataHeaderFlyweight.HEADER_LENGTH)
        {
            break;
        }

        final int limit = min(frameLength - HEADER_LENGTH, messageDumpLimit);
        out.println(LogInspector.formatBytes(buffer, offset + HEADER_LENGTH, limit));
        offset += BitUtil.align(frameLength, FrameDescriptor.FRAME_ALIGNMENT);
    }
}
 
Example 2
Source File: ReceiverTest.java    From aeron with Apache License 2.0 6 votes vote down vote up
private void fillDataFrame(final DataHeaderFlyweight header, final int termOffset, final byte[] payload)
{
    header.wrap(dataBuffer);
    header
        .termOffset(termOffset)
        .termId(ACTIVE_TERM_ID)
        .streamId(STREAM_ID)
        .sessionId(SESSION_ID)
        .frameLength(DataHeaderFlyweight.HEADER_LENGTH + payload.length)
        .headerType(HeaderFlyweight.HDR_TYPE_DATA)
        .flags(DataHeaderFlyweight.BEGIN_AND_END_FLAGS)
        .version(HeaderFlyweight.CURRENT_VERSION);

    if (0 < payload.length)
    {
        dataBuffer.putBytes(header.dataOffset(), payload);
    }
}
 
Example 3
Source File: ReplaySessionTest.java    From aeron with Apache License 2.0 5 votes vote down vote up
private void recordFragment(
    final RecordingWriter recordingWriter,
    final UnsafeBuffer buffer,
    final DataHeaderFlyweight headerFlyweight,
    final Header header,
    final int offset,
    final int frameLength,
    final int message,
    final byte flags,
    final int type,
    final int sessionId)
{
    headerFlyweight.wrap(buffer, offset, HEADER_LENGTH);
    headerFlyweight
        .streamId(STREAM_ID)
        .sessionId(sessionId)
        .termOffset(offset)
        .termId(INITIAL_TERM_ID)
        .reservedValue(message)
        .headerType(type)
        .flags(flags)
        .frameLength(frameLength);

    buffer.setMemory(
        offset + HEADER_LENGTH,
        frameLength - HEADER_LENGTH,
        (byte)message);

    header.offset(offset);

    final int alignedLength = align(frameLength, FRAME_ALIGNMENT);
    recordingWriter.onBlock(buffer, offset, alignedLength, SESSION_ID, INITIAL_TERM_ID);
    recordingPosition += alignedLength;
}
 
Example 4
Source File: RecordingSessionTest.java    From aeron with Apache License 2.0 5 votes vote down vote up
@BeforeEach
public void before() throws Exception
{
    when(mockPosition.getWeak()).then((invocation) -> positionLong);
    when(mockPosition.get()).then((invocation) -> positionLong);
    doAnswer(
        (invocation) ->
        {
            positionLong = invocation.getArgument(0);
            return null;
        })
        .when(mockPosition).setOrdered(anyLong());

    termFile = File.createTempFile("test.rec", "sourceIdentity");

    mockLogBufferChannel = FileChannel.open(termFile.toPath(), CREATE, READ, WRITE);
    mockLogBufferMapped = new UnsafeBuffer(
        mockLogBufferChannel.map(FileChannel.MapMode.READ_WRITE, 0, TERM_BUFFER_LENGTH));

    final DataHeaderFlyweight headerFlyweight = new DataHeaderFlyweight();
    headerFlyweight.wrap(mockLogBufferMapped, TERM_OFFSET, DataHeaderFlyweight.HEADER_LENGTH);
    headerFlyweight
        .termOffset(TERM_OFFSET)
        .sessionId(SESSION_ID)
        .streamId(STREAM_ID)
        .headerType(DataHeaderFlyweight.HDR_TYPE_DATA)
        .frameLength(RECORDED_BLOCK_LENGTH);

    context = new Archive.Context()
        .segmentFileLength(SEGMENT_LENGTH)
        .archiveDir(archiveDir)
        .epochClock(epochClock);
}