org.jppf.comm.socket
Class SocketChannelClient

java.lang.Object
  extended by org.jppf.comm.socket.SocketChannelClient
All Implemented Interfaces:
SocketWrapper

public class SocketChannelClient
extends Object
implements 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

Field Summary
 
Fields inherited from interface org.jppf.comm.socket.SocketWrapper
SOCKET_RECEIVE_BUFFER_SIZE
 
Constructor Summary
SocketChannelClient(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
 void close()
          Close the underlying socket connection.
 void flush()
          This method does nothing, there is no flush on socket channels.
 SocketChannel getChannel()
          Get the underlying socket used by this socket wrapper.
 String getHost()
          Get the remote host the underlying socket connects to.
 int getPort()
          Get the remote port the underlying socket connects to.
 ObjectSerializer getSerializer()
          Get an object serializer / deserializer to convert an object to or from an array of bytes.
 Socket getSocket()
          Get the underlying socket used by this socket wrapper.
 long getSocketTimestamp()
          Returns a timestamp that should reflect the system millisecond counter at the last known good usage of the underlying socket.
 boolean isOpened()
          Determine whether this socket client is opened or not.
 void open()
          Open the underlying socket connection.
 int read(byte[] data, int offset, int len)
          Read len bytes from a TCP connection into a byte array, starting at position offset in that array.
 int readInt()
          Read an int value from a socket connection.
 Object receive()
          Read an object from a TCP socket connection.
 Object receive(int timeout)
          Read an object from a TCP socket connection.
 JPPFBuffer receiveBytes(int timeout)
          Read an object from a TCP socket connection.
 void send(Object o)
          Send an object over a TCP socket connection.
 void sendBytes(JPPFBuffer buf)
          Send an array of bytes over a TCP socket connection.
 void setChannel(SocketChannel channel)
          Set the underlying socket to be used by this socket wrapper.
 void setHost(String host)
          Set the remote host the underlying socket connects to.
 void setPort(int port)
          Get the remote port the underlying socket connects to.
 void setSerializer(ObjectSerializer serializer)
          Set the object serializer / deserializer to convert an object to or from an array of bytes.
 void setSocket(Socket socket)
          Set the underlying socket to be used by this socket wrapper.
 int skip(int n)
          Skip n bytes of data from the sokcet of channel input stream.
 void write(byte[] data)
          Send an array of bytes over a TCP socket connection.
 void write(byte[] data, int offset, int len)
          Send an array of bytes over a TCP socket connection.
 void writeInt(int n)
          Write an int value over a socket connection.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SocketChannelClient

public SocketChannelClient(boolean blocking)
                    throws IOException
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

public SocketChannelClient(String host,
                           int port,
                           boolean blocking)
                    throws IOException
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 Detail

send

public void send(Object o)
          throws Exception
Send an object over a TCP socket connection.

Specified by:
send in interface SocketWrapper
Parameters:
o - the object to send.
Throws:
Exception - if the underlying output stream throws an exception.
See Also:
SocketWrapper.send(java.lang.Object)

sendBytes

public void sendBytes(JPPFBuffer buf)
               throws Exception
Send an array of bytes over a TCP socket connection.

Specified by:
sendBytes in interface SocketWrapper
Parameters:
buf - the buffer container for the data to send.
Throws:
Exception - if the underlying output stream throws an exception.
See Also:
SocketWrapper.sendBytes(org.jppf.utils.JPPFBuffer)

write

public void write(byte[] data,
                  int offset,
                  int len)
           throws Exception
Send an array of bytes over a TCP socket connection.

Specified by:
write in interface SocketWrapper
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.
See Also:
SocketWrapper.write(byte[], int, int)

writeInt

public void writeInt(int n)
              throws Exception
Write an int value over a socket connection.

Specified by:
writeInt in interface SocketWrapper
Parameters:
n - the value to write.
Throws:
Exception - if the underlying output stream throws an exception.
See Also:
SocketWrapper.writeInt(int)

flush

public void flush()
           throws IOException
This method does nothing, there is no flush on socket channels.

Specified by:
flush in interface SocketWrapper
Throws:
IOException - if an I/O error occurs.
See Also:
SocketWrapper.flush()

receive

public Object receive()
               throws Exception
Read an object from a TCP socket connection. This method blocks until an object is received.

Specified by:
receive in interface SocketWrapper
Returns:
the object that was read from the underlying input stream.
Throws:
Exception - if the underlying input stream throws an exception.
See Also:
SocketWrapper.receive()

receive

public Object receive(int timeout)
               throws Exception
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:
receive in interface SocketWrapper
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.
See Also:
SocketWrapper.receive(int)

receiveBytes

public JPPFBuffer receiveBytes(int timeout)
                        throws Exception
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:
receiveBytes in interface SocketWrapper
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.
See Also:
SocketWrapper.receiveBytes(int)

read

public int read(byte[] data,
                int offset,
                int len)
         throws Exception
Read len bytes from a TCP connection into a byte array, starting at position offset in that array. This method blocks until at least one byte of data is received.

Specified by:
read in interface SocketWrapper
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.
See Also:
SocketWrapper.read(byte[], int, int)

readInt

public int readInt()
            throws Exception
Read an int value from a socket connection.

Specified by:
readInt in interface SocketWrapper
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.
See Also:
SocketWrapper.readInt()

open

public void open()
          throws ConnectException,
                 IOException
Open the underlying socket connection.

Specified by:
open in interface SocketWrapper
Throws:
ConnectException - if the socket fails to connect.
IOException - if the underlying input and output streams raise an error.
See Also:
SocketWrapper.open()

close

public void close()
           throws ConnectException,
                  IOException
Close the underlying socket connection.

Specified by:
close in interface SocketWrapper
Throws:
ConnectException - if the socket connection is not opened.
IOException - if the underlying input and output streams raise an error.
See Also:
SocketWrapper.close()

isOpened

public boolean isOpened()
Determine whether this socket client is opened or not.

Specified by:
isOpened in interface SocketWrapper
Returns:
true if this client is opened, false otherwise.
See Also:
SocketWrapper.isOpened()

getSerializer

public ObjectSerializer getSerializer()
Get an object serializer / deserializer to convert an object to or from an array of bytes.

Specified by:
getSerializer in interface SocketWrapper
Returns:
an ObjectSerializer instance.
See Also:
SocketWrapper.getSerializer()

setSerializer

public void setSerializer(ObjectSerializer serializer)
Set the object serializer / deserializer to convert an object to or from an array of bytes.

Specified by:
setSerializer in interface SocketWrapper
Parameters:
serializer - an ObjectSerializer instance.
See Also:
SocketWrapper.setSerializer(org.jppf.utils.ObjectSerializer)

getHost

public String getHost()
Get the remote host the underlying socket connects to.

Specified by:
getHost in interface SocketWrapper
Returns:
the host name or ip address as a string.
See Also:
SocketWrapper.getHost()

setHost

public void setHost(String host)
Set the remote host the underlying socket connects to.

Specified by:
setHost in interface SocketWrapper
Parameters:
host - the host name or ip address as a string.
See Also:
SocketWrapper.setHost(java.lang.String)

getPort

public int getPort()
Get the remote port the underlying socket connects to.

Specified by:
getPort in interface SocketWrapper
Returns:
the port number on the remote host.
See Also:
SocketWrapper.getPort()

setPort

public void setPort(int port)
Get the remote port the underlying socket connects to.

Specified by:
setPort in interface SocketWrapper
Parameters:
port - the port number on the remote host.
See Also:
SocketWrapper.setPort(int)

getSocket

public Socket getSocket()
Get the underlying socket used by this socket wrapper.

Specified by:
getSocket in interface SocketWrapper
Returns:
a Socket instance.
See Also:
SocketWrapper.getSocket()

setSocket

public void setSocket(Socket socket)
Set the underlying socket to be used by this socket wrapper.

Specified by:
setSocket in interface SocketWrapper
Parameters:
socket - a Socket instance.
See Also:
SocketWrapper.setSocket(java.net.Socket)

getChannel

public SocketChannel getChannel()
Get the underlying socket used by this socket wrapper.

Returns:
a SocketChannel instance.

setChannel

public void setChannel(SocketChannel channel)
Set the underlying socket to be used by this socket wrapper.

Parameters:
channel - a SocketChannel instance.

skip

public int skip(int n)
         throws Exception
Skip n bytes of data from the sokcet of channel input stream.

Specified by:
skip in interface SocketWrapper
Parameters:
n - the number of bytes to skip.
Returns:
the actual number of bytes skipped, or -1 if the end of sream is reached.
Throws:
Exception - if an IO error occurs.
See Also:
SocketWrapper.skip(int)

write

public void write(byte[] data)
           throws Exception
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.
See Also:
write(byte[],int,int)

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:
getSocketTimestamp in interface SocketWrapper
Returns:
the socket usage timestamp


Copyright © 2005-2010 JPPF Team.