登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

Code@Pig Home

喜欢背着一袋Code傻笑的Pig .. 忧美.欢笑.记忆.忘却 .之. 角落

 
 
 

日志

 
 

[ice] 使用 Ice::Application 简化异常处理逻辑  

2010-05-29 19:43:19|  分类: net_Ice |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
上一篇的 hello world,我们看到 main() 中有那些繁琐的 try ...  catch ...,每次都写,很麻烦。
所以 Ice 给弄了个类 Application 封装了一下。

-------------------------- client.cpp ------------------------------
#include <Ice/Ice.h>
#include <foo.h>

using namespace std;
using namespace Demo;

class MyApplication : virtual public Ice::Application {
public:
    virtual int run(int, char *[]) {
        Ice::ObjectPrx base = communicator()->stringToProxy("SimpleFoo:default -h localhost -p 10000");
        FooPrx foo = FooPrx::checkedCast(base);
        if (!foo)
            throw "Invalid proxy";

        cout << "name: " << foo->getName(10) << endl;
        return 0;
    }
};

int main(int argc, char *argv[])
{
    MyApplication app;
    return app.main(argc, argv);
}
-------------------------- server.cpp ------------------------------
#include <Ice/Ice.h>
#include <foo.h>

using namespace std;
using namespace Demo;

class FooI : virtual public Foo
{
public:
    virtual std::string getName(Ice::Int value, const Ice::Current&);
};

std::string
FooI::getName(Ice::Int value, const Ice::Current&)
{
    cout << "value: " << value << endl;
    return "kasicass";
}

class MyApplication : virtual public Ice::Application {
public:
    virtual int run(int, char *[]) {
        shutdownOnInterrupt();

        Ice::ObjectAdapterPtr adapter = communicator()->createObjectAdapterWithEndpoints(
           "SimpleFooAdapter", "default -h localhost -p 10000");
        Ice::ObjectPtr obj = new FooI;
        adapter->add(obj, communicator()->stringToIdentity("SimpleFoo"));
        adapter->activate();

        communicator()->waitForShutdown();

        if (interrupted()) {
            cerr << appName() << ": recv signal, shutting down" << endl;
        }

        return 0;
    }
};

int main(int argc, char *argv[])
{
    MyApplication app;
    return app.main(argc, argv);
}
-----------------------------------------------------------------------

而且还封装了 Ctrl + C 之类的 signal handle,很方便。
还有个类 Ice::Service,用于将程序启动为 *nix daemon or win32 service。
  评论这张
 
阅读(1454)| 评论(0)

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2018