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的運行過程。

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