ContextHub: ContextHubClientBroker

ContextHubClientBroker的功能 

class ContextHubClientBroker extends IContextHubClient.Stub 实现IContextHubClient的server端:
SendMessageToNanoApp, 还处理 IContextHubClientCallback
* A class that acts as a broker for the ContextHubClient, which handles messaging and life-cycle
 * notification callbacks. This class implements the IContextHubClient object, and the implemented
 * APIs must be thread-safe.

构造函数ContextHubClientBroker

       ContextHubClientBroker(//构造需要这么多已知对象,具体看上图
            Context context, IContexthub contextHubProxy, ContextHubClientManager clientManager,
            int contextHubId, short hostEndPointId, IContextHubClientCallback callback) {
        mContext = context;
        mContextHubProxy = contextHubProxy;
        mClientManager = clientManager;
        mAttachedContextHubId = contextHubId;
        mHostEndPointId = hostEndPointId;
        mCallbackInterface = callback;
    }

sendMessageToNanoApp(NanoAppMessage message)

    /**
     * Sends from this client to a nanoapp.
     *
     * @param message the message to send
     * @return the error code of sending the message
     */
    @ContextHubTransaction.Result
    @Override
    public int sendMessageToNanoApp(NanoAppMessage message) {

        int result;
            ContextHubMsg messageToNanoApp = ContextHubServiceUtil.createHidlContextHubMessage(
                    mHostEndPointId, message);//数据类型转换

            try {//通过IContextHub代理调用 hal接口函数
                result = mContextHubProxy.sendMessageToHub(mAttachedContextHubId, messageToNanoApp);
            } 

        return ContextHubServiceUtil.toTransactionResult(result);
    }

IContextHubClientCallbacks的封装实现

通过构造函数 mCallbackInterface被赋值,实际不是直接使用client 传入的mCallbackInterface.

sendMessageToClient

    /**
     * Sends a message to the client associated with this object.
     *
     * @param message the message that came from a nanoapp
     */
    /* package */ void sendMessageToClient(NanoAppMessage message) {
        if (mConnectionOpen.get()) {
            try {
                mCallbackInterface.onMessageFromNanoApp(message);
            } catch (RemoteException e) {
                Log.e(TAG, "RemoteException while sending message to client (host endpoint ID = "
                        + mHostEndPointId + ")", e);
            }
        }
    }

onNanoAppLoaded

    /**
     * Notifies the client of a nanoapp load event if the connection is open.
     *
     * @param nanoAppId the ID of the nanoapp that was loaded.
     */
    /* package */ void onNanoAppLoaded(long nanoAppId) {
        if (mConnectionOpen.get()) {
            try {
                mCallbackInterface.onNanoAppLoaded(nanoAppId);
            } catch (RemoteException e) {
                Log.e(TAG, "RemoteException while calling onNanoAppLoaded on client"
                        + " (host endpoint ID = " + mHostEndPointId + ")", e);
            }
        }
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值