Class NetConfHelper

  • All Implemented Interfaces:
    java.lang.Runnable

    public class NetConfHelper
    extends java.lang.Object
    implements java.lang.Runnable
    Utility class for automatic IP address selection. Contains both a server part and a client part, to implement a bidirectional TCP-based communication for selecting an IP address that is reachable from the server part:
    1. the server must be running and listening to an address that is reachable from the client;
    2. the client opens a TCP connection to the server, and successively sends the IP addresses of all its locally configured network interfaces (but localhost);
    3. for each received address, the server tries to open a new socket towards the client towards the received address, and sends the actual client address advertised by the socket previously opened by the client;
    4. if the client receives the address sent by the server (within a given time-out), and if this address equals the candidate address that was first sent by the client, then this address is validated. Otherwise, the candidate address is rejected. Most probably, the client went through a proxy or Network Address Translation (NAT).
    The server part logs all submitted candidate addresses and tells whether they seem to be suitable or not (comparing the sent address with the actual client address advertised by the incoming socket). If no trace is displayed while a configuration client completes, then, either the configuration server is not bound to a reachable IP address, or the client's network connectivity is not sufficient to reach the server. You should try another server IP address.
    Author:
    Bruno Dillenseger
    • Constructor Summary

      Constructors 
      Constructor Description
      NetConfHelper()  
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      static void main​(java.lang.String[] args)
      Runs either the server part or the client part.
      void run()
      Server part activity
      void startServer​(java.net.InetSocketAddress localAddr)
      Start the server part.
      void stopServer()
      Stop the server part
      static java.net.InetAddress tryAddresses​(java.lang.String ip, int port, int timeout_ms)
      Submit all IP addresses of locally configured network interfaces to the server part.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • NetConfHelper

        public NetConfHelper()
    • Method Detail

      • main

        public static void main​(java.lang.String[] args)
                         throws java.lang.Exception
        Runs either the server part or the client part.
        Parameters:
        args - two forms:
        • server server_IP_address [server_port] - runs the server part, opening a TCP server socket on the given IP address and port number. When the port number is not given, it defaults to the CLIF registry's default port number + 1.
        • client server_IP_address server_port timeout_ms - runs the client part, trying all available local IP addresses.
        Throws:
        java.lang.Exception - either the provided arguments are not correct, or the server part could not be started because of a network-related problem (e.g. it can't bind to the provided IP address/port).
      • tryAddresses

        public static java.net.InetAddress tryAddresses​(java.lang.String ip,
                                                        int port,
                                                        int timeout_ms)
        Submit all IP addresses of locally configured network interfaces to the server part.
        Parameters:
        ip - the IP address of the configuration helper's server part
        port - the port number of the configuration helper's server part
        timeout_ms - the time-out value waiting for a reply from the server after having submitted an IP address.
        Returns:
        a valid IP address, or null if none passed the selection.
      • startServer

        public void startServer​(java.net.InetSocketAddress localAddr)
                         throws ClifException
        Start the server part.
        Parameters:
        localAddr - the local IP address and port to bind to.
        Throws:
        ClifException - the server part of the configuration helper could not be started.
      • stopServer

        public void stopServer()
                        throws ClifException
        Stop the server part
        Throws:
        ClifException - problem while trying to close the server socket
      • run

        public void run()
        Server part activity
        Specified by:
        run in interface java.lang.Runnable