/*
 * Basic TCP Sampler Client class
 * 
 * Can be used to test the TCP Sampler against an HTTP server
 * 
 * The protocol handler class name is defined by the property tcp.handler
 * 
 */
package org.apache.jmeter.protocol.tcp.sampler;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.SocketTimeoutException;

import org.apache.jorphan.logging.LoggingManager;
import org.apache.log.Logger;

/**
 *
 * @author sebb AT apache DOT org
 * @version $Revision$ $Date$  
 *
 */
public class TCPClientImpl implements TCPClient
{
	private static Logger log = LoggingManager.getLoggerForClass();

    public TCPClientImpl()
    {
        super();
        log.info("Created "+this);
    }


	/* (non-Javadoc)
	 * @see org.apache.jmeter.protocol.tcp.sampler.TCPClient#setupTest()
	 */
	public void setupTest()
	{
		log.info("setuptest");
        
	}

    /* (non-Javadoc)
     * @see org.apache.jmeter.protocol.tcp.sampler.TCPClient#teardownTest()
     */
    public void teardownTest()
    {
		log.info("teardowntest");
        
    }

    /* (non-Javadoc)
     * @see org.apache.jmeter.protocol.tcp.sampler.TCPClient#write(java.io.OutputStream)
     */
    public String write(OutputStream os)
    {
		String s = "GET /  HTTP/1.1\nHost: www.dummy.invalid\n\n"; //or get from file etc
		try
        {
            os.write(s.getBytes());
			os.flush();
        }
        catch (IOException e)
        {
            log.debug("Write error",e);
        }
        log.debug("Wrote: "+s);
        return s;
    }


    /* (non-Javadoc)
     * @see org.apache.jmeter.protocol.tcp.sampler.TCPClient#read(java.io.InputStream)
     */
    public String read(InputStream is)
    {
		byte [] buffer = new byte[4096];
		ByteArrayOutputStream w = new ByteArrayOutputStream();
		int x = 0;
		try {
			while ((x = is.read(buffer)) > -1)
			{
				w.write(buffer, 0, x);
			}
		} catch (SocketTimeoutException e) {
			// drop out to handle buffer
		} catch (IOException e) {
			log.debug("Read error",e);
			return "";
		}
		
		// do we need to close byte array (or flush it?)
		log.debug("Read:\n"+w.toString());
		return w.toString();
    }
}

@@ -317,32 +317,71 @@
 io.InputStream;%0A
+import java.io.InterruptedIOException;%0A
 import java.io.O
@@ -1978,30 +1978,481 @@
 %7D%0A%09%09
-%7D catch (SocketTimeout
+/*%0A%09%09 * Timeout is reported as follows:%0A%09%09 * JDK1.3: InterruptedIOException%0A%09%09 * JDK1.4: SocketTimeoutException, which extends InterruptedIOException%0A%09%09 * %0A%09%09 * So to make the code work on both, just check for InterruptedIOException%0A%09%09 *%0A%09%09 * If 1.3 support is dropped, can change to using SocketTimeoutException%0A%09%09 *  %0A%09%09 * For more accurate detection of timeouts under 1.3,%0A%09%09 * one could perhaps examine the Exception message text...%0A%09%09 * %0A%09%09 */%0A%09%09%7D catch (InterruptedIO
 Exce
@@ -2525,29 +2525,28 @@
 e) %7B%0A%09%09%09log.
-debug
+warn
 (%22Read error
@@ -2541,26 +2541,27 @@
 (%22Read error
-%22,
+:%22+
 e);%0A%09%09%09retur

Make compliant with JDK1.3