Package org.activecomponents.udp
Class UdpConnectionHandler
- java.lang.Object
-
- org.activecomponents.udp.UdpConnectionHandler
-
public class UdpConnectionHandler extends java.lang.ObjectConnection handler for UDP connections.
-
-
Field Summary
Fields Modifier and Type Field Description protected booleanactiveFlag if the handler is active.protected java.util.List<IConnectionListener>connectionlistenersListeners for new connections.protected java.util.Map<java.net.SocketAddress,Connection>connectionsCurrent connections with remote hosts.static intDEFAULT_CIPHERCipher class ID for default encryptionprotected java.net.DatagramSocketdgsocketThe socket.protected java.util.List<IIncomingListener>inclistenersListeners for incoming messages and packets.protected IIncomingListenerinternalinclistenerInternal listener for incoming messages and packets.protected KeyExchangeGeneratorkeyexgenProvider for randomly generated key exchanges.protected java.util.List<IKeyVerifier>keyverifiersList of externally-provided key verifiers.static intNULL_CIPHERCipher class ID for no encryptionprotected intsymcipherclassidID of the class used as cipher for symmetric encryption.protected IThreadExecutortexecThe thread executor.
-
Constructor Summary
Constructors Modifier Constructor Description UdpConnectionHandler(int port, int symcipherclassid, boolean nonblocking, IIncomingListener[] initiallisteners)Creates the connection handler.protectedUdpConnectionHandler(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 voidaddConnectionListener(IConnectionListener listener)Adds a connection listener.Connectionconnect(java.lang.String host, int port)Attempts to connect to a host.voiddisconnect(Connection conn)Terminates a connection.protected voidfireConnectionEstablished(Connection connection)Notifies connection listeners about a new connection.static intgetCipherIdForClass(java.lang.String symcipherclass)Returns a cipher class ID for a class name.ConnectiongetConnection(java.lang.String host, int port)Gets the connection if available.ConnectiongetConnection(java.net.SocketAddress remoteaddr)Gets the connection if available.intgetPort()Gets the local port.voidholePunch(java.lang.String remotehost, int remoteport)Attempts to punch a hole in the local firewall for the remote host.static voidmain(java.lang.String[] args)Test.voidremoveConnectionListener(IConnectionListener listener)Removes a connection listener.voidstart(IThreadExecutor texec)Starts the handling of incoming packages.voidstop()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.ExceptionTest.- 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.
-
-