masterkey 发表于 2013-1-27 05:28:22

ZeroC ICE之旅------C++

书接上回,今天基于C++的Ice实现,在我们的环境下已经安装了Ice相关的库。
还是采用相同的demo.ice.
module Demo{interface test{ string   execute(string mth,string cmd);};};
btw:
关于ICE的介绍
http://masterkey.iteye.com/blog/182954

ZeroC ICE之旅------java
http://masterkey.iteye.com/blog/182975

ZeroC ICE之旅------多语言互通互联
http://masterkey.iteye.com/blog/183742

ZeroC ICE之旅------集群和容错
http://masterkey.iteye.com/blog/185081
这次我们要使用slice2cpp ,把slice的语言描述转化为对应的C++代码。按照同样的思路,实现一个我们自己的execute方法

首先我们运行

slice2cpp demo.ice

在当前目录生成:
-rw-r--r--1 root root111744月 16 10:58 demo.cpp
-rw-r--r--1 root root   50564月 16 10:58 demo.h


好像比较简洁哦,至少比slice2java生成的文件要少多了,赫赫.

由于最新的3.3b还存在一定的问题,所以我们还是采用3.2.0(很稳定,已经部署到生产环境)的环境,上面的slice2cpp也是3.2.0的程序.


下面我们实现自己的Server,和java版的Server差不多.
//Server.cpp#include <Ice/Ice.h>#include <demo.h>using namespace std;using namespace Demo;class Server:public test{public:::std::string execute (const string & mth, const string & str,                         const Ice::Current &);public:    Server ();};Server::Server (){ };std::string Server::execute (const string & mth, const string & str,                           const Ice::Current &){cout << mth + str << endl;return mth + str;} intmain (int argc, char *argv[]){int    status = 0;Ice::CommunicatorPtr ic;try{    ic = Ice::initialize (argc, argv);    Ice::ObjectAdapterPtr adapter      =      ic->createObjectAdapterWithEndpoints ("TestAdapter",                                          "default -p 10000");    Ice::ObjectPtr object = new Server;    adapter->add (object, ic->stringToIdentity ("TestAdapter"));    adapter->activate ();    ic->waitForShutdown ();} catch (const Ice::Exception & e){    cerr << e << endl;    status = 1;} catch (const char *msg){    cerr << msg << endl;    status = 1;}if (ic)    {      try      {      ic->destroy ();      }      catch (const Ice::Exception & e)      {      cerr << e << endl;      status = 1;      }    }return status;}
其中:
std::string Server::execute (const string & mth, const string & str,                           const Ice::Current &){cout << mth + str << endl;return mth + str;}
实现我们自己的逻辑。有了Server,

下面再实现一个client:
//clent.cpp#include <string>#include <Ice/Ice.h>#include <demo.h>using namespace std;using namespace Demo;int main(void) {       try {      int argc=0;      char* a="";      char** argv=&a;      int status = 0;      Ice::CommunicatorPtr ic;      testPrx testServer;      Ice::ObjectPrx base;      ic = Ice::initialize(argc, argv);      base = ic->stringToProxy("TestAdapter:default -p 10000");      testServer = testPrx::checkedCast(base);      string mystr = testServer->execute("My first cpp "," ice demo");      printf("result:%s\n",mystr.c_str());      }catch (const Ice::Exception& ex) {                cerr << ex << endl;                //status = 1;      }      //printf("result:%s\n",mystr.c_str());}
是不是很眼熟,怎么和java版本的例子差不多,有些部分就是固定格式,自己的修改的部分,不多阿。

细心的读者会发现,在Server,Client中。

Server 中

createObjectAdapterWithEndpoints 采用的TestAdapter

Client 中

stringToProxy 也是采用TestAdapter

注意这两个名称要保持一致,这样Client才能正确连接到Server上,也就是说这个是一个服务ID的标示.


default -p 10000 ,我们Server,Client都运行于同一台机器,所以采用default连接模式,


赫赫,看一下如何编译Server,Client


编译服务端采用:
g++ -I. -I$ICE_HOME/include-o Server demo.cpp Server.cpp-L$ICE_HOME/lib -lIce -lIceUtil

编译客户端采用:
g++ -I. -I$ICE_HOME/include-o client client.cpp demo.cpp-L$ICE_HOME/lib -lIce -lIceUtil



关于如何设置ICE_HOME多看看它的安装帮助把,赫赫


正确编译连接之后,生成Server,client两个程序.

运行Server,再在两一个控制台下,运行client,

client控制台:result:My first cppice demo

server控制台:My first cppice demo

哈哈,到目前为止基于C++的Ice例子介绍到这里,虽然例子很小,很简单,不过充分对其工作模式的展示,也说明采用Ice进行远程过程调用开发的简单,简洁.Ice的Server可以支持多个Client连接到同一个Server,更厉害的是一个Client可以连接多个Server。Ice是不是很神奇,我会在日后的blog向大家介绍。


ICE之轻量级分布式通讯中间件
http://masterkey.iteye.com/blog/182954
ZeroC ICE之旅------java
http://masterkey.iteye.com/blog/182975
ZeroC ICE之旅------Slice
http://masterkey.iteye.com/blog/184064
ZeroC ICE之旅------多语言互通互联
http://masterkey.iteye.com/blog/183742
更多精彩,请关注:
Titan的天空
http://masterkey.iteye.com
页: [1]
查看完整版本: ZeroC ICE之旅------C++