Zeroc ICE 源码分析一 HelloWorldICE
2014-03-05 15:03
260 查看
因为公司需要让我了解一下ICE,所以写下来我的一些了解情况供分享。因为接触不久所以写的比较浅显,请多包涵。
ICE is a modern object-oriented toolkit that enables you to build distributed applications with minimal effort:
•Provide an object-oriented middleware platform suitable for use in different environments.
•Provide a full set of features that support development of realistic distributed applications for a wide variety of domains.
•Provide an implementation that has built-in security, making it suitable for use over insecure public networks.
Ice-3.5.1.msi
编译好的Redistribution. 包含了运行时依赖了一些库,头文件,以及一些工具等。
Ice-3.5.1-VS2013.msi
ICE为VS2013开发的插件,用于简化开发期间的ICE 编译操作等。
Ice-3.5.1-ThirdParty.msi
ICE所依赖的一些第三方库,如OpenSSL 等。在编译ICE的源码时需要用到。
Ice-3.5.1.zip
ICE的源码包。
Ice-3.5.1-VS2013.zip
ICE对VS2013发布的Patch。
Ice-3.5.1-VS2013-demos.zip
ICE的官方示例程序。
只需要安装前三个安装包,准备工作就完成了。
1. 添加Printer.ice 文件:
这样Ice就会自动将Printer.ice 编译成Printer.h 和Printer.cpp两个文件。VS add-in调用了slice2cpp.exe完成代码的自动生成。
3. 添加Server端printString() 的实现:
1. 解压ICE的源码包和VS2013 Patch包到同一目录
2. 打开Developer Command Prompt for VS2013,cd到ICE源码包的cpp目录
3. nmake /f Makefile.mak,等待数分钟后编译应该就完成了。与cpp目录同级的bin目录就是ICE的输出目录。
1. Ice Manual 全面介绍了ICE 和Slice语言以及相关工具。
2. Technical Articles 中介绍了一些技术实现的细节,如ICE连接的建立等等。
3. ICE 的官方论坛 http://www.zeroc.com/forums/index.php 也是一个不错的好去处。
ICE是什么
从ICE的官网 http://www.zeroc.com/index.html 可以看到对ICE的介绍:ICE is a modern object-oriented toolkit that enables you to build distributed applications with minimal effort:
•Provide an object-oriented middleware platform suitable for use in different environments.
•Provide a full set of features that support development of realistic distributed applications for a wide variety of domains.
•Provide an implementation that has built-in security, making it suitable for use over insecure public networks.
配置VS2013开发环境
从ICE官网的下载页面 http://www.zeroc.com/download.html#win32_msi 下载所需要的文件如下:Ice-3.5.1.msi
编译好的Redistribution. 包含了运行时依赖了一些库,头文件,以及一些工具等。
Ice-3.5.1-VS2013.msi
ICE为VS2013开发的插件,用于简化开发期间的ICE 编译操作等。
Ice-3.5.1-ThirdParty.msi
ICE所依赖的一些第三方库,如OpenSSL 等。在编译ICE的源码时需要用到。
Ice-3.5.1.zip
ICE的源码包。
Ice-3.5.1-VS2013.zip
ICE对VS2013发布的Patch。
Ice-3.5.1-VS2013-demos.zip
ICE的官方示例程序。
只需要安装前三个安装包,准备工作就完成了。
HelloWorld
在VS2013中新建一个空的Server和Client端工程后:1. 添加Printer.ice 文件:
module Demo { interface Printer { void printString(string s); }; };2. VS2013 ICE add-in 安装后,可在VS TOOLS -> Ice Configuration 中勾选Enable Ice Builder。
这样Ice就会自动将Printer.ice 编译成Printer.h 和Printer.cpp两个文件。VS add-in调用了slice2cpp.exe完成代码的自动生成。
3. 添加Server端printString() 的实现:
#include <iostream> #include <Ice/Ice.h> #include <Printer.h> using namespace std; using namespace Demo; class PrinterI : public Printer // the servant { public: virtual void printString(const string &s, const Ice::Current ¤t); }; void PrinterI::printString(const string &s, const Ice::Current ¤t) { cout << "In function " << __FUNCTION__ << " with string: " << s << endl; } int main(int arc, char **argv) { int status = 0; Ice::CommunicatorPtr ic; try { ic = Ice::initialize(arc, argv); Ice::ObjectAdapterPtr adapter = ic->createObjectAdapterWithEndpoints("SimplePrinterAdapter", "default -p 10000:default -p 10001"); Ice::ObjectPtr object = new PrinterI(); adapter->add(object, ic->stringToIdentity("SimplePrinter")); adapter->activate(); ic->waitForShutdown(); } catch (const Ice::Exception &e) { cerr << e << endl; status = 1; } catch (const char *msg) { cerr << msg << endl; status = 1; } if (NULL != ic) { try { ic->destroy(); } catch (const Ice::Exception &e) { cerr << e << endl; status = 1; } } return status; }4. 添加Client 端调用 printString()的代码:
#include <iostream> #include <Ice/Ice.h> #include <Printer.h> using namespace std; using namespace Demo; int main(int argc, char **argv) { int status = 0; Ice::CommunicatorPtr ic; try { ic = Ice::initialize(argc, argv); Ice::ObjectPrx base = ic->stringToProxy("SimplePrinter:default -p 10000:default -p 10001"); PrinterPrx printer = PrinterPrx::checkedCast(base); // PrinterPrx printer = PrinterPrx::uncheckedCast(base->ice_connectionCached(false)); if (!printer) throw "Invalid proxy"; printer->printString("Hello, World!"); } catch (const Ice::Exception &e) { cerr << e << endl; status = 1; } catch (const char *msg) { cerr << msg << endl; status = 1; } if (NULL != ic) ic->destroy(); return status; }5. 编译运行,运气好的话调用客户端的代码后在服务器端就可以看到 Hello, World了。
ICE 源码编译
如果分析ICE源码的话就需要下载源码做分析。在安装好ThirdParty并指定PATH环境变量后:1. 解压ICE的源码包和VS2013 Patch包到同一目录
2. 打开Developer Command Prompt for VS2013,cd到ICE源码包的cpp目录
3. nmake /f Makefile.mak,等待数分钟后编译应该就完成了。与cpp目录同级的bin目录就是ICE的输出目录。
ICE的相关资料
据我所知ICE的中文资料不多,官方文档 http://doc.zeroc.com/display/Doc/Home 中:1. Ice Manual 全面介绍了ICE 和Slice语言以及相关工具。
2. Technical Articles 中介绍了一些技术实现的细节,如ICE连接的建立等等。
3. ICE 的官方论坛 http://www.zeroc.com/forums/index.php 也是一个不错的好去处。
ICE官方示例程序
上面提到的Ice-3.5.1-VS2013-demos.zip中包含了官方的很多示例程序。这些程序可以帮助我们有效的了解ICE的运行过程。相关文章推荐
- libevent源码分析:hello-world例子
- Libevent源码分析 (1) hello-world
- Libevent源码分析 (1) hello-world
- helloworld.c源码分析
- Zeroc ICE 源码分析二 IceUtil
- Zeroc ICE 源码分析三 ICE的网络通信
- Cocos2d-x HelloWorld 之源码分析
- Coco2d-x Hello World分析
- 写一个程序,用于分析一个字符串中各个单词出现的频率,并将单词和它出现的频率输出显示。(单词之间用空格隔开,如“Hello World My First Unit Test”);
- 写一个程序,用于分析一个字符串中各个单词出现的频率,并将单词和它出现的频率输出显示。(单词之间用空格隔开,如“Hello World My First Unit Test”)
- 【cocos2dx】新建helloworld.cpp项目代码分析
- BlackBerry PlayBook NDK 2.0样例分析之“HelloWorldConsole”
- intel dpdk api helloworld 源码学习
- 从helloworld.c到a.out(3)词法分析
- dpdk helloworld代码分析
- WPF HelloWorld分析
- LDD3源码分析之hello.c与Makefile模板
- BlackBerry PlayBook NDK 2.0样例分析之“HelloWorldDisplay”
- ABP源码分析四十一:ZERO的Audit,Setting,Background Job
- LDD3源码分析之hello.c与Makefile模板