com.kuka.comm.net
Class TcpConnection

java.lang.Object
  extended by com.kuka.comm.AbstractConnection
      extended by com.kuka.comm.net.NetConnection
          extended by com.kuka.comm.net.TcpConnection
All Implemented Interfaces:
Connection

public final class TcpConnection
extends NetConnection

A connection instantiation for TCP over JavaIO.


Field Summary
 
Fields inherited from class com.kuka.comm.net.NetConnection
_inputBuffer, _localAddress, _outputBuffer, _remoteAddress
 
Constructor Summary
TcpConnection(InetSocketAddress remoteAddress)
          Constructor using InetSocketAddress as connection target.
TcpConnection(InetSocketAddress remoteAddress, InetSocketAddress localAddress)
          Constructor using first InetSocketAddress as connection target and a second one as its own address/port values..
TcpConnection(Socket socket)
          Constructor for a connection using an already existing socket.
 
Method Summary
 void connect()
          Connects (or initiates) the connection asynchronously.
 void disconnect()
          Disconnects (or closes) the connection asynchronously.
 TcpConnection enableLinger(boolean enable, int timeout)
          En-/Disable linger option for socket.
 TcpConnectionStatistics getStatistics()
          Returns general connection statistics about this connection.
 boolean isAbleToReceive()
          Returns true if the current state of the connection allows to receive messages.
 boolean isAbleToSend()
          Returns true if the current state of the connection allows to send messages.
 boolean isConnected()
          Returns true of the connection is working as expected.
 boolean isConnectionless()
          Checks if the underlying connection type implements a connectionless communication method.
 boolean isTcpNoDelay()
          Method to retrieve the current status for the TCP sending parameter.
 TcpConnection keepConnectionAlive(boolean keepAlive)
          Option to en-/disable the sending to a keep alive packet after two hours without incoming data.
 void send(Object msg)
          This method will send the message.
 TcpConnection setTcpNoDelay(boolean tcpNoDelay)
          TCP specific setting.
 TcpConnection setTimeout(int timeout)
          Sets the timeout in milliseconds, which determines the time the Socket got block during reading.
 String toString()
           
 
Methods inherited from class com.kuka.comm.net.NetConnection
getDecoder, getEncoder, getLocalAddress, getRemoteAddress, hasDecoder, hasEncoder, setDecoder, setEncoder, setInputBufferSize, setOutputBufferSize
 
Methods inherited from class com.kuka.comm.AbstractConnection
addConnectionListener, addMessageHandler, awaitIsConnected, dispatchMessages, fireConnectionClosed, fireConnectionFailed, fireConnectionSuccessful, fireConnectionTimedOut, fireReceiveError, getAutoReconnectStrategy, removeConnectionListener, removeMessageHandler, setAutoReconnectStrategy
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

TcpConnection

public TcpConnection(InetSocketAddress remoteAddress)
Constructor using InetSocketAddress as connection target.

Parameters:
remoteAddress - The connection target as InetSocketAddress

TcpConnection

public TcpConnection(InetSocketAddress remoteAddress,
                     InetSocketAddress localAddress)
Constructor using first InetSocketAddress as connection target and a second one as its own address/port values..

Parameters:
remoteAddress - The remote target ip address and port as InetSocketAddress.
localAddress - The local address and port for this connection as InetSocketAddress.

TcpConnection

public TcpConnection(Socket socket)
Constructor for a connection using an already existing socket.

Parameters:
socket - The Socket to use with this connection.
Method Detail

send

public void send(Object msg)
          throws ConnectionException
Description copied from interface: Connection
This method will send the message. It blocks until the message has been sent.

The message type is Object to ensure that all kinds of messages can be send by the same interface. Type safety is ensured by the used Encoder, that must be able to handle the actual message type (i.e. serialize it correctly), otherwise the message will not be send.

Parameters:
msg - The message to be send.
Throws:
ConnectionException - if sending fails

connect

public void connect()
Description copied from interface: Connection
Connects (or initiates) the connection asynchronously. The success of this method can be checked by calling Connection.isConnected(). Only a connected connection can be used for sending or receiving messages.


disconnect

public void disconnect()
Description copied from interface: Connection
Disconnects (or closes) the connection asynchronously. The success of this method can be checked by calling Connection.isConnected(). A disconnected connection cannot be used for sending and will not receive any messages. A disconnected connection can be reconnected by calling Connection.connect().

Specified by:
disconnect in interface Connection
Overrides:
disconnect in class AbstractConnection

setTcpNoDelay

public TcpConnection setTcpNoDelay(boolean tcpNoDelay)
TCP specific setting. Disables Nagle's algorithm so data is send directly without delay.

Otherwise it would wait until TCP-packet size is reached to reduce communication overhead.

Parameters:
tcpNoDelay - Boolean value. true disables Nagle's algorithm, while false will enable it.
Returns:
Instance of object to append this method during construction time.

isTcpNoDelay

public boolean isTcpNoDelay()
Method to retrieve the current status for the TCP sending parameter.

Returns:
true if packets should be send even if TCP dataframe is not completely full (see Nagle's algorithm for further information), false otherwise

setTimeout

public TcpConnection setTimeout(int timeout)
Sets the timeout in milliseconds, which determines the time the Socket got block during reading. Any value different from zero will block the socket only for this amount of time. If reading is unable to finish successfully in given time, an SocketTimeoutException will be raised. The socket could be used again for other operations (e.g. writing, Socket.close(), ...).

If a timeout has occured, the a connection listener's ConnectionListener.onConnectionTimeout(com.kuka.comm.Connection) method will be called. The listener can be added with AbstractConnection.addConnectionListener(com.kuka.comm.ConnectionListener). The user can then close the connection manually.

Parameters:
timeout - Integer value stating the amount of milliseconds. A value of zero disables timeout.
Returns:
This connection to use this method with fluent interface.

keepConnectionAlive

public TcpConnection keepConnectionAlive(boolean keepAlive)
Option to en-/disable the sending to a keep alive packet after two hours without incoming data.

Parameters:
keepAlive - true enables the option, false disables it.
Returns:
Instance of object to append this method during construction time.

enableLinger

public TcpConnection enableLinger(boolean enable,
                                  int timeout)
En-/Disable linger option for socket. Linger timeout defines how long a socket will kept open after the connection has been closed to remain for delayed data packets.

Parameters:
enable - true enables linger option, false disables linger.
timeout - The amount of time to wait after a closed connection for remaining data.
Returns:
Instance of object to append this method during construction time.

getStatistics

public TcpConnectionStatistics getStatistics()
Description copied from interface: Connection
Returns general connection statistics about this connection.

Specified by:
getStatistics in interface Connection
Specified by:
getStatistics in class NetConnection
Returns:
general connection statistics about this connection

isConnected

public boolean isConnected()
Description copied from interface: Connection
Returns true of the connection is working as expected. The methods Connection.isAbleToSend() and Connection.isAbleToReceive() can be used to further check if sending or receiving of messages is possible.

Returns:
True, if the connection is connected and working as expected.

isAbleToReceive

public boolean isAbleToReceive()
Description copied from interface: Connection
Returns true if the current state of the connection allows to receive messages. For this, the connection at least must be connected (see also Connection.isConnected()) and an encoder must have been set. The method Connection.hasDecoder() can be used to check if a Decoder has been set.

Specified by:
isAbleToReceive in interface Connection
Overrides:
isAbleToReceive in class AbstractConnection
Returns:
true, if the connection can be used to receive messages

isAbleToSend

public boolean isAbleToSend()
Description copied from interface: Connection
Returns true if the current state of the connection allows to send messages. For this, the connection at least must be connected (see also Connection.isConnected()) and an Encoder must have been set in the concrete class implementing the Connection interface. The method Connection.hasEncoder() can be used to check if an Encoder has been set.

Specified by:
isAbleToSend in interface Connection
Overrides:
isAbleToSend in class AbstractConnection
Returns:
True, if the connection can be used to send messages.

isConnectionless

public boolean isConnectionless()
Description copied from interface: Connection
Checks if the underlying connection type implements a connectionless communication method. If so, the method returns true. A connectionless connection will behave in a slightly different way than a connection-oriented connection, i.e. the connection will not be disconnected if the remote end point stops working properly. Further, the method Connection.isConnected() only ensures that the local setup is working properly but does not give any guarantees that the remote end point is working properly. Likewise, Connection.isAbleToSend() will refer to the state of the local setup, but it will not guarantee that the message will also be received or that the remote end point is even existing.

Returns:
true, if the connection implements a connectionless communication method, false otherwise

toString

public String toString()
Overrides:
toString in class Object


Copyright © 2019. All rights reserved.