Package org.jppf.comm.socket
Interface SocketWrapper
- All Superinterfaces:
AutoCloseable,Closeable
- All Known Implementing Classes:
AbstractSocketWrapper,BootstrapSocketClient,SocketChannelClient,SocketClient
This interface is common to all classes wrapping an underlying socket connections.
The underlying socket API can be either based on a socket channel (blocking or non-blocking), or just based on a plain socket.
The underlying socket API can be either based on a socket channel (blocking or non-blocking), or just based on a plain socket.
- Author:
- Laurent Cohen, Jeff Rosen
-
Method Summary
Modifier and TypeMethodDescriptionvoidflush()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.Get an object serializer / deserializer to convert an object to or from an array of bytes.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.booleanisOpened()Determine whether this socket client is opened or not.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.receive(int timeout) Read an object from a TCP socket connection.receiveBytes(int timeout) Read an array of bytes from a TCP socket connection.voidSend an object over 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) Set the object serializer / deserializer to convert an object to or from an array of bytes.voidSet the underlying socket to be used by this socket wrapper.intskip(int n) Skipnbytes of data from the socket or channel input stream.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.
-
Method Details
-
send
Send an object over a TCP socket connection.- Parameters:
o- the object to send.- Throws:
Exception- if the underlying output stream throws an exception.
-
sendBytes
Send an array of bytes over a TCP socket connection.- Parameters:
buf- the buffer container for the data to send.- Throws:
Exception- if the underlying output stream throws an exception.
-
write
Send an array of bytes over a TCP socket connection.- 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.- Parameters:
n- the value to write.- Throws:
Exception- if the underlying output stream throws an exception.
-
flush
Flush the data currently in the send buffer.- 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.- Returns:
- the object that was read from the underlying input stream.
- Throws:
Exception- if the underlying input stream throws an exception.
-
receive
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.- Parameters:
timeout- timeout after which the operation is aborted. A timeout of zero is interpreted as an infinite timeout.- Returns:
- the object that was read from the underlying input stream or null if the operation timed out.
- Throws:
Exception- 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 alllenbytes of data are received.- 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:
Exception- if the underlying input stream throws an exception.
-
readPartial
Read 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.- 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:
Exception- if the underlying input stream throws an exception.
-
readInt
Read an int value from a socket connection.- Returns:
- n the value to read from the socket, or -1 if end of stream was reached.
- Throws:
Exception- if the underlying input stream throws an exception.
-
receiveBytes
Read an array of bytes from a TCP socket connection. The data read is prefixed by an int header whose value is the actual length of data to read. This method blocks until data is received or the specified timeout has expired, whichever happens first.- Parameters:
timeout- timeout after which the operation is aborted. A timeout of zero is interpreted as an infinite timeout.- Returns:
- a buffer holding the length of data and the data itself.
- Throws:
Exception- if the underlying input stream throws an exception.
-
skip
Skipnbytes of data from the socket or channel input stream.- Parameters:
n- the number of bytes to skip.- Returns:
- the actual number of bytes skipped.
- Throws:
Exception- if an IO error occurs.
-
open
Open the underlying socket connection.- Throws:
Exception- if the underlying input and output streams raise an error.
-
isOpened
boolean isOpened()Determine whether this socket client is opened or not.- Returns:
- true if this client is opened, false otherwise.
-
getSerializer
ObjectSerializer getSerializer()Get an object serializer / deserializer to convert an object to or from an array of bytes.- Returns:
- an
ObjectSerializerinstance.
-
setSerializer
Set the object serializer / deserializer to convert an object to or from an array of bytes.- Parameters:
serializer- anObjectSerializerinstance.
-
getHost
String getHost()Get the remote host the underlying socket connects to.- Returns:
- the host name or ip address as a string.
-
setHost
Set the remote host the underlying socket connects to.- Parameters:
host- the host name or ip address as a string.
-
getPort
int getPort()Get the remote port the underlying socket connects to.- Returns:
- the port number on the remote host.
-
setPort
void setPort(int port) Get the remote port the underlying socket connects to.- Parameters:
port- the port number on the remote host.
-
getSocket
Socket getSocket()Get the underlying socket used by this socket wrapper.- Returns:
- a Socket instance.
-
setSocket
Set the underlying socket to be used by this socket wrapper.- Parameters:
socket- a Socket instance.
-
getSocketTimestamp
long getSocketTimestamp()Returns a timestamp that should reflect the system millisecond counter at the last known good usage of the underlying socket.- Returns:
- the socket usage timestamp
-