Ubuntu下搭建|Ubuntu下搭建 ice 开发环境

Ubuntu上安装Ice 3.3很方便 ,软件源里有,直接apt-get就可以
要安装Ice 3.4需要自己下载源码,编译安装,安装之前要安装Ice所依赖的软件包

apt-get install libdb4.6++-dev libbz2-dev bzip2 libssl-dev libreadline5-dev libexpat1-dev mcppI

然后:下载Ice-3.4.1.tar.gz
下载后安装解压,进入cpp目录,运行make ,make install 进行安装,默认安装路径在/opt/Ice-3.4.1
安装完后添加环境变量:

vi~/.bashrc

添加如下内容:

export ICE_HOME=/opt/Ice-3.4.1export PATH=$ICE_HOME/bin:$PATHexport LD_LIBRARY_PATH=$ICE_HOME/lib:$LD_LIBRARY_PATH

然后你就可以使用如下命令去生成代码了:

slice2cppslice2freezeslice2htmlslice2phpslice2rbslice2csslice2freezejslice2javaslice2py

如果我们使用java开发ice接口需要Ice.jar,进入java目录,运行ant build.xml ,会在lib目录下生成Ice.jar
如果我们使用python开发ice接口需要安装ice-python 使用命令:sudo apt-get install ice-python 安装
下面来做一个小例子:
slice文件:

module com{ module demo{ module mingfei{ module core{ module generated{ struct Person{ string name; int age; }; sequence personList; dictionary personMap; interface HelloMingFei{ string hello(string name); personList getPersonList(); personMap getPersonMap(); }; }; }; }; }; };

我们使用python开发server端:
server.py:

import sys,traceback,Ice,IcePy Ice.loadSlice("Test.ice")import com.demo.mingfei.core.generatedclassHelloMingFeiI(com.demo.mingfei.core.generated.HelloMingFei ): def hello(slef ,s,current=None): return "hello %s" % s def getPersonList(self,current=None): p1 = com.demo.mingfei.core.generated.Person("mingfei",1) p2 = com.demo.mingfei.core.generated.Person("mingtao",2) personList = [p1,p2] return personList def getPersonMap(self,current=None): p1 = com.demo.mingfei.core.generated.Person("mingfei",1) personDict = {"mingfei":p1} p2 = com.demo.mingfei.core.generated.Person("mingtao",2) personDict["mingtao"] = p2 return personDictstatus = 0 ice = None try: ic = Ice.initialize(sys.argv) adapter = ic.createObjectAdapterWithEndpoints("HelloMingFeiAdapter","default -p 10000") objectI = HelloMingFeiI() adapter.add(objectI,ic.stringToIdentity("HelloMingFei")) adapter.activate() ic.waitForShutdown() except: traceback.print_exc() status = 1if ic : #Clean up try: ic.destroy() except: traceback.print_exc() status = 1sys.exit(status)

使用java来开发client:
首先使用slice2java Test.ice 生成java代码
【Ubuntu下搭建|Ubuntu下搭建 ice 开发环境】 Client.java:
package com.demo.mingfei.core.generated; public class Client { public static void main(String [] args){ int status = 0; Ice.Communicator ic = null; try { ic = Ice.Util.initialize(args); Ice.ObjectPrx base = ic.stringToProxy("HelloMingFei:default -p 10000"); HelloMingFeiPrx printer = HelloMingFeiPrxHelper.checkedCast(base) ; if (printer == null) { System.out.println("invalid proxy"); } String out = printer.hello("mingfei"); System.out.println(out); } catch (Ice.LocalException e) { e.printStackTrace(); status = 1; } catch (Exception e) { System.err.print(e.getMessage()); status = 1; } finally { if (ic != null) { ic.destroy(); } } System.exit(status); }}

再讲一讲使用java开发server:

首先使用slice2java Test.ice 生成java代码

package com.demo.mingfei.core.generated; import java.util.HashMap; import java.util.Map; import Ice.Current; public class Server extends _HelloMingFeiDisp{ @Override public Person[] getPersonList(Current current) { Person p1 = new Person(); p1.name = "mingtao"; p1.age = 2; return new Person[]{p1}; } @Override public Map getPersonMap(Current current) { Person p1 = new Person(); p1.name = "mingtao"; p1.age = 2; Person p2 = new Person(); p2.name = "mingfei"; p2.age = 2; Map map = new HashMap(); map.put("mingtao", p1); map.put("mingfei", p2); return map; } @Override public String hello(String name, Current current) { // TODO Auto-generated method stub return "hello " + name; } public static void main(String [] args){ int status = 0; Ice.Communicator ic = null; try { ic = Ice.Util.initialize(args); // 初始化参数 Ice.ObjectAdapter adapter = ic.createObjectAdapterWithEndpoints( "HelloMingFeiAdapter", "default -p 10000"); // 创造对象适配器 Ice.Object objcet = new Server(); // 创建servant对象 adapter.add(objcet, Ice.Util.stringToIdentity("HelloMingFei")); // 适配器添加一个新的servant,给servant对象取个名字 adapter.activate(); System.out.println("connection success"); ic.waitForShutdown(); } catch (Ice.LocalException e) { e.printStackTrace(); status = 1; } catch (Exception e) { System.err.print(e.getMessage()); status = 1; } finally { if (ic != null) { ic.destroy(); } } System.exit(status); } }

使用python开发client: client.py:
import sys,Ice,traceback Ice.loadSlice("Test.ice") import com.demo.mingfei.core.generatedstatus = 0 ice = None try: ic = Ice.initialize(sys.argv) base = ic.stringToProxy("HelloMingFei:default -p 10000") serviceI = com.demo.mingfei.core.generated.HelloMingFeiPrx.checkedCast(base) if not serviceI: raise RuntimeError("Invalid proxy")printserviceI.hello("mingfei") print "\r\n" personList =serviceI.getPersonList() personListSize = len(personList) for i in range(len(personList)): print "my name is %s ,and %d years old" %(personList[i].name , personList[i].age) print "\r\n" personDict = serviceI.getPersonMap() print "my name is %s ,and %d years old"%( personDict["mingfei"].name, personDict["mingfei"].age) print "my name is %s ,and %d years old"% (personDict["mingtao"].name, personDict["mingtao"].age)except: traceback.print_exc() status = 1if ic: # Clean up try: ic.destroy() except: traceback.print_exc() status = 1sys.exit(status)


    推荐阅读