/*
 * Copyright 2014-2018 Real Logic Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.aeron.archive;

import io.aeron.Publication;
import io.aeron.archive.codecs.*;
import org.agrona.ExpandableDirectByteBuffer;
import org.agrona.concurrent.IdleStrategy;

class RecordingEventsProxy
{
    private final IdleStrategy idleStrategy;
    private final Publication recordingEventsPublication;
    private final ExpandableDirectByteBuffer outboundBuffer = new ExpandableDirectByteBuffer(512);
    private final MessageHeaderEncoder messageHeaderEncoder = new MessageHeaderEncoder();
    private final RecordingStartedEncoder recordingStartedEncoder = new RecordingStartedEncoder();
    private final RecordingProgressEncoder recordingProgressEncoder = new RecordingProgressEncoder();
    private final RecordingStoppedEncoder recordingStoppedEncoder = new RecordingStoppedEncoder();

    RecordingEventsProxy(final IdleStrategy idleStrategy, final Publication recordingEventsPublication)
    {
        this.idleStrategy = idleStrategy;
        this.recordingEventsPublication = recordingEventsPublication;
    }

    void started(
        final long recordingId,
        final long startPosition,
        final int sessionId,
        final int streamId,
        final String channel,
        final String sourceIdentity)
    {
        recordingStartedEncoder
            .wrapAndApplyHeader(outboundBuffer, 0, messageHeaderEncoder)
            .recordingId(recordingId)
            .startPosition(startPosition)
            .sessionId(sessionId)
            .streamId(streamId)
            .channel(channel)
            .sourceIdentity(sourceIdentity);

        send(recordingStartedEncoder.encodedLength());
    }

    void progress(final long recordingId, final long startPosition, final long position)
    {
        recordingProgressEncoder
            .wrapAndApplyHeader(outboundBuffer, 0, messageHeaderEncoder)
            .recordingId(recordingId)
            .startPosition(startPosition)
            .position(position);

        send(recordingProgressEncoder.encodedLength());
    }

    void stopped(final long recordingId, final long startPosition, final long stopPosition)
    {
        recordingStoppedEncoder
            .wrapAndApplyHeader(outboundBuffer, 0, messageHeaderEncoder)
            .recordingId(recordingId)
            .startPosition(startPosition)
            .stopPosition(stopPosition);

        send(recordingStoppedEncoder.encodedLength());
    }

    private void send(final int length)
    {
        final int fullLength = MessageHeaderEncoder.ENCODED_LENGTH + length;
        while (true)
        {
            // TODO: Under back pressure it should drop sends and then do an update on timeout to avoid tail loss.
            final long result = recordingEventsPublication.offer(outboundBuffer, 0, fullLength);
            if (result > 0 || result == Publication.NOT_CONNECTED)
            {
                idleStrategy.reset();
                break;
            }

            if (result == Publication.CLOSED || result == Publication.MAX_POSITION_EXCEEDED)
            {
                throw new IllegalStateException();
            }

            idleStrategy.idle();
        }
    }
}

@@ -707,34 +707,29 @@
 a.Expandable
-DirectByte
+Array
 Buffer;%0Aimpo
@@ -924,34 +924,29 @@
 l Expandable
-DirectByte
+Array
 Buffer outbo
@@ -975,18 +975,13 @@
 able
-DirectByte
+Array
 Buff
@@ -3548,55 +3548,8 @@
 OSED
- %7C%7C result == Publication.MAX_POSITION_EXCEEDED
 )%0A  
@@ -3608,16 +3608,243 @@
 ception(
+%22Recording events publication is closed%22);%0A            %7D%0A%0A            if (result == Publication.MAX_POSITION_EXCEEDED)%0A            %7B%0A                throw new IllegalStateException(%22Recording events publication at max position%22
 );%0A     

Change recording events proxy to use an array buffer rather than direct and to be more precise when the offer fails in the exception.