AsynchronousByteCharChannel and Timeouts

Gili cowwoc at bbs.darktech.org
Fri Aug 28 21:05:27 PDT 2009


Hi Alan,

Replies below...


Alan Bateman wrote:
> 
> Gili wrote:
>> :
>> Okay. I agree with everything you wrote except for the earlier post about
>> how it should be possible to implement read-with-timeout in terms of
>> read-forever. 
> Right, this is where we differ because AsynchronousSocketChannel does 
> not require the implementation to have a mechanism to cancel the 
> underlying I/O operation.
> 

I've got an idea I want to run by you. I propose defining read/write with
timeout at the AsynchronousByteChannel level because it'll simplify the
class hierarchy (no need to define separate helper classes for
read-with-timeout versus read-without-timeout). I believe that these methods
can be implemented across all platforms in the following way:

Case 1: read(forever), followed by Future.cancel()
\-> if the native layer supports read(timeout) then we emulate read(forever)
in terms of a loop containing read(timeout) where timeout is a reasonably
small value. The loop runs until data becomes available or until
Future.cancel() is invoked. In the latter case, Future.cancel() blocks until
the timeout completes and then return true. Any data read before
Future.cancel() is invoked is buffered for subsequent reads.
\-> if the native layer does not support read(timeout), Future.cancel()
returns false and subsequent reads throw RuntimeException (similar to
AsynchronousSocketChannel).

Case 2: read(forever) with a CompletionHandler: the native read terminates
when data becomes available or if the channel is closed.

Case 3: read(timeout) with a CompletionHandler
\-> if the native layer supports timeouts, the native read terminates when
data becomes available, a timeout occurs or the channel is closed.
\-> if the native layer does not support timeouts and a timeout occurs
before the native read completes then subsequent reads throw
RuntimeException.

Case 4: write(forever), followed by Future.cancel()
\-> if the native layer supports write(timeout) then we emulate
write(forever) in terms of a loop containing write(timeout) where timeout is
a reasonably small value. The loop runs until data becomes available or
until Future.cancel() is invoked. In the latter case, Future.cancel() blocks
until the timeout completes and then return true. If Future.cancel() is
invoked before a single byte is written then no data is written, otherwise
any data not yet sent is buffered to proceed subsequent writes.
\-> if the native layer does not support write(timeout), Future.cancel()
returns false and subsequent reads throw RuntimeException.

Case 5: write(timeout) with a CompletionHandler: the native write terminates
when the data is fully written or if the channel is closed.

Case 6: write(timeout) with a CompletionHandler
\-> if the native layer supports timeouts, the native write terminates when
the data is fully written, a timeout occurs or the channel is closed.
\-> if the native layer does not support timeouts and a timeout occurs
before the native write completes then subsequent reads throw
RuntimeException.

Let me know what you think.

Thanks,
Gili
-- 
View this message in context: http://n2.nabble.com/AsynchronousByteCharChannel-and-Timeouts-tp3363004p3540435.html
Sent from the nio-discuss mailing list archive at Nabble.com.


More information about the nio-discuss mailing list