01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: *
17: */
18:
19: package org.apache.jmeter.protocol.http.util;
20:
21: import java.io.IOException;
22: import java.net.InetAddress;
23: import java.net.Socket;
24: import java.net.UnknownHostException;
25:
26: import org.apache.commons.httpclient.ConnectTimeoutException;
27: import org.apache.commons.httpclient.params.HttpConnectionParams;
28: import org.apache.commons.httpclient.protocol.ProtocolSocketFactory;
29: import org.apache.jmeter.util.SlowSocket;
30:
31: /**
32: * HttpClient protocol factory to generate "slow" sockets for emulating dial-up modems
33: */
34:
35: public class SlowHttpClientSocketFactory implements
36: ProtocolSocketFactory {
37:
38: private final int CPS; // Characters per second to emulate
39:
40: /**
41: *
42: * @param cps - characters per second
43: */
44: public SlowHttpClientSocketFactory(final int cps) {
45: super ();
46: CPS = cps;
47: }
48:
49: public Socket createSocket(String host, int port,
50: InetAddress clientHost, int clientPort) throws IOException,
51: UnknownHostException {
52: return new SlowSocket(CPS, host, port, clientHost, clientPort);
53: }
54:
55: public Socket createSocket(String host, int port)
56: throws IOException, UnknownHostException {
57: return new SlowSocket(CPS, host, port);
58: }
59:
60: public Socket createSocket(String host, int port,
61: InetAddress localAddress, int localPort,
62: HttpConnectionParams params) throws IOException,
63: UnknownHostException, ConnectTimeoutException {
64: int timeout = params.getConnectionTimeout();
65: if (timeout == 0) {
66: return new SlowSocket(CPS, host, port, localAddress,
67: localPort);
68: } else {
69: return new SlowSocket(CPS, host, port, localAddress,
70: localPort, timeout);
71: }
72: }
73: }
|