-
- All Superinterfaces:
java.lang.AutoCloseable
,java.io.Closeable
,java.lang.Iterable<byte[]>
,java.util.Iterator<byte[]>
- Enclosing class:
- Network
public static interface Network.Connection extends java.io.Closeable, java.lang.Iterable<byte[]>, java.util.Iterator<byte[]>
This class represents an established network connection ready read and write data.The
onReady()
callback will be called every time some data is available. You should then callnext()
to fetch the data.Once and if the connection is closed, the
onClose()
callback will be called.You may call the
Closeable.close()
method to close the connection yourself.
-
-
Method Summary
All Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description boolean
active()
Returns true if this connection has not been closedjava.lang.String
alpn()
Returns the Application-Layer Protocol Negotiation (ALPN) protocol once the TLS connection is establishedjava.lang.String
clientIp()
Returns the client IP addressboolean
hasNext()
Returns whether or not some data is available at the time of the call.boolean
isClientMode()
Returns whether or not this connection is a client connection connected to a remote serverboolean
isSecure()
Returns whether or not this connection is secure (typically using TLS or some other encryption mechanism)default boolean
isServerMode()
Returns whether or not this connection is a server connection connected to a remote clientdefault java.util.Iterator<byte[]>
iterator()
Returns an iterator over the data read by this connection.byte[]
next()
This is a non-blocking method to fetch the next batch of data that has been read from the network.Callback<java.lang.Void,Network.Connection>
onClose()
Gets the callback object that will be called once the connection is closed.Callback<java.lang.Void,Network.Connection>
onReady()
Gets the callback object that will be called when data has been read from the network and is available.java.lang.String
serverIp()
Returns the server IP addressvoid
timeout(long ms)
Sets the timeout on this connection.default void
write(byte[] data)
Writes the specified data on the networkdefault void
write(Data data)
Writes the specified data on the networkdefault void
write(java.lang.String data)
Writes the specified data on the networkvoid
write(java.nio.ByteBuffer data)
Writes the specified data on the network.
-
-
-
Method Detail
-
isSecure
boolean isSecure()
Returns whether or not this connection is secure (typically using TLS or some other encryption mechanism)- Returns:
- whether or not this connection is secure
-
isClientMode
boolean isClientMode()
Returns whether or not this connection is a client connection connected to a remote server- Returns:
- whether or not this connection is a client connection connected to a remote server
-
isServerMode
default boolean isServerMode()
Returns whether or not this connection is a server connection connected to a remote client- Returns:
- whether or not this connection is a server connection connected to a remote client
-
onReady
Callback<java.lang.Void,Network.Connection> onReady()
Gets the callback object that will be called when data has been read from the network and is available. Since data read on the network should preserve its ordering, the callback signals that data is available and you should then callnext()
to fetch it.- Returns:
- the onReady callback
-
next
byte[] next()
This is a non-blocking method to fetch the next batch of data that has been read from the network. There might be more than one batch of data, so use this method in a loop. If no further data is available, this method returns null.This method is not thread safe, you should handle the fact that
onReady()
might be called at any time and the callback may be activated to use this method in parallel.Consider the following construct:
AtomicBoolean busy = new AtomicBoolean(false); connection.onReady().then((c) -> { while( c.hasNext() ) { if (!busy.compareAndSet(false, true)) return; try { for( byte[] data = c.next(); data != null; data = c.next() ) { // ... process data } } finally { busy.set(false); } } });
- Specified by:
next
in interfacejava.util.Iterator<byte[]>
- Returns:
- the next batch of data or null if no data is available
-
hasNext
boolean hasNext()
Returns whether or not some data is available at the time of the call. This method does not ensure that a call tonext()
will return a value because of thread concurrency.- Specified by:
hasNext
in interfacejava.util.Iterator<byte[]>
- Returns:
- whether or not some data is available
-
iterator
default java.util.Iterator<byte[]> iterator()
Returns an iterator over the data read by this connection. It may return a null element in case of thread concurrency.
-
write
default void write(byte[] data) throws java.io.IOException
Writes the specified data on the network- Parameters:
data
- the data to write- Throws:
java.io.IOException
- if a underlying I/O error happens
-
write
default void write(java.lang.String data) throws java.io.IOException
Writes the specified data on the network- Parameters:
data
- the data to write- Throws:
java.io.IOException
- if a underlying I/O error happens
-
write
default void write(Data data) throws java.io.IOException
Writes the specified data on the network- Parameters:
data
- the data to write- Throws:
java.io.IOException
- if a underlying I/O error happens
-
write
void write(java.nio.ByteBuffer data)
Writes the specified data on the network. This method is synchronous, so if you want an asynchronous behavior, you can wrap the call in anExecutor
method.- Parameters:
data
- the data to write- See Also:
Executor.io(aeonics.util.Functions.Runnable)
-
onClose
Callback<java.lang.Void,Network.Connection> onClose()
Gets the callback object that will be called once the connection is closed.- Returns:
- the onClose callback
-
timeout
void timeout(long ms)
Sets the timeout on this connection. If no network activity is detected in the specified interval, the connection shall be closed.- Parameters:
ms
- the timeout delay in milliseconds
-
clientIp
java.lang.String clientIp()
Returns the client IP address- Returns:
- the client IP address
-
serverIp
java.lang.String serverIp()
Returns the server IP address- Returns:
- the server IP address
-
alpn
java.lang.String alpn()
Returns the Application-Layer Protocol Negotiation (ALPN) protocol once the TLS connection is established- Returns:
- the ALPN or an empty string if no protocol was negotiated or if ALPN is not available
-
active
boolean active()
Returns true if this connection has not been closed- Returns:
- true if this connection has not been closed
-
-