当前位置: 首页 > 工具软件 > Zeroc ICE > 使用案例 >

zero ICE androidstdio 上面的集成操作

花欣然
2023-12-01
apply plugin: 'slice'
slice {
    java {
//        srcDir = '.'
        output = file('src/main/java') //这里最好是写,不然到默认的输出目录,就是build下面去了,src下面引用不到
    }
}

buildscript {
    repositories {
        maven {
            url 'https://repo.zeroc.com/nexus/content/repositories/releases'
        }
    }
    dependencies {
        classpath group: 'com.zeroc.gradle.ice-builder', name: 'slice', version: '1.3.14'
    }
}

//    compile 'com.zeroc:ice:3.6.3'
    compile files('libs/Ice.jar')   //这里需要额外导包,不然代码里面报错,可能是由于我版本不对应造成的,3.6.3,3.5.1的区别

下面直接贴出一个工具类VerifyClient.java

package com.xxx.xxx.verify.client;

import com.xxx.xxx.utils.LogUtil;
import com.xxx.xxx.verify.KeeperServerPrx;
import com.xxx.xxx.verify.KeeperServerPrxHelper;
import com.xxx.xxx.verify.MessageReceiverPrx;
import com.xxx.xxx.verify.MessageReceiverPrxHelper;
import com.xxx.xxx.verify._MessageReceiverDisp;
import Ice.Current;

/**
 * Created by ChineseBoy on 2017/7/5.
 */

public class VerifyClient {

    public static final String TAG = "VerifyClient";

    private Ice.Communicator ic = null;
    private KeeperServerPrx keeperServerPrx = null;
    private MessageReceiverPrx messageReceiverPrx = null;
    private Ice.ObjectAdapter adapter = null;

    private MessageCallback mMessageCallback;
    public interface MessageCallback{
        void onMessage(String message, byte[] imageBuff);
    }

    public void setMessageCallback(MessageCallback messageCallback){
        this.mMessageCallback = messageCallback;
    }

    public boolean init(String ip, int port){
        try {
            ic = Ice.Util.initialize();
            Ice.ObjectPrx base = ic.stringToProxy("verify.KeeperServer:tcp -h "+ip+" -p " + port);
            keeperServerPrx = KeeperServerPrxHelper.checkedCast(base);
            if (keeperServerPrx == null) {
                return false;
            }

            adapter = ic.createObjectAdapterWithEndpoints("verify.MessageReceiver", "default");
            Ice.Object object = new _MessageReceiverDisp() {
                @Override
                public void onMessage(String message, byte[] imageBuff, Current __current) {
                    if(mMessageCallback!=null){
                        mMessageCallback.onMessage(message,imageBuff);
                    }
                }
            };
            adapter.add(object, Ice.Util.stringToIdentity("messageReceiver"));
            adapter.activate();
            messageReceiverPrx = MessageReceiverPrxHelper.uncheckedCast(adapter.createProxy(Ice.Util.stringToIdentity("messageReceiver")));
            if(messageReceiverPrx == null){
                return false;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    public boolean login(String deviceID){
        return keeperServerPrx.login(deviceID, messageReceiverPrx);
    }

    public void loginOut(String deviceID){
        keeperServerPrx.logout(deviceID);
    }

    public void send(String deviceId, String message, byte[] imgBuff){
        keeperServerPrx.sendMessage(deviceId, message, imgBuff);
    }

    public void destory(){
        if(adapter !=null && adapter.isDeactivated()){
            adapter.deactivate();
//            adapter.destroy();
        }
        if (ic != null && !ic.isShutdown()) {
            ic.shutdown();
//            ic.destroy();
            LogUtil.d(TAG,"shutdown");
        }
    }
}

在src/main/ 下面新建slice文件夹,然后放入keeper_verify.ice 文件

[["java:package:com.XXX.XXX"]]
module verify 
{
    
    sequence<byte> ByteSeq;

    interface MessageReceiver 
    {
        void onMessage(string message, ByteSeq imageBuff);
    };

    dictionary<string, MessageReceiver*> DeviceList;
    
    interface KeeperServer 
    {
        bool login(string deviceID, MessageReceiver* receiver);
        void logout(string deviceID);
        void sendMessage(string deviceID, string message, ByteSeq imageBuff);
    };

};

福利区域:

Ice-3.4.0开发手册中文版https://download.csdn.net/download/q979713444/11330082

 类似资料: