配置ICE开发环境
Berkeley DB expat OpenSSL bzip2 mcpp
set THIRDPARTY_HOME = d:\ice3party
nmake -f Makefile.mak STATICLIBS=yes OPTIMIZE=yes
6. 最最后,把bin目录加入path变量,以便让系统能找到ICE的dll文件(其实主要是三个dll文件,ice33.dll、iceutil33.dll和bzip2.dll)
ICE的HelloWorld
- module Demo {
- interface Printer {
- void printString(string s);
- };
- };
slice2cpp Printer.ice
下表是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 |
std::vector |
dictionary |
std::map |
exception Err | class Err:public Ice:UserException |
nonmutating方法限定符 | const方法 |
idempotent方法限定符 | - |
out 参数限定符 | 引用类型 |
* | 对应类型的代理类 |
- namespace Demo {
- struct Printer {
- virtual void printString(string s) = 0;
- };
- };
编写服务器端代码:
- 新建一个控制台项目
- 将$ICE\include添加到头文件目录列表中
- 将$ICE\lib\ice.lib和iceutil.lib(对应的Debug版本是iced.lib和iceutild.lib)链接入项目
- 把生成Printer.cpp加入项目
- #include
- #include "printer.h" //slice2cpp生成的文件
- 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
- #include
- using namespace std;
- using namespace Demo;
- int main(int argc, char* argv[])
- {
- Ice::CommunicatorPtr ic;
- try{
- // 初始化Ice运行库
- ic = Ice::initialize(argc, argv);
- // 在10000端口取得SimplePrinter代理对象
- 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;
- }
使用Ice::Application简化代码的编写
virtual int run(int, char*[]) = 0;
服务器端:
- #include
- #include "printer.h"
- using namespace std;
- using namespace Demo;
- struct PrinterImp : Printer{
- virtual void printString(const ::std::string& s, const ::Ice::Current& = ::Ice::Current())
- {
- cout << s << endl;
- }
- };
- class MyApp : public Ice::Application{
- public:
- virtual int run(int, char*[]){
- Ice::CommunicatorPtr& ic = communicator();
- Ice::ObjectAdapterPtr adapter
- = ic->createObjectAdapterWithEndpoints(
- "SimplePrinterAdapter", "default -p 10000");
- Ice::ObjectPtr object = new PrinterImp;
- adapter->add(object, ic->stringToIdentity("SimplePrinter"));
- adapter->activate();
- ic->waitForShutdown();
- return 0;
- }
- };
- int main(int argc, char* argv[])
- {
- MyApp app;
- return app.main(argc, argv);
- }
客户端:
- #include
- #include
- using namespace std;
- using namespace Demo;
- class MyApp: public Ice::Application{
- public:
- virtual int run(int, char*[])
- {
- Ice::CommunicatorPtr ic = communicator();
- Ice::ObjectPrx base = ic->stringToProxy("SimplePrinter:default -p 10000");
- PrinterPrx printer = PrinterPrx::checkedCast(base);
- if(!printer) throw "Invalid Proxy!";
- printer->printString("Hello World!");
- }
- };
- int main(int argc, char* argv[])
- {
- MyApp app;
- return app.main(argc,argv);
- }