com.sun.faban.driver.transport.util
Class TimedSocket

java.lang.Object
  extended by java.net.Socket
      extended by com.sun.faban.driver.transport.util.TimedSocket

public class TimedSocket
extends Socket

The TimedSocket class extends the Socket class by timing the opening of connections and using TimedInputStream and TimedOutputStream as the streams for this socket. This allows collecting times for the benchmark at a very low level, just before the request goes onto the socket and right after the response comes back.

Author:
Akara Sucharitakul

Constructor Summary
  TimedSocket()
          Creates an unconnected socket, with the system-default type of SocketImpl.
  TimedSocket(InetAddress address, int port)
          Creates a stream socket and connects it to the specified port number at the specified IP address.
  TimedSocket(InetAddress address, int port, InetAddress localAddr, int localPort)
          Creates a socket and connects it to the specified remote address on the specified remote port.
  TimedSocket(Proxy proxy)
          Creates an unconnected socket, specifying the type of proxy, if any, that should be used regardless of any other settings.
protected TimedSocket(SocketImpl impl)
          Creates an unconnected Socket with a user-specified SocketImpl.
  TimedSocket(String host, int port)
          Creates a stream socket and connects it to the specified port number on the named host.
  TimedSocket(String host, int port, InetAddress localAddr, int localPort)
          Creates a socket and connects it to the specified remote host on the specified remote port.
 
Method Summary
 void connect(SocketAddress endpoint, int timeout)
          Connects this socket to the server with a specified timeout value.
 InputStream getInputStream()
          Returns an input stream for this socket.
 OutputStream getOutputStream()
          Returns an output stream for this socket.
 
Methods inherited from class java.net.Socket
bind, close, connect, getChannel, getInetAddress, getKeepAlive, getLocalAddress, getLocalPort, getLocalSocketAddress, getOOBInline, getPort, getReceiveBufferSize, getRemoteSocketAddress, getReuseAddress, getSendBufferSize, getSoLinger, getSoTimeout, getTcpNoDelay, getTrafficClass, isBound, isClosed, isConnected, isInputShutdown, isOutputShutdown, sendUrgentData, setKeepAlive, setOOBInline, setPerformancePreferences, setReceiveBufferSize, setReuseAddress, setSendBufferSize, setSocketImplFactory, setSoLinger, setSoTimeout, setTcpNoDelay, setTrafficClass, shutdownInput, shutdownOutput, toString
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

TimedSocket

public TimedSocket()
Creates an unconnected socket, with the system-default type of SocketImpl.


TimedSocket

public TimedSocket(Proxy proxy)
Creates an unconnected socket, specifying the type of proxy, if any, that should be used regardless of any other settings.

If there is a security manager, its checkConnect method is called with the proxy host address and port number as its arguments. This could result in a SecurityException.

Examples:

Parameters:
proxy - a Proxy object specifying what kind of proxying should be used.
Throws:
IllegalArgumentException - if the proxy is of an invalid type or null.
SecurityException - if a security manager is present and permission to connect to the proxy is denied.
Since:
1.5
See Also:
ProxySelector, Proxy

TimedSocket

public TimedSocket(InetAddress address,
                   int port)
            throws IOException
Creates a stream socket and connects it to the specified port number at the specified IP address.

If the application has specified a socket factory, that factory's createSocketImpl method is called to create the actual socket implementation. Otherwise a "plain" socket is created.

If there is a security manager, its checkConnect method is called with the host address and port as its arguments. This could result in a SecurityException.

Parameters:
address - the IP address.
port - the port number.
Throws:
IOException - if an I/O error occurs when creating the socket.
SecurityException - if a security manager exists and its checkConnect method doesn't allow the operation.
See Also:
Socket.setSocketImplFactory(java.net.SocketImplFactory), SocketImpl, SocketImplFactory.createSocketImpl(), SecurityManager.checkConnect(java.lang.String, int)

TimedSocket

protected TimedSocket(SocketImpl impl)
               throws SocketException
Creates an unconnected Socket with a user-specified SocketImpl.

Parameters:
impl - an instance of a SocketImpl the subclass wishes to use on the Socket.
Throws:
SocketException - if there is an error in the underlying protocol, such as a TCP error.
Since:
JDK1.1

TimedSocket

public TimedSocket(String host,
                   int port)
            throws UnknownHostException,
                   IOException
Creates a stream socket and connects it to the specified port number on the named host.

If the specified host is null it is the equivalent of specifying the address as InetAddress.getByName(null). In other words, it is equivalent to specifying an address of the loopback interface.

If the application has specified a server socket factory, that factory's createSocketImpl method is called to create the actual socket implementation. Otherwise a "plain" socket is created.

If there is a security manager, its checkConnect method is called with the host address and port as its arguments. This could result in a SecurityException.

Parameters:
host - the host name, or null for the loopback address.
port - the port number.
Throws:
UnknownHostException - if the IP address of the host could not be determined.
IOException - if an I/O error occurs when creating the socket.
SecurityException - if a security manager exists and its checkConnect method doesn't allow the operation.
See Also:
Socket.setSocketImplFactory(java.net.SocketImplFactory), SocketImpl, SocketImplFactory.createSocketImpl(), SecurityManager.checkConnect(java.lang.String, int)

TimedSocket

public TimedSocket(String host,
                   int port,
                   InetAddress localAddr,
                   int localPort)
            throws IOException
Creates a socket and connects it to the specified remote host on the specified remote port. The Socket will also bind() to the local address and port supplied.

If the specified host is null it is the equivalent of specifying the address as InetAddress.getByName(null). In other words, it is equivalent to specifying an address of the loopback interface.

If there is a security manager, its checkConnect method is called with the host address and port as its arguments. This could result in a SecurityException.

Parameters:
host - the name of the remote host, or null for the loopback address.
port - the remote port
localAddr - the local address the socket is bound to
localPort - the local port the socket is bound to
Throws:
IOException - if an I/O error occurs when creating the socket.
SecurityException - if a security manager exists and its checkConnect method doesn't allow the operation.
Since:
JDK1.1
See Also:
SecurityManager.checkConnect(java.lang.String, int)

TimedSocket

public TimedSocket(InetAddress address,
                   int port,
                   InetAddress localAddr,
                   int localPort)
            throws IOException
Creates a socket and connects it to the specified remote address on the specified remote port. The Socket will also bind() to the local address and port supplied.

If there is a security manager, its checkConnect method is called with the host address and port as its arguments. This could result in a SecurityException.

Parameters:
address - the remote address
port - the remote port
localAddr - the local address the socket is bound to
localPort - the local port the socket is bound to
Throws:
IOException - if an I/O error occurs when creating the socket.
SecurityException - if a security manager exists and its checkConnect method doesn't allow the operation.
Since:
JDK1.1
See Also:
SecurityManager.checkConnect(java.lang.String, int)
Method Detail

connect

public void connect(SocketAddress endpoint,
                    int timeout)
             throws IOException
Connects this socket to the server with a specified timeout value. A timeout of zero or negative values default to 30,000 (30 seconds). The connection will then block until established or an error occurs.

Overrides:
connect in class Socket
Parameters:
endpoint - the SocketAddress
timeout - the timeout value to be used in milliseconds.
Throws:
IllegalBlockingModeException - if this socket has an associated channel, and the channel is in non-blocking mode
IllegalArgumentException - if endpoint is null or is a SocketAddress subclass not supported by this socket
IOException - if an error occurs during the connection
SocketTimeoutException - if timeout expires before connecting
Since:
1.4

getInputStream

public InputStream getInputStream()
                           throws IOException
Returns an input stream for this socket.

If this socket has an associated channel then the resulting input stream delegates all of its operations to the channel. If the channel is in non-blocking mode then the input stream's read operations will throw an IllegalBlockingModeException.

Under abnormal conditions the underlying connection may be broken by the remote host or the network software (for example a connection reset in the case of TCP connections). When a broken connection is detected by the network software the following applies to the returned input stream :-

Overrides:
getInputStream in class Socket
Returns:
an input stream for reading bytes from this socket.
Throws:
IOException - if an I/O error occurs when creating the input stream, the socket is closed, the socket is not connected, or the socket input has been shutdown using Socket.shutdownInput()

getOutputStream

public OutputStream getOutputStream()
                             throws IOException
Returns an output stream for this socket.

If this socket has an associated channel then the resulting output stream delegates all of its operations to the channel. If the channel is in non-blocking mode then the output stream's write operations will throw an IllegalBlockingModeException.

Overrides:
getOutputStream in class Socket
Returns:
an output stream for writing bytes to this socket.
Throws:
IOException - if an I/O error occurs when creating the output stream or if the socket is not connected.