Class Controller<T,V extends DataConnection<T,?>>

java.lang.Object
eu.mihosoft.devcom.Controller<T,V>
All Implemented Interfaces:
AutoCloseable

public class Controller<T,V extends DataConnection<T,?>> extends Object implements AutoCloseable
A device controller for concurrent communication, e.g., via (virtual) COM ports.
  • Constructor Details

    • Controller

      public Controller()
      Creates a new controller instance.
    • Controller

      public Controller(Consumer<InterruptedException> onInterrupted)
      Creates a new controller instance.
      Parameters:
      onInterrupted - the consumer to call if the command queue thread is interrupted (may be null)
  • Method Details

    • setCommandTimeout

      public Controller<T,V> setCommandTimeout(long milliseconds)
      Sets the command timeout (until the command is sent).
      Parameters:
      milliseconds - duration in milliseconds (0 means no timeout)
      Returns:
      this controller
    • setDataTimeout

      public Controller<T,V> setDataTimeout(long milliseconds)
      Sets the data timeout (until the command is sent).
      Parameters:
      milliseconds - duration in milliseconds (0 means no timeout)
      Returns:
      this controller
    • setOnInterrupted

      public final void setOnInterrupted(Consumer<InterruptedException> onInterrupted)
      Specifies the consumer to call if the command queue thread is interrupted.
      Parameters:
      onInterrupted - the consumer to call (may be null)
    • init

      public void init(DataConnection<T,V> dataConnection)
      Initializes this controller.
      Parameters:
      dataConnection - the data connection to use for communication
    • close

      public void close()
      Closes this controller (also shuts down schedulers/executors).
      Specified by:
      close in interface AutoCloseable
    • close

      public boolean close(long timeout) throws InterruptedException
      Closes this controller. This method blocks until the controller is closed (all tasks have been executed), or the timeout occurs. If no timeout is specified the associated executor will shutdown immediately.
      Parameters:
      timeout - the timeout in milliseconds
      Throws:
      InterruptedException
    • sendCommand

      public Command<T> sendCommand(Command<T> cmd)
      Sends a command to the device and waits for a reply (blocking).
      Parameters:
      cmd - command to send
    • sendCommandAsync

      public Command<T> sendCommandAsync(Command<T> cmd)
      Sends a command to the device and waits for a reply (blocking).
      Parameters:
      cmd - command to send
    • sendCommandAsync

      public Command<T> sendCommandAsync(T msg)
      Sends the specified data to the device asynchronously.
      Parameters:
      msg - the message to send
      Returns:
      a future that will be completed when the reply message has been received
    • sendCommand

      public T sendCommand(T msg)
      Sends the specified data to the device (blocking).
      Parameters:
      msg - the message to send
      Returns:
      the reply message
    • sendDataAsync

      public CompletableFuture<T> sendDataAsync(T msg)
      Sends the specified data to the device (no reply expected).
      Parameters:
      msg - the message to send
      Returns:
      a future that will be completed when the data has been sent
    • sendData

      public void sendData(T msg)
      Sends the specified data to the device (no reply expected). This method blocks until the data has been sent.
      Parameters:
      msg - the message to send