org.opcfoundation.ua.utils.asyncsocket
Class AsyncSocketImpl

java.lang.Object
  extended by org.opcfoundation.ua.utils.AbstractState<SocketState,IOException>
      extended by org.opcfoundation.ua.utils.asyncsocket.AsyncSocketImpl
All Implemented Interfaces:
AsyncSocket, IStatefulObject<SocketState,IOException>

public class AsyncSocketImpl
extends AbstractState<SocketState,IOException>
implements AsyncSocket, IStatefulObject<SocketState,IOException>

Async socket.

The object has monitorable internal state SocketState.

Author:
Toni Kalajainen (toni.kalajainen@vtt.fi)
See Also:
Methods for monitoring the state, State of the socket, Async input stream, Async output stream, Stream alarms

Constructor Summary
AsyncSocketImpl()
          Constructor for AsyncSocketImpl.
AsyncSocketImpl(SocketChannel chan)
          Constructor for AsyncSocketImpl.
AsyncSocketImpl(SocketChannel channel, Executor triggerExecutor, AsyncSelector selectorThread)
          Constructor for AsyncSocketImpl.
 
Method Summary
 AsyncSocketImpl close()
          close.
 void closeOnFlush()
          Convenience method.
 void connect(SocketAddress addr)
          connect.
 AsyncInputStream getInputStream()
          getInputStream.
 AsyncOutputStream getOutputStream()
          getOutputStream.
 IStatefulObject<SocketState,IOException> getStateMonitor()
          getStateMonitor.
protected  boolean isStateTransitionAllowed(SocketState oldState, SocketState newState)
          Checks whether state transition is allowed.
protected  void onStateTransition(SocketState oldState, SocketState newState)
          Override this.
protected  boolean setState(SocketState state)
          setState.
 Socket socket()
          socket.
 SocketChannel socketChannel()
          socketChannel.
 boolean syncConnect(SocketAddress addr)
          syncConnect.
 
Methods inherited from class org.opcfoundation.ua.utils.AbstractState
addStateListener, addStateNotifiable, assertNoError, attemptSetState, clearError, getError, getState, hasError, onListenerException, removeStateListener, removeStateNotifiable, setError, setState, waitForState, waitForState, waitForStateUninterruptibly
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface org.opcfoundation.ua.utils.IStatefulObject
addStateListener, addStateNotifiable, getError, getState, removeStateListener, removeStateNotifiable, waitForState, waitForState, waitForStateUninterruptibly
 

Constructor Detail

AsyncSocketImpl

public AsyncSocketImpl()
                throws IOException

Constructor for AsyncSocketImpl.

Throws:
IOException - if any.

AsyncSocketImpl

public AsyncSocketImpl(SocketChannel chan)
                throws IOException

Constructor for AsyncSocketImpl.

Parameters:
chan - a SocketChannel object.
Throws:
IOException - if any.

AsyncSocketImpl

public AsyncSocketImpl(SocketChannel channel,
                       Executor triggerExecutor,
                       AsyncSelector selectorThread)
                throws IOException

Constructor for AsyncSocketImpl.

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

onStateTransition

protected void onStateTransition(SocketState oldState,
                                 SocketState newState)
Override this.

Overrides:
onStateTransition in class AbstractState<SocketState,IOException>
Parameters:
oldState - a StateType object.
newState - a StateType object.

getInputStream

public AsyncInputStream getInputStream()

getInputStream.

Specified by:
getInputStream in interface AsyncSocket
Returns:
a AsyncInputStream object.

getOutputStream

public AsyncOutputStream getOutputStream()

getOutputStream.

Specified by:
getOutputStream in interface AsyncSocket
Returns:
a AsyncOutputStream object.

close

public AsyncSocketImpl close()
                      throws IOException

close.

Specified by:
close in interface AsyncSocket
Returns:
a AsyncSocketImpl object.
Throws:
IOException - if any.

socketChannel

public SocketChannel socketChannel()

socketChannel.

Specified by:
socketChannel in interface AsyncSocket
Returns:
a SocketChannel object.

socket

public Socket socket()

socket.

Specified by:
socket in interface AsyncSocket
Returns:
a Socket object.

setState

protected boolean setState(SocketState state)

setState.

Overrides:
setState in class AbstractState<SocketState,IOException>
Parameters:
state - a SocketState object.
Returns:
a boolean.

isStateTransitionAllowed

protected boolean isStateTransitionAllowed(SocketState oldState,
                                           SocketState newState)
Checks whether state transition is allowed. Override this

Overrides:
isStateTransitionAllowed in class AbstractState<SocketState,IOException>
Parameters:
oldState - a StateType object.
newState - a StateType object.
Returns:
true if state transition is allowed

connect

public void connect(SocketAddress addr)
             throws IOException

connect.

Async connect. The state is set to Connecting upon successful attempt (method returns without exeception). Once the connection attempt is finished the object state will shift either to Error or to Connected (See getState()). State changes can be monitored by attaching listeners (See addStateListener()).

Specified by:
connect in interface AsyncSocket
Parameters:
addr - a SocketAddress object.
Throws:
IOException - if any.

syncConnect

public boolean syncConnect(SocketAddress addr)
                    throws IOException

syncConnect.

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

getStateMonitor

public IStatefulObject<SocketState,IOException> getStateMonitor()

getStateMonitor.

Specified by:
getStateMonitor in interface AsyncSocket
Returns:
a IStatefulObject object.

closeOnFlush

public void closeOnFlush()
Convenience method. Closes socket when all data is flushed



Copyright © 2018. All rights reserved.