Merge "[VCN02] Make LingerTimer takes request Id instead of NetworkRequest"

This commit is contained in:
Treehugger Robot
2021-01-12 08:59:04 +00:00
committed by Gerrit Code Review
3 changed files with 35 additions and 21 deletions

View File

@@ -39,6 +39,18 @@ import java.util.Set;
* via {@link ConnectivityManager#registerNetworkCallback}. * via {@link ConnectivityManager#registerNetworkCallback}.
*/ */
public class NetworkRequest implements Parcelable { public class NetworkRequest implements Parcelable {
/**
* The first requestId value that will be allocated.
* @hide only used by ConnectivityService.
*/
public static final int FIRST_REQUEST_ID = 1;
/**
* The requestId value that represents the absence of a request.
* @hide only used by ConnectivityService.
*/
public static final int REQUEST_ID_NONE = -1;
/** /**
* The {@link NetworkCapabilities} that define this request. * The {@link NetworkCapabilities} that define this request.
* @hide * @hide

View File

@@ -620,7 +620,7 @@ public class ConnectivityService extends IConnectivityManager.Stub
private LingerMonitor mLingerMonitor; private LingerMonitor mLingerMonitor;
// sequence number of NetworkRequests // sequence number of NetworkRequests
private int mNextNetworkRequestId = 1; private int mNextNetworkRequestId = NetworkRequest.FIRST_REQUEST_ID;
// Sequence number for NetworkProvider IDs. // Sequence number for NetworkProvider IDs.
private final AtomicInteger mNextNetworkProviderId = new AtomicInteger( private final AtomicInteger mNextNetworkProviderId = new AtomicInteger(
@@ -1238,6 +1238,8 @@ public class ConnectivityService extends IConnectivityManager.Stub
} }
private synchronized int nextNetworkRequestId() { private synchronized int nextNetworkRequestId() {
// TODO: Consider handle wrapping and exclude {@link NetworkRequest#REQUEST_ID_NONE} if
// doing that.
return mNextNetworkRequestId++; return mNextNetworkRequestId++;
} }
@@ -7065,11 +7067,11 @@ public class ConnectivityService extends IConnectivityManager.Stub
log(" accepting network in place of " + previousSatisfier.toShortString()); log(" accepting network in place of " + previousSatisfier.toShortString());
} }
previousSatisfier.removeRequest(nri.request.requestId); previousSatisfier.removeRequest(nri.request.requestId);
previousSatisfier.lingerRequest(nri.request, now, mLingerDelayMs); previousSatisfier.lingerRequest(nri.request.requestId, now, mLingerDelayMs);
} else { } else {
if (VDBG || DDBG) log(" accepting network in place of null"); if (VDBG || DDBG) log(" accepting network in place of null");
} }
newSatisfier.unlingerRequest(nri.request); newSatisfier.unlingerRequest(nri.request.requestId);
if (!newSatisfier.addRequest(nri.request)) { if (!newSatisfier.addRequest(nri.request)) {
Log.wtf(TAG, "BUG: " + newSatisfier.toShortString() + " already has " Log.wtf(TAG, "BUG: " + newSatisfier.toShortString() + " already has "
+ nri.request); + nri.request);

View File

@@ -202,28 +202,28 @@ public class NetworkAgentInfo implements Comparable<NetworkAgentInfo> {
// either the linger timeout expiring and the network being taken down, or the network // either the linger timeout expiring and the network being taken down, or the network
// satisfying a request again. // satisfying a request again.
public static class LingerTimer implements Comparable<LingerTimer> { public static class LingerTimer implements Comparable<LingerTimer> {
public final NetworkRequest request; public final int requestId;
public final long expiryMs; public final long expiryMs;
public LingerTimer(NetworkRequest request, long expiryMs) { public LingerTimer(int requestId, long expiryMs) {
this.request = request; this.requestId = requestId;
this.expiryMs = expiryMs; this.expiryMs = expiryMs;
} }
public boolean equals(Object o) { public boolean equals(Object o) {
if (!(o instanceof LingerTimer)) return false; if (!(o instanceof LingerTimer)) return false;
LingerTimer other = (LingerTimer) o; LingerTimer other = (LingerTimer) o;
return (request.requestId == other.request.requestId) && (expiryMs == other.expiryMs); return (requestId == other.requestId) && (expiryMs == other.expiryMs);
} }
public int hashCode() { public int hashCode() {
return Objects.hash(request.requestId, expiryMs); return Objects.hash(requestId, expiryMs);
} }
public int compareTo(LingerTimer other) { public int compareTo(LingerTimer other) {
return (expiryMs != other.expiryMs) ? return (expiryMs != other.expiryMs) ?
Long.compare(expiryMs, other.expiryMs) : Long.compare(expiryMs, other.expiryMs) :
Integer.compare(request.requestId, other.request.requestId); Integer.compare(requestId, other.requestId);
} }
public String toString() { public String toString() {
return String.format("%s, expires %dms", request.toString(), return String.format("%s, expires %dms", requestId,
expiryMs - SystemClock.elapsedRealtime()); expiryMs - SystemClock.elapsedRealtime());
} }
} }
@@ -693,7 +693,7 @@ public class NetworkAgentInfo implements Comparable<NetworkAgentInfo> {
updateRequestCounts(REMOVE, existing); updateRequestCounts(REMOVE, existing);
mNetworkRequests.remove(requestId); mNetworkRequests.remove(requestId);
if (existing.isRequest()) { if (existing.isRequest()) {
unlingerRequest(existing); unlingerRequest(existing.requestId);
} }
} }
@@ -839,33 +839,33 @@ public class NetworkAgentInfo implements Comparable<NetworkAgentInfo> {
} }
/** /**
* Sets the specified request to linger on this network for the specified time. Called by * Sets the specified requestId to linger on this network for the specified time. Called by
* ConnectivityService when the request is moved to another network with a higher score. * ConnectivityService when the request is moved to another network with a higher score.
*/ */
public void lingerRequest(NetworkRequest request, long now, long duration) { public void lingerRequest(int requestId, long now, long duration) {
if (mLingerTimerForRequest.get(request.requestId) != null) { if (mLingerTimerForRequest.get(requestId) != null) {
// Cannot happen. Once a request is lingering on a particular network, we cannot // Cannot happen. Once a request is lingering on a particular network, we cannot
// re-linger it unless that network becomes the best for that request again, in which // re-linger it unless that network becomes the best for that request again, in which
// case we should have unlingered it. // case we should have unlingered it.
Log.wtf(TAG, toShortString() + ": request " + request.requestId + " already lingered"); Log.wtf(TAG, toShortString() + ": request " + requestId + " already lingered");
} }
final long expiryMs = now + duration; final long expiryMs = now + duration;
LingerTimer timer = new LingerTimer(request, expiryMs); LingerTimer timer = new LingerTimer(requestId, expiryMs);
if (VDBG) Log.d(TAG, "Adding LingerTimer " + timer + " to " + toShortString()); if (VDBG) Log.d(TAG, "Adding LingerTimer " + timer + " to " + toShortString());
mLingerTimers.add(timer); mLingerTimers.add(timer);
mLingerTimerForRequest.put(request.requestId, timer); mLingerTimerForRequest.put(requestId, timer);
} }
/** /**
* Cancel lingering. Called by ConnectivityService when a request is added to this network. * Cancel lingering. Called by ConnectivityService when a request is added to this network.
* Returns true if the given request was lingering on this network, false otherwise. * Returns true if the given requestId was lingering on this network, false otherwise.
*/ */
public boolean unlingerRequest(NetworkRequest request) { public boolean unlingerRequest(int requestId) {
LingerTimer timer = mLingerTimerForRequest.get(request.requestId); LingerTimer timer = mLingerTimerForRequest.get(requestId);
if (timer != null) { if (timer != null) {
if (VDBG) Log.d(TAG, "Removing LingerTimer " + timer + " from " + toShortString()); if (VDBG) Log.d(TAG, "Removing LingerTimer " + timer + " from " + toShortString());
mLingerTimers.remove(timer); mLingerTimers.remove(timer);
mLingerTimerForRequest.remove(request.requestId); mLingerTimerForRequest.remove(requestId);
return true; return true;
} }
return false; return false;