Interface SocketWrapper

All Superinterfaces:
AutoCloseable, Closeable
All Known Implementing Classes:
AbstractSocketWrapper, BootstrapSocketClient, SocketChannelClient, SocketClient

public interface SocketWrapper extends Closeable
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.
Author:
Laurent Cohen, Jeff Rosen
  • Method Summary

    Modifier and Type
    Method
    Description
    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.
    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.
    long
    Returns a timestamp that should reflect the system millisecond counter at the last known good usage of the underlying socket.
    boolean
    Determine whether this socket client is opened or not.
    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.
    receive(int timeout)
    Read an object from a TCP socket connection.
    receiveBytes(int timeout)
    Read an array of bytes from a TCP socket connection.
    void
    Send an object over 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
    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 socket or channel input stream.
    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 interface java.io.Closeable

    close
  • Method Details

    • send

      void send(Object o) throws Exception
      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

      void sendBytes(JPPFBuffer buf) throws Exception
      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

      void write(byte[] data, int offset, int len) throws Exception
      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

      void writeInt(int n) throws Exception
      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

      void flush() throws IOException
      Flush the data currently in the send buffer.
      Throws:
      IOException - if an I/O error occurs.
    • receive

      Object receive() throws Exception
      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

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

      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 all len bytes 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

      int readPartial(byte[] data, int offset, int len) throws Exception
      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.
      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

      int readInt() throws Exception
      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

      JPPFBuffer receiveBytes(int timeout) throws Exception
      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

      int skip(int n) throws Exception
      Skip n bytes 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

      void open() throws Exception
      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 ObjectSerializer instance.
    • setSerializer

      void setSerializer(ObjectSerializer serializer)
      Set the object serializer / deserializer to convert an object to or from an array of bytes.
      Parameters:
      serializer - an ObjectSerializer instance.
    • getHost

      String getHost()
      Get the remote host the underlying socket connects to.
      Returns:
      the host name or ip address as a string.
    • setHost

      void setHost(String host)
      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

      void setSocket(Socket socket)
      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