/**
 *
 * Copyright 2004 Hiram Chirino
 *
 *  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 org.activeio.filter;

import java.io.IOException;
import java.io.InterruptedIOException;

import org.activeio.FilterSynchChannel;
import org.activeio.Packet;
import org.activeio.SynchChannel;
import org.activeio.SynchChannelServer;

import EDU.oswego.cs.dl.util.concurrent.Mutex;
import EDU.oswego.cs.dl.util.concurrent.Sync;

/**
 * Used to synchronize concurrent access to a SynchChannel.  
 * 
 * Uses two different {@see EDU.oswego.cs.dl.util.concurrent.Sync} objects
 * for write and read operations.  All other operations such as {@see #stop(long)}
 * and {@see #stop} just do a normal java synchronization against the SynchornizedSynchChannel
 * object instance.
 * 
 */
public class SynchornizedSynchChannel extends FilterSynchChannel {

    private final Sync readLock;
    private final Sync writeLock;

    public SynchornizedSynchChannel(SynchChannel next) {
        this(next, new Mutex(), new Mutex());
    }
    
    public SynchornizedSynchChannel(SynchChannel next, Sync readLock, Sync writeLock) {
        super(next);
        this.readLock = readLock;
        this.writeLock = writeLock;
    }
    
    public Packet read(long timeout) throws IOException {
        try {            
            
            if( timeout==SynchChannelServer.WAIT_FOREVER_TIMEOUT ) {
                readLock.acquire();
            } else {
                long start = System.currentTimeMillis();
                if( !readLock.attempt(0) ) {
                    return null;
                }
                // Adjust the resulting timeout down to account for time taken to 
                // get the readLock.
                timeout = Math.max(0, timeout-(System.currentTimeMillis()-start));
            }
            
        } catch (InterruptedException e) {
            throw new InterruptedIOException(e.getMessage());            
        }
        
        try {
            return getNext().read(timeout);            
        } finally {
            readLock.release();
        }
    }
    
    public void write(Packet packet) throws IOException {
        try {                        
            writeLock.acquire();
        } catch (InterruptedException e) {
            throw new InterruptedIOException(e.getMessage());
        }
        try {
            getNext().write(packet);            
        } finally {
            writeLock.release();
        }
    }
    
    public void flush() throws IOException {
        try {                        
            writeLock.acquire();
        } catch (InterruptedException e) {
            throw new InterruptedIOException(e.getMessage());
        }
        try {
            getNext().flush();            
        } finally {
            writeLock.release();
        }
    }

    synchronized public void dispose() {
        super.dispose();
    }

    synchronized public Object narrow(Class target) {
        return super.narrow(target);
    }

    synchronized public void start() throws IOException {
        super.start();
    }

    synchronized public void stop(long timeout) throws IOException {
        super.stop(timeout);
    }
    
    public Sync getReadLock() {
        return readLock;
    }
    
    public Sync getWriteLock() {
        return writeLock;
    }
}
