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

import java.io.IOException;
import java.io.InterruptedIOException;
import java.net.URI;

import org.activeio.Channel;
import org.activeio.FilterSynchChannel;
import org.activeio.FilterSynchChannelServer;
import org.activeio.Packet;
import org.activeio.SynchChannel;
import org.activeio.SynchChannelFactory;
import org.activeio.SynchChannelServer;

/**
 * Makes all the channels produced by another [@see org.activeio.SynchChannelFactory}
 * have write operations that have built in delays for testing. 
 * 
 * @version $Revision$
 */
public class SlowWriteSynchChannelFactory implements SynchChannelFactory {
    
    final SynchChannelFactory next;
    private final int maxPacketSize;
    private final long packetDelay;

    public SlowWriteSynchChannelFactory(final SynchChannelFactory next, int maxPacketSize, long packetDelay) {
        this.next = next;
        this.maxPacketSize = maxPacketSize;
        this.packetDelay = packetDelay;
    }
    
    class SlowWriteSynchChannel extends FilterSynchChannel {
        public SlowWriteSynchChannel(SynchChannel next) {
            super(next);
        }
        public void write(Packet packet) throws IOException {
            packet = packet.slice();
            while(packet.hasRemaining()) {
                int size = Math.max(maxPacketSize, packet.remaining());
                packet.position(size);
                Packet remaining = packet.slice();
                packet.flip();
                Packet data = packet.slice();
                super.write(data);
                packet = remaining;
                try {
                    Thread.sleep(packetDelay);
                } catch (InterruptedException e) {
                    throw new InterruptedIOException();
                }
            }
        }
    }

    class SlowWriteSynchChannelServer extends FilterSynchChannelServer {
        public SlowWriteSynchChannelServer(SynchChannelServer next) {
            super(next);
        }
        public Channel accept(long timeout) throws IOException {
            Channel channel = super.accept(timeout);
            if( channel != null ) {
                channel =  new SlowWriteSynchChannel((SynchChannel) channel);
            }
            return channel;
        }
    }
    
    public SynchChannelServer bindSynchChannel(URI location) throws IOException {
        return next.bindSynchChannel(location);
    }
    
    public SynchChannel openSynchChannel(URI location) throws IOException {
        return new SlowWriteSynchChannel(next.openSynchChannel(location));
    }
    
}
