public interface EndPoint extends Closeable
The asynchronous scheduling methods of EndPoint
has been influenced by NIO.2 Futures and Completion
handlers, but does not use those actual interfaces because they have
some inefficiencies.
This class will frequently be used in conjunction with some of the utility
implementations of Callback
, such as FutureCallback
and
ExecutorCallback
. Examples are:
A FutureCallback can be used to block until an endpoint is ready to be filled from:
FutureCallback<String> future = new FutureCallback<>(); endpoint.fillInterested("ContextObj",future); ... String context = future.get(); // This blocks int filled=endpoint.fill(mybuffer);
By using a different callback, the read can be done asynchronously in its own dispatched thread:
endpoint.fillInterested("ContextObj",new ExecutorCallback<String>(executor) { public void onCompleted(String context) { int filled=endpoint.fill(mybuffer); ... } public void onFailed(String context,Throwable cause) {...} });
The executor callback can also be customized to not dispatch in some circumstances when it knows it can use the callback thread and does not need to dispatch.
The write contract is that the callback complete is not called until all data has been written or there is a failure. For blocking this looks like:
FutureCallback<String> future = new FutureCallback<>(); endpoint.write("ContextObj",future,headerBuffer,contentBuffer); String context = future.get(); // This blocks
Note also that multiple buffers may be passed in write so that gather writes can be done:
endpoint.write("ContextObj",new ExecutorCallback<String>(executor) { public void onCompleted(String context) { int filled=endpoint.fill(mybuffer); ... } public void onFailed(String context,Throwable cause) {...} },headerBuffer,contentBuffer);
Modifier and Type | Method and Description |
---|---|
void |
close()
Close any backing stream associated with the endpoint
|
int |
fill(ByteBuffer buffer)
Fill the passed buffer with data from this endpoint.
|
void |
fillInterested(Callback callback)
Requests callback methods to be invoked when a call to
fill(ByteBuffer) would return data or EOF. |
boolean |
flush(ByteBuffer... buffer)
Flush data from the passed header/buffer to this endpoint.
|
Connection |
getConnection() |
long |
getCreatedTimeStamp() |
long |
getIdleTimeout()
Get the max idle time in ms.
|
InetSocketAddress |
getLocalAddress() |
InetSocketAddress |
getRemoteAddress() |
Object |
getTransport() |
boolean |
isInputShutdown()
Test if the input is shutdown.
|
boolean |
isOpen() |
boolean |
isOutputShutdown()
Test if output is shutdown.
|
void |
onClose()
Callback method invoked when this
EndPoint is close. |
void |
onOpen()
Callback method invoked when this
EndPoint is opened. |
void |
setConnection(Connection connection) |
void |
setIdleTimeout(long idleTimeout)
Set the idle timeout.
|
void |
shutdownOutput()
Shutdown the output.
|
void |
write(Callback callback,
ByteBuffer... buffers)
Writes the given buffers via
flush(ByteBuffer...) and invokes callback methods when either
all the data has been flushed or an error occurs. |
InetSocketAddress getLocalAddress()
EndPoint
is bound, or null
if this EndPoint
does not represent a network connection.InetSocketAddress getRemoteAddress()
EndPoint
is bound, or null
if this EndPoint
does not represent a network connection.boolean isOpen()
long getCreatedTimeStamp()
void shutdownOutput()
This call indicates that no more data will be sent on this endpoint that that the remote end should read an EOF once all previously sent data has been consumed. Shutdown may be done either at the TCP/IP level, as a protocol exchange (Eg TLS close handshake) or both.
If the endpoint has isInputShutdown()
true, then this call has the same effect
as close()
.
boolean isOutputShutdown()
shutdownOutput()
or close()
.boolean isInputShutdown()
fill(ByteBuffer)
. Once the input is shutdown, all calls to
fill(ByteBuffer)
will return -1, until such time as the
end point is close, when they will return EofException
.void close()
close
in interface AutoCloseable
close
in interface Closeable
int fill(ByteBuffer buffer) throws IOException
buffer
- The buffer to fill. The position and limit are modified during the fill. After the
operation, the position is unchanged and the limit is increased to reflect the new data filled.int
value indicating the number of bytes
filled or -1 if EOF is read or the input is shutdown.EofException
- If the endpoint is closed.IOException
boolean flush(ByteBuffer... buffer) throws IOException
EofException
- If the endpoint is closed or output is shutdown.IOException
Object getTransport()
long getIdleTimeout()
The max idle time is the time the endpoint can be idle before extraordinary handling takes place.
void setIdleTimeout(long idleTimeout)
idleTimeout
- the idle timeout in MS. Timeout <= 0 implies an infinite timeoutvoid fillInterested(Callback callback) throws ReadPendingException
Requests callback methods to be invoked when a call to fill(ByteBuffer)
would return data or EOF.
callback
- the callback to call when an error occurs or we are readable.ReadPendingException
- if another read operation is concurrent.void write(Callback callback, ByteBuffer... buffers) throws WritePendingException
Writes the given buffers via flush(ByteBuffer...)
and invokes callback methods when either
all the data has been flushed or an error occurs.
callback
- the callback to call when an error occurs or the write completed.buffers
- one or more ByteBuffer
s that will be flushed.WritePendingException
- if another write operation is concurrent.Connection getConnection()
Connection
associated with this EndPoint
setConnection(Connection)
void setConnection(Connection connection)
connection
- the Connection
associated with this EndPoint
getConnection()
Copyright © 1995-2015 Webtide. All Rights Reserved.