2015-05-07 58 views
0

我在我的代碼中使用ICE。我想運行需要GameObserverPrx作爲參數的函數。我不想按價值傳遞GameObserver,並且我在切片接口中使用GameObserver *來傳遞代理。ICE - 如何將實現轉換爲代理?

我應該使用什麼函數將GameObserver投射到GameObserverPrx? 第二個問題 - 爲什麼ICE不會代替我做? 我在網上搜索答案。我只發現ObjectAdapter.checkedCast,但它用於另一個目的。

這裏是錯誤:

The method addObserver(String, GameObserverPrx, Current) in the type GameProxyImpl is not applicable for the arguments (String, GameObserverImpl, null) PrzeciwnikKomputerowy.java /warcaby-serwer/src/main/java/sr/warcaby/serwer line 74 Java Problem

下面是我的代碼片段: 在此行中我看到一個錯誤。 GameObserver實施

partia.addObserver(token, new GameObserverImpl(this)), null); 

片段:api.ice的

class GameObserverImpl extends _GameObserverDisp { //extends IGameObserverPOA{ 

    private static final long serialVersionUID = 1L; 
    PrzeciwnikKomputerowy p; 
    public GameObserverImpl(PrzeciwnikKomputerowy p) { 
     this.p = p; 
    } 

片段:

interface GameObserver { 
    void notifyObserver( CORBAMove lastMove); 
}; 



interface GameProxy { 
    void addObserver( string token, GameObserver* o) throws MyException; 
    bool isMyTurn( string token) throws MyException; 
    void doMove( string token, CORBAMove move) throws MyException; 
    Position getPosition( string token) throws MyException; 
    string showPosition( string token) throws MyException; 
}; 

不要感到困惑的名字CORBAMove。我使用CORBA,但是我將代碼更改爲ICE。

回答

0

我找到了我的問題的答案。 現在我的應用程序按預期工作。

我寫了從Ice.Object創建ObjectPrx的方法。 此方法使用反射來查找指定類的轉換方法。

在這個網站上,我發現我所需要的功能:https://doc.zeroc.com/display/Ice/Object+Incarnation+in+Java#ObjectIncarnationinJava-proxies

最重要的路線是:ObjectPrx objectPrx = adapter.addWithUUID(iceObject)。

然後我使用方法xxxPrxHelper.checkedCast(objectPrx),我從反射中獲得。 這裏改變代碼:

partia.addObserver(token, (GameObserverPrx) 
    serwer.createProxyForObject(observer, GameObserverPrxHelper.class), null) 

類ServerImpl方法:

public ObjectPrx createProxyForObject(Ice.Object iceObject, Class<?> clazz) { 
    ObjectPrx objectPrx = adapter.addWithUUID(iceObject); 
    try { 
     Method method = clazz.getMethod("checkedCast", ObjectPrx.class); 
     objectPrx = (ObjectPrx) method.invoke(null, objectPrx);//adapter.createIndirectProxy(id)); 
    } catch (Exception e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

    return objectPrx; 

方法createProxyForObject使用在服務器構造初始化正常的適配器(因爲PrzeciwnikKomputerowy類仍然是服務器程序的一部分)。

Ice.Communicator communicator = Ice.Util.initialize(args); 

    ObjectAdapter adapter = communicator.createObjectAdapter("ChessServer"); 
相關問題