注册 登录
  • 欢迎访问Sharezer Blog

Android版本ICE服务端配置

Android sharezer 1285次浏览 已收录 3个评论

配置环境

全局build.gradle

配置全局的gradle

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.3.0'
    }
}

subprojects {
    project.version = iceVersion
    project.group = "com.zeroc"

    apply plugin: 'idea'
    apply from: "$rootProject.projectDir/gradle/props.gradle"

    repositories {
        if (distJarDir != null) {
            flatDir {
                name "local"
                dirs distJarDir
            }
        }

        if (!useLocalOnly) {
            maven {
                url "https://${iceMavenRepo}/nexus/content/repositories/thirdparty"
            }
            maven {
                url "https://${iceMavenRepo}/nexus/content/repositories/releases"
            }
        }
    }
}
//
// Used to build the gradle wrapper to automatically download and install
// the version of gradle needed to build Ice.
//
task wrapper(type: Wrapper) {
    gradleVersion = "2.6"
}

grade.properites

增加一些属性值

// **********************************************************************
//
// Copyright (c) 2003-2017 ZeroC, Inc. All rights reserved.
//
// **********************************************************************

iceVersion = 3.6.3

// The Android versions used for the Ice build.
ice_buildToolsVersion = 21.1.1
ice_compileSdkVersion = 17
ice_minSdkVersion = 17
ice_targetSdkVersion = 17

//
// Set iceHome to location of Ice installation if Ice was installed in a
// non-standard location.
//
iceHome =

//
// Set the location of a local Ice Builder for Gradle source directory
//
iceBuilderHome =

//
// Set the version of the Ice Builder to use for building
//
iceBuilderVersion = 1.4.0

//
// Gradle build properties
//
org.gradle.daemon = true

拷贝props.gradle

拷贝props.gradle到gradle目录中

配置项目build.gradle

apply plugin: 'com.android.application'
apply from: "$rootProject.projectDir/../gradle/ice.gradle"


project.slice.output = project.file("${project.buildDir}/generated/source/ice")

slice {
    java {
        set1 {
            include = ["${projectDir}/src/main/slice"]
            srcDir = 'src/main'
        }
    }
}

android {
    compileSdkVersion 22
    buildToolsVersion '22.0.1'

    defaultConfig {
        applicationId "com.wangsu.iceserver"
        minSdkVersion ice_minSdkVersion.toInteger()
        targetSdkVersion ice_targetSdkVersion.toInteger()
        compileOptions {
            sourceCompatibility JavaVersion.VERSION_1_6
            targetCompatibility JavaVersion.VERSION_1_6
        }
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
    }
    // It would be ideal if the slice plugin did this, but it doesn't work as expected.
    sourceSets {
        main {
            java {
                srcDirs = ['src/main/java', project.slice.output]
            }
        }
    }
    buildToolsVersion '25.0.0'
}

task copyBksTask(type: Copy) {
//    from "${rootProject.projectDir}/../../certs/client.bks"
    from "${rootProject.projectDir}/../../certs/server.bks"
    into "src/main/res/raw"
}
preBuild.dependsOn(copyBksTask)

clean {
//    delete("src/main/res/raw/client.bks")
    delete("src/main/res/raw/server.bks")
//    delete rootProject.buildDir
}

dependencies {
//    compile fileTree(dir: 'libs', include: ['*.jar'])
//    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
//        exclude group: 'com.android.support', module: 'support-annotations'
//    })
//    compile 'com.android.support:appcompat-v7:25.3.0'
//    compile 'com.android.support.constraint:constraint-layout:1.0.2'
//    testCompile 'junit:junit:4.12'
    compile localDependency('ice')
    compile 'com.android.support:appcompat-v7:22.2.1'
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
}

修改AndroidManifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.wangsu.iceserver">

    <uses-sdk
        android:minSdkVersion="17"
        android:targetSdkVersion="17" />

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

    <application
        android:allowBackup="false"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name">
        <activity
            android:name=".HelloWorld">
            <!--<intent-filter>-->
                <!--<action android:name="android.intent.action.MAIN" />-->
                <!--<category android:name="android.intent.category.LAUNCHER" />-->
            <!--</intent-filter>-->
        </activity>
        <activity android:name=".MainActivity"
            android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

Ice开发

修改Activity

让继承AppCompatActivity,继承到其他Activity

编写Ice文件

在src/main中新建slice目录,在slice目录下编写ice文件
可参考ice-demos中的hello.ice

编译

gradle中有配置Ice Builder,所以编译时会生成对应的Java接口文件

实现

继承_HelloDisp,并重载接口,实现内容

创建服务端

new Thread(new Runnable() {
  public void run() {
      Communicator ic = null;
      try {
          InitializationData initData = new InitializationData();

          /*
          initData.dispatcher = new Dispatcher()
          {
              @Override
              public void
              dispatch(Runnable runnable, Connection connection)
              {
                  Log.d(TAG, "dispatch: dddddddd");
                  //_uiHandler.post(runnable);
              }
          };
          */

          initData.properties = Util.createProperties();
          initData.properties.setProperty("Ice.Warn.Connections", "1");
          initData.properties.setProperty("Ice.Trace.Network", "3");
          initData.properties.setProperty("IceSSL.Trace.Security", "3");
          initData.properties.setProperty("IceSSL.KeystoreType", "BKS");
          initData.properties.setProperty("IceSSL.TruststoreType", "BKS");
          initData.properties.setProperty("IceSSL.Password", "password");
          initData.properties.setProperty("Ice.InitPlugins", "0");
          initData.properties.setProperty("Ice.Plugin.IceSSL", "IceSSL.PluginFactory");
          initData.properties.setProperty("Ice.Admin.InstanceName", "server");
          initData.properties.setProperty("IceMX.Metrics.Debug.GroupBy", "id");
          initData.properties.setProperty("IceMX.Metrics.ByParent.GroupBy", "parent");

          initData.properties.setProperty("Ice.MessageSizeMax", "12288");
          initData.properties.setProperty("Ice.ThreadPool.Server.Size", "10");
          initData.properties.setProperty("Ice.Override.Timeout", "180000");
          initData.properties.setProperty("Ice.Override.ConnectTimeout", "180000");

          // SDK versions < 21 only support TLSv1 with SSLEngine.
          if (Build.VERSION.SDK_INT < 21) {
              initData.properties.setProperty("IceSSL.Protocols", "tls1_0");
          }

          ic = Util.initialize(initData);
          Plugin plugin = (Plugin) ic.getPluginManager().getPlugin("IceSSL");
          //
          // Be sure to pass the same input stream to the SSL plug-in for
          // both the keystore and the truststore. This makes startup a
          // little faster since the plugin will not initialize
          // two keystores.
          //
          InputStream certs = getResources().openRawResource(R.raw.server);
          plugin.setKeystoreStream(certs);
          plugin.setTruststoreStream(certs);
          ic.getPluginManager().initializePlugins();


          Log.i(TAG, "run: ice");
          // ic = Util.initialize();
          // 创建提供服务的站点,缺省协议为TCP/IP,端口为10000
          ObjectAdapter adapter = ic.createObjectAdapterWithEndpoints("helloAdapter", "default -p 10000");
          // 创建具体提供服务的实例
          HelloI object = new HelloI();
          // 将服务实例添加到提供服务的站点
          adapter.add(object, ic.stringToIdentity("hello"));
          adapter.activate();
          ic.waitForShutdown();
          Log.d(TAG, "run: ice end");
      } catch (LocalException e) {
          Log.e(TAG, "ice error 1: " + e.getMessage());
      }
      if (ic != null) {
          try {
              Log.d(TAG, "ICE Clean up");
              ic.destroy();
          } catch (Exception e) {
              Log.e(TAG, "ice error 2: " + e.getMessage());
          }
      }
  }
}).start();


Sharezer , 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明Android版本ICE服务端配置
喜欢 (0)
[liangshaoze@sina.cn]
分享 (0)
发表我的评论
取消评论
表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
(3)个小伙伴在吐槽
  1. I saѵor, lead to ӏ discoѵered just what I was looking for. You have ended my four day long hunt! Ꮐod Bless you man. Hаve a nice daʏ. Bye
    xxx2017-04-30 20:43 回复
    • sharezer
      :mrgreen: It's my pleasure!
      sharezer2017-05-07 20:56 回复
  2. 富强、民主、文明、和谐,自由、平等、公正、法治, 爱国、敬业、诚信、友善。
    QQ4941077192017-03-28 10:43 回复