Zeroc ICE 源码分析一 HelloWorldICE

因为公司需要让我了解一下ICE,所以写下来我的一些了解情况供分享。因为接触不久所以写的比较浅显,请多包涵。

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 下载所需要的文件如下:
编译好的Redistribution. 包含了运行时依赖了一些库,头文件,以及一些工具等。
ICE为VS2013开发的插件,用于简化开发期间的ICE 编译操作等。
ICE所依赖的一些第三方库,如OpenSSL 等。在编译ICE的源码时需要用到。
ICE的源码包。
ICE对VS2013发布的Patch。
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的运行过程。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章