Search in sources :

Example 51 with Network

use of android.net.Network in project platform_frameworks_base by android.

the class ConnectivityServiceTest method testRequestBenchmark.

@SmallTest
public void testRequestBenchmark() throws Exception {
    // TODO: turn this unit test into a real benchmarking test.
    // Benchmarks connecting and switching performance in the presence of a large number of
    // NetworkRequests.
    // 1. File NUM_REQUESTS requests.
    // 2. Have a network connect. Wait for NUM_REQUESTS onAvailable callbacks to fire.
    // 3. Have a new network connect and outscore the previous. Wait for NUM_REQUESTS onLosing
    //    and NUM_REQUESTS onAvailable callbacks to fire.
    // See how long it took.
    final int NUM_REQUESTS = 90;
    final NetworkRequest request = new NetworkRequest.Builder().clearCapabilities().build();
    final NetworkCallback[] callbacks = new NetworkCallback[NUM_REQUESTS];
    final CountDownLatch availableLatch = new CountDownLatch(NUM_REQUESTS);
    final CountDownLatch losingLatch = new CountDownLatch(NUM_REQUESTS);
    for (int i = 0; i < NUM_REQUESTS; i++) {
        callbacks[i] = new NetworkCallback() {

            @Override
            public void onAvailable(Network n) {
                availableLatch.countDown();
            }

            @Override
            public void onLosing(Network n, int t) {
                losingLatch.countDown();
            }
        };
    }
    final int REGISTER_TIME_LIMIT_MS = 180;
    assertTimeLimit("Registering callbacks", REGISTER_TIME_LIMIT_MS, () -> {
        for (NetworkCallback cb : callbacks) {
            mCm.registerNetworkCallback(request, cb);
        }
    });
    final int CONNECT_TIME_LIMIT_MS = 40;
    mCellNetworkAgent = new MockNetworkAgent(TRANSPORT_CELLULAR);
    // Don't request that the network validate, because otherwise connect() will block until
    // the network gets NET_CAPABILITY_VALIDATED, after all the callbacks below have fired,
    // and we won't actually measure anything.
    mCellNetworkAgent.connect(false);
    long onAvailableDispatchingDuration = durationOf(() -> {
        if (!awaitLatch(availableLatch, CONNECT_TIME_LIMIT_MS)) {
            fail(String.format("Only dispatched %d/%d onAvailable callbacks in %dms", NUM_REQUESTS - availableLatch.getCount(), NUM_REQUESTS, CONNECT_TIME_LIMIT_MS));
        }
    });
    Log.d(TAG, String.format("Connect, %d callbacks: %dms, acceptable %dms", NUM_REQUESTS, onAvailableDispatchingDuration, CONNECT_TIME_LIMIT_MS));
    final int SWITCH_TIME_LIMIT_MS = 40;
    mWiFiNetworkAgent = new MockNetworkAgent(TRANSPORT_WIFI);
    // Give wifi a high enough score that we'll linger cell when wifi comes up.
    mWiFiNetworkAgent.adjustScore(40);
    mWiFiNetworkAgent.connect(false);
    long onLostDispatchingDuration = durationOf(() -> {
        if (!awaitLatch(losingLatch, SWITCH_TIME_LIMIT_MS)) {
            fail(String.format("Only dispatched %d/%d onLosing callbacks in %dms", NUM_REQUESTS - losingLatch.getCount(), NUM_REQUESTS, SWITCH_TIME_LIMIT_MS));
        }
    });
    Log.d(TAG, String.format("Linger, %d callbacks: %dms, acceptable %dms", NUM_REQUESTS, onLostDispatchingDuration, SWITCH_TIME_LIMIT_MS));
    final int UNREGISTER_TIME_LIMIT_MS = 10;
    assertTimeLimit("Unregistering callbacks", UNREGISTER_TIME_LIMIT_MS, () -> {
        for (NetworkCallback cb : callbacks) {
            mCm.unregisterNetworkCallback(cb);
        }
    });
}
Also used : Network(android.net.Network) NetworkRequest(android.net.NetworkRequest) CountDownLatch(java.util.concurrent.CountDownLatch) NetworkCallback(android.net.ConnectivityManager.NetworkCallback) SmallTest(android.test.suitebuilder.annotation.SmallTest)

Example 52 with Network

use of android.net.Network in project android_frameworks_base by crdroidandroid.

the class ConnectivityService method getDefaultNetworkCapabilitiesForUser.

@Override
public NetworkCapabilities[] getDefaultNetworkCapabilitiesForUser(int userId) {
    // The basic principle is: if an app's traffic could possibly go over a
    // network, without the app doing anything multinetwork-specific,
    // (hence, by "default"), then include that network's capabilities in
    // the array.
    //
    // In the normal case, app traffic only goes over the system's default
    // network connection, so that's the only network returned.
    //
    // With a VPN in force, some app traffic may go into the VPN, and thus
    // over whatever underlying networks the VPN specifies, while other app
    // traffic may go over the system default network (e.g.: a split-tunnel
    // VPN, or an app disallowed by the VPN), so the set of networks
    // returned includes the VPN's underlying networks and the system
    // default.
    enforceAccessPermission();
    HashMap<Network, NetworkCapabilities> result = new HashMap<Network, NetworkCapabilities>();
    NetworkAgentInfo nai = getDefaultNetwork();
    NetworkCapabilities nc = getNetworkCapabilitiesInternal(nai);
    if (nc != null) {
        result.put(nai.network, nc);
    }
    if (!mLockdownEnabled) {
        synchronized (mVpns) {
            Vpn vpn = mVpns.get(userId);
            if (vpn != null) {
                Network[] networks = vpn.getUnderlyingNetworks();
                if (networks != null) {
                    for (Network network : networks) {
                        nai = getNetworkAgentInfoForNetwork(network);
                        nc = getNetworkCapabilitiesInternal(nai);
                        if (nc != null) {
                            result.put(network, nc);
                        }
                    }
                }
            }
        }
    }
    NetworkCapabilities[] out = new NetworkCapabilities[result.size()];
    out = result.values().toArray(out);
    return out;
}
Also used : HashMap(java.util.HashMap) Vpn(com.android.server.connectivity.Vpn) NetworkAgentInfo(com.android.server.connectivity.NetworkAgentInfo) Network(android.net.Network) NetworkCapabilities(android.net.NetworkCapabilities)

Example 53 with Network

use of android.net.Network in project android_frameworks_base by crdroidandroid.

the class ConnectivityService method registerNetworkAgent.

public int registerNetworkAgent(Messenger messenger, NetworkInfo networkInfo, LinkProperties linkProperties, NetworkCapabilities networkCapabilities, int currentScore, NetworkMisc networkMisc) {
    enforceConnectivityInternalPermission();
    // TODO: Instead of passing mDefaultRequest, provide an API to determine whether a Network
    // satisfies mDefaultRequest.
    final NetworkAgentInfo nai = new NetworkAgentInfo(messenger, new AsyncChannel(), new Network(reserveNetId()), new NetworkInfo(networkInfo), new LinkProperties(linkProperties), new NetworkCapabilities(networkCapabilities), currentScore, mContext, mTrackerHandler, new NetworkMisc(networkMisc), mDefaultRequest, this);
    synchronized (this) {
        nai.networkMonitor.systemReady = mSystemReady;
    }
    addValidationLogs(nai.networkMonitor.getValidationLogs(), nai.network, networkInfo.getExtraInfo());
    if (DBG)
        log("registerNetworkAgent " + nai);
    mHandler.sendMessage(mHandler.obtainMessage(EVENT_REGISTER_NETWORK_AGENT, nai));
    return nai.network.netId;
}
Also used : NetworkMisc(android.net.NetworkMisc) NetworkInfo(android.net.NetworkInfo) NetworkAgentInfo(com.android.server.connectivity.NetworkAgentInfo) Network(android.net.Network) AsyncChannel(com.android.internal.util.AsyncChannel) LinkProperties(android.net.LinkProperties) NetworkCapabilities(android.net.NetworkCapabilities)

Example 54 with Network

use of android.net.Network in project robolectric by robolectric.

the class ShadowNetworkTest method getNetId_shouldReturnConstructorNetId.

@Test
public void getNetId_shouldReturnConstructorNetId() {
    final int netId = 123;
    Network network = ShadowNetwork.newInstance(netId);
    ShadowNetwork shadowNetwork = Shadows.shadowOf(network);
    assertThat(shadowNetwork.getNetId()).isEqualTo(netId);
}
Also used : Network(android.net.Network) Test(org.junit.Test)

Example 55 with Network

use of android.net.Network in project platform_frameworks_base by android.

the class ConnectivityService method getUnfilteredActiveNetworkState.

private NetworkState getUnfilteredActiveNetworkState(int uid) {
    NetworkAgentInfo nai = getDefaultNetwork();
    final Network[] networks = getVpnUnderlyingNetworks(uid);
    if (networks != null) {
        //                    first one.
        if (networks.length > 0) {
            nai = getNetworkAgentInfoForNetwork(networks[0]);
        } else {
            nai = null;
        }
    }
    if (nai != null) {
        return nai.getNetworkState();
    } else {
        return NetworkState.EMPTY;
    }
}
Also used : NetworkAgentInfo(com.android.server.connectivity.NetworkAgentInfo) Network(android.net.Network)

Aggregations

Network (android.net.Network)92 SmallTest (android.test.suitebuilder.annotation.SmallTest)28 NetworkAgentInfo (com.android.server.connectivity.NetworkAgentInfo)25 NetworkCapabilities (android.net.NetworkCapabilities)22 NetworkRequest (android.net.NetworkRequest)18 NetworkInfo (android.net.NetworkInfo)17 LinkProperties (android.net.LinkProperties)16 ConnectivityManager (android.net.ConnectivityManager)11 NetworkCallback (android.net.ConnectivityManager.NetworkCallback)11 Test (org.junit.Test)7 InetAddress (java.net.InetAddress)6 Nullable (android.annotation.Nullable)5 NetworkMisc (android.net.NetworkMisc)5 NetworkState (android.net.NetworkState)5 WifiConfiguration (android.net.wifi.WifiConfiguration)5 WifiInfo (android.net.wifi.WifiInfo)5 ConditionVariable (android.os.ConditionVariable)5 WebSettings (android.webkit.WebSettings)5 HomeSP (com.android.hotspot2.pps.HomeSP)5 LegacyVpnInfo (com.android.internal.net.LegacyVpnInfo)5