com.kuka.comm.net
Class UdpConnection

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

public final class UdpConnection
extends NetConnection

Class to establish and use a UDP connection message exchange with other endpoints. Extends AbstractConnection regarding to UDP.


Field Summary
 
Fields inherited from class com.kuka.comm.net.NetConnection
_inputBuffer, _localAddress, _outputBuffer, _remoteAddress
 
Constructor Summary
UdpConnection(InetAddress address, int port)
          Constructor which uses a specific target addresses and target port, which are used for sending and receiving datagrams.
UdpConnection(InetSocketAddress remoteAddress)
          Constructor which uses a specific target addresses and target port as InetSocketAddress, which are used for sending and receiving datagrams.
UdpConnection(InetSocketAddress remoteAddress, InetSocketAddress localAddress)
          Constructor which uses a specific target addresses and target port as InetSocketAddress, which are used for sending and receiving datagrams.
UdpConnection(UdpServer server, InetSocketAddress remoteAddress)
          Constructor which uses a specific target addresses and target port, which are used for sending and receiving datagrams.
 
Method Summary
 void bindToPort(int portNumber)
          Method used to bind this connection to a fixed internal port.
 void connect()
          For a UDP connection the connect()-method ensures a functional DatagramSocket and starts the connection in an new Thread.
 void disconnect()
          The UDP connection will be freed from a previously binded address and port and closed afterwards.
 UdpConnectionStatistics getStatistics()
          Returns general connection statistics about this connection.
 boolean isConnected()
          Returns true of the connection is working as expected.
 boolean isConnectionless()
          Checks if the underlying connection type implements a connectionless communication method.
 void send(Object msg)
          This method will send the message.
 UdpConnection setTimeout(int timeout)
          Sets the timeout, which determines the time the socket got block during DatagramSocket.receive(DatagramPacket)-call.
 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, isAbleToReceive, isAbleToSend, removeConnectionListener, removeMessageHandler, setAutoReconnectStrategy
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

UdpConnection

public UdpConnection(InetAddress address,
                     int port)
Constructor which uses a specific target addresses and target port, which are used for sending and receiving datagrams.

Parameters:
address - The address of the remote endpoint.
port - The port number of the remote endpoint.

UdpConnection

public UdpConnection(InetSocketAddress remoteAddress)
Constructor which uses a specific target addresses and target port as InetSocketAddress, which are used for sending and receiving datagrams.

Parameters:
remoteAddress - The Address and port number of remote endpoint as InetSocketAddress.

UdpConnection

public UdpConnection(InetSocketAddress remoteAddress,
                     InetSocketAddress localAddress)
Constructor which uses a specific target addresses and target port as InetSocketAddress, which are used for sending and receiving datagrams.

Parameters:
remoteAddress - The address and port number of remote endpoint as InetSocketAddress.
localAddress - The local address and port number where the UDP socket should bind to InetSocketAddress.

UdpConnection

public UdpConnection(UdpServer server,
                     InetSocketAddress remoteAddress)
Constructor which uses a specific target addresses and target port, which are used for sending and receiving datagrams.

Parameters:
server - The server that created this connection.
remoteAddress - The address of the remote endpoint.
Method Detail

setTimeout

public UdpConnection setTimeout(int timeout)
Sets the timeout, which determines the time the socket got block during DatagramSocket.receive(DatagramPacket)-call. Any value different from zero will block the socket only for this amount of time. If DatagramSocket.receive(DatagramPacket) is unable to finish successfully in given time, an SocketTimeoutException will be raised. The socket could be used again for other operations (e.g. DatagramSocket.send(DatagramPacket), DatagramSocket.close(), DatagramSocket.disconnect(), ...).

If an error occurs the timeout will in fact be set, but the socket won't use this timeout. Instead it will throw an exception which got caught by the default errorHandler, if no errorHandler has been defined manually.

Parameters:
timeout - Integer value stating the amount of milliseconds. A value of zero disables timeout.

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()
For a UDP connection the connect()-method ensures a functional DatagramSocket and starts the connection in an new Thread.


disconnect

public void disconnect()
The UDP connection will be freed from a previously binded address and port and closed afterwards.

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

getStatistics

public UdpConnectionStatistics 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

bindToPort

public void bindToPort(int portNumber)
Method used to bind this connection to a fixed internal port. Listens to all incoming datagram packets on this port. Useful if remote connections expect a port to work and want to send data to a specific port.

Does a check for "Port out of range" to ensure that only valid ports are used. If given port number is out of range an IllegalArgumentException is thrown.

Parameters:
portNumber - Could be any integer between 0 and 65535. Also zero would be a possible value (not specified).

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.

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.