进阶之路

首页 新随笔 管理

忙了三天,总算浏览完此书。藉此记下 Ice 的 IceBox 服务框架。

在此用 IceBox 框架写 Hello World 程序,即以载体来体现其特性。

第一步:编写 Slice 文件,映射生成 hello.h && hello.cpp。

// hello.ice
module Demo
{
    interface print {
       void stringPrint(string s); 
    };  
};

第二步:服务器端,将服务的组件放入动态库。

{打印}

// helloI.h
#include <Ice/Ice.h>
#include <hello.h>

using namespace std;
using namespace Demo;

class helloI : public print {
public:
    virtual void stringPrint(const string& s, const Ice::Current&);
};

 

// helloI.cpp
#include <helloI.h>

using namespace std;
using namespace Demo;

void helloI::stringPrint(const string& s, const Ice::Current&)
{
     cout << s << endl;
}

{服务接口}

// HelloService.h
#include <IceBox/IceBox.h>

class HelloServiceI : public IceBox::Service {
public: 
    virtual void start(const std::string&, 
                       const Ice::CommunicatorPtr&,
                       const Ice::StringSeq&);
    virtual void stop();

private:
    Ice::ObjectAdapterPtr _adapter;
};

 

// HelloServiceI.cpp
#include <Ice/Ice.h>
#include <HelloServiceI.h>
#include <helloI.h>

using namespace std;
using namespace Demo;

extern "C" {  //  服务进入点
    IceBox::Service*
    create(Ice::CommunicatorPtr communicator)
    {   
        return new HelloServiceI;
    }   
}

void HelloServiceI::start( const string& name, const Ice::CommunicatorPtr& communicator, const Ice::StringSeq& args)
{
    _adapter = communicator->createObjectAdapter(name);
    Ice::ObjectPtr object = new helloI();
    _adapter->add(object, communicator->stringToIdentity("hello"));
    _adapter->activate();
}

void HelloServiceI::stop()
{
    _adapter->deactivate();
}

 接下来制作动态库(.so)。此时,已有程序如下:

  

 编译生成动态库。(hello.cpp , helloI.cpp , HelloServiceI.cpp 三个文件)

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

 

   

 

第三步:在客户端, 编写客户端代码。

// Client.cpp
#include <Ice/Ice.h>
#include <hello.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("hello:default -p 9990");
        printPrx printer = printPrx::checkedCast(base);

        if (!printer)  
            throw "Invalid proxy";
        printer->stringPrint("Hello World!");
    } catch (const Ice::Exception& ex) {
        cerr << ex << endl;
        status = 1;
    } catch (const char* msg) {
        cerr << msg << endl;
        status = 1;
    }   
    if (ic)
        ic->destroy();
    return status;
}

 

    编译并链接生成可执行程序 client。

$ c++ -I. -I$ICE_HOME/include -c hello.cpp Client.cpp
$ c++ -o client hello.o Client.o -L$ICE_HOME/lib -lIce -lIceUtil

 

第四步:配置服务端点和客户。

        A. 服务器配置。

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

 

       B. 服务端点配置。

// config.service
Hello.Endpoints=tcp -p 9990:udp -p 9990

 

     

/*
C. 客户配置。

// config.client
Hello.Proxy=Hello:tcp -p 9990:udp -p 9990
*/

 

 

第五步:程序示例。

       A. 启动 IceBox 服务器.

$ icebox --Ice.Config=config.icebox

 

     B. Run the client.(连接服务器并请求打印服务)

$ ./client

 

    C.  服务器端进行打印。

 

      

 

 

 

 

 

 

 

 

         

posted on 2015-07-21 17:34  进阶之路  阅读(908)  评论(0编辑  收藏  举报