org.opcfoundation.ua.transport.tcp.io
Class SecureChannelTcp

java.lang.Object
  extended by org.opcfoundation.ua.transport.tcp.io.SecureChannelTcp
All Implemented Interfaces:
IConnectionListener, RequestChannel, SecureChannel, IConnection.IMessageListener, ITransportChannel

public class SecureChannelTcp
extends Object
implements IConnection.IMessageListener, IConnectionListener, ITransportChannel, SecureChannel

Client's Secure Channel connection to an endpoint.

Secure channel creates transport channel(s) as needed. If the connection fails, and the transport channel is stateful (TCP), and the secure channel has pending service requests, it attempts to reconnect the transport channel. If the reconnect fails there is a timeout sequence of the following wait periods { 0, 1, 2, 4, 8, 16, 32, 64, 120, 120, ... }.

If error recovery state fails to re-establish new security token before the old expires, the secure channel will be closed. Despite the name SecureChannelTcp, the class is 99% implemented as transport channel agnostic. The plan is to upgrade the class to support SOAP transport and rename to SecureChannelImpl.


Nested Class Summary
 
Nested classes/interfaces inherited from interface org.opcfoundation.ua.transport.tcp.io.ITransportChannel
ITransportChannel.TransportChannelFeature
 
Constructor Summary
SecureChannelTcp()
          Constructor for SecureChannelTcp.
 
Method Summary
 void close()
          Close the secure channel.
 AsyncResult<SecureChannel> closeAsync()
          closeAsync.
 void dispose()
          dispose.
 ServerConnection getConnection()
          Get current socket connection if applicable for the binding type.
 String getConnectURL()
          Return the URL of the connection.
 EndpointConfiguration getEndpointConfiguration()
          getEndpointConfiguration.
 EndpointDescription getEndpointDescription()
          getEndpointDescription.
 EncoderContext getMessageContext()
          getMessageContext.
 MessageSecurityMode getMessageSecurityMode()
          Get message security mode.
 int getOperationTimeout()
          getOperationTimeout.
protected  long getRequestTimeout(ServiceRequest request)
          getRequestTimeout.
 int getSecureChannelId()
          Get secure channel id.
 SecurityPolicy getSecurityPolicy()
          Get security policy
 EnumSet<ITransportChannel.TransportChannelFeature> getSupportedFeatures()
          Get a list of features supported by the channel.
 void initialize(InetSocketAddress addr, TransportChannelSettings settings, EncoderContext ctx)
           Configure the secure channel.
 void initialize(String url, TransportChannelSettings settings, EncoderContext ctx)
          Initialize a secure channel with endpoint identified by the URL.
 void initialize(TransportChannelSettings settings, EncoderContext ctx)
          Initialize a secure channel with endpoint identified by the URL.
 boolean isOpen()
          Return true if the secure channel has been opened and is not (hopefully) closed on the server.
 void onClosed(ServiceResultException closeError)
          The connection has been closed
 void onMessage(int requestId, int secureChannelId, IEncodeable message)
          New message arrived to the connection.
 void onOpen()
          onOpen.
 void open()
          Opens a secure channel.
 AsyncResult<SecureChannel> openAsync()
          Asynchronous open channel.
 ServiceResponse serviceRequest(ServiceRequest request)
          Send a service request over the secure channel.
 ServiceResponse serviceRequest(ServiceRequest request, long operationTimeout)
          Send a service request over the secure channel.
 AsyncResult<ServiceResponse> serviceRequestAsync(ServiceRequest request)
          Asynchronous operation to send a request over the secure channel.
 AsyncResult<ServiceResponse> serviceRequestAsync(ServiceRequest request, long operationTimeout)
          Asynchronous operation to send a request over the secure channel.
 void setOperationTimeout(int timeout)
          Set operation timeout
protected  void setTransportChannel(IConnection transportChannel)
          Setter for the field transportChannel.
 String toString()
          
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

SecureChannelTcp

public SecureChannelTcp()

Constructor for SecureChannelTcp.

Method Detail

initialize

public void initialize(TransportChannelSettings settings,
                       EncoderContext ctx)
                throws ServiceResultException
Initialize a secure channel with endpoint identified by the URL. Configure the secure channel

Specified by:
initialize in interface SecureChannel
Parameters:
settings - a TransportChannelSettings object.
ctx - a EncoderContext object.
Throws:
ServiceResultException - if any.

initialize

public void initialize(String url,
                       TransportChannelSettings settings,
                       EncoderContext ctx)
                throws ServiceResultException
Initialize a secure channel with endpoint identified by the URL. Configure the secure channel

Specified by:
initialize in interface SecureChannel
Specified by:
initialize in interface ITransportChannel
Parameters:
url - a String object.
settings - a TransportChannelSettings object.
ctx - a EncoderContext object.
Throws:
ServiceResultException - if any.

initialize

public void initialize(InetSocketAddress addr,
                       TransportChannelSettings settings,
                       EncoderContext ctx)
                throws ServiceResultException
Configure the secure channel.

Throws:
ServiceResultException

getRequestTimeout

protected long getRequestTimeout(ServiceRequest request)

getRequestTimeout.

Parameters:
request - a ServiceRequest object.
Returns:
a long.

serviceRequest

public ServiceResponse serviceRequest(ServiceRequest request)
                               throws ServiceFaultException,
                                      ServiceResultException
Send a service request over the secure channel.

If the operation timeouts or the thread is interrupted a ServiceResultException is thrown with StatusCodes.Bad_Timeout.

Send service request to the server. If the secure channel is in error recovery state, the request is put to a send queue. Message is sent upon successful reconnection.

If the transport channel fails and cannot be restablished within operation timeout period, StatusCodes.Bad_RequestTimeout is set as error. If the secure channel is not open, is expired or closed StatusCodes.Bad_SecureChannelClosed is thrown. If the thread is interrupted with Thread.interrupt(), the operation aborts and ServiceResultException Bad_RequestCancelledByClient is thrown.

Specified by:
serviceRequest in interface RequestChannel
Specified by:
serviceRequest in interface SecureChannel
Specified by:
serviceRequest in interface ITransportChannel
Parameters:
request - a ServiceRequest object.
Returns:
a ServiceResponse object.
Throws:
ServiceResultException - if error
ServiceFaultException

serviceRequest

public ServiceResponse serviceRequest(ServiceRequest request,
                                      long operationTimeout)
                               throws ServiceFaultException,
                                      ServiceResultException
Send a service request over the secure channel.

If the operation timeouts or the operation is interrupted and a ServiceResultException is thrown with StatusCodes.Bad_Timeout.

Specified by:
serviceRequest in interface ITransportChannel
Parameters:
request - a ServiceRequest object.
operationTimeout - timeout time in milliseconds
Returns:
a ServiceResponse object.
Throws:
ServiceResultException - if any.
ServiceFaultException

serviceRequestAsync

public AsyncResult<ServiceResponse> serviceRequestAsync(ServiceRequest request)
Asynchronous operation to send a request over the secure channel. Send service request to the server. If the secure channel is in error recovery state, the request is put to a send queue. Message is sent upon successful reconnection.

If the transport channel fails and cannot be re-established within operation timeout period, StatusCodes.Bad_RequestTimeout is set as error. If the secure channel is not open, is expired or closed StatusCodes.Bad_SecureChannelClosed is thrown. Errors are written to the result object. ServiceFaultException There was a service fault in processing of the operation in the server. ServiceResultException There was an error while transferring the operation over network.

Specified by:
serviceRequestAsync in interface RequestChannel
Specified by:
serviceRequestAsync in interface SecureChannel
Specified by:
serviceRequestAsync in interface ITransportChannel
Parameters:
request - the request
Returns:
the result

serviceRequestAsync

public AsyncResult<ServiceResponse> serviceRequestAsync(ServiceRequest request,
                                                        long operationTimeout)
Asynchronous operation to send a request over the secure channel. Send service request to the server. If the secure channel is in error recovery state, the request is put to a send queue. Message is sent upon successful reconnection.

If the transport channel fails and cannot be restablished within operation timeout period, StatusCodes.Bad_RequestTimeout is set as error. If the secure channel is not open, is expired or closed StatusCodes.Bad_SecureChannelClosed is thrown. Errors are written to the result object. ServiceFaultException There was a service fault in processing of the operation in the server. ServiceResultException There was an error while transferring the operation over network.

Specified by:
serviceRequestAsync in interface ITransportChannel
Parameters:
request - the request
operationTimeout - timeout time
Returns:
the result

open

public void open()
          throws ServiceResultException
Opens a secure channel. This method does nothing if the secure channel is already open. Sets up a connection, opens it, creates a secure channel. If unable to open connection an exception is thrown and the secure channel remains closed. If the operation timeouts or user interrupts the thread with Thread.interrupt() a Bad_Timeout is thrown.

Specified by:
open in interface SecureChannel
Throws:
ServiceResultException - if any.

openAsync

public AsyncResult<SecureChannel> openAsync()
Asynchronous open channel.

Specified by:
openAsync in interface SecureChannel
Returns:
async result object

close

public void close()
Close the secure channel. This method does nothing if the channel is already closed or has never been opened.

This method sends CloseSecureChannelRequest to the server and closes the socket connection. If sending of the message fails and thus the servers never receives notification about closed secure channel, then there is no resend attempt, instead the secure channel will eventually time out in the server.

All pending requests will fault with Bad_SecureChannelClosed

Specified by:
close in interface SecureChannel

setTransportChannel

protected void setTransportChannel(IConnection transportChannel)

Setter for the field transportChannel.

Parameters:
transportChannel - the transportChannel to set

closeAsync

public AsyncResult<SecureChannel> closeAsync()

closeAsync.

Specified by:
closeAsync in interface SecureChannel
Returns:
a AsyncResult object.

dispose

public void dispose()

dispose.

Specified by:
dispose in interface SecureChannel
Specified by:
dispose in interface ITransportChannel

getEndpointConfiguration

public EndpointConfiguration getEndpointConfiguration()

getEndpointConfiguration.

Specified by:
getEndpointConfiguration in interface SecureChannel
Specified by:
getEndpointConfiguration in interface ITransportChannel
Returns:
a EndpointConfiguration object.

getEndpointDescription

public EndpointDescription getEndpointDescription()

getEndpointDescription.

Specified by:
getEndpointDescription in interface SecureChannel
Specified by:
getEndpointDescription in interface ITransportChannel
Returns:
a EndpointDescription object.

getMessageContext

public EncoderContext getMessageContext()

getMessageContext.

Specified by:
getMessageContext in interface SecureChannel
Specified by:
getMessageContext in interface ITransportChannel
Returns:
a EncoderContext object.

setOperationTimeout

public void setOperationTimeout(int timeout)
Set operation timeout

Specified by:
setOperationTimeout in interface SecureChannel
Specified by:
setOperationTimeout in interface ITransportChannel
Parameters:
timeout - in milliseconds

getOperationTimeout

public int getOperationTimeout()

getOperationTimeout.

Specified by:
getOperationTimeout in interface SecureChannel
Specified by:
getOperationTimeout in interface ITransportChannel
Returns:
a int.

getSecureChannelId

public int getSecureChannelId()
Get secure channel id.

Specified by:
getSecureChannelId in interface SecureChannel
Returns:
secure channel id or -1 if channel is closed.

onMessage

public void onMessage(int requestId,
                      int secureChannelId,
                      IEncodeable message)
New message arrived to the connection. Incoming message listeners. All incoming messages are notified to all listeners. It is up to the listener to find the interesting messages. Implementation to IMessageListener. Listens to messages incoming from TcpConnection.

Specified by:
onMessage in interface IConnection.IMessageListener

isOpen

public boolean isOpen()
Return true if the secure channel has been opened and is not (hopefully) closed on the server. Secure channel is open as long as it as security token that is alive, even if its transport layer connection is disconnected.

Specified by:
isOpen in interface SecureChannel
Returns:
a boolean.

onClosed

public void onClosed(ServiceResultException closeError)
The connection has been closed Transport channel has been closed. Implementation to IConnectionListener

Specified by:
onClosed in interface IConnectionListener
Parameters:
closeError - a ServiceResultException object.

onOpen

public void onOpen()

onOpen.

Specified by:
onOpen in interface IConnectionListener

getSupportedFeatures

public EnumSet<ITransportChannel.TransportChannelFeature> getSupportedFeatures()
Get a list of features supported by the channel.

Specified by:
getSupportedFeatures in interface ITransportChannel
Returns:
a EnumSet object.

getConnectURL

public String getConnectURL()
Return the URL of the connection. This value is only available when the channel is in Open or Closing state, if not the return value is null.

Specified by:
getConnectURL in interface SecureChannel
Returns:
connect URL or null

getConnection

public ServerConnection getConnection()
Get current socket connection if applicable for the binding type.

Specified by:
getConnection in interface SecureChannel
Returns:
connection or null

getMessageSecurityMode

public MessageSecurityMode getMessageSecurityMode()
Get message security mode.

Specified by:
getMessageSecurityMode in interface SecureChannel
Returns:
security mode or null

getSecurityPolicy

public SecurityPolicy getSecurityPolicy()
Get security policy

Specified by:
getSecurityPolicy in interface SecureChannel
Returns:
security policy or null if channel has not been initialized

toString

public String toString()

Overrides:
toString in class Object


Copyright © 2018. All rights reserved.