Class UdpConnectionHandler


  • public class UdpConnectionHandler
    extends java.lang.Object
    Connection handler for UDP connections.
    • 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
      • 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.