ubuntu 在 R40e 上 還有 Debian 在 Sempron 2600 上

2013年1月2日 星期三

IWifiManager.aidl , WifiService & WifiManager

把 IWifiManager.aidl 產生的 IWifiManager.java 重新 indent 打開來看。
再配合 .aidl 的 server , client class

aidl 的 java source 內含兩個 class 和一個 interface:
  • class : Stub
  • class : Proxy
  • interface : IWifiManager


就是 .. 一個 interface,兩個 class 各自解讀..

Stub 是給提供 Service 的 class 用的,繼承這個。然後實做 interface 介面

Proxy 是給 Client 的 class 用的,設法取得這個class,然後就可以 call interface 介面的function




Service 的 class 比較容易懂,就直接繼承 IWifiManager.Stub 就可以。
然後實做出每一個 interface 的 function。

Client 比較麻煩..因為 Proxy 是 Stub 的 private class,所以要用類似 factory patten 的方式,用
 IWifiManager.stub.asInterface( ) 

拿到 Proxy class (然後 cast 成 Interface 物件)




Proxy 是 Stub 的 inner class,因為這樣他才可以存取 Stub 的 private 變數 "DESCRIPTOR"
-- 至於 Binder Transation 用的 string constant TRANSATION_xxx 都不是 private..

Proxy 是 static class,這樣使用者 (client) 就不用一定要生成 Stub 物件 才能取得 Proxy 。

android 好像實做一個 singleton 在 Stub. asIterface( ) (類似 getProxy()),然後把 Proxy 設為 private class。




Stub, Proxy class 的 code 完全由 aidl compiler 產生,因為這是固定的行為:
Proxy function:
  • 包裝 argument
  • tranact
  • read exception , reply


Stub 的 code, interface 部份維持 abstract,讓 Server inplement。
code gen 只寫出 Binder 需要的部份.. onTranact( )
onTranact( ) 就是依照 參數決定 call 哪一個 interface function,並且 return result。




Design Patters 有 Proxy 這一項,其中的範例大多....
沒有 Android 這個範例來得好,

Proxy 建立 Server 的分身,把動作改為 packet,經過包裝後送給 真正執行的 實體 (Server)。
這樣比較有...非用 Proxy 這個 pattern 不可.. 的感覺。

沒有留言:

標籤

網誌存檔