/*
 * Copyright 2014 - 2015 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.driver;

/**
 * Tracks a aeron client interest registration in a {@link NetworkPublication}.
 */
public class PublicationLink implements DriverManagedResource
{
    private final long registrationId;
    private final long unblockTimeoutNs;
    private final DriverManagedResource publication;
    private final AeronClient client;
    private final SystemCounters systemCounters;

    private boolean reachedEndOfLife = false;
    private long previousConsumerPosition;
    private long previousProducerPosition;
    private long timeOfLastConsumerPositionChange;

    public PublicationLink(
        final long registrationId,
        final DriverManagedResource publication,
        final AeronClient client,
        final long now,
        final long unblockTimeoutNs,
        final SystemCounters systemCounters)
    {
        this.registrationId = registrationId;
        this.publication = publication;
        this.client = client;
        this.publication.incRef();
        this.previousConsumerPosition = publication.consumerPosition();
        this.previousProducerPosition = publication.producerPosition();
        this.timeOfLastConsumerPositionChange = now;
        this.unblockTimeoutNs = unblockTimeoutNs;
        this.systemCounters = systemCounters;
    }

    public void close()
    {
        publication.decRef();
    }

    public long registrationId()
    {
        return registrationId;
    }

    public void onTimeEvent(final long time, final DriverConductor conductor)
    {
        if (client.hasTimedOut(time))
        {
            reachedEndOfLife = true;
        }

        final long producerPosition = publication.producerPosition();
        final long consumerPosition = publication.consumerPosition();

        if (producerPosition > consumerPosition &&
            consumerPosition == previousConsumerPosition &&
            producerPosition == previousProducerPosition)
        {
            if (time > (timeOfLastConsumerPositionChange + unblockTimeoutNs))
            {
                if (publication.unblockAtConsumerPosition())
                {
                    systemCounters.unblockedPublications().orderedIncrement();
                }
            }
        }
        else
        {
            // want movement of either consumer or producer to reset time?
            timeOfLastConsumerPositionChange = time;
        }

        previousConsumerPosition = consumerPosition;
        previousProducerPosition = producerPosition;
    }

    public boolean hasReachedEndOfLife()
    {
        return reachedEndOfLife;
    }

    public void timeOfLastStateChange(final long time)
    {
        // not set this way
    }

    public long timeOfLastStateChange()
    {
        return client.timeOfLastKeepalive();
    }

    public void delete()
    {
        close();
    }
}

@@ -1071,24 +1071,20 @@
 te long 
-previous
+last
 Consumer
@@ -1110,24 +1110,20 @@
 te long 
-previous
+last
 Producer
@@ -1606,24 +1606,20 @@
    this.
-previous
+last
 Consumer
@@ -1674,24 +1674,20 @@
    this.
-previous
+last
 Producer
@@ -2433,24 +2433,20 @@
 tion == 
-previous
+last
 Consumer
@@ -2489,24 +2489,20 @@
 tion == 
-previous
+last
 Producer
@@ -2983,24 +2983,20 @@
         
-previous
+last
 Consumer
@@ -3036,16 +3036,12 @@
     
-previous
+last
 Prod

Rename for consistency.