NIO issues (repeating events)

O

Owen Jacobson

Salve.

I'm working on an event-driven network system based on an NIO selector
(for determining when an event has "fired"). The basic process is to
register a SelectableChannel and a handler for that channel, which
registers the channel with the underlying Selector (obtaining a
SelectionKey). I then loop, calling selector.select () and iterating over
the selector.selectedKeys () set and dispatching events as indicated by
the isAcceptable(), isConnectable(), isReadable(), and isWritable()
methods on each key.

Obviously, each channel must be placed in non-blocking mode before
registering it with the Selector.

I've run into a bit of an interesting behaviour on the part of the
Selector, though: once an event fires, it never "unfires". If socket A is
a ServerSocketChannel and is interested in OP_ACCEPT, when a connection
comes in the key for A is marked isAcceptable() and an appropriate event
is dispatched, which results in the handler calling A.accept () and
registering the resulting socket (B).

The problem arises when data arrives on B: selector.select() returns, as
expected, but the key for A is still marked isAcceptable (), even though
no new connection has been recieved for A -- only data on B. This works
the other way around, too: if a new connection arrives, select() returns,
and the key for B is marked isReadable() if any data has ever arrived on
that socket, even if none is present right now.

While these two situations are easy to detect (accept returns null, read
returns 0), it seems awfully wasteful to call accept/read/write on every
socket. What have I missed?

Owen
 
O

Owen Jacobson

What have I missed?

It never fails: in order to solve a head-crushingly obscure problem, post
about it on the internet. Not five minutes later, an n'th reading of the
Selector documentation illuminated me:

A key may be removed directly from the selected-key set by invoking the
set's remove method or by invoking the remove method of an iterator
obtained from the set. Keys are never removed from the selected-key set
in any other way; they are not, in particular, removed as a side effect
of selection operations.

Sorry for the waste of bandwidth!

Owen
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top