|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--com.ibm.etill.framework.io.ReconnectableClientSocket
A class that encapsulates a reconnectable TCP socket that diagnoses whether the socket is down or not when function are called to get/put data to the socket. A user of this class must verify on their own, that they are writing to the proper connection (virtual socket) and that it has not gone down and come up between writing to the socket. This check is made for reading from this socket by providing the expected virtual socket number on the read method. This socket is designed to be used by multiple threads, and as such, it is possible to have numerous threads all blocked on connecting to a host. N-1 of the threads are blocked because of locking on this socket (synchronized methods) while the other 1 thread is waiting for the connection to occur. If the host is down, 1 thread will block for about 30 seconds, while the others wait. The others threads, will successively incur a similar wait of 30 seconds since the host will likely still be down once they get their time to connect. This is not good because the delay to the thread doing the connection is not predictable as it is based on how many other threads are ahead of it trying to connect to the same host using this same socket. Because of this unpredictable delay, there exists the capability of "shortcutting connection failures" using a "last failed connection timestamp". That is, if the user is trying to make a connection, and the last connection attempt failed within some reasonable amount of time (say 1 second), one can ask the socket to not try the connection and rather report the last failing connection status since the failing connection occurred so shortly ago. The virtual socket number is used in a couple different ways. 1) When it is odd, the socket connection is down. When it is even the connection is up. Each time a socket connection is made (reconnected) it is changed to the next higher even number. Each time a socket connection is found to be down, it is changed to the next higher odd number. 2) A user of this object may get this "virtual socket number" and use it to tell whether the connection currently up (or down) is the same connection that data was sent on earlier. We use this method in our VisaNet retransmitter to know if a retransmital is necessary.
Field Summary | |
---|---|
static int |
READBYTE_NO_DATA
no data available to read on connection, but connection is still up |
Constructor Summary | |
---|---|
ReconnectableClientSocket(TcpipHost targetHost,
int shortcutConnectionPeriodInMilliseconds)
|
|
ReconnectableClientSocket(TcpipHost targetHost,
TcpipHost socksHost,
int shortcutConnectionPeriodInMilliseconds)
|
Method Summary | |
---|---|
int |
available(int expectingToReadFromVirtualSocketNumber)
|
void |
close()
Closes the socket and amy also flush bufferd output data. |
void |
flush()
Flushes buffered data for the socket's OutputStream to written to the destination. |
InetAddress |
getInetAddress()
Gets the socket's internet address |
InetAddress |
getLocalAddress()
Gets the socket's local internet address |
int |
getLocalPort()
Gets the socket's local port number |
int |
getPort()
Gets the socket's port number |
int |
getSoLinger()
Returns setting for SO_LINGER. |
int |
getSoTimeout()
Returns the socket timeout setting |
boolean |
getTcpNoDelay()
Returns whether Nagle's Algorithm is disabled or not |
int |
getVirtualSocketNumber()
|
boolean |
isUp()
Determines if the socket is up |
int |
read(int expectingToReadFromVirtualSocketNumber,
byte[] ba,
int offset,
int maxLengthToRead)
Reads a set of bytes from the socket Same args (after the first one) and return values as InputStream.read(byte[], int, int) |
int |
readByte(int expectingToReadFromVirtualSocketNumber)
Reads a byte from the socket |
boolean |
reconnectIfKnownDown()
Reconnects the socket if it is known to be down. |
void |
setSoLinger(boolean lingervalue,
int val)
Enable/disable SO_LINGER with the specified linger time in seconds. |
void |
setSoTimeout(int milliseconds)
Enable/disable SO_TIMEOUT with the specified timeout, in milliseconds. |
void |
setTcpNoDelay(boolean on)
Disable Nagle's Algorithm if you supply true. |
String |
toString()
Gets the socket in a string format If the socket has not be connected, "socket not connected" will be returned |
void |
write(byte thebyte)
Writes a byte to the socket. |
void |
write(byte[] bytes)
Writes a set of bytes to the socket. |
void |
yourSocketIsDown()
This function gets called if our socket is used and found to be down ... |
Methods inherited from class java.lang.Object |
---|
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Field Detail |
public static final int READBYTE_NO_DATA
Constructor Detail |
public ReconnectableClientSocket(TcpipHost targetHost, int shortcutConnectionPeriodInMilliseconds)
public ReconnectableClientSocket(TcpipHost targetHost, TcpipHost socksHost, int shortcutConnectionPeriodInMilliseconds)
Method Detail |
public int getVirtualSocketNumber()
public void yourSocketIsDown()
yourSocketIsDown
in interface SocketDownInterface
public boolean isUp()
true
if the socket is uppublic boolean reconnectIfKnownDown() throws IOException
true
if we brought up a new connection,
false
if we thought the socket was still up and no new connection was neededIOException
- if the socket connection can not be made
If the user wants us to shortcut connection failures and the most recent
connection was a failure and it occurred not long ago.public int available(int expectingToReadFromVirtualSocketNumber) throws IOException
public int readByte(int expectingToReadFromVirtualSocketNumber) throws IOException
READBYTE_NO_DATA
if no data available to be read on the socket
0
to 255
byte value if a byte was readIOException
- if the expected (or current) connection is downpublic int read(int expectingToReadFromVirtualSocketNumber, byte[] ba, int offset, int maxLengthToRead) throws IOException
InputStream.read(byte[], int, int)
ba
- the array in to which the set bytes will be placedoffset
- the starting offset in ba
where the bytes will be placedmaxLengthToRead
- the maximum number of bytes to readIOException
- if the connection has gone down or an error occurs reading from the socket.public void write(byte[] bytes) throws IOException
IOException
- if an I/O error occurs.public void write(byte thebyte) throws IOException
IOException
- if an I/O error occurs.public void flush() throws IOException
OutputStream
to written to the destination.
A user of this class must verify on their own, that they are writing to the
proper connection (virtual socket) and that it has not gone down and
come up between writing to the socket.IOException
- if an I/O error occurs.public void close()
public void setSoTimeout(int milliseconds)
read()
call on the
InputStream
associated with this Socket will block for only this amount of time.
If the timeout expires, a java.io.InterruptedIOException
is raised, though the
Socket
is still valid.
The option must be enabled prior to entering the blocking operation to have effect.
The timeout must be greater than 0
. A timeout of zero is interpreted as an infinite timeout.timeout
- the socket timeout value in milliseconds.public void setTcpNoDelay(boolean on)
true
on a socket, the TCP/IP stack does not wait for
ACKs when sending "small" packets of data.
See TCP/IP Illustrated Volume 1 for more discussion on Nagle's algorithm.on
- true
if NODELAY algorithm is to be disabledpublic void setSoLinger(boolean lingervalue, int val) throws SocketException
65,535
it will be reduced to 65,535
.on
- true
indicates the socket should linger on.val
- the linger time in seconds.public InetAddress getInetAddress()
InetAddress
representing the IP address of the target hostpublic InetAddress getLocalAddress()
InetAddress
representing the IP address of the local hostpublic int getLocalPort()
public int getPort()
public boolean getTcpNoDelay() throws IOException
true
if TCP_NODELAY is enabledpublic int getSoTimeout() throws IOException
0
indicates the timeout option is disabled (a timeout of infinity)
Other values returned are the timeout setting in millisecondsIOException
- if a error occurs obtaining the timeout value.public int getSoLinger() throws IOException
-1
implies that the option is disabled.-1
if the linger option is disabled.IOException
- if the linger setting can not be obtained.public String toString()
toString
in class Object
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |