Class NetworkInterfaceManager


  • public class NetworkInterfaceManager
    extends Object
    A manager for the network interfaces. It allows to get the current IP configuration of a network interface and to reconfigure it.
    It allows to enable/disable and start/stop an interface.
    - Enable/disable is simply a logical state of the interface managed by software.
    - Start/stop is more hardware-related. It may physically turn on/off the hardware module and it is intended to manage power consumption.


    Scenario example: a system uses Ethernet as its main interface and 3G as its backup interface. In normal mode, Ethernet is started and enabled while 3G is stopped (and hence disabled) and communications all go through the Ethernet interface. When Ethernet loses Internet connection, it is disabled and 3G is started. Communications will then go through the 3G interface. When Ethernet gets Internet connection back, it is re-enabled, 3G is stopped and the system goes back to normal mode.
    • Constructor Detail

      • NetworkInterfaceManager

        public NetworkInterfaceManager()
    • Method Detail

      • configure

        public static void configure​(NetworkInterface netif,
                                     IPConfiguration conf)
                              throws IOException
        Sets the IP configuration of a network interface. The configuration may be sparse. Only not-null and relevant fields are applied. For instance, static IP address is ignored if DHCP is to be used. When the configuration is applied, be aware that:
        • current communications may be interrupted, raising IOException,
        • the hardware module associated to the network interface may reboot, making the interface not usable for a few seconds,
        • it may take time to acquire an IP address if the configuration requires the use of DHCP, making the interface not usable for a few seconds.
        Note that some DNS server addresses may be silently ignored if the underlying module doesn't support several DNS servers.
        Parameters:
        netif - the network interface.
        conf - the network interface IP configuration.
        Throws:
        IOException - if it is not possible to reconfigure the interface.
        IllegalArgumentException - if netif or conf is null.
      • isEnabled

        public static boolean isEnabled​(NetworkInterface netif)
        Tells whether a network interface is enabled or not.
        Parameters:
        netif - the network interface.
        Returns:
        true if enabled, false otherwise
        Throws:
        IllegalArgumentException - if netif is null.
      • isStarted

        public static boolean isStarted​(NetworkInterface netif)
        Tells whether a network interface is started or not.
        Parameters:
        netif - the network interface.
        Returns:
        true if started, false otherwise.
        Throws:
        IllegalArgumentException - if netif is null.
      • stop

        public static void stop​(NetworkInterface netif)
                         throws IOException
        Stops a network interface (hardware side). Stopping a network interface will also disable it. Current communications may be interrupted, raising IOExceptions.
        Parameters:
        netif - the interface to stop.
        Throws:
        IOException - if an I/O error occurs.
        IllegalArgumentException - if netif is null.
      • setDefault

        public static void setDefault​(NetworkInterface netif)
                               throws IOException
        Set a network interface as default. This default network interface will be used for all communications which do not specified a targeted network interface.
        Parameters:
        netif - the network interface to use as the default one.
        Throws:
        IOException - if an I/O error occurs.
        IllegalArgumentException - if netif is null.