【Java开源代码栏目提醒】:网学会员,鉴于大家对Java开源代码十分关注,论文会员在此为大家搜集整理了“HeartbeatClientSocket.java”一文,供大家参考学习!
import java.io.*;
import java.net.*;
/**
* InputStreams that read from an instance of this subclass of
* Socket throws a HeartbeatException if it does not receive a
* heartbeat message from the server within a specified timeout
* period. The transmission of heartbeat messages between a
* HeartbeatClientSocket and a HeartbeatServerSocket is
* accomplished through a private protocol.
*/
class HeartbeatClientSocket extends Socket {
/**
* The maximum amount of time that should elapse before an
* HeartbeatException should be thrown from a read.
*/
private int heartbeatTimeout;
/**
* The last value passed to this object's setSoTimeout
* method.
*/
private int soTimeout = 0;
/**
* The InputStream for this socket.
*/
private HeartbeatInputStream myInputStream;
/**
* Creates a stream socket and connects it to the specified
* port number on the named host.
* <p>
* If the application has specified a server socket
* factory, that factory's <code>createSocketImpl</code>
* method is called to create the actual socket
* implementation. Otherwise a "plain" socket is created.
*
* @param host the host name.
* @param port the port number.
* @param timeout The amount of time in milliseconds that
* this socket should wait for a read
* operation to be satisfied or to receive a
* heartbeat message before throwing a
* HeartbeatException.
* @exception IOException if an I/O error occurs when
* creating the socket.
*/
public HeartbeatClientSocket(String host, int port, int timeout)
throws UnknownHostException, IOException {
super(host, port);
heartbeatTimeout = timeout;
super.setSoTimeout(timeout);
} // constructor(String, int)
/**
* Enable/disable SO_TIMEOUT with the specified timeout,
* in milliseconds. With this option set to a non-zero
* timeout, a read() call on the InputStream associated
* with this Socket will block for only this amount of
* time. If the timeout expires, a
* <B>java.io.InterruptedIOException</B> is raised, though
* the Socket is still valid. The option <B>must</B> be
* enabled prior to entering the blocking operation to have
* effect. The timeout must be > 0.
*<p>
* Timeout values that are zero disable this feature and
* enable the throwing of a HeartbeatException after the
* number of milliseconds passed into this object's
* constructor have elapsed. Timeout valuess greater than
* the timeout value passed to this object's constructor
* are treated as zero.
*/
public synchronized void setSoTimeout(int timeout)
throws SocketException {
if (timeout>heartbeatTimeout || timeout==0) {
super.setSoTimeout(heartbeatTimeout);
soTimeout = 0;
} else {
super.setSoTimeout(timeout);
soTimeout = timeout;
} // if
} // setSoTimeout(int)
/**
* Returns setting for SO_TIMEOUT. 0 returns implies that the
* option is disabled.
*/
public synchronized int getSoTimeout()
throws SocketException {
return soTimeout;
} // getSoTimeout()
/**
* Returns an input stream for this socket.
*
* @return an input stream to read bytes from this socket.
* @exception IOException if an I/O error occurs when
* creating the input stream.
*/
public InputStream getInputStream() throws IOException {
if (myInputStream==null) {
synchronized (this) {
if (myInputStream==null) {
InputStream superIn;
superIn = super.getInputStream();
myInputStream
= new HeartbeatInputStream(superIn);
} // if
} // synchronized
} // if
return myInputStream;
} // getInputStream()
/**
* Input stream to manage the soTimeouts. The class
* implements the client side of the protocol that delivers
* heartbeat messages to the client.
*/
private class HeartbeatInputStream extends FilterInputStream
implements Runnable {
HeartbeatInputStream(InputStream stream) {
super(stream);
Thread cleanupThread = new Thread(this);
cleanupThread.setdaemon(true);
cleanupThread.start();
} // constructor (InputStream)
private byte[] byteBuffer = new byte[1];
/**
* Reads the next byte of data from this input stream. The
* value byte is returned as an <code>int</code> in the
* range <code>0</code> to <code>255</code>. If no byte is
* available because the end of the stream has been
* reached, the value <code>-1</code> is returned. This
* method blocks until input data is available, the end of
* the stream is detected, or an exception is thrown.
* <p>
* This method simply performs <code>in.read()</code> and
* returns the result.
*
* @return The next byte of data, or <code>-1</code> if the
* end of the stream is reached.
* @exception IOException if an I/O error occurs.
* @exception HeartbeatException if no hearbeat message is
* received within the
* specifieds timeout
*/
public int read() throws IOException {
try {
read(byteBuffer, 0, 1);
} catch (EOFException e) {
return -1;
} // try
return byteBuffer[0];
} // read()
/**
* The protocol that the
* HeartbeatServerSocket.HeartbeatOutputStream uses to
* pass hearbeat messages and data in the same data
* stream to prefix the data from each call to its
* write method with an int that is the number of bytes
* written. Heartbeat messages are transmitted as
* zero length writes.
*<p>
* If a call to one of this object's read methods
* leaves some bytes of a server write operation
* unread, then this variable contains the number of
* bytes from that operation waiting to be read.
*/
int bytesUnread;
/**
* Reads up to <code>len</code> bytes of data from this input stream
* into an array of bytes. This method blocks until some input is
* available.
* <p>
* This method simply performs <code>in.read(b, off, len)</code>
* and returns the result.
*
* @param b the buffer into which the data is read.
* @param off the start offset of the data.
* @param len the maximum number of bytes read.
* @return the total number of bytes read into the
* buffer, or <code>-1</code> if there is
* no more data because the end of
* the stream has been reached.
* @exception IOException if an I/O error occurs.
* @see java.io.FilterInputStream#in
*/
public synchronized int read(byte b[],
int off,
int len)
throws IOException {
if (len==0) {
return 0;
} // if
try {
while (bytesUnread<=0) {
bytesUnread = readInt();
} // while
int bytesToRead = Math.min(bytesUnread, len);
int bytesRead = in.read(b, off, bytesToRead);
bytesUnread -= bytesRead;
return bytesRead;
} catch (InterruptedIOException e) {
if (soTimeout!=0) {
throw e
上一篇:
HeapSort.java
下一篇:
还记得,那年的风车吗?