NetworkAgentInfo.java revision 954394653dad05838235f48244a4320893e0f0cf
1/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server.connectivity;
18
19import android.content.Context;
20import android.net.LinkProperties;
21import android.net.Network;
22import android.net.NetworkCapabilities;
23import android.net.NetworkInfo;
24import android.net.NetworkMisc;
25import android.net.NetworkRequest;
26import android.os.Handler;
27import android.os.Messenger;
28import android.util.SparseArray;
29
30import com.android.internal.util.AsyncChannel;
31import com.android.server.connectivity.NetworkMonitor;
32
33import java.util.ArrayList;
34
35/**
36 * A bag class used by ConnectivityService for holding a collection of most recent
37 * information published by a particular NetworkAgent as well as the
38 * AsyncChannel/messenger for reaching that NetworkAgent and lists of NetworkRequests
39 * interested in using it.
40 */
41public class NetworkAgentInfo {
42    public NetworkInfo networkInfo;
43    public Network network;
44    public LinkProperties linkProperties;
45    public NetworkCapabilities networkCapabilities;
46    public final NetworkMonitor networkMonitor;
47    public final NetworkMisc networkMisc;
48    public boolean created;
49    public boolean validated;
50
51    // This represents the last score received from the NetworkAgent.
52    private int currentScore;
53    // Penalty applied to scores of Networks that have not been validated.
54    private static final int UNVALIDATED_SCORE_PENALTY = 40;
55
56    // Score for explicitly connected network.
57    private static final int EXPLICITLY_SELECTED_NETWORK_SCORE = 100;
58
59    // The list of NetworkRequests being satisfied by this Network.
60    public final SparseArray<NetworkRequest> networkRequests = new SparseArray<NetworkRequest>();
61    public final ArrayList<NetworkRequest> networkLingered = new ArrayList<NetworkRequest>();
62
63    public final Messenger messenger;
64    public final AsyncChannel asyncChannel;
65
66    // Used by ConnectivityService to keep track of 464xlat.
67    public Nat464Xlat clatd;
68
69    public NetworkAgentInfo(Messenger messenger, AsyncChannel ac, NetworkInfo info,
70            LinkProperties lp, NetworkCapabilities nc, int score, Context context, Handler handler,
71            NetworkMisc misc) {
72        this.messenger = messenger;
73        asyncChannel = ac;
74        network = null;
75        networkInfo = info;
76        linkProperties = lp;
77        networkCapabilities = nc;
78        currentScore = score;
79        networkMonitor = new NetworkMonitor(context, handler, this);
80        networkMisc = misc;
81        created = false;
82        validated = false;
83    }
84
85    public void addRequest(NetworkRequest networkRequest) {
86        networkRequests.put(networkRequest.requestId, networkRequest);
87    }
88
89    public boolean isVPN() {
90        return networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_VPN);
91    }
92
93    // Get the current score for this Network.  This may be modified from what the
94    // NetworkAgent sent, as it has modifiers applied to it.
95    public int getCurrentScore() {
96        // TODO: We may want to refactor this into a NetworkScore class that takes a base score from
97        // the NetworkAgent and signals from the NetworkAgent and uses those signals to modify the
98        // score.  The NetworkScore class would provide a nice place to centralize score constants
99        // so they are not scattered about the transports.
100
101        int score = currentScore;
102
103        if (!validated) score -= UNVALIDATED_SCORE_PENALTY;
104        if (score < 0) score = 0;
105
106        if (networkMisc.explicitlySelected) score = EXPLICITLY_SELECTED_NETWORK_SCORE;
107
108        return score;
109    }
110
111    public void setCurrentScore(int newScore) {
112        currentScore = newScore;
113    }
114
115    public String toString() {
116        return "NetworkAgentInfo{ ni{" + networkInfo + "}  network{" +
117                network + "}  lp{" +
118                linkProperties + "}  nc{" +
119                networkCapabilities + "}  Score{" + getCurrentScore() + "} " +
120                "validated{" + validated + "} created{" + created + "} " +
121                "explicitlySelected{" + networkMisc.explicitlySelected + "} }";
122    }
123
124    public String name() {
125        return "NetworkAgentInfo [" + networkInfo.getTypeName() + " (" +
126                networkInfo.getSubtypeName() + ") - " +
127                (network == null ? "null" : network.toString()) + "]";
128    }
129}
130