org.opcfoundation.ua.utils.asyncsocket
Class AsyncSelector

java.lang.Object
  extended by org.opcfoundation.ua.utils.asyncsocket.AsyncSelector
All Implemented Interfaces:
Runnable

public class AsyncSelector
extends Object
implements Runnable

Create asyncronous selector. Selector has one selector thread for each cpu core in the system. SelectionKeys are listened with register method.

To close async selector, close its selector (getSelector().close()).

AsyncSelector guarantees that selection event of a key is handled in one thread at a time, and it the event handled accordingly, new selection events do not occur.

Author:
Toni Kalajainen (toni.kalajainen@vtt.fi)

Nested Class Summary
static interface AsyncSelector.SelectListener
           
 
Constructor Summary
AsyncSelector()
          Construct AsyncSelector with brand new selector
AsyncSelector(Selector sel)
          Construct new AsyncSelector
 
Method Summary
 void close()
          close.
 Selector getSelector()
          getSelector.
 void interestOps(SelectableChannel channel, int interestOps)
          Modify interest ops of a key.
 void register(SelectableChannel channel, int ops, AsyncSelector.SelectListener selectEventListener)
          Register a selection event handler to a selectable channel.
 void run()
          
 void unregister(SelectableChannel channel)
          unregister.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

AsyncSelector

public AsyncSelector()
              throws IOException
Construct AsyncSelector with brand new selector

Throws:
IOException - if any.

AsyncSelector

public AsyncSelector(Selector sel)
              throws IOException
Construct new AsyncSelector

Parameters:
sel - a Selector object.
Throws:
IOException - if any.
Method Detail

getSelector

public Selector getSelector()

getSelector.

Returns:
a Selector object.

interestOps

public void interestOps(SelectableChannel channel,
                        int interestOps)
                 throws CancelledKeyException
Modify interest ops of a key.

Parameters:
channel - registered key
interestOps - new interest op set
Throws:
CancelledKeyException - if any.

register

public void register(SelectableChannel channel,
                     int ops,
                     AsyncSelector.SelectListener selectEventListener)
              throws ClosedChannelException
Register a selection event handler to a selectable channel.

selectEventListener is invoked by one thread at a time. The rule of thumb is that the listener must not block. Note! If channel is registered and closed, select event is invoked until the channel is unregistered.

Parameters:
channel - a SelectableChannel object.
ops - initial interest ops See SelectionKey
selectEventListener - a AsyncSelector.SelectListener object.
Throws:
ClosedChannelException - if any.

unregister

public void unregister(SelectableChannel channel)

unregister.

Parameters:
channel - a SelectableChannel object.

close

public void close()
           throws IOException

close.

Throws:
IOException - if any.

run

public void run()

Specified by:
run in interface Runnable


Copyright © 2018. All rights reserved.