/*
 * Copyright 2005 The Apache Software Foundation.
 *
 * 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.apache.jmeter.util;

import java.io.FilterInputStream;
import java.io.FilterOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;

/**
 * "Slow" socket implementation to emulate dial-up modems etc
 */
public class SlowSocket extends Socket {

    private final int CPS; // Characters per second to emulate
    
    /**
     * 
     * @param cps characters per second
     * @param host hostname
     * @param port port
     * @param localAddr local address
     * @param localPort local port
     * 
     * @throws IOException
     * @throws IllegalArgumentException if cps <=0
     */
    public SlowSocket(int cps, String host, int port, InetAddress localAddr, int localPort) throws IOException {
        super(host, port, localAddr, localPort);
        if (cps <=0) {
            throw new IllegalArgumentException("Speed (cps) <= 0");
        }
        CPS=cps;
    }

    /**
     * 
     * @param cps characters per second
     * @param host hostname
     * @param port port
     * 
     * @throws UnknownHostException
     * @throws IOException
     * @throws IllegalArgumentException if cps <=0
     */
    public SlowSocket(int cps, String host, int port) throws UnknownHostException, IOException {
        super(host, port);
        if (cps <=0) {
            throw new IllegalArgumentException("Speed (cps) <= 0");
        }
        CPS=cps;
    }

    // Override so we can intercept the stream
    public OutputStream getOutputStream() throws IOException {
        return new SlowOutputStream(super.getOutputStream());
    }
    
    // Override so we can intercept the stream
    public InputStream getInputStream() throws IOException {
        return new SlowInputStream(super.getInputStream());
    }

    private void pause(int bytes){
        try {
            Thread.sleep(bytes*1000/CPS);
        } catch (InterruptedException ignored) {
        }
    }
    
    class SlowInputStream extends FilterInputStream {

        public SlowInputStream(InputStream in) {
            super(in);
        }

        public int read() throws IOException {
            pause(1);
            return in.read();
        }

        public int read(byte[] b, int off, int len) throws IOException {
            pause(len);
            return in.read(b, off, len);
        }

    }
    
    class SlowOutputStream extends FilterOutputStream {

        public SlowOutputStream(OutputStream out) {
            super(out);
        }

        public void write(byte[] b, int off, int len) throws IOException {
            pause(len);
            out.write(b, off, len);
        }

        public void write(int b) throws IOException {
            pause(1);
            out.write(b);
        }
    }
}
@@ -2280,32 +2280,36 @@
 etOutputStream()
+,CPS
 );%0A    %7D%0A    %0A  
@@ -2471,16 +2471,20 @@
 Stream()
+,CPS
 );%0A    %7D
@@ -2493,38 +2493,381 @@
     
-private void pause(int bytes)%7B
+// Conversions for milli and nano seconds%0A    private static final int MS_PER_SEC = 1000;%0A    private static final int NS_PER_SEC = 1000000000;%0A    private static final int NS_PER_MS  = NS_PER_SEC/MS_PER_SEC;%0A    %0A    private static void pause(int bytes, int cps)%7B%0A    %09long sleepMS = (bytes*MS_PER_SEC)/cps;%0A    %09int  sleepNS = (int)(((bytes*MS_PER_SEC)/cps) %25 NS_PER_MS);
 %0A   
@@ -2906,21 +2906,22 @@
 eep(
-bytes*1000/CP
+sleepMS,sleepN
 S);%0A
@@ -2986,32 +2986,40 @@
 %0A    %7D%0A    %0A    
+private 
 class SlowInputS
@@ -3045,32 +3045,66 @@
 rInputStream %7B%0A%0A
+    %09private final int CPS;%0A    %09%0A
         public S
@@ -3132,16 +3132,25 @@
 tream in
+, int cps
 ) %7B%0A    
@@ -3164,24 +3164,45 @@
  super(in);%0A
+            CPS=cps;%0A
         %7D%0A%0A 
@@ -3258,32 +3258,36 @@
          pause(1
+,CPS
 );%0A            r
@@ -3400,32 +3400,36 @@
        pause(len
+,CPS
 );%0A            r
@@ -3482,16 +3482,24 @@
    %0A    
+private 
 class Sl
@@ -3539,24 +3539,58 @@
 utStream %7B%0A%0A
+    %09private final int CPS;%0A    %09%0A
         publ
@@ -3625,16 +3625,25 @@
 ream out
+, int cps
 ) %7B%0A    
@@ -3658,24 +3658,45 @@
 super(out);%0A
+            CPS=cps;%0A
         %7D%0A%0A 
@@ -3790,16 +3790,20 @@
 ause(len
+,CPS
 );%0A     
@@ -3917,16 +3917,20 @@
  pause(1
+,CPS
 );%0A     

Calculate nano-seconds as well; reorganise slightly