Class OncRpcTcpSocketHelper
- java.lang.Object
-
- org.acplt.oncrpc.OncRpcTcpSocketHelper
-
public class OncRpcTcpSocketHelper extends java.lang.ObjectWraps JRE-specific networking code for TCP/IP-based client sockets. So much for compile once, make it unuseable everywhere. Why could our great Sun simply not get their socket class straight from the beginning? The BSD socket API has been around since long enough and that real network applications need to control certain aspects of the transport layer's behaviour was also well known at this time -- looks like the one exceptions was -- and still is -- Sun, and the second one is MS.Sun always toutes Java as the perfect network "whatever" (replace with buzzword-of-the-day, like "programming language", "operating system",...) and especially their support for the Web. Sweet irony that it took them until JRE 1.3 to realize that half-closed connections are the way to do HTTP/1.0 non-persistent connections. And even more irony that they are now beginning to understand about polled network i/o.
The following JRE-dependent methods are wrapped and will just do nothing or return fake information on old JRE plattforms. The number after each method wrapper indicates the first JRE version supporting a particular feature:
- setSendBufferSize() -- 1.2
- setReceiveBufferSize() -- 1.2
The following methods have been around since JDK 1.1, so we do not need to wrap them as we will never support JDK 1.0 -- let it rest in piece(s):
- getTcpNoDelay() / setTcpNoDelay()
- getSoTimeout() / setSoTimeout()
In order to support connect() timeouts before JDK 1.4, there's now a
connect()method available. It is more than just a simple wrapper for pre JDK 1.4.- Version:
- $Revision: 1.3 $ $Date: 2007/05/29 19:45:46 $ $State: Exp $ $Locker: $
- Author:
- Harald Albrecht
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private classOncRpcTcpSocketHelper.ConnectiatorThe classConnectiatorhas a short and sometimes sad life, as its only purpose is trying to connect to a TCP port at another host machine.
-
Field Summary
Fields Modifier and Type Field Description private java.lang.reflect.ConstructorctorConstructor Socket() without any parameters ornullif not available in the class library of a particular JRE.private java.lang.reflect.MethodmethodConnectMethod Socket.connect with timeout ornullif not available in the class library of a particular JRE.private java.lang.reflect.MethodmethodGetReceiveBufferSizeMethod Socket.getReceiveBufferSize ornullif not available in the class library of a particular JRE.private java.lang.reflect.MethodmethodGetSendBufferSizeMethod Socket.getSendBufferSize ornullif not available in the class library of a particular JRE.private java.lang.reflect.MethodmethodSetReceiveBufferSizeMethod Socket.setReceiverBufferSize ornullif not available in the class library of a particular JRE.private java.lang.reflect.MethodmethodSetSendBufferSizeMethod Socket.setSendBufferSize ornullif not available in the class library of a particular JRE.private java.net.SocketsocketThe socket for which we have to help out with some missing methods.
-
Constructor Summary
Constructors Constructor Description OncRpcTcpSocketHelper()Creates a stream socket helper but does not associates it with a real stream socket object.OncRpcTcpSocketHelper(java.net.Socket socket)Creates a stream socket helper and associates it with the given stream-based socket.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description java.net.Socketconnect(java.net.InetAddress address, int port, int timeout)Connects to specified TCP port at given host address, but aborts after timeout milliseconds if the connection could not be established within this time frame.intgetReceiveBufferSize()Get size of receive buffer for this socket.intgetSendBufferSize()Get size of send buffer for this socket.protected voidinspectSocketClassMethods()Looks up methods of class Socket whether they are supported by the class libraries of the JRE we are currently executing on.voidsetReceiveBufferSize(int size)Sets the socket's receive buffer size as a hint to the underlying transport layer to use appropriately sized I/O buffers.voidsetSendBufferSize(int size)Sets the socket's send buffer size as a hint to the underlying transport layer to use appropriately sized I/O buffers.
-
-
-
Field Detail
-
socket
private java.net.Socket socket
The socket for which we have to help out with some missing methods.
-
methodSetSendBufferSize
private java.lang.reflect.Method methodSetSendBufferSize
Method Socket.setSendBufferSize ornullif not available in the class library of a particular JRE.
-
methodSetReceiveBufferSize
private java.lang.reflect.Method methodSetReceiveBufferSize
Method Socket.setReceiverBufferSize ornullif not available in the class library of a particular JRE.
-
methodGetSendBufferSize
private java.lang.reflect.Method methodGetSendBufferSize
Method Socket.getSendBufferSize ornullif not available in the class library of a particular JRE.
-
methodGetReceiveBufferSize
private java.lang.reflect.Method methodGetReceiveBufferSize
Method Socket.getReceiveBufferSize ornullif not available in the class library of a particular JRE.
-
methodConnect
private java.lang.reflect.Method methodConnect
Method Socket.connect with timeout ornullif not available in the class library of a particular JRE.
-
ctor
private java.lang.reflect.Constructor ctor
Constructor Socket() without any parameters ornullif not available in the class library of a particular JRE.
-
-
Constructor Detail
-
OncRpcTcpSocketHelper
public OncRpcTcpSocketHelper(java.net.Socket socket)
Creates a stream socket helper and associates it with the given stream-based socket.- Parameters:
socket- The socket associated with this helper.
-
OncRpcTcpSocketHelper
public OncRpcTcpSocketHelper()
Creates a stream socket helper but does not associates it with a real stream socket object. You need to callconnect(java.net.InetAddress, int, int)lateron for a timeout-controlled connect.
-
-
Method Detail
-
connect
public java.net.Socket connect(java.net.InetAddress address, int port, int timeout) throws java.io.IOExceptionConnects to specified TCP port at given host address, but aborts after timeout milliseconds if the connection could not be established within this time frame.On pre-JRE 1.4 systems, this method will create a new thread to handle connection establishment. This should be no problem, as in general you might not want to connect to many ONC/RPC servers at the same time; but surely someone will soon pop up with a perfect reason just to do so...
- Parameters:
timeout- Timeout in milliseconds for connection operation. A negative timeout leaves the exact timeout up to the particular JVM and java.net implementation.- Throws:
java.io.IOException- with the message "connect interrupted" in case the timeout was reached before the connection could be established.
-
setSendBufferSize
public void setSendBufferSize(int size) throws java.net.SocketExceptionSets the socket's send buffer size as a hint to the underlying transport layer to use appropriately sized I/O buffers. If the class libraries of the underlying JRE do not support setting the send buffer size, this is silently ignored.- Parameters:
size- The size to which to set the send buffer size. This value must be greater than 0.- Throws:
java.net.SocketException- if the socket's send buffer size could not be set, because the transport layer decided against accepting the new buffer size.java.lang.IllegalArgumentException- ifsizeis 0 or negative.
-
getSendBufferSize
public int getSendBufferSize() throws java.net.SocketExceptionGet size of send buffer for this socket.- Returns:
- Size of send buffer.
- Throws:
java.net.SocketException- If the transport layer could not be queried for the size of this socket's send buffer.
-
setReceiveBufferSize
public void setReceiveBufferSize(int size) throws java.net.SocketExceptionSets the socket's receive buffer size as a hint to the underlying transport layer to use appropriately sized I/O buffers. If the class libraries of the underlying JRE do not support setting the receive buffer size, this is silently ignored.- Parameters:
size- The size to which to set the receive buffer size. This value must be greater than 0.- Throws:
java.net.SocketException- if the socket's receive buffer size could not be set, because the transport layer decided against accepting the new buffer size.java.lang.IllegalArgumentException- ifsizeis 0 or negative.
-
getReceiveBufferSize
public int getReceiveBufferSize() throws java.net.SocketExceptionGet size of receive buffer for this socket.- Returns:
- Size of receive buffer.
- Throws:
java.net.SocketException- If the transport layer could not be queried for the size of this socket's receive buffer.
-
inspectSocketClassMethods
protected void inspectSocketClassMethods()
Looks up methods of class Socket whether they are supported by the class libraries of the JRE we are currently executing on.
-
-