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);
}
}
}