IceBox

IceBox是一个Ice应用服务框架。加载多个服务进行配置,进行一定优化;通过配置加载服务,解除服务和服务器的耦合。

IceBox服务需要Slice定义、骨架代码(slice定义映射的特定语言代码)、Servant类、IceBox服务。

具体实现:

Hello.ice、Hello.h/Hello.cpp、HelloI.h/HelloI.cpp(Servant类,实现Hello)不再介绍,参考上篇日志。

IceBox服务

HelloServiceI.h

#include <IceBox/IceBox.h>
#if defined(_WIN32)
#       define HELLO_API __declspec(dllexport)
#else
#       define HELLO_API
#endif
class HELLO_API HelloServiceI : public IceBox::Service {
    public:
        virtual void start(const std::string&, const Ice::CommunicatorPtr&,
                const Ice::StringSeq&);
        virtual void stop();
    private:
        Ice::ObjectAdapterPtr adapter;
};


每个IceBox服务都要继承IceBox::Service,实现start和stop,以供IceBox服务管理器调用。start在服务加载后被调用,stop在服务关闭时被调用。

//创建服务
extern "C" {
     HELLO_API IceBox::Service* create(Ice::CommunicatorPtr communicator){
         return new HelloServiceI;
     }
}

void HelloServiceI::start(const string& name,
         const Ice::CommunicatorPtr& communicator,
         const Ice::StringSeq& args){
	//加入Servant实例
    adapter=communicator -> createObjectAdapter(name);
    Ice::ObjectPtr object=new HelloI;
    adapter->add(object, communicator->stringToIdentity("hello"));
    adapter->activate();
}
void HelloServiceI::stop(){
    adapter->deactivate();
}


配置服务器

config.icebox主配置文件

 IceBox.Service.Hello=HelloService:create --Ice.Config=config.service


config.service次配置文件(服务的端口和协议)

Hello.Endpoints=tcp -p 10000:udp -p 10000


打包

g++ -o -shared libHelloService.so HelloServiceI.cpp HelloI.cpp Hello.cpp -I. -I $ICE_HOME/include -fPIC


启动IceBox

icebox --Ice.Config=config.icebox


 

posted @ 2012-07-16 11:09  春文秋武  阅读(463)  评论(0编辑  收藏  举报