Class TcpSocketChannel

java.lang.Object
org.harctoolbox.harchardware.comm.TcpSocketChannel
All Implemented Interfaces:
Closeable, AutoCloseable, IBytesCommand, ICommandLineDevice, IHarcHardware

public class TcpSocketChannel extends Object implements ICommandLineDevice, IBytesCommand, AutoCloseable
This a helper class, to bundle the socket operations in a unified manner. It can be instantiated, possibly in multiple instances. It is not meant to be inherited from, or exported. It should therefore throw low-level exceptions, not HarcHardwareException.
  • Constructor Details

  • Method Details

    • connect

      public void connect() throws IOException
      Throws:
      IOException
    • close

      public void close(boolean force) throws IOException
      Parameters:
      force -
      Throws:
      IOException
    • getOut

      public OutputStream getOut()
    • getIn

      public InputStream getIn()
    • getBufferedIn

      public BufferedReader getBufferedIn()
    • isValid

      public boolean isValid()
      Description copied from interface: IHarcHardware
      Checks validity of an instance. Exact meaning can vary.
      Specified by:
      isValid in interface IHarcHardware
      Returns:
      validity of the instance.
    • setTimeout

      public void setTimeout(int timeout) throws SocketException
      Description copied from interface: IHarcHardware
      Set timeout in milliseconds. Exact semantics may be dependent on the hardware.
      Specified by:
      setTimeout in interface IHarcHardware
      Parameters:
      timeout - Timeout in milliseconds.
      Throws:
      SocketException
    • setVerbose

      public void setVerbose(boolean verbose)
      Description copied from interface: IHarcHardware
      Sets a verbosity flag, causing commands to be executed verbosely. Exact semantic depends on the implementation. Typically, all communication with the device is echoed to stderr.
      Specified by:
      setVerbose in interface IHarcHardware
      Parameters:
      verbose - on or off
    • setDebug

      public void setDebug(int debug)
      Description copied from interface: IHarcHardware
      Sets a debug parameter. Exact semantic depends on the implementation.
      Specified by:
      setDebug in interface IHarcHardware
    • sendString

      public void sendString(String cmd) throws IOException
      Description copied from interface: ICommandLineDevice
      Sends a command (a String) to the instance.
      Specified by:
      sendString in interface ICommandLineDevice
      Parameters:
      cmd - Command string to be sent.
      Throws:
      IOException
    • readString

      public String readString() throws IOException
      Description copied from interface: ICommandLineDevice
      Reads a line of text. A line is considered to be terminated by any one of a line feed ('\n'), a carriage return ('\r'), or a carriage return followed immediately by a linefeed.
      Specified by:
      readString in interface ICommandLineDevice
      Returns:
      A String containing the contents of the line, not including any line-termination characters, or null if no input available.
      Throws:
      IOException
    • readString

      public String readString(boolean wait) throws IOException
      Description copied from interface: ICommandLineDevice
      Reads a line of text. A line is considered to be terminated by any one of a line feed ('\n'), a carriage return ('\r'), or a carriage return followed immediately by a linefeed.
      Specified by:
      readString in interface ICommandLineDevice
      Parameters:
      wait - if true, wait until something arrives, otherwise return null if nothing there.
      Returns:
      A String containing the contents of the line, not including any line-termination characters, or null if no input available.
      Throws:
      IOException
    • close

      public void close() throws IOException
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Throws:
      IOException
    • sendBytes

      public void sendBytes(byte[] cmd) throws IOException
      Specified by:
      sendBytes in interface IBytesCommand
      Throws:
      IOException
    • readBytes

      public byte[] readBytes(int length) throws IOException
      Specified by:
      readBytes in interface IBytesCommand
      Throws:
      IOException
    • getVersion

      public String getVersion()
      Description copied from interface: IHarcHardware
      Returns the hardware version (not the version of the driver software). May be null if no relevant information available.
      Specified by:
      getVersion in interface IHarcHardware
      Returns:
      Version string. Semantics can vary.
    • open

      public void open()
      Description copied from interface: IHarcHardware
      Opens the device with previously set parameters.
      Specified by:
      open in interface IHarcHardware
    • ready

      public boolean ready() throws IOException
      Description copied from interface: ICommandLineDevice
      Tells whether this stream is ready to be read. A buffered character stream is ready if the buffer is not empty, or if the underlying character stream is ready.
      Specified by:
      ready in interface ICommandLineDevice
      Returns:
      True if the next read() is guaranteed not to block for input, false otherwise. Note that returning false does not guarantee that the next read will block.
      Throws:
      IOException - If an I/O error occurs
    • flushInput

      public void flushInput() throws IOException
      Specified by:
      flushInput in interface ICommandLineDevice
      Throws:
      IOException
    • getVerbose

      public boolean getVerbose()