org.jppf.comm.socket
Class AbstractSocketWrapper

java.lang.Object
  extended by org.jppf.comm.socket.AbstractSocketWrapper
All Implemented Interfaces:
SocketWrapper
Direct Known Subclasses:
BootstrapSocketClient, SocketClient

public abstract class AbstractSocketWrapper
extends Object
implements SocketWrapper

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
protected  DataInputStream dis
          A buffered stream built on top of to the underlying socket's input stream.
protected  DataOutputStream dos
          A reference to the underlying socket's output stream.
protected  String host
          The host the socket connects to.
protected  boolean opened
          Flag indicating the opened state of the underlying socket.
protected  int port
          The port number on the host the socket connects to.
protected  ObjectSerializer serializer
          Used to serialize or deserialize an object into or from an array of bytes.
protected  Socket socket
          The underlying socket wrapped by this SocketClient.
 
Fields inherited from interface org.jppf.comm.socket.SocketWrapper
SOCKET_RECEIVE_BUFFER_SIZE
 
Constructor Summary
protected AbstractSocketWrapper()
          Default constructor is visible to subclasses only.
  AbstractSocketWrapper(Socket socket)
          Initialize this socket client with an already opened and connected socket.
  AbstractSocketWrapper(String host, int port, ObjectSerializer serializer)
          Initialize this socket client and connect it to the specified host on the specified port.
 
Method Summary
protected  void checkOpened()
          Check whether the underlying socket is opened or not.
 void close()
          Close the underlying socket connection.
 void flush()
          Flush the data currently in the send buffer.
 String getHost()
          Get the remote host the underlying socket connects to.
 int getPort()
          Get the remote port the underlying socket connects to.
 ObjectSerializer getSerializer()
          This implementation does nothing.
 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.
protected  void initStreams()
          Initialize all the stream used for receiving and sending objects through the underlying socket connection.
 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.
 JPPFBuffer receiveBytes(int timeout)
          Read an object from a TCP socket connection.
 void sendBytes(JPPFBuffer buf)
          Send an array of bytes over a TCP socket connection.
 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)
          This implementation does nothing.
 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 socket or channel input stream.
protected  void updateSocketTimestamp()
          Marks the socket "known good" usage timestamp with the current value of the system millisecond counter.
 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
 
Methods inherited from interface org.jppf.comm.socket.SocketWrapper
receive, send
 

Field Detail

socket

protected Socket socket
The underlying socket wrapped by this SocketClient.


dos

protected DataOutputStream dos
A reference to the underlying socket's output stream.


dis

protected DataInputStream dis
A buffered stream built on top of to the underlying socket's input stream.


host

protected String host
The host the socket connects to.


port

protected int port
The port number on the host the socket connects to.


opened

protected boolean opened
Flag indicating the opened state of the underlying socket.


serializer

protected ObjectSerializer serializer
Used to serialize or deserialize an object into or from an array of bytes.

Constructor Detail

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(Socket socket)
                      throws JPPFException
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 Detail

sendBytes

public void sendBytes(JPPFBuffer buf)
               throws IOException
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:
IOException - 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
Flush the data currently in the send buffer.

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()

receiveBytes

public JPPFBuffer receiveBytes(int timeout)
                        throws IOException
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:
IOException - if the underlying input stream throws an exception.
See Also:
SocketWrapper.receiveBytes(int)

read

public int read(byte[] data,
                int offset,
                int len)
         throws IOException
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:
IOException - 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 final 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()

initStreams

protected final void initStreams()
                          throws IOException
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

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()

checkOpened

protected void checkOpened()
                    throws ConnectException
Check whether the underlying socket is opened or not.

Throws:
ConnectException - if the connection is not opened.

getSerializer

public ObjectSerializer getSerializer()
This implementation does nothing.

Specified by:
getSerializer in interface SocketWrapper
Returns:
null.
See Also:
SocketWrapper.getSerializer()

setSerializer

public void setSerializer(ObjectSerializer serializer)
This implementation does nothing.

Specified by:
setSerializer in interface SocketWrapper
Parameters:
serializer - not used.
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.

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.

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.

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.

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)

skip

public int skip(int n)
         throws Exception
Skip n bytes of data from the socket or 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 file is reached..
Throws:
Exception - if an IO error occurs.
See Also:
SocketWrapper.skip(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.

Specified by:
getSocketTimestamp in interface SocketWrapper
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.



Copyright © 2005-2010 JPPF Team.