/**
 *
 * 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;

import java.io.IOException;


/**
 * A AsynchChannelFilter can be used as a filter between a {@see org.activeio.AsynchChannel}
 * and it's {@see org.activeio.ChannelConsumer}.  Most {@see org.activeio.AsynchChannel}
 * that are not directly accessing the network will extends the AsynchChannelFilter since they act as a
 * filter between the client and the network.  O 
 * 
 * @version $Revision$
 */
public class FilterAsynchChannel implements AsynchChannel, AsynchChannelListener {

    final protected AsynchChannel next;
    protected AsynchChannelListener channelListener;

    public FilterAsynchChannel(AsynchChannel next) {
        this.next = next;
    }

    /**
     */
    public void setAsynchChannelListener(AsynchChannelListener channelListener) {
        this.channelListener = channelListener;
        if (channelListener == null)
            next.setAsynchChannelListener(null);
        else
            next.setAsynchChannelListener(this);
    }

    /**
     * @see org.activeio.Channel#write(org.activeio.channel.Packet)
     */
    public void write(Packet packet) throws IOException {
        next.write(packet);
    }

    /**
     * @see org.activeio.Channel#flush()
     */
    public void flush() throws IOException {
        next.flush();
    }

    /**
     * @see org.activeio.Disposable#dispose()
     */
    public void dispose() {
        next.dispose();
    }

    /**
     * @see org.activeio.Service#start()
     * @throws IOException if the next channel has not been set.
     */
    public void start() throws IOException {
        if( next == null )
            throw new IOException("The next channel has not been set.");
        if( channelListener ==null )
            throw new IOException("The UpPacketListener has not been set.");
        next.start();
    }

    /**
     * @see org.activeio.Service#stop(long)
     */
    public void stop(long timeout) throws IOException {
        next.stop(timeout);
    }

    /**
     * @see org.activeio.AsynchChannelListener#onPacket(org.activeio.channel.Packet)
     */
    public void onPacket(Packet packet) {
        channelListener.onPacket(packet);
    }

    /**
     * @see org.activeio.AsynchChannelListener#onPacketError(org.activeio.channel.ChannelException)
     */
    public void onPacketError(IOException error) {
        channelListener.onPacketError(error);
    }

    /**
     * @return Returns the next.
     */
    public AsynchChannel getNext() {
        return next;
    }

    /**
     * @return Returns the packetListener.
     */
    public AsynchChannelListener getAsynchChannelListener() {
        return channelListener;
    }

    public Object narrow(Class target) {
        if( target.isAssignableFrom(getClass()) ) {
            return this;
        }
        return next.narrow(target);
    }  
    
    public String toString() {
        return next.toString();
    }
}