Package org.activecomponents.udp
Class UdpConnectionHandler
- java.lang.Object
-
- org.activecomponents.udp.UdpConnectionHandler
-
public class UdpConnectionHandler extends java.lang.Object
Connection handler for UDP connections.
-
-
Field Summary
Fields Modifier and Type Field Description protected boolean
active
Flag if the handler is active.protected java.util.List<IConnectionListener>
connectionlisteners
Listeners for new connections.protected java.util.Map<java.net.SocketAddress,Connection>
connections
Current connections with remote hosts.static int
DEFAULT_CIPHER
Cipher class ID for default encryptionprotected java.net.DatagramSocket
dgsocket
The socket.protected java.util.List<IIncomingListener>
inclisteners
Listeners for incoming messages and packets.protected IIncomingListener
internalinclistener
Internal listener for incoming messages and packets.protected KeyExchangeGenerator
keyexgen
Provider for randomly generated key exchanges.protected java.util.List<IKeyVerifier>
keyverifiers
List of externally-provided key verifiers.static int
NULL_CIPHER
Cipher class ID for no encryptionprotected int
symcipherclassid
ID of the class used as cipher for symmetric encryption.protected IThreadExecutor
texec
The thread executor.
-
Constructor Summary
Constructors Modifier Constructor Description UdpConnectionHandler(int port, int symcipherclassid, boolean nonblocking, IIncomingListener[] initiallisteners)
Creates the connection handler.protected
UdpConnectionHandler(int port, java.net.DatagramSocket inisocket, int symcipherclassid, boolean nonblocking, IIncomingListener[] initiallisteners)
Creates the connection handler.UdpConnectionHandler(java.net.DatagramSocket inisocket, int symcipherclassid, boolean nonblocking, IIncomingListener[] initiallisteners)
Creates the connection handler.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addConnectionListener(IConnectionListener listener)
Adds a connection listener.Connection
connect(java.lang.String host, int port)
Attempts to connect to a host.void
disconnect(Connection conn)
Terminates a connection.protected void
fireConnectionEstablished(Connection connection)
Notifies connection listeners about a new connection.static int
getCipherIdForClass(java.lang.String symcipherclass)
Returns a cipher class ID for a class name.Connection
getConnection(java.lang.String host, int port)
Gets the connection if available.Connection
getConnection(java.net.SocketAddress remoteaddr)
Gets the connection if available.int
getPort()
Gets the local port.void
holePunch(java.lang.String remotehost, int remoteport)
Attempts to punch a hole in the local firewall for the remote host.static void
main(java.lang.String[] args)
Test.void
removeConnectionListener(IConnectionListener listener)
Removes a connection listener.void
start(IThreadExecutor texec)
Starts the handling of incoming packages.void
stop()
Stops the handling of incoming packages.
-
-
-
Field Detail
-
NULL_CIPHER
public static final int NULL_CIPHER
Cipher class ID for no encryption- See Also:
- Constant Field Values
-
DEFAULT_CIPHER
public static final int DEFAULT_CIPHER
Cipher class ID for default encryption- See Also:
- Constant Field Values
-
texec
protected IThreadExecutor texec
The thread executor.
-
keyexgen
protected KeyExchangeGenerator keyexgen
Provider for randomly generated key exchanges.
-
keyverifiers
protected java.util.List<IKeyVerifier> keyverifiers
List of externally-provided key verifiers.
-
dgsocket
protected java.net.DatagramSocket dgsocket
The socket.
-
connections
protected java.util.Map<java.net.SocketAddress,Connection> connections
Current connections with remote hosts.
-
active
protected volatile boolean active
Flag if the handler is active.
-
symcipherclassid
protected int symcipherclassid
ID of the class used as cipher for symmetric encryption.
-
connectionlisteners
protected java.util.List<IConnectionListener> connectionlisteners
Listeners for new connections.
-
inclisteners
protected java.util.List<IIncomingListener> inclisteners
Listeners for incoming messages and packets.
-
internalinclistener
protected IIncomingListener internalinclistener
Internal listener for incoming messages and packets.
-
-
Constructor Detail
-
UdpConnectionHandler
public UdpConnectionHandler(int port, int symcipherclassid, boolean nonblocking, IIncomingListener[] initiallisteners)
Creates the connection handler.- Parameters:
port
- The port.inisocket
- Pre-initialized socket, overrides port argument, can be null.symcipherclass
- Class used for symmetric encryption.- Throws:
java.net.SocketException
- Socket exception.
-
UdpConnectionHandler
public UdpConnectionHandler(java.net.DatagramSocket inisocket, int symcipherclassid, boolean nonblocking, IIncomingListener[] initiallisteners)
Creates the connection handler.- Parameters:
port
- The port.inisocket
- Pre-initialized socket, overrides port argument, can be null.symcipherclass
- Class used for symmetric encryption.- Throws:
java.net.SocketException
- Socket exception.
-
UdpConnectionHandler
protected UdpConnectionHandler(int port, java.net.DatagramSocket inisocket, int symcipherclassid, boolean nonblocking, IIncomingListener[] initiallisteners)
Creates the connection handler.- Parameters:
port
- The port.inisocket
- Pre-initialized socket, overrides port argument, can be null.symcipherclass
- Class used for symmetric encryption.- Throws:
java.net.SocketException
- Socket exception.
-
-
Method Detail
-
start
public void start(IThreadExecutor texec)
Starts the handling of incoming packages.
-
stop
public void stop()
Stops the handling of incoming packages.
-
main
public static void main(java.lang.String[] args) throws java.lang.Exception
Test.- Throws:
java.lang.Exception
-
connect
public Connection connect(java.lang.String host, int port)
Attempts to connect to a host.- Parameters:
host
- Remote host.port
- Remote port.- Returns:
- The connection.
-
disconnect
public void disconnect(Connection conn)
Terminates a connection.- Parameters:
conn
- The connection.
-
getPort
public int getPort()
Gets the local port.- Returns:
- The local port.
-
getConnection
public Connection getConnection(java.lang.String host, int port)
Gets the connection if available.- Parameters:
host
- The remote host.port
- The remote port.- Returns:
- The connection, null if unavailable.
-
getConnection
public Connection getConnection(java.net.SocketAddress remoteaddr)
Gets the connection if available.- Parameters:
remoteaddr
- The remote address.- Returns:
- The connection, null if unavailable.
-
holePunch
public void holePunch(java.lang.String remotehost, int remoteport)
Attempts to punch a hole in the local firewall for the remote host.- Parameters:
remotehost
- The remote host.remoteport
- The port the remote host is using.
-
addConnectionListener
public void addConnectionListener(IConnectionListener listener)
Adds a connection listener.- Parameters:
listener
- The listener.
-
removeConnectionListener
public void removeConnectionListener(IConnectionListener listener)
Removes a connection listener.- Parameters:
listener
- The listener.
-
getCipherIdForClass
public static final int getCipherIdForClass(java.lang.String symcipherclass)
Returns a cipher class ID for a class name.- Parameters:
symcipherclass
- The class name.- Returns:
- The ID.
-
fireConnectionEstablished
protected void fireConnectionEstablished(Connection connection)
Notifies connection listeners about a new connection.- Parameters:
connection
- The connection.
-
-