Class AbstractSocketWrapper

java.lang.Object
org.jppf.comm.socket.AbstractSocketWrapper
All Implemented Interfaces:
Closeable, AutoCloseable, 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

    Fields
    Modifier and Type
    Field
    Description
    protected DataInputStream
    A buffered stream built on top of to the underlying socket's input stream.
    A reference to the underlying socket's output stream.
    protected String
    The host the socket connects to.
    protected boolean
    Flag indicating the opened state of the underlying socket.
    protected int
    The port number on the host the socket connects to.
    Used to serialize or deserialize an object into or from an array of bytes.
    protected Socket
    The underlying socket wrapped by this SocketClient.
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    protected
    Default 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.
     
    Initialize this socket client with an already opened and connected socket.
  • Method Summary

    Modifier and Type
    Method
    Description
    protected void
    Check whether the underlying socket is opened or not.
    void
    Close the underlying socket connection.
    void
    Flush the data currently in the send buffer.
    Get the remote host the underlying socket connects to.
    int
    Get the remote port the underlying socket connects to.
    This implementation does nothing.
    Get the underlying socket used by this socket wrapper.
    long
    Returns a timestamp that should reflect the system millisecond counter at the last known good usage of the underlying socket.
    protected final void
    Initialize all the stream used for receiving and sending objects through the underlying socket connection.
    boolean
    Determine whether this socket client is opened or not.
    final void
    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
    Read an int value from a socket connection.
    int
    readPartial(byte[] data, int offset, int len)
    Read bytes from a TCP connection into a byte array, starting at position offset in that array.
    Read an object from a TCP socket connection.
    receiveBytes(int timeout)
    Read an object from a TCP socket connection.
    void
    Send an array of bytes over a TCP socket connection.
    void
    Set the remote host the underlying socket connects to.
    void
    setPort(int port)
    Get the remote port the underlying socket connects to.
    void
    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
    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, wait, wait, wait

    Methods inherited from interface org.jppf.comm.socket.SocketWrapper

    receive, send
  • Field Details

    • 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 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

      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 Details

    • 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.
    • 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.
    • writeInt

      public void writeInt(int n) throws IOException
      Write an int value over a socket connection.
      Specified by:
      writeInt in interface SocketWrapper
      Parameters:
      n - the value to write.
      Throws:
      IOException - if the underlying output stream throws an exception.
    • 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.
    • 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.
    • 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.
    • 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.
    • readPartial

      public int readPartial(byte[] data, int offset, int len) throws IOException
      Description copied from interface: SocketWrapper
      Read 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:
      readPartial 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 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

      public int readInt() throws IOException
      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:
      IOException - if the underlying input stream throws an exception.
    • 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.
    • 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 AutoCloseable
      Specified by:
      close in interface Closeable
      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:
      isOpened in interface SocketWrapper
      Returns:
      true if this client is opened, false otherwise.
    • 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:
    • setSerializer

      public void setSerializer(ObjectSerializer serializer)
      This implementation does nothing.
      Specified by:
      setSerializer in interface SocketWrapper
      Parameters:
      serializer - not used.
    • 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.
    • 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.
    • 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.
    • 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.
    • toString

      public String toString()
      Overrides:
      toString in class Object