Merge "Remember to cancel lingering when a network again satsifies a NetworkRequest." into lmp-mr1-dev

This commit is contained in:
Paul Jensen
2014-12-04 19:27:50 +00:00
committed by Android (Google) Code Review
2 changed files with 36 additions and 36 deletions

View File

@@ -2076,6 +2076,17 @@ public class ConnectivityService extends IConnectivityManager.Stub
} }
} }
// Cancel any lingering so the linger timeout doesn't teardown a network.
// This should be called when a network begins satisfying a NetworkRequest.
// Note: depending on what state the NetworkMonitor is in (e.g.,
// if it's awaiting captive portal login, or if validation failed), this
// may trigger a re-evaluation of the network.
private void unlinger(NetworkAgentInfo nai) {
if (VDBG) log("Canceling linger of " + nai.name());
nai.networkLingered.clear();
nai.networkMonitor.sendMessage(NetworkMonitor.CMD_NETWORK_CONNECTED);
}
private void handleAsyncChannelHalfConnect(Message msg) { private void handleAsyncChannelHalfConnect(Message msg) {
AsyncChannel ac = (AsyncChannel) msg.obj; AsyncChannel ac = (AsyncChannel) msg.obj;
if (mNetworkFactoryInfos.containsKey(msg.replyTo)) { if (mNetworkFactoryInfos.containsKey(msg.replyTo)) {
@@ -2111,6 +2122,7 @@ public class ConnectivityService extends IConnectivityManager.Stub
} }
} }
} }
private void handleAsyncChannelDisconnected(Message msg) { private void handleAsyncChannelDisconnected(Message msg) {
NetworkAgentInfo nai = mNetworkAgentInfos.get(msg.replyTo); NetworkAgentInfo nai = mNetworkAgentInfos.get(msg.replyTo);
if (nai != null) { if (nai != null) {
@@ -2160,11 +2172,8 @@ public class ConnectivityService extends IConnectivityManager.Stub
mNetworkForRequestId.remove(request.requestId); mNetworkForRequestId.remove(request.requestId);
sendUpdatedScoreToFactories(request, 0); sendUpdatedScoreToFactories(request, 0);
NetworkAgentInfo alternative = null; NetworkAgentInfo alternative = null;
for (Map.Entry entry : mNetworkAgentInfos.entrySet()) { for (NetworkAgentInfo existing : mNetworkAgentInfos.values()) {
NetworkAgentInfo existing = (NetworkAgentInfo)entry.getValue(); if (existing.satisfies(request) &&
if (existing.networkInfo.isConnected() &&
request.networkCapabilities.satisfiedByNetworkCapabilities(
existing.networkCapabilities) &&
(alternative == null || (alternative == null ||
alternative.getCurrentScore() < existing.getCurrentScore())) { alternative.getCurrentScore() < existing.getCurrentScore())) {
alternative = existing; alternative = existing;
@@ -2184,8 +2193,7 @@ public class ConnectivityService extends IConnectivityManager.Stub
requestNetworkTransitionWakelock(nai.name()); requestNetworkTransitionWakelock(nai.name());
} }
for (NetworkAgentInfo networkToActivate : toActivate) { for (NetworkAgentInfo networkToActivate : toActivate) {
networkToActivate.networkLingered.clear(); unlinger(networkToActivate);
networkToActivate.networkMonitor.sendMessage(NetworkMonitor.CMD_NETWORK_CONNECTED);
rematchNetworkAndRequests(networkToActivate, false); rematchNetworkAndRequests(networkToActivate, false);
} }
} }
@@ -2220,44 +2228,35 @@ public class ConnectivityService extends IConnectivityManager.Stub
private void handleRegisterNetworkRequest(Message msg) { private void handleRegisterNetworkRequest(Message msg) {
final NetworkRequestInfo nri = (NetworkRequestInfo) (msg.obj); final NetworkRequestInfo nri = (NetworkRequestInfo) (msg.obj);
final NetworkCapabilities newCap = nri.request.networkCapabilities;
int score = 0;
mNetworkRequests.put(nri.request, nri); mNetworkRequests.put(nri.request, nri);
// TODO: This logic may be better replaced with a call to rematchNetworkAndRequests
// Check for the best currently alive network that satisfies this request // Check for the best currently alive network that satisfies this request
NetworkAgentInfo bestNetwork = null; NetworkAgentInfo bestNetwork = null;
for (NetworkAgentInfo network : mNetworkAgentInfos.values()) { for (NetworkAgentInfo network : mNetworkAgentInfos.values()) {
if (DBG) log("handleRegisterNetworkRequest checking " + network.name()); if (DBG) log("handleRegisterNetworkRequest checking " + network.name());
if (newCap.satisfiedByNetworkCapabilities(network.networkCapabilities)) { if (network.satisfies(nri.request)) {
if (DBG) log("apparently satisfied. currentScore=" + network.getCurrentScore()); if (DBG) log("apparently satisfied. currentScore=" + network.getCurrentScore());
if ((bestNetwork == null) ||
bestNetwork.getCurrentScore() < network.getCurrentScore()) {
if (!nri.isRequest) { if (!nri.isRequest) {
// Not setting bestNetwork here as a listening NetworkRequest may be // Not setting bestNetwork here as a listening NetworkRequest may be
// satisfied by multiple Networks. Instead the request is added to // satisfied by multiple Networks. Instead the request is added to
// each satisfying Network and notified about each. // each satisfying Network and notified about each.
network.addRequest(nri.request); network.addRequest(nri.request);
notifyNetworkCallback(network, nri); notifyNetworkCallback(network, nri);
} else { } else if (bestNetwork == null ||
bestNetwork.getCurrentScore() < network.getCurrentScore()) {
bestNetwork = network; bestNetwork = network;
} }
} }
} }
}
if (bestNetwork != null) { if (bestNetwork != null) {
if (DBG) log("using " + bestNetwork.name()); if (DBG) log("using " + bestNetwork.name());
if (bestNetwork.networkInfo.isConnected()) { unlinger(bestNetwork);
// Cancel any lingering so the linger timeout doesn't teardown this network
// even though we have a request for it.
bestNetwork.networkLingered.clear();
bestNetwork.networkMonitor.sendMessage(NetworkMonitor.CMD_NETWORK_CONNECTED);
}
// TODO: This logic may be better replaced with a call to rematchNetworkAndRequests
bestNetwork.addRequest(nri.request); bestNetwork.addRequest(nri.request);
mNetworkForRequestId.put(nri.request.requestId, bestNetwork); mNetworkForRequestId.put(nri.request.requestId, bestNetwork);
notifyNetworkCallback(bestNetwork, nri); notifyNetworkCallback(bestNetwork, nri);
score = bestNetwork.getCurrentScore();
if (nri.request.legacyType != TYPE_NONE) { if (nri.request.legacyType != TYPE_NONE) {
mLegacyTypeTracker.add(nri.request.legacyType, bestNetwork); mLegacyTypeTracker.add(nri.request.legacyType, bestNetwork);
} }
@@ -2265,6 +2264,7 @@ public class ConnectivityService extends IConnectivityManager.Stub
if (nri.isRequest) { if (nri.isRequest) {
if (DBG) log("sending new NetworkRequest to factories"); if (DBG) log("sending new NetworkRequest to factories");
final int score = bestNetwork == null ? 0 : bestNetwork.getCurrentScore();
for (NetworkFactoryInfo nfi : mNetworkFactoryInfos.values()) { for (NetworkFactoryInfo nfi : mNetworkFactoryInfos.values()) {
nfi.asyncChannel.sendMessage(android.net.NetworkFactory.CMD_REQUEST_NETWORK, score, nfi.asyncChannel.sendMessage(android.net.NetworkFactory.CMD_REQUEST_NETWORK, score,
0, nri.request); 0, nri.request);
@@ -3966,8 +3966,7 @@ public class ConnectivityService extends IConnectivityManager.Stub
// check if it satisfies the NetworkCapabilities // check if it satisfies the NetworkCapabilities
if (VDBG) log(" checking if request is satisfied: " + nri.request); if (VDBG) log(" checking if request is satisfied: " + nri.request);
if (nri.request.networkCapabilities.satisfiedByNetworkCapabilities( if (newNetwork.satisfies(nri.request)) {
newNetwork.networkCapabilities)) {
if (!nri.isRequest) { if (!nri.isRequest) {
// This is not a request, it's a callback listener. // This is not a request, it's a callback listener.
// Add it to newNetwork regardless of score. // Add it to newNetwork regardless of score.
@@ -4047,12 +4046,7 @@ public class ConnectivityService extends IConnectivityManager.Stub
nai.networkMonitor.sendMessage(NetworkMonitor.CMD_NETWORK_LINGER); nai.networkMonitor.sendMessage(NetworkMonitor.CMD_NETWORK_LINGER);
notifyNetworkCallbacks(nai, ConnectivityManager.CALLBACK_LOSING); notifyNetworkCallbacks(nai, ConnectivityManager.CALLBACK_LOSING);
} else { } else {
// not going to linger, so kill the list of linger networks.. only unlinger(nai);
// notify them of linger if it happens as the result of gaining another,
// but if they transition and old network stays up, don't tell them of linger
// or very delayed loss
nai.networkLingered.clear();
if (VDBG) log("Lingered for " + nai.name() + " cleared");
} }
} }
if (keep) { if (keep) {

View File

@@ -96,6 +96,12 @@ public class NetworkAgentInfo {
networkRequests.put(networkRequest.requestId, networkRequest); networkRequests.put(networkRequest.requestId, networkRequest);
} }
// Does this network satisfy request?
public boolean satisfies(NetworkRequest request) {
return created &&
request.networkCapabilities.satisfiedByNetworkCapabilities(networkCapabilities);
}
public boolean isVPN() { public boolean isVPN() {
return networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_VPN); return networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_VPN);
} }