Refactor NetworkUtils interaction with DHCP.

Separate out starting DHCP (DISCOVER) and RENEW operations from fetching
the results.  Add NetworkUtils.getDhcpResults(), to enable quick checks
of any available DhcpResults without extraneous interaction with the
DHCP daemon.

Bug: 19422416
Change-Id: I58808e529dda8429737e749f5caef56d923c0809
This commit is contained in:
Erik Kline 2015-02-19 19:51:15 +09:00
commit 9def7be67d

View file

@ -54,6 +54,30 @@ public class NetworkUtils {
*/
public native static int resetConnections(String interfaceName, int mask);
/**
* Start the DHCP client daemon, in order to have it request addresses
* for the named interface. This returns {@code true} if the DHCPv4 daemon
* starts, {@code false} otherwise. This call blocks until such time as a
* result is available or the default discovery timeout has been reached.
* Callers should check {@link #getDhcpResults} to determine whether DHCP
* succeeded or failed, and if it succeeded, to fetch the {@link DhcpResults}.
* @param interfaceName the name of the interface to configure
* @return {@code true} for success, {@code false} for failure
*/
public native static boolean startDhcp(String interfaceName);
/**
* Initiate renewal on the DHCP client daemon for the named interface. This
* returns {@code true} if the DHCPv4 daemon has been notified, {@code false}
* otherwise. This call blocks until such time as a result is available or
* the default renew timeout has been reached. Callers should check
* {@link #getDhcpResults} to determine whether DHCP succeeded or failed,
* and if it succeeded, to fetch the {@link DhcpResults}.
* @param interfaceName the name of the interface to configure
* @return {@code true} for success, {@code false} for failure
*/
public native static boolean startDhcpRenew(String interfaceName);
/**
* Start the DHCP client daemon, in order to have it request addresses
* for the named interface, and then configure the interface with those
@ -64,17 +88,31 @@ public class NetworkUtils {
* the IP address information.
* @return {@code true} for success, {@code false} for failure
*/
public native static boolean runDhcp(String interfaceName, DhcpResults dhcpResults);
public static boolean runDhcp(String interfaceName, DhcpResults dhcpResults) {
return startDhcp(interfaceName) && getDhcpResults(interfaceName, dhcpResults);
}
/**
* Initiate renewal on the Dhcp client daemon. This call blocks until it obtains
* Initiate renewal on the DHCP client daemon. This call blocks until it obtains
* a result (either success or failure) from the daemon.
* @param interfaceName the name of the interface to configure
* @param dhcpResults if the request succeeds, this object is filled in with
* the IP address information.
* @return {@code true} for success, {@code false} for failure
*/
public native static boolean runDhcpRenew(String interfaceName, DhcpResults dhcpResults);
public static boolean runDhcpRenew(String interfaceName, DhcpResults dhcpResults) {
return startDhcpRenew(interfaceName) && getDhcpResults(interfaceName, dhcpResults);
}
/**
* Fetch results from the DHCP client daemon. This call returns {@code true} if
* if there are results available to be read, {@code false} otherwise.
* @param interfaceName the name of the interface to configure
* @param dhcpResults if the request succeeds, this object is filled in with
* the IP address information.
* @return {@code true} for success, {@code false} for failure
*/
public native static boolean getDhcpResults(String interfaceName, DhcpResults dhcpResults);
/**
* Shut down the DHCP client daemon.