三、Android 网络评分机制

在前两节简单介绍了连接管理的大致框架,数据链接的准备工作,包括APN的初始化与默认APN使能,DcTracker的构造,包括各种事件的注册等工作。但是数据链接的打开不止是只有用户主动去打开,Android可以提供数据业务的对象主要有,移动数据网络、WIFI、蓝牙、网线等,这些连接本身都可以独立使用,但是对于用户来说,每一时刻又最多只能使用一种方式接入网络,那么当这些功能同时打开时,比如即使用户打开了移动数据连接,但是又打开了wifi,那么只要wifi畅通,移动数据链接是不会用于上网的,那究竟如何选择最佳的接入环境呢?这就需要提供一个能够动态管理他们的打开与断开的功能,Android专门设计了一套管理方法来实现上面的这种机制,包括ConnectivityManager、ConnectivityService、NetworkAgent等对象之间的关系以及消息流走向,本节在这些知识的基础上介绍连接管理的核心机制,即连接管理中的评分机制,其中ConnectivityService是管理员身份,没种网络都会去向它注册,网络的使用权全靠它来分配。

连接管理通过一个评分机制来实现不同接入方式的选择。具体来说就是,每一种上网方式在初始化时,都向ConnectivityService标明自己网络的分值(比如数据连接50,WIFI60,蓝牙69,网线70),当有更高分数的网络就绪时,就将当前分值低的连接断开。而当当前网络被断开时,就寻找当前就绪的其他网络连接,选取分值高的进行接入。并且,每一个网络接入时,都会进行有效性检测,如果检测不通过,将会被扣掉一定分数,此时该网络的优先级也会相应的下降。下面我们利用三个部分来分析评分机制的原理:
1、NetworkFactory
2、NetworkAgent
3、NetworkMonitor
其中NetworkFactory是每一种网络持有一个,比如WIFI和Telephony会分别注册一个,但是NetworkAgent和NetworkMonitor是一种数据类型就会有一个,比如数据连接总的APN TYPE有8种,其中任意一种链接上之后都会各注册一个。

1、NetworkFactory

NetworkFactory直译就是网络工厂,开机之后每种网络都必须注册自己的NetworkFactory,NetworkFactory的作用是用来创建NetworkAgent,同时作为ConnectivityService与网络之间的通讯枢纽

private DctController(PhoneProxy[] phones) {
    for (int i = 0; i < mPhoneNum; ++i) {
        // Register for radio state change
        PhoneBase phoneBase = (PhoneBase)mPhones[i].getActivePhone();
        updatePhoneBaseForIndex(i, phoneBase);
    }
}    

private void updatePhoneBaseForIndex(int index, PhoneBase phoneBase) {
    phoneBase.getServiceStateTracker().registerForDataConnectionAttached(mRspHandler,
               EVENT_DATA_ATTACHED + index, null);
    phoneBase.getServiceStateTracker().registerForDataConnectionDetached(mRspHandler,
               EVENT_DATA_DETACHED + index, null);

    mNetworkFilter[index] = new NetworkCapabilities();
    mNetworkFilter[index].addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR);
    mNetworkFilter[index].addCapability(NetworkCapabilities.NET_CAPABILITY_MMS);
    mNetworkFilter[index].addCapability(NetworkCapabilities.NET_CAPABILITY_SUPL);
    mNetworkFilter[index].addCapability(NetworkCapabilities.NET_CAPABILITY_DUN);
    mNetworkFilter[index].addCapability(NetworkCapabilities.NET_CAPABILITY_FOTA);
    mNetworkFilter[index].addCapability(NetworkCapabilities.NET_CAPABILITY_IMS);
    mNetworkFilter[index].addCapability(NetworkCapabilities.NET_CAPABILITY_CBS);
    mNetworkFilter[index].addCapability(NetworkCapabilities.NET_CAPABILITY_IA);
    mNetworkFilter[index].addCapability(NetworkCapabilities.NET_CAPABILITY_RCS);
    mNetworkFilter[index].addCapability(NetworkCapabilities.NET_CAPABILITY_XCAP);
    mNetworkFilter[index].addCapability(NetworkCapabilities.NET_CAPABILITY_EIMS);
    mNetworkFilter[index].addCapability(NetworkCapabilities.NET_CAPABILITY_NOT_RESTRICTED);
    mNetworkFilter[index].addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET);

    mNetworkFactory[index] = new TelephonyNetworkFactory(this.getLooper(),
            mPhones[index].getContext(), "TelephonyNetworkFactory", phoneBase,
            mNetworkFilter[index]);
    mNetworkFactory[index].setScoreFilter(50);
    mNetworkFactoryMessenger[index] = new Messenger(mNetworkFactory[index]);
    cm.registerNetworkFactory(mNetworkFactoryMessenger[index], "Telephony");
}

可以看出来一个NetworkFactory 支持多种网络类型(NetworkCapabilities),网络类型与APN的TYPE相对应。
以移动数据网络为例,TelephonyNetworkFactory 将会继承NetworkFactory ,并重写其中两个重要的方法,needNetworkFor和releaseNetworkFor,这两个方法就是ConnectivityService与移动网络之间桥梁,分别负责请求当前网络和断开当前网络。

private class TelephonyNetworkFactory extends NetworkFactory {
    protected void needNetworkFor(NetworkRequest networkRequest, int score) {
        // figure out the apn type and enable it
        if (!SubscriptionManager.isUsableSubIdValue(mPhone.getSubId())) {
            mPendingReq.put(networkRequest.requestId, networkRequest);
            return;
        }
        if (getRequestPhoneId(networkRequest) == mPhone.getPhoneId()) { 
            DcTrackerBase dcTracker =((PhoneBase)mPhone).mDcTracker;
            String apn = apnForNetworkRequest(networkRequest);
            if (dcTracker.isApnSupported(apn)) {
                requestNetwork(networkRequest, dcTracker.getApnPriority(apn));
            }
        } else {
            mPendingReq.put(networkRequest.requestId, networkRequest);
        }
    }  
    protected void releaseNetworkFor(NetworkRequest networkRequest) {
        if (!SubscriptionManager.isUsableSubIdValue(mPhone.getSubId())) {
            mPendingReq.remove(networkRequest.requestId);
            return;
        }
        if (getRequestPhoneId(networkRequest) == mPhone.getPhoneId()) { 
            DcTrackerBase dcTracker =((PhoneBase)mPhone).mDcTracker;
            String apn = apnForNetworkRequest(networkRequest);
            if (dcTracker.isApnSupported(apn)) {
                releaseNetwork(networkRequest);
            }
        }
    }     

再看NetworkFactory 的注册cm.registerNetworkFactory(mNetworkFactoryMessenger[index], “Telephony”);其中mNetworkFactoryMessenger是一个包装了mNetworkFactory的Messenger对象,这个主要是建立AsyncChannel通道时用。

@ConnectivityService.java
public void registerNetworkFactory(Messenger messenger, String name) {
    enforceConnectivityInternalPermission();
    NetworkFactoryInfo nfi = new NetworkFactoryInfo(name, messenger, new AsyncChannel());
    mHandler.sendMessage(mHandler.obtainMessage(EVENT_REGISTER_NETWORK_FACTORY, nfi));
}

handleRegisterNetworkFactory处理EVENT_REGISTER_NETWORK_FACTORY消息

private void handleRegisterNetworkFactory(NetworkFactoryInfo nfi) {
    mNetworkFactoryInfos.put(nfi.messenger, nfi);
    nfi.asyncChannel.connect(mContext, mTrackerHandler, nfi.messenger);
}

在这里,ConnectivityService做了两个事情:
1、将新注册的NetworkFactoryInfo 保存到mNetworkFactoryInfos中;
2、利用刚才创建的AsyncChannel向NetworkAgent发起单向连接请求;
nfi.asyncChannel.connect(mContext, mTrackerHandler, nfi.messenger);即利用传入的Messenger对象建立起ConnectivityService与NetworkFactory的通讯通道,ConnectivityService后续的消息都将通过这个asyncChannel传入到数据网络中的NetworkFactory。
当asyncChannel通道建立成功后ConnectivityService会收到CMD_CHANNEL_HALF_CONNECTED消息。

@Override
public void handleMessage(Message msg) {
    NetworkInfo info;
    switch (msg.what) {
        case AsyncChannel.CMD_CHANNEL_HALF_CONNECTED: {
            handleAsyncChannelHalfConnect(msg);
            break;
        }
}
private void handleAsyncChannelHalfConnect(Message msg) {
    AsyncChannel ac = (AsyncChannel) msg.obj;
    if (mNetworkFactoryInfos.containsKey(msg.replyTo)) {  //此时是链接的是NetworkFactory,走这个path
        if (msg.arg1 == AsyncChannel.STATUS_SUCCESSFUL) {
            // A network factory has connected.  Send it all current NetworkRequests.
            for (NetworkRequestInfo nri : mNetworkRequests.values()) {
                if (nri.isRequest == false) continue;
                NetworkAgentInfo nai = mNetworkForRequestId.get(nri.request.requestId);
                ac.sendMessage(android.net.NetworkFactory.CMD_REQUEST_NETWORK,(nai != null ? nai.getCurrentScore() : 0), 0, nri.request);
            }
        } else {
            mNetworkFactoryInfos.remove(msg.obj);
        }
    } else if (mNetworkAgentInfos.containsKey(msg.replyTo)) {

    }
}

此时是链接的是NetworkFactory,走这个path,mNetworkFactoryInfos是在handleRegisterNetworkFactory时保存的。
在这里,ConnectivityService通过AsyncChannel通道向当前的NetworkFactory发起CMD_REQUEST_NETWORK的请求,需要注意的是,该请求所附带的第二个参数选择,由于当前处于初始化阶段,因此当前的mNetworkForRequestId中为空,也就是说此时传递的第二个参数必然为0。
我们接下来看NetworkFactory收到该请求时的处理:

@NetworkFactory.java
public void handleMessage(Message msg) {
    switch (msg.what) {
        case CMD_REQUEST_NETWORK: {
            handleAddRequest((NetworkRequest)msg.obj, msg.arg1);
            break;
        }
    }
}
private void handleAddRequest(NetworkRequest request, int score) {
    NetworkRequestInfo n = mNetworkRequests.get(request.requestId);
    if (n == null) {
        if (DBG) log("got request " + request + " with score " + score);
        n = new NetworkRequestInfo(request, score);
        mNetworkRequests.put(n.request.requestId, n);
    } else {
        n.score = score;
    }
    evalRequest(n);
}

接下来评估网络评分,是需要链接网络还是断开网路

private void evalRequest(NetworkRequestInfo n) {
    if (n.requested == false && n.score < mScore &&
            n.request.networkCapabilities.satisfiedByNetworkCapabilities(
            mCapabilityFilter) && acceptRequest(n.request, n.score)) {
        needNetworkFor(n.request, n.score);
        n.requested = true;
    } else if (n.requested == true &&
            (n.score > mScore || n.request.networkCapabilities.satisfiedByNetworkCapabilities(
            mCapabilityFilter) == false || acceptRequest(n.request, n.score) == false)) {
        releaseNetworkFor(n.request);
        n.requested = false;
    } 
}

该逻辑就是整个网络评价系统最关键的地方,如果NetworkRequestInfo没有被requested过,并且其分值(n.score)小于当前NetworkFactory自己的分值(mScore),那么就说明,当前NetworkFactory所处的网络优先级高于其他网络的优先级,就会触发当前NetworkFactory所在网络的needNetworkFor()流程,也就是连接建立流程,并将标记NetworkRequestInfo.requested=true。
当NetworkRequestInfo被requested过(也就是当前网络被needNetworkFor过),此时如果再次收到请求,并且携带的新score大于当前NetworkFactory所处网络的mScore,那么就说明当前NetworkFactory所在网络优先级已经不是最高,需要将其releaseNetworkFor掉,并标记NetworkRequestInfo.requested=false。

evalRequest中调用TelephonyNetworkFactory 重写的needNetworkFor或者releaseNetworkFor,分别是链接网络和断开网络,后续的流程如下图(请求网络的情况)
这里写图片描述
在此数据链接的NetworkFactory算是创建完毕,并将自己注册到ConnectivityService中。

2、NetworkAgent

前面提到NetworkFactory是在系统初始化时就被创建,而NetworkAgent是在真正接入网络时才会创建,NetworkAgent的创建在DataConnection状态机里的DcActiveState状态时。

private class DcActiveState extends State {
    @Override public void enter() {
        // If we were retrying there maybe more than one, otherwise they'll only be one.
        notifyAllOfConnected(Phone.REASON_CONNECTED);

        mNetworkInfo.setDetailedState(NetworkInfo.DetailedState.CONNECTED,
                mNetworkInfo.getReason(), null);
        mNetworkInfo.setExtraInfo(mApnSetting.apn);
        updateTcpBufferSizes(mRilRat);

        final NetworkMisc misc = new NetworkMisc();
        misc.subscriberId = mPhone.getSubscriberId();
        mNetworkAgent = new DcNetworkAgent(getHandler().getLooper(), mPhone.getContext(),
                "DcNetworkAgent", mNetworkInfo, makeNetworkCapabilities(), mLinkProperties,
                50, misc);
    }
}
public NetworkAgent(Looper looper, Context context, String logTag, NetworkInfo ni,
        NetworkCapabilities nc, LinkProperties lp, int score, NetworkMisc misc) {
    super(looper);

    mContext = context;
    ConnectivityManager cm = (ConnectivityManager)mContext.getSystemService(
            Context.CONNECTIVITY_SERVICE);
    cm.registerNetworkAgent(new Messenger(this), new NetworkInfo(ni),
            new LinkProperties(lp), new NetworkCapabilities(nc), score, misc);
}  

当网络链接完成之后,就会新建一个DcNetworkAgent,接着分析NetworkAgent的构造,和NetworkFactory类似,也是将自己注册到ConnectivityService中去,继续看registerNetworkAgent

public void registerNetworkAgent(Messenger messenger, NetworkInfo networkInfo,
        LinkProperties linkProperties, NetworkCapabilities networkCapabilities,
        int currentScore, NetworkMisc networkMisc) {

    NetworkAgentInfo nai = new NetworkAgentInfo(messenger, new AsyncChannel(),
        new NetworkInfo(networkInfo), new LinkProperties(linkProperties),
        new NetworkCapabilities(networkCapabilities), currentScore, mContext, mTrackerHandler,
        new NetworkMisc(networkMisc), mDefaultRequest);
    synchronized (this) {
        nai.networkMonitor.systemReady = mSystemReady;
    }
    mHandler.sendMessage(mHandler.obtainMessage(EVENT_REGISTER_NETWORK_AGENT, nai));
}

private void handleRegisterNetworkAgent(NetworkAgentInfo na) {
    mNetworkAgentInfos.put(na.messenger, na);
    assignNextNetId(na);
    na.asyncChannel.connect(mContext, mTrackerHandler, na.messenger);
    NetworkInfo networkInfo = na.networkInfo;
    na.networkInfo = null;
    updateNetworkInfo(na, networkInfo);
}

在这里,ConnectivityService做了三个事情:
1、将新注册的NetworkAgentInfo保存到mNetworkAgentInfos中;
2、利用刚才创建的AsyncChannel向NetworkAgent发起单向连接请求;
3、更新最新的NetworkAgentInfo状态;

@Override
public void handleMessage(Message msg) {
    NetworkInfo info;
    switch (msg.what) {
        case AsyncChannel.CMD_CHANNEL_HALF_CONNECTED: {
            handleAsyncChannelHalfConnect(msg);
            break;
        }
}

以上流程和NetworkFactory注册时几乎一模一样的模式

private void handleAsyncChannelHalfConnect(Message msg) {
    AsyncChannel ac = (AsyncChannel) msg.obj;
    if (mNetworkFactoryInfos.containsKey(msg.replyTo)) {  

    }  else if (mNetworkAgentInfos.containsKey(msg.replyTo)) {  //此时是链接的是NetworkAgent,走这个path
        if (msg.arg1 == AsyncChannel.STATUS_SUCCESSFUL) {
            // A network agent has requested a connection.  Establish the connection.
            mNetworkAgentInfos.get(msg.replyTo).asyncChannel.
                    sendMessage(AsyncChannel.CMD_CHANNEL_FULL_CONNECTION);
        } 
    }
}

唯一的区别是在handleAsyncChannelHalfConnect中这里,当ConnectivityService与NetworkAgent之间单向通道建立完成后,又发起了双向通道的请求,此时在NetworkAgent端,将会收到CMD_CHANNEL_FULL_CONNECTION的消息,建立双向通道的目的是,有时候网络也需要通过AsyncChannel向ConnectivityService发送消息。至此,NetworkAgent的初始化完毕。
现在的问题是NetworkAgent如何影响网络链接的?
NetworkAgent提供了两种方法更新评分管理:
1、sendNetworkScore

public void sendNetworkScore(int score) {
    queueOrSendMessage(EVENT_NETWORK_SCORE_CHANGED, new Integer(score));
}

2、sendNetworkInfo

public void sendNetworkInfo(NetworkInfo networkInfo) {
    queueOrSendMessage(EVENT_NETWORK_INFO_CHANGED, new NetworkInfo(networkInfo));
}

先来分析第二种情况,比如移动数据网络的断开时就会调用此方法:

@DataConnection.java
private class DcActiveState extends State {
    public void exit() {
        mNetworkInfo.setDetailedState(NetworkInfo.DetailedState.DISCONNECTED,
                mNetworkInfo.getReason(), mNetworkInfo.getExtraInfo());
        mNetworkAgent.sendNetworkInfo(mNetworkInfo);
        mNetworkAgent = null;
    }
}

接着就会进入ConnectivityService

@Override
public void handleMessage(Message msg) {
    NetworkInfo info;
        case NetworkAgent.EVENT_NETWORK_INFO_CHANGED: {
            NetworkAgentInfo nai = mNetworkAgentInfos.get(msg.replyTo);
            info = (NetworkInfo) msg.obj;
            updateNetworkInfo(nai, info);
            break;
        }
}
private void updateNetworkInfo(NetworkAgentInfo networkAgent, NetworkInfo newInfo) {
    if (state == NetworkInfo.State.CONNECTED && !networkAgent.created) {

    } else if (state == NetworkInfo.State.DISCONNECTED || state == NetworkInfo.State.SUSPENDED) {
        networkAgent.asyncChannel.disconnect();        
    }   

由于是断开数据网络,因此这里是断开AsyncChannel,从而进入

AsyncChannel.CMD_CHANNEL_DISCONNECTED
        @Override
        public void handleMessage(Message msg) {
            NetworkInfo info;
            switch (msg.what) {
                case AsyncChannel.CMD_CHANNEL_DISCONNECTED: {
                    handleAsyncChannelDisconnected(msg);
                    break;
                }
        }
private void handleAsyncChannelDisconnected(Message msg) {  
    NetworkAgentInfo nai = mNetworkAgentInfos.get(msg.replyTo);  
    if (nai != null) {  
        //删掉当前NetworkAgent对象  
        mNetworkAgentInfos.remove(msg.replyTo);  
        final ArrayList<NetworkAgentInfo> toActivate = new ArrayList<NetworkAgentInfo>();  
        for (int i = 0; i < nai.networkRequests.size(); i++) {  
            NetworkRequest request = nai.networkRequests.valueAt(i);  
            NetworkAgentInfo currentNetwork = mNetworkForRequestId.get(request.requestId);  
            if (currentNetwork != null && currentNetwork.network.netId == nai.network.netId) {  
                mNetworkForRequestId.remove(request.requestId);  
                //将0分更新到各个NetworkFactory中  
                sendUpdatedScoreToFactories(request, 0);  
            }  
        }  
    }  
}  
private void sendUpdatedScoreToFactories(NetworkRequest networkRequest, int score) {
    for (NetworkFactoryInfo nfi : mNetworkFactoryInfos.values()) {
        nfi.asyncChannel.sendMessage(android.net.NetworkFactory.CMD_REQUEST_NETWORK, score, 0,networkRequest);
    }
}

在这里,由于当前连接是断开状态,因此其分值必然为0,这样就把他的0分值通知到各个NetworkFactory中,由NetworkFactory判断是否需要开启自己的网络,通知方法同样是CMD_REQUEST_NETWORK,也就是说,无论是直接更新NetworkAgent中的分数,还是更新NetworkAgent的状态,最终都会触发NetworkFactory中的评分机制。

3、NetworkMonitor

NetworkMonitor的构造是在注册NetworkAgent,构造NetworkAgentInfo是创建的,其实质ping网络是在updateNetworkInfo中,细节不分析,但是NetworkMonitor对网络可用性的评分是有影响的,即当网络链接上之后,会去ping当前网络是否可用,如果不可用则会影响getCurrentScore获取的分数值,getCurrentScore是每次网络评分获取的分数的必经之路:

private int getCurrentScore(boolean pretendValidated) {
    int score = currentScore;

    if (!everValidated && !pretendValidated) score -= UNVALIDATED_SCORE_PENALTY;
    if (score < 0) score = 0;

    if (networkMisc.explicitlySelected) score = EXPLICITLY_SELECTED_NETWORK_SCORE;

    return score;
}

当一个网络连接建立时,系统将用该连接Ping一个Google的网站来判断该连接是否真的可以上网,如果不可以,那么就会扣掉该网络40分,从而可能导致该网络的评分低于其他网络评分
如果是用户指定了网络那么分数直接等于EXPLICITLY_SELECTED_NETWORK_SCORE(100分)
至此网络评分就分析完毕

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值