/*
 * Copyright 2014 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 uk.co.real_logic.aeron.util.command;

import uk.co.real_logic.aeron.util.Flyweight;

import static java.nio.ByteOrder.LITTLE_ENDIAN;
import static uk.co.real_logic.aeron.util.BitUtil.SIZE_OF_LONG;

/**
 * Control message for adding or removing a subscription.
 *
 * <p>
 * 0                   1                   2                   3
 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 * |                           Channel Id                          +
 * +---------------------------------------------------------------+
 * |      Destination Length       |   Destination               ...
 * |                                                             ...
 * +---------------------------------------------------------------+
 */
public class SubscriptionMessageFlyweight extends Flyweight
{
    private static final int CHANNEL_ID_OFFSET = 0;
    private static final int DESTINATION_OFFSET = CHANNEL_ID_OFFSET + SIZE_OF_LONG;

    private int lengthOfDestination;

    /**
     * Get the channel id
     *
     * @return the channel id
     */
    public long channelId()
    {
        return uint32Get(offset() + CHANNEL_ID_OFFSET, LITTLE_ENDIAN);
    }

    /**
     * Set the channel id
     *
     * @param value the channel id
     */
    public SubscriptionMessageFlyweight channelId(long value)
    {
        uint32Put(offset() + CHANNEL_ID_OFFSET, value, LITTLE_ENDIAN);
        return this;
    }

    /**
     * Get the destination field
     *
     * @return destination field
     */
    public String destination()
    {
        return stringGet(offset() + DESTINATION_OFFSET, LITTLE_ENDIAN);
    }

    /**
     * Set destination field
     *
     * @param destination field value
     * @return flyweight
     */
    public SubscriptionMessageFlyweight destination(final String destination)
    {
        lengthOfDestination = stringPut(offset() + DESTINATION_OFFSET, destination, LITTLE_ENDIAN);
        return this;
    }

    public int length()
    {
        return SIZE_OF_LONG + lengthOfDestination;
    }
}

@@ -638,55 +638,8 @@
 d;%0A%0A
-import uk.co.real_logic.aeron.util.Flyweight;%0A%0A
 impo
@@ -1020,16 +1020,154 @@
 +-+-+-+%0A
+ * %7C                         Correlation ID                        %7C%0A * +---------------------------------------------------------------+%0A
  * %7C    
@@ -1225,17 +1225,17 @@
         
-+
+%7C
 %0A * +---
@@ -1557,16 +1557,33 @@
 extends 
+CorrelatedMessage
 Flyweigh
@@ -1635,17 +1635,58 @@
 FFSET = 
-0
+CORRELATION_ID_FIELD_OFFSET + SIZE_OF_LONG
 ;%0A    pr
@@ -1813,27 +1813,30 @@
  /**%0A     * 
-G
+r
 et
+urn
  the channel
@@ -2262,19 +2262,22 @@
 %0A     * 
-G
+r
 et
+urn
  the des
@@ -2842,16 +2842,31 @@
 F_LONG +
+ SIZE_OF_LONG +
  lengthO

add correlation ids to subscription messages