0

我在我的代码中使用 ICE。我想运行需要 GameObserverPrx 作为参数的函数。我不想按值传递 GameObserver,我在切片接口中使用 GameObserver* 来传递代理。

我应该使用什么函数将 GameObserver 转换为 GameObserverPrx?第二个问题 - 为什么 ICE 不能代替我做呢?我在网上搜索答案。我只找到了 ObjectAdapter.checkedCast 但它用于另一个目的。

这是错误:

GameProxyImpl 类型中的方法 addObserver(String, GameObserverPrx, Current) 不适用于参数 (String, GameObserverImpl, null) PrzeciwnikKomputerowy.java /warcaby-serwer/src/main/java/sr/warcaby/serwer 第 74 行 Java 问题

以下是我的代码片段: 在这一行中,我看到了一个错误。

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

GameObserver 实现的片段:

class GameObserverImpl extends _GameObserverDisp { //extends IGameObserverPOA{

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

api.ice 的片段:

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。

4

1 回答 1

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");
于 2015-05-07T18:49:54.517 回答