初学ICE中间件总结笔记(一)
2014-07-31 12:54
573 查看
参考文章连接:http://blog.csdn.net/whuqin/article/details/7750107
http://blog.csdn.net/nokianasty/article/details/9701863
http://yangguo.iteye.com/blog/1096630
ICE介绍
是ZeroC公司开发的一款高效的开源中间件平台,全称是Internet Communications Engine。
它的主要设计目标是:
ICE支持多种编程语言:C++、Java、C#、VB、Python、Ruby,也就是说使用ICE时我们可以让这些语言无缝沟通,不过由于ICE是用C++编写的,不管用什么语言,你都需要先用C++编译出一个ICE才行(或者下载已编译的版本)。
Ice应用的结构如下:
Slice 语言
Slice( Specification Language for Ice )是一种用于使对象接口与其实现相分离的基础性抽象机制。Slice 在客户与服务器之间建立合约,描述应用所使用的各种类型及对象接口。这种描述与实现语言无关,所以编写客户所用的语言是否与编写服务器所用的语言相同,这没有什么关系。 Slice 定义由编译器编译到特定的实现语言 。编译器把与语言无关的定义翻译成针对特定语言的类型定义和 API 。开发者使用这些类型和 API 来提供应用功能,并与 Ice 交互。用于各种实现语言的翻译算法称为语言映射( language mappings )。
Slice与C++的映射关系(参考来自网络):
注:本篇文章以C++语言作为演示语言,其它语言除语法不同外,使用方法非常类似。
在Linux环境下配置ICE开发环境
1.从http://www.zeroc.com/download.html下载ICE ,我下载版本为Ice-3.5.1-el6-x86_64-rpm.tar.gz
2.解压后进行安装分别安装:
#rpm -ivh ice-3.5.1-1.rhel5.noarch.rpm
#rpm -ivh db53-5.3.21-1ice.rhel5
#rpm -ivh ice-libs-3.5.1-1.rhel5
#rpm -ivh ice-utils-3.5.1-1.rhel5
#rpm -ivh ice-servers-3.5.1-1.rhel5
3.根据需要安装宿主语言支持,本文为C++
安装
db53-c++、ice-c++、ice-c++-devl rpm安装包
ICE
的HelloWorld
下面是一个简单的接口的Slice定义:
它定义一个Printer接口(interface),这个接口只有一个printString方法,输入参数是一个字符串(string)。最后,这个接口位于Demo模块(module)之下。
把它保存为Printer.ice后接着我们使用slice2cpp程序依据这个Slice定义生成C++使用的头文件和对应的代理代码:
#include <ice/ice.h>
#include <printer.h>
using namespace std;
using namespace Demo;
//实现printString方法
struct PrinterImp:Printer{
virtual void printString(const ::std::string &s,
const ::Ice::Current & = ::Ice::Current()){
cout << s << endl;
}
};
int
main(int argc, char* argv[])
{
Ice::CommunicatorPtr ic;
try{
// 初始化Ice运行库
ic = Ice::initialize(argc, argv);
// 建立ObjectAdapter(对象适配器),命名为SimplePrinterAdapter
// 绑定端口:使用默认协议(一般是tcp)并在10000端口监听。
Ice::ObjectAdapterPtr adapter = ic->createObjectAdapterWithEndpoints(
"SimplePrinterAdapter", "default -p 10000");
// 把Printer加入ObjectAdapter,并命名为SimplePrinter
Ice::ObjectPtr object = new PrinterImp;
adapter->add(object, ic->stringToIdentity("SimplePrinter"));
//启动适配器
adapter->activate();
//等待直到Communicator关闭
ic->waitForShutdown();
}
catch(const Ice::Exception &e){
cerr << e << endl;
}
catch(const char* msg){
cerr << msg << endl;
}
// 回收Ice运行库所用的资源
if(ic) ic->destroy();
return 0;
}
#include <ice/ice.h>
#include <printer.h>
using namespace std;
using namespace Demo;
int
main(int argc, char* argv[])
{
Ice::CommunicatorPtr ic;
try{
// 初始化Ice运行库
ic = Ice::initialize(argc, argv);
//获得Ice对象代理,SimplePrinter-对象标识符,default -p 10000-协议与端口
Ice::ObjectPrx base = ic->stringToProxy("SimplePrinter:default -p 10000");
// 把对象转换成Printer代理
PrinterPrx printer = PrinterPrx::checkedCast(base);
if(!printer) throw "Invalid Proxy!";
// 调用printString方法操作
printer->printString("Hello World!");
}
catch(const Ice::Exception &e){
cerr << e << endl;
}
catch(const char* msg){
cerr << msg << endl;
}
// 回收Ice运行库所用的资源
if(ic) ic->destroy();
return 0;
}
编译服务器端和客户端:
然后启动一个服务器端,每次调用客户端后服务器端会显示一行Hello
world!
你也可以把服务器端放到别的电脑上,客户端代码改成:Ice::ObjectPrx base = ic->stringToProxy("SimplePrinter:default -h 服务端IP -p 10000");即可实现远程调用。
http://blog.csdn.net/nokianasty/article/details/9701863
http://yangguo.iteye.com/blog/1096630
ICE介绍
是ZeroC公司开发的一款高效的开源中间件平台,全称是Internet Communications Engine。
它的主要设计目标是:
● 提供适用于异种环境的面向对象中间件平台。
● 提供一组完整的特性,支持广泛的领域中的实际的分布式应用的开发。
● 避免不必要的复杂性,使平台更易于学习和使用。
● 提供一种在网络带宽、内存使用和 CPU 开销方面都很高效的实现。
● 提供一种具有内建安全性的实现,使它适用于不安全的公共网络。
ICE支持多种编程语言:C++、Java、C#、VB、Python、Ruby,也就是说使用ICE时我们可以让这些语言无缝沟通,不过由于ICE是用C++编写的,不管用什么语言,你都需要先用C++编译出一个ICE才行(或者下载已编译的版本)。
Ice应用的结构如下:
Slice 语言
Slice( Specification Language for Ice )是一种用于使对象接口与其实现相分离的基础性抽象机制。Slice 在客户与服务器之间建立合约,描述应用所使用的各种类型及对象接口。这种描述与实现语言无关,所以编写客户所用的语言是否与编写服务器所用的语言相同,这没有什么关系。 Slice 定义由编译器编译到特定的实现语言 。编译器把与语言无关的定义翻译成针对特定语言的类型定义和 API 。开发者使用这些类型和 API 来提供应用功能,并与 Ice 交互。用于各种实现语言的翻译算法称为语言映射( language mappings )。
Slice与C++的映射关系(参考来自网络):
Slice | C++ |
---|---|
#include | #include |
#ifndef | #ifndef |
#define | #define |
#endif | #endif |
module | namespace |
bool | bool |
byte | Ice::Byte |
short | Ice::Short |
int | Ice::Int |
long | Ice::Long |
float | Ice::Float |
double | Ice::Double |
string | Ice::string |
enum | enum(不支持指定数字) |
struct | struct |
class | class(所有方法都是纯虚函数) |
interface | struct(所有方法都是纯虚函数,没有成员变量) |
sequence<T> | std::vector<T> |
dictionary<Key,Value> | std::map<Key,Value> |
exception Err | class Err:public Ice:UserException |
nonmutating方法限定符 | const方法 |
idempotent方法限定符 | - |
out 参数限定符 | 引用类型 |
* | 对应类型的代理类 |
在Linux环境下配置ICE开发环境
1.从http://www.zeroc.com/download.html下载ICE ,我下载版本为Ice-3.5.1-el6-x86_64-rpm.tar.gz
2.解压后进行安装分别安装:
#rpm -ivh ice-3.5.1-1.rhel5.noarch.rpm
#rpm -ivh db53-5.3.21-1ice.rhel5
#rpm -ivh ice-libs-3.5.1-1.rhel5
#rpm -ivh ice-utils-3.5.1-1.rhel5
#rpm -ivh ice-servers-3.5.1-1.rhel5
3.根据需要安装宿主语言支持,本文为C++
安装
db53-c++、ice-c++、ice-c++-devl rpm安装包
ICE
的HelloWorld
下面是一个简单的接口的Slice定义:
module Demo { interface Printer { void printString(string s); }; };
它定义一个Printer接口(interface),这个接口只有一个printString方法,输入参数是一个字符串(string)。最后,这个接口位于Demo模块(module)之下。
把它保存为Printer.ice后接着我们使用slice2cpp程序依据这个Slice定义生成C++使用的头文件和对应的代理代码:
slice2cpp Printer.ice生成Printer.h和Printer.cpp,把这两个文件加入到服务器端项目和客户端项目后就可以互相对话。
编写服务器端代码:
#include <ice/ice.h>#include <printer.h>
using namespace std;
using namespace Demo;
//实现printString方法
struct PrinterImp:Printer{
virtual void printString(const ::std::string &s,
const ::Ice::Current & = ::Ice::Current()){
cout << s << endl;
}
};
int
main(int argc, char* argv[])
{
Ice::CommunicatorPtr ic;
try{
// 初始化Ice运行库
ic = Ice::initialize(argc, argv);
// 建立ObjectAdapter(对象适配器),命名为SimplePrinterAdapter
// 绑定端口:使用默认协议(一般是tcp)并在10000端口监听。
Ice::ObjectAdapterPtr adapter = ic->createObjectAdapterWithEndpoints(
"SimplePrinterAdapter", "default -p 10000");
// 把Printer加入ObjectAdapter,并命名为SimplePrinter
Ice::ObjectPtr object = new PrinterImp;
adapter->add(object, ic->stringToIdentity("SimplePrinter"));
//启动适配器
adapter->activate();
//等待直到Communicator关闭
ic->waitForShutdown();
}
catch(const Ice::Exception &e){
cerr << e << endl;
}
catch(const char* msg){
cerr << msg << endl;
}
// 回收Ice运行库所用的资源
if(ic) ic->destroy();
return 0;
}
客户端代码:
#include <ice/ice.h>#include <printer.h>
using namespace std;
using namespace Demo;
int
main(int argc, char* argv[])
{
Ice::CommunicatorPtr ic;
try{
// 初始化Ice运行库
ic = Ice::initialize(argc, argv);
//获得Ice对象代理,SimplePrinter-对象标识符,default -p 10000-协议与端口
Ice::ObjectPrx base = ic->stringToProxy("SimplePrinter:default -p 10000");
// 把对象转换成Printer代理
PrinterPrx printer = PrinterPrx::checkedCast(base);
if(!printer) throw "Invalid Proxy!";
// 调用printString方法操作
printer->printString("Hello World!");
}
catch(const Ice::Exception &e){
cerr << e << endl;
}
catch(const char* msg){
cerr << msg << endl;
}
// 回收Ice运行库所用的资源
if(ic) ic->destroy();
return 0;
}
编译服务器端和客户端:
//服务端 c++ -I. -I$ICE_HOME/include -c Printer.cpp Server.cpp c++ -o server Printer.o Server.o -L$ICE_HOME/lib -lIce -lIceUtil //客户端 c++ -I. -I$ICE_HOME/include -c Printer.cpp Client.cpp c++ -o client Printer.o Client.o -L$ICE_HOME/lib -lIce -lIceUtil
然后启动一个服务器端,每次调用客户端后服务器端会显示一行Hello
world!
你也可以把服务器端放到别的电脑上,客户端代码改成:Ice::ObjectPrx base = ic->stringToProxy("SimplePrinter:default -h 服务端IP -p 10000");即可实现远程调用。
相关文章推荐
- 初学ICE中间件总结笔记(一)续
- ICE中间件笔记
- 关于java初学的一些笔记总结(收集+总结)与大家分享,关于自学java,有好的建议可以回帖。
- 学习中间件ICE笔记(初学者必读)
- 关于java初学的一些笔记总结(收集+总结)与大家分享,关于自学java,有好的建议可以回帖。
- RxJava2.0初学总结笔记
- ICE中间件学习笔记 基础理论篇
- 初学SQL Server笔记之修改字段属性总结
- 初学链表第一天总结笔记
- ICE中间件研究笔记
- ICE中间件研究笔记
- qmail总结笔记(2)
- 初学笔记B:TOMCAT下目录WEBAPPS的初识,和加载SERVLET和JSP的方法
- 两年前初学.NET时写的关于DataGrid的笔记
- 网站重构笔记总结
- 初学Python笔记(一)
- Java初学笔记:安装和设置JDK
- qmail总结笔记(1)
- Gentle.Net学习笔记三:常用更新数据的方法总结
- Java初学笔记