Package org.jppf.comm.socket
Class SocketChannelClient
java.lang.Object
org.jppf.comm.socket.SocketChannelClient
- All Implemented Interfaces:
Closeable,AutoCloseable,SocketWrapper
This SocketWrapper implementation relies on an underlying SocketChannel, in order to allow
writing to, and reading from, at the same time from the same socket connection.
- Author:
- Laurent Cohen, Jeff Rosen
-
Constructor Summary
ConstructorsConstructorDescriptionSocketChannelClient(boolean blocking) Initialize this socket channel client.SocketChannelClient(String host, int port, boolean blocking) Initialize this socket channel client with a specified host and port. -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Close the underlying socket connection.voidflush()This method does nothing, there is no flush on socket channels.Get the underlying socket channel used by this socket wrapper.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 object 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.voidsetChannel(SocketChannel channel) Set the underlying socket to be used by this socket wrapper.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 of channel input stream.voidwrite(byte[] data) Send an array of bytes over a TCP socket connection.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.
-
Constructor Details
-
SocketChannelClient
Initialize this socket channel client.- Parameters:
blocking- true if the socket channel is in blocking mode, false otherwise.- Throws:
IOException- if the socket channel could not be opened.
-
SocketChannelClient
Initialize this socket channel client with a specified host and port.- Parameters:
host- the host to connect to.port- the port to listen on the host.blocking- true if the socket channel is in blocking mode, false otherwise.- Throws:
IOException- if the socket channel could not be opened.
-
-
Method Details
-
send
Send an object over a TCP socket connection.- Specified by:
sendin interfaceSocketWrapper- 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.- Specified by:
sendBytesin interfaceSocketWrapper- 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.- 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:
Exception- if the underlying output stream throws an exception.
-
flush
This method does nothing, there is no flush on socket channels.- 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.
-
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.- Specified by:
receivein interfaceSocketWrapper- 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.
-
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:
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 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:
Exception- 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:
Exception- if the underlying input stream throws an exception.
-
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:
Exception- 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.
-
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.
-
getSerializer
Get an object serializer / deserializer to convert an object to or from an array of bytes.- Specified by:
getSerializerin interfaceSocketWrapper- Returns:
- an
ObjectSerializerinstance.
-
setSerializer
Set the object serializer / deserializer to convert an object to or from an array of bytes.- Specified by:
setSerializerin interfaceSocketWrapper- Parameters:
serializer- anObjectSerializerinstance.
-
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.
-
getChannel
Get the underlying socket channel used by this socket wrapper.- Returns:
- a SocketChannel instance.
-
setChannel
Set the underlying socket to be used by this socket wrapper.- Parameters:
channel- a SocketChannel instance.
-
skip
Skipnbytes of data from the socket of 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 stream is reached.
- Throws:
Exception- if an IO error occurs.
-
write
Send an array of bytes over a TCP socket connection.- Parameters:
data- the data to send.- Throws:
Exception- if the underlying output stream throws an exception.
-
getSocketTimestamp
public long getSocketTimestamp()Returns a timestamp that should reflect the system millisecond counter at the last known good usage of the underlying socket. NOTE: In this case we assume that the channel is "known good" and just return the current system tick.- Specified by:
getSocketTimestampin interfaceSocketWrapper- Returns:
- the socket usage timestamp
-