org.simantics.databoard.method
Class TcpConnection

java.lang.Object
  extended by org.simantics.databoard.method.TcpConnection
All Implemented Interfaces:
MethodInterface

public class TcpConnection
extends java.lang.Object
implements MethodInterface

Connection is a class that handles request-response communication over a socket.

Requests have asynchronous result. The result can be acquired using one of the three methods: 1) Blocking read AsyncResult.waitForResponse() 2) Poll AsyncResult.getResponse() 3) Listen AsyncResult.setListener()

The socket must be established before Connection is instantiated. Closing connection does not close its Socket. If the socket is closed before connection there an error is thrown. The error is available by placing listener. The proper order to close a connection is to close Connection first and then Socket.

Author:
Toni Kalajainen

Nested Class Summary
static interface TcpConnection.ConnectionListener
           
static class TcpConnection.Exception_
           
static class TcpConnection.ExecutionError_
           
static class TcpConnection.InvalidMethodError
           
static class TcpConnection.Message
           
static class TcpConnection.RequestHeader
           
static class TcpConnection.ResponseHeader
           
static class TcpConnection.ResponseTooLargeError
           
 
Nested classes/interfaces inherited from interface org.simantics.databoard.method.MethodInterface
MethodInterface.AsyncRequestStatus, MethodInterface.AsyncResult, MethodInterface.ExecutionError, MethodInterface.InvokeListener, MethodInterface.Method
 
Field Summary
static java.util.concurrent.ExecutorService SHARED_EXECUTOR_SERVICE
           
 java.util.concurrent.ExecutorService writeExecutor
           
 
Constructor Summary
TcpConnection(java.net.Socket socket, MethodInterface methodInterface, Handshake localData, Handshake remoteData)
          Create a connection to a hand-shaken socket
 
Method Summary
 void addConnectionListener(TcpConnection.ConnectionListener listener)
           
 void close()
          Close the connection.
static TcpConnection getCurrentConnection()
          Get the active connection of current thread
 Interface getInterface()
          Get method descriptions
 MethodTypeDefinition[] getLocalMethodDescriptions()
          Get method interface that handles services locally (service requests by peer)
 MethodInterface getLocalMethodInterface()
          Get method interface that handles services locally (service requests by peer)
 MethodInterface.Method getMethod(MethodTypeBinding binding)
          Get an access to the implementation of a method.
 MethodInterface.Method getMethod(MethodTypeDefinition description)
          Get an access to the implementation of a method.
 MethodInterface getRemoteMethodInterface()
           
 java.net.Socket getSocket()
           
static Handshake handshake(java.net.Socket socket, Handshake localData)
          Handshake a socket
 void removeConnectionListener(TcpConnection.ConnectionListener listener)
           
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

SHARED_EXECUTOR_SERVICE

public static final java.util.concurrent.ExecutorService SHARED_EXECUTOR_SERVICE

writeExecutor

public java.util.concurrent.ExecutorService writeExecutor
Constructor Detail

TcpConnection

public TcpConnection(java.net.Socket socket,
                     MethodInterface methodInterface,
                     Handshake localData,
                     Handshake remoteData)
              throws java.io.IOException
Create a connection to a hand-shaken socket

Parameters:
socket -
methodInterface - local method handler
localData -
remoteData -
Throws:
java.io.IOException
Method Detail

handshake

public static Handshake handshake(java.net.Socket socket,
                                  Handshake localData)
                           throws java.io.IOException
Handshake a socket

Parameters:
socket -
localData - local data
Returns:
the remote data
Throws:
java.io.IOException
java.lang.RuntimeException - unexpected error (BindingException or EncodingException)

getInterface

public Interface getInterface()
Description copied from interface: MethodInterface
Get method descriptions

Specified by:
getInterface in interface MethodInterface
Returns:
method descriptions

getMethod

public MethodInterface.Method getMethod(MethodTypeBinding binding)
                                 throws MethodNotSupportedException
Description copied from interface: MethodInterface
Get an access to the implementation of a method. The binding is suggested by the consumer.

Specified by:
getMethod in interface MethodInterface
Parameters:
binding - binding to use
Returns:
method access
Throws:
MethodNotSupportedException

getMethod

public MethodInterface.Method getMethod(MethodTypeDefinition description)
                                 throws MethodNotSupportedException
Description copied from interface: MethodInterface
Get an access to the implementation of a method. The binding is suggested by the producer.

Specified by:
getMethod in interface MethodInterface
Parameters:
description - method description
Returns:
method access
Throws:
MethodNotSupportedException

getSocket

public java.net.Socket getSocket()

addConnectionListener

public void addConnectionListener(TcpConnection.ConnectionListener listener)

removeConnectionListener

public void removeConnectionListener(TcpConnection.ConnectionListener listener)

getLocalMethodInterface

public MethodInterface getLocalMethodInterface()
Get method interface that handles services locally (service requests by peer)

Returns:
local method interface

getLocalMethodDescriptions

public MethodTypeDefinition[] getLocalMethodDescriptions()
Get method interface that handles services locally (service requests by peer)

Returns:
local method interface

getRemoteMethodInterface

public MethodInterface getRemoteMethodInterface()

close

public void close()
Close the connection. All pending service request are canceled. The socket is not closed.


getCurrentConnection

public static TcpConnection getCurrentConnection()
Get the active connection of current thread

Returns:
Connection or null if current thread does not run connection