org.opcfoundation.ua.utils.asyncsocket
Class ListenableSocketChannel

java.lang.Object
  extended by org.opcfoundation.ua.utils.asyncsocket.ListenableSocketChannel

public class ListenableSocketChannel
extends Object

ListenableSocketChannel adds event listening convenience to the use of async sockets.

Select events (read, write, connect) are handled in thread other than selector. The thread is determined by an executor which is given as argument to the constructor. ListenableSocketChannel guarantees that each event type (read/write/connect) is handled at most by one thread.

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

Nested Class Summary
static interface ListenableSocketChannel.ConnectionListener
           
static interface ListenableSocketChannel.ReadableListener
           
static interface ListenableSocketChannel.WriteableListener
           
 
Constructor Summary
ListenableSocketChannel(Executor eventExecutor)
          Create new async-socket with given event handling thread
ListenableSocketChannel(SocketChannel channel, Executor eventExecutor)
          Create async-socket wrapper over given socket channel and given listener handling thread.
ListenableSocketChannel(SocketChannel channel, Executor executor, AsyncSelector selectorThread)
          Create async-socket wrapper over given socket channel, given listener handling thread, and given selector thread.
 
Method Summary
 void close()
          close.
 void connect(SocketAddress addr)
          Connect to a remote socket.
protected  void finalize()
          
 SocketChannel getChannel()
          Getter for the field channel.
 ListenableSocketChannel.ConnectionListener getConnectListener()
          Getter for the field connectListener.
 ListenableSocketChannel.ReadableListener getReadListener()
          Getter for the field readListener.
 AsyncSelector getSelectorThread()
          getSelectorThread.
 ListenableSocketChannel.WriteableListener getWriteListener()
          Getter for the field writeListener.
 void setConnectListener(ListenableSocketChannel.ConnectionListener connectListener)
          Set connection listener (make its selector key interested on connect events)
 void setReadListener(ListenableSocketChannel.ReadableListener readListener)
          Set Read listener (makes its selector key interested on read events)
 void setWriteListener(ListenableSocketChannel.WriteableListener writeListener)
          Set write listener (makes its selector key interAsyncSocketImplested on write events)
 boolean syncConnect(SocketAddress addr, long timeout)
          syncConnect.
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ListenableSocketChannel

public ListenableSocketChannel(Executor eventExecutor)
                        throws IOException
Create new async-socket with given event handling thread

Parameters:
eventExecutor - event handling thread or null for selector thread
Throws:
IOException - if any.

ListenableSocketChannel

public ListenableSocketChannel(SocketChannel channel,
                               Executor eventExecutor)
                        throws IOException
Create async-socket wrapper over given socket channel and given listener handling thread.

Parameters:
channel - a SocketChannel object.
eventExecutor - event worker thread or null for selector thread
Throws:
IOException - if any.

ListenableSocketChannel

public ListenableSocketChannel(SocketChannel channel,
                               Executor executor,
                               AsyncSelector selectorThread)
                        throws IOException
Create async-socket wrapper over given socket channel, given listener handling thread, and given selector thread.

Parameters:
channel - a SocketChannel object.
executor - a Executor object.
selectorThread - a AsyncSelector object.
Throws:
IOException - if any.
Method Detail

close

public void close()

close.


getChannel

public SocketChannel getChannel()

Getter for the field channel.

Returns:
a SocketChannel object.

getSelectorThread

public AsyncSelector getSelectorThread()

getSelectorThread.

Returns:
a AsyncSelector object.

connect

public void connect(SocketAddress addr)
             throws IOException
Connect to a remote socket. To monitor connect result, set ConnectListener first.

Parameters:
addr - a SocketAddress object.
Throws:
IOException - if any.

syncConnect

public boolean syncConnect(SocketAddress addr,
                           long timeout)
                    throws IOException

syncConnect.

Parameters:
addr - a SocketAddress object.
timeout - timeout in milliseconds
Returns:
true if connected
Throws:
IOException - if any.

setConnectListener

public void setConnectListener(ListenableSocketChannel.ConnectionListener connectListener)
Set connection listener (make its selector key interested on connect events)

Parameters:
connectListener - a ListenableSocketChannel.ConnectionListener object.

setReadListener

public void setReadListener(ListenableSocketChannel.ReadableListener readListener)
Set Read listener (makes its selector key interested on read events)

Parameters:
readListener - a ListenableSocketChannel.ReadableListener object.

setWriteListener

public void setWriteListener(ListenableSocketChannel.WriteableListener writeListener)
Set write listener (makes its selector key interAsyncSocketImplested on write events)

Parameters:
writeListener - a ListenableSocketChannel.WriteableListener object.

getConnectListener

public ListenableSocketChannel.ConnectionListener getConnectListener()

Getter for the field connectListener.

Returns:
a ListenableSocketChannel.ConnectionListener object.

getReadListener

public ListenableSocketChannel.ReadableListener getReadListener()

Getter for the field readListener.

Returns:
a ListenableSocketChannel.ReadableListener object.

getWriteListener

public ListenableSocketChannel.WriteableListener getWriteListener()

Getter for the field writeListener.

Returns:
a ListenableSocketChannel.WriteableListener object.

finalize

protected void finalize()
                 throws Throwable

Overrides:
finalize in class Object
Throws:
Throwable


Copyright © 2018. All rights reserved.