Package org.jppf.comm.socket
Class AbstractSocketWrapper
java.lang.Object
org.jppf.comm.socket.AbstractSocketWrapper
- All Implemented Interfaces:
Closeable,AutoCloseable,SocketWrapper
- Direct Known Subclasses:
BootstrapSocketClient,SocketClient
Common abstract superclass for all socket clients. This class is provided as a convenience and provides
as set of common methods to all classes implementing the
SocketWrapper interface.- Author:
- Laurent Cohen, Domingos Creado, Jeff Rosen
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected DataInputStreamA buffered stream built on top of to the underlying socket's input stream.protected DataOutputStreamA reference to the underlying socket's output stream.protected StringThe host the socket connects to.protected booleanFlag indicating the opened state of the underlying socket.protected intThe port number on the host the socket connects to.protected ObjectSerializerUsed to serialize or deserialize an object into or from an array of bytes.protected SocketThe underlying socket wrapped by this SocketClient. -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedDefault constructor is visible to subclasses only.AbstractSocketWrapper(String host, int port, ObjectSerializer serializer) Initialize this socket client and connect it to the specified host on the specified port.AbstractSocketWrapper(String host, int port, ObjectSerializer serializer, boolean doOpen) Initialize this socket client and connect it to the specified host on the specified port.AbstractSocketWrapper(Socket socket) Initialize this socket client with an already opened and connected socket. -
Method Summary
Modifier and TypeMethodDescriptionprotected voidCheck whether the underlying socket is opened or not.voidclose()Close the underlying socket connection.voidflush()Flush the data currently in the send buffer.getHost()Get the remote host the underlying socket connects to.intgetPort()Get the remote port the underlying socket connects to.This implementation does nothing.Get the underlying socket used by this socket wrapper.longReturns a timestamp that should reflect the system millisecond counter at the last known good usage of the underlying socket.protected final voidInitialize all the stream used for receiving and sending objects through the underlying socket connection.booleanisOpened()Determine whether this socket client is opened or not.final voidopen()Open the underlying socket connection.intread(byte[] data, int offset, int len) Readlenbytes from a TCP connection into a byte array, starting at positionoffsetin that array.intreadInt()Read an int value from a socket connection.intreadPartial(byte[] data, int offset, int len) Read bytes from a TCP connection into a byte array, starting at positionoffsetin that array.receive()Read an object from a TCP socket connection.receiveBytes(int timeout) Read an object from a TCP socket connection.voidsendBytes(JPPFBuffer buf) Send an array of bytes over a TCP socket connection.voidSet the remote host the underlying socket connects to.voidsetPort(int port) Get the remote port the underlying socket connects to.voidsetSerializer(ObjectSerializer serializer) This implementation does nothing.voidSet the underlying socket to be used by this socket wrapper.intskip(int n) Skipnbytes of data from the socket or channel input stream.toString()protected voidMarks the socket "known good" usage timestamp with the current value of the system millisecond counter.voidwrite(byte[] data, int offset, int len) Send an array of bytes over a TCP socket connection.voidwriteInt(int n) Write an int value over a socket connection.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface org.jppf.comm.socket.SocketWrapper
receive, send
-
Field Details
-
socket
The underlying socket wrapped by this SocketClient. -
dos
A reference to the underlying socket's output stream. -
dis
A buffered stream built on top of to the underlying socket's input stream. -
host
The host the socket connects to. -
port
protected int portThe port number on the host the socket connects to. -
opened
protected boolean openedFlag indicating the opened state of the underlying socket. -
serializer
Used to serialize or deserialize an object into or from an array of bytes.
-
-
Constructor Details
-
AbstractSocketWrapper
protected AbstractSocketWrapper()Default constructor is visible to subclasses only. -
AbstractSocketWrapper
public AbstractSocketWrapper(String host, int port, ObjectSerializer serializer) throws ConnectException, IOException Initialize this socket client and connect it to the specified host on the specified port.- Parameters:
host- the remote host this socket client connects to.port- the remote port on the host this socket client connects to.serializer- the object serializer used by this socket client.- Throws:
ConnectException- if the connection fails.IOException- if there is an issue with the socket streams.
-
AbstractSocketWrapper
public AbstractSocketWrapper(String host, int port, ObjectSerializer serializer, boolean doOpen) throws ConnectException, IOException Initialize this socket client and connect it to the specified host on the specified port.- Parameters:
host- the remote host this socket client connects to.port- the remote port on the host this socket client connects to.serializer- the object serializer used by this socket client.doOpen- whether to actually open the conenction.- Throws:
ConnectException- if the connection fails.IOException- if there is an issue with the socket streams.
-
AbstractSocketWrapper
Initialize this socket client with an already opened and connected socket.- Parameters:
socket- the underlying socket this socket client wraps around.- Throws:
JPPFException- if the socket connection fails.
-
-
Method Details
-
sendBytes
Send an array of bytes over a TCP socket connection.- Specified by:
sendBytesin interfaceSocketWrapper- Parameters:
buf- the buffer container for the data to send.- Throws:
IOException- if the underlying output stream throws an exception.
-
write
Send an array of bytes over a TCP socket connection.- Specified by:
writein interfaceSocketWrapper- Parameters:
data- the data to send.offset- the position where to start reading data from the input array.len- the length of data to write.- Throws:
Exception- if the underlying output stream throws an exception.
-
writeInt
Write an int value over a socket connection.- Specified by:
writeIntin interfaceSocketWrapper- Parameters:
n- the value to write.- Throws:
IOException- if the underlying output stream throws an exception.
-
flush
Flush the data currently in the send buffer.- Specified by:
flushin interfaceSocketWrapper- Throws:
IOException- if an I/O error occurs.
-
receive
Read an object from a TCP socket connection. This method blocks until an object is received.- Specified by:
receivein interfaceSocketWrapper- Returns:
- the object that was read from the underlying input stream.
- Throws:
Exception- if the underlying input stream throws an exception.
-
receiveBytes
Read an object from a TCP socket connection. This method blocks until an object is received or the specified timeout has expired, whichever happens first.- Specified by:
receiveBytesin interfaceSocketWrapper- Parameters:
timeout- timeout after which the operation is aborted. A timeout of zero is interpreted as an infinite timeout.- Returns:
- an array of bytes containing the serialized object to receive.
- Throws:
IOException- if the underlying input stream throws an exception.
-
read
Readlenbytes from a TCP connection into a byte array, starting at positionoffsetin that array. This method blocks until at least one byte of data is received.- Specified by:
readin interfaceSocketWrapper- Parameters:
data- an array of bytes into which the data is stored.offset- the position where to start storing data read from the socket.len- the length of data to read.- Returns:
- the number of bytes actually read or -1 if the end of stream was reached.
- Throws:
IOException- if the underlying input stream throws an exception.
-
readPartial
Description copied from interface:SocketWrapperRead bytes from a TCP connection into a byte array, starting at positionoffsetin that array. This method blocks until at least one byte of data is received.- Specified by:
readPartialin interfaceSocketWrapper- Parameters:
data- an array of bytes into which the data is stored.offset- the position where to start storing data read from the socket.len- the maximum length of data to read.- Returns:
- the number of bytes actually read or -1 if the end of stream was reached.
- Throws:
IOException
-
readInt
Read an int value from a socket connection.- Specified by:
readIntin interfaceSocketWrapper- Returns:
- n the value to read from the socket, or -1 if end of stream was reached.
- Throws:
IOException- if the underlying input stream throws an exception.
-
open
Open the underlying socket connection.- Specified by:
openin interfaceSocketWrapper- Throws:
ConnectException- if the socket fails to connect.IOException- if the underlying input and output streams raise an error.
-
initStreams
Initialize all the stream used for receiving and sending objects through the underlying socket connection.- Throws:
IOException- if an error occurs during the streams initialization.
-
close
Close the underlying socket connection.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Throws:
ConnectException- if the socket connection is not opened.IOException- if the underlying input and output streams raise an error.
-
isOpened
public boolean isOpened()Determine whether this socket client is opened or not.- Specified by:
isOpenedin interfaceSocketWrapper- Returns:
- true if this client is opened, false otherwise.
-
checkOpened
Check whether the underlying socket is opened or not.- Throws:
ConnectException- if the connection is not opened.
-
getSerializer
This implementation does nothing.- Specified by:
getSerializerin interfaceSocketWrapper- Returns:
- null.
- See Also:
-
setSerializer
This implementation does nothing.- Specified by:
setSerializerin interfaceSocketWrapper- Parameters:
serializer- not used.
-
getHost
Get the remote host the underlying socket connects to.- Specified by:
getHostin interfaceSocketWrapper- Returns:
- the host name or ip address as a string.
-
setHost
Set the remote host the underlying socket connects to.- Specified by:
setHostin interfaceSocketWrapper- Parameters:
host- the host name or ip address as a string.
-
getPort
public int getPort()Get the remote port the underlying socket connects to.- Specified by:
getPortin interfaceSocketWrapper- Returns:
- the port number on the remote host.
-
setPort
public void setPort(int port) Get the remote port the underlying socket connects to.- Specified by:
setPortin interfaceSocketWrapper- Parameters:
port- the port number on the remote host.
-
getSocket
Get the underlying socket used by this socket wrapper.- Specified by:
getSocketin interfaceSocketWrapper- Returns:
- a Socket instance.
-
setSocket
Set the underlying socket to be used by this socket wrapper.- Specified by:
setSocketin interfaceSocketWrapper- Parameters:
socket- a Socket instance.
-
skip
Skipnbytes of data from the socket or channel input stream.- Specified by:
skipin interfaceSocketWrapper- Parameters:
n- the number of bytes to skip.- Returns:
- the actual number of bytes skipped, or -1 if the end of file is reached..
- Throws:
Exception- if an IO error occurs.
-
getSocketTimestamp
public long getSocketTimestamp()Returns a timestamp that should reflect the system millisecond counter at the last known good usage of the underlying socket.- Specified by:
getSocketTimestampin interfaceSocketWrapper- Returns:
- the socket usage timestamp
-
updateSocketTimestamp
protected void updateSocketTimestamp()Marks the socket "known good" usage timestamp with the current value of the system millisecond counter. -
toString
-