Package eu.mihosoft.devcom
Class StreamConnection<T>
java.lang.Object
eu.mihosoft.devcom.StreamConnection<T>
- All Implemented Interfaces:
DataConnection<T,
,StreamConnection<T>> AutoCloseable
public final class StreamConnection<T>
extends Object
implements DataConnection<T,StreamConnection<T>>
Stream connection for reading and writing data from/to io streams.
-
Nested Class Summary
Nested classes/interfaces inherited from interface eu.mihosoft.devcom.DataConnection
DataConnection.Subscription
-
Constructor Summary
ConstructorDescriptionStreamConnection
(DataFormat<T> format) Creates a new connection instance.StreamConnection
(DataFormat<T> format, Consumer<StreamConnection<T>> onConnectionOpened, BiConsumer<StreamConnection<T>, Exception> onIOError) Creates a new connection instance. -
Method Summary
Modifier and TypeMethodDescriptionvoid
close()
Closes the connection to the specified port.Returns the data format used by this connection.boolean
isOpen()
Indicates whether this connection is currently open.void
open()
Opens the specified port and connects to it.void
open
(InputStream inputStream, OutputStream outputStream) Opens the specified port and connects to it.Registers a listener that is notified whenever the connection is closed.Registers a listener that is notified whenever the connection is opened.Registers a data listener that is notified whenever a message has been received.Registers a listener that is notified whenever an I/O error occurees.void
setOnConnectionClosed
(Consumer<DataConnection<T, ?>> onConnectionClosed) setOnConnectionOpened
(Consumer<StreamConnection<T>> onConnectionOpened) Specifies the action to be performed if the connection has been opened.setOnDataReceived
(Consumer<T> onDataReceived) Sets the action to be executed if data has been received.setOnIOError
(BiConsumer<StreamConnection<T>, Exception> onIOError) Specifies the action to be performed if an I/O error occurs.setStreams
(InputStream inputStream, OutputStream outputStream) Sets the configuration to use for connecting to a port.void
Writes the specified raw message to the output stream.
-
Constructor Details
-
StreamConnection
Creates a new connection instance.- Parameters:
format
- the data format to use for communication
-
StreamConnection
public StreamConnection(DataFormat<T> format, Consumer<StreamConnection<T>> onConnectionOpened, BiConsumer<StreamConnection<T>, Exception> onIOError) Creates a new connection instance.- Parameters:
onConnectionOpened
- task to be executed if the selected COM-port has been successfully openedonIOError
- task to be executed if an io error has occurred
-
-
Method Details
-
setOnDataReceived
Description copied from interface:DataConnection
Sets the action to be executed if data has been received.- Specified by:
setOnDataReceived
in interfaceDataConnection<T,
StreamConnection<T>> - Parameters:
onDataReceived
- consumer to be called if data has been received
-
setOnIOError
Description copied from interface:DataConnection
Specifies the action to be performed if an I/O error occurs.- Specified by:
setOnIOError
in interfaceDataConnection<T,
StreamConnection<T>> - Parameters:
onIOError
- the action to be performed if an I/O error occurs
-
setOnConnectionOpened
Specifies the action to be performed if the connection has been opened.- Parameters:
onConnectionOpened
- the action to be performed if the connection has been opened
-
registerDataListener
Description copied from interface:DataConnection
Registers a data listener that is notified whenever a message has been received.- Specified by:
registerDataListener
in interfaceDataConnection<T,
StreamConnection<T>> - Parameters:
l
- the data listener to register- Returns:
- a subscription that allows a listener to be unsubscribed
-
registerConnectionOpenedListener
public DataConnection.Subscription registerConnectionOpenedListener(Consumer<DataConnection<T, ?>> l) Description copied from interface:DataConnection
Registers a listener that is notified whenever the connection is opened.- Specified by:
registerConnectionOpenedListener
in interfaceDataConnection<T,
StreamConnection<T>> - Parameters:
l
- the data listener to register- Returns:
- a subscription that allows a listener to be unsubscribed
-
registerConnectionClosedListener
public DataConnection.Subscription registerConnectionClosedListener(Consumer<DataConnection<T, ?>> l) Description copied from interface:DataConnection
Registers a listener that is notified whenever the connection is closed.- Specified by:
registerConnectionClosedListener
in interfaceDataConnection<T,
StreamConnection<T>> - Parameters:
l
- the data listener to register- Returns:
- a subscription that allows a listener to be unsubscribed
-
registerIOErrorListener
public DataConnection.Subscription registerIOErrorListener(BiConsumer<DataConnection<T, ?>, Exception> l) Description copied from interface:DataConnection
Registers a listener that is notified whenever an I/O error occurees.- Specified by:
registerIOErrorListener
in interfaceDataConnection<T,
StreamConnection<T>> - Parameters:
l
- the data listener to register- Returns:
- a subscription that allows a listener to be unsubscribed
-
open
Opens the specified port and connects to it.- Parameters:
inputStream
- input stream to be used by this connectionoutputStream
- output stream to be used by this connection
-
setStreams
Sets the configuration to use for connecting to a port.- Parameters:
inputStream
- the input stream to be used by this connectionoutputStream
- the output stream to be used by this connection- Returns:
- this connection
-
open
public void open()Opens the specified port and connects to it.- Specified by:
open
in interfaceDataConnection<T,
StreamConnection<T>>
-
writeData
Description copied from interface:DataConnection
Writes the specified raw message to the output stream.- Specified by:
writeData
in interfaceDataConnection<T,
StreamConnection<T>> - Parameters:
msg
- the message to send- Throws:
IOException
- if an i/o error occurs during message sending
-
close
public void close()Closes the connection to the specified port.- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceDataConnection<T,
StreamConnection<T>>
-
setOnConnectionClosed
- Specified by:
setOnConnectionClosed
in interfaceDataConnection<T,
StreamConnection<T>>
-
getFormat
Description copied from interface:DataConnection
Returns the data format used by this connection.- Specified by:
getFormat
in interfaceDataConnection<T,
StreamConnection<T>> - Returns:
- the data format used by this connection
-
isOpen
public boolean isOpen()Description copied from interface:DataConnection
Indicates whether this connection is currently open.- Specified by:
isOpen
in interfaceDataConnection<T,
StreamConnection<T>> - Returns:
true
if this connection is currently open;false
otherwise
-