001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018:
019: package org.apache.jmeter.util;
020:
021: import java.io.IOException;
022: import java.io.InputStream;
023: import java.io.OutputStream;
024: import java.net.InetAddress;
025: import java.net.InetSocketAddress;
026: import java.net.Socket;
027: import java.net.SocketAddress;
028: import java.net.UnknownHostException;
029:
030: /**
031: * "Slow" (non-SSL) socket implementation to emulate dial-up modems etc
032: */
033: public class SlowSocket extends Socket {
034:
035: private final int CPS; // Characters per second to emulate
036:
037: public SlowSocket(final int cps, String host, int port,
038: InetAddress localAddress, int localPort, int timeout)
039: throws IOException {
040: super ();
041: if (cps <= 0) {
042: throw new IllegalArgumentException("Speed (cps) <= 0");
043: }
044: CPS = cps;
045: // This sequence is borrowed from:
046: // org.apache.commons.httpclient.protocol.ReflectionSocketFactory.createSocket
047: SocketAddress localaddr = new InetSocketAddress(localAddress,
048: localPort);
049: SocketAddress remoteaddr = new InetSocketAddress(host, port);
050: bind(localaddr);
051: connect(remoteaddr, timeout);
052: }
053:
054: /**
055: *
056: * @param cps characters per second
057: * @param host hostname
058: * @param port port
059: * @param localAddr local address
060: * @param localPort local port
061: *
062: * @throws IOException
063: * @throws IllegalArgumentException if cps <=0
064: */
065: public SlowSocket(int cps, String host, int port,
066: InetAddress localAddr, int localPort) throws IOException {
067: super (host, port, localAddr, localPort);
068: if (cps <= 0) {
069: throw new IllegalArgumentException("Speed (cps) <= 0");
070: }
071: CPS = cps;
072: }
073:
074: /**
075: *
076: * @param cps characters per second
077: * @param host hostname
078: * @param port port
079: *
080: * @throws UnknownHostException
081: * @throws IOException
082: * @throws IllegalArgumentException if cps <=0
083: */
084: public SlowSocket(int cps, String host, int port)
085: throws UnknownHostException, IOException {
086: super (host, port);
087: if (cps <= 0) {
088: throw new IllegalArgumentException("Speed (cps) <= 0");
089: }
090: CPS = cps;
091: }
092:
093: // Override so we can intercept the stream
094: public OutputStream getOutputStream() throws IOException {
095: return new SlowOutputStream(super .getOutputStream(), CPS);
096: }
097:
098: // Override so we can intercept the stream
099: public InputStream getInputStream() throws IOException {
100: return new SlowInputStream(super.getInputStream(), CPS);
101: }
102:
103: }
|