Class SocketChannelClient

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

    Constructors
    Constructor
    Description
    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

    Modifier and Type
    Method
    Description
    void
    Close the underlying socket connection.
    void
    This method does nothing, there is no flush on socket channels.
    Get the underlying socket channel used by this socket wrapper.
    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 object 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 underlying socket to be used by this socket wrapper.
    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 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 Details

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

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

      public int readPartial(byte[] data, int offset, int len) throws Exception
      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:
      Exception - if the underlying input stream throws an exception.
    • 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.
    • 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.
    • 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.
    • 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.
    • 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.
    • 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.
    • getChannel

      public SocketChannel getChannel()
      Get the underlying socket channel 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 socket 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 stream is reached.
      Throws:
      Exception - if an IO error occurs.
    • 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.
    • 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