Package eu.mihosoft.devcom
Class Controller<T,V extends DataConnection<T,?>>
java.lang.Object
eu.mihosoft.devcom.Controller<T,V>
- All Implemented Interfaces:
AutoCloseable
A device controller for concurrent communication, e.g., via (virtual) COM ports.
-
Constructor Summary
ConstructorDescriptionCreates a new controller instance.Controller
(Consumer<InterruptedException> onInterrupted) Creates a new controller instance. -
Method Summary
Modifier and TypeMethodDescriptionvoid
close()
Closes this controller (also shuts down schedulers/executors).boolean
close
(long timeout) Closes this controller.void
init
(DataConnection<T, V> dataConnection) Initializes this controller.sendCommand
(Command<T> cmd) Sends a command to the device and waits for a reply (blocking).sendCommand
(T msg) Sends the specified data to the device (blocking).sendCommandAsync
(Command<T> cmd) Sends a command to the device and waits for a reply (blocking).sendCommandAsync
(T msg) Sends the specified data to the device asynchronously.void
Sends the specified data to the device (no reply expected).sendDataAsync
(T msg) Sends the specified data to the device (no reply expected).Controller<T,
V> setCommandTimeout
(long milliseconds) Sets the command timeout (until the command is sent).Controller<T,
V> setDataTimeout
(long milliseconds) Sets the data timeout (until the command is sent).final void
setOnInterrupted
(Consumer<InterruptedException> onInterrupted) Specifies the consumer to call if the command queue thread is interrupted.
-
Constructor Details
-
Controller
public Controller()Creates a new controller instance. -
Controller
Creates a new controller instance.- Parameters:
onInterrupted
- the consumer to call if the command queue thread is interrupted (may be null)
-
-
Method Details
-
setCommandTimeout
Sets the command timeout (until the command is sent).- Parameters:
milliseconds
- duration in milliseconds (0 means no timeout)- Returns:
- this controller
-
setDataTimeout
Sets the data timeout (until the command is sent).- Parameters:
milliseconds
- duration in milliseconds (0 means no timeout)- Returns:
- this controller
-
setOnInterrupted
Specifies the consumer to call if the command queue thread is interrupted.- Parameters:
onInterrupted
- the consumer to call (may be null)
-
init
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 interfaceAutoCloseable
-
close
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
Sends a command to the device and waits for a reply (blocking).- Parameters:
cmd
- command to send
-
sendCommandAsync
Sends a command to the device and waits for a reply (blocking).- Parameters:
cmd
- command to send
-
sendCommandAsync
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
Sends the specified data to the device (blocking).- Parameters:
msg
- the message to send- Returns:
- the reply message
-
sendDataAsync
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
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
-