忙了三天,总算浏览完此书。藉此记下 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.  服务器端进行打印。

Ice分布式程序设计—IceBox(Hello World Application)的更多相关文章

  1. ICE异步程序设计-----AMI/AMD

    1 简介 AMI 异步方法调用(AMI) 这个术语描述的是客户端的异步编程模型支持. 如果你使用AMI 发出远地调用,在Ice run time 等待答复的同时,发出调用的线程不会阻塞.相反,发出调用 ...

  2. Ice的HelloWorld(Java)

    Ice是一种面向对象的中间间平台,入门ice,简单的HelloWorld是必不可少的. 转载请注明http://www.cnblogs.com/zrtqsk/p/3745286.html,谢谢. 一. ...

  3. Ice系列--傻瓜式服务开发IceBox

    前言 相信大家在没有接触过框架之前,都自己或多或少的开发过一些应用服务.每个应用服务除了业务配置还有很多环境配置,资源配置等,这些跟部署相关的配置.服务跟配置文件是一种静态绑定的方式,更新配置还需要重 ...

  4. P2P通信标准协议(三)之ICE

    在P2P通信标准协议(二)中,介绍了TURN的基本交互流程,在上篇结束部分也有说到,TURN作为STUN 协议的一个拓展,保持了STUN的工具性质,而不作为完整的NAT传输解决方案,只提供穿透NAT的 ...

  5. 从一般分布式设计看HDFS设计思想与架构

     要想深入学习HDFS就要先了解其设计思想和架构,这样才能继续深入使用HDFS或者深入研究源代码.懂得了"所以然"才能在实际使用中灵活运用.快速解决遇到的问题.下面这篇博文我们就先 ...

  6. ICE第二篇--一个"hello world"的简单例子

    1 本文介绍一个hello world输出的例子. ice应用的步骤如下: 1. 编写 Slice 定义并编译它. 2. 编写服务器并编译它. 3. 编写客户并编译它. 基本框架图示: 本文代码图示: ...

  7. ice grid配置使用第二篇------实际使用

    一    首先,启动ice grid 1 修改配置文件 node.cfg,appication.xml 修改registry.cfg 配置注册表信息: IceGrid.Registry.Client. ...

  8. 《程序设计语言——实践之路》【PDF】下载

    程序设计语言--实践之路>[PDF]下载链接: https://u253469.pipipan.com/fs/253469-230382240 内容简介 本书在美国大学已有使用了十余年,目前被欧 ...

  9. zeroc ice log4net 多进程log文件问题

    使用zeroc ice 中的icebox 的时候多服务会有多个服务实例,每个实例都要写日志文件,所以要配置多个日志文件区分开来, 类似这样  orderservice_1_20160101.log   ...

随机推荐

  1. MFC中输入框的文本转换为char[]字符数组类型

    在MFC的输入框中得到输入字符串用如下形式: CString v_inputstring; (( CEdit *) GetDlgItem (IDC_EDIT1 ))-> GetWindowTex ...

  2. 6.Counting Point Mutations

    Problem Figure 2. The Hamming distance between these two strings is 7. Mismatched symbols are colore ...

  3. Servlet-RequestDispatcher.forward方法

    forward方法与Include方法非常相似,但有5点不同 1,在调用forward方法之前,输出缓冲区的数据会被清空,也就是说,在使用forward方法进行请求转发时,只可能输出别转发的web资源 ...

  4. poj1298 The Hardest Problem Ever 简单题

    链接:http://poj.org/problem?id=1298&lang=default&change=true 简单的入门题目也有这么强悍的技巧啊!! 书上面的代码: 很厉害有没 ...

  5. nodejs--模块

    在客户端可以将所有的javascript代码分割成几个JS文件,然后在浏览器中将这些JS文件合并.但是在nodejs中是通过以模块为单位来划分所有功能的.每一个模块为一个JS文件,每一个模块中定义的全 ...

  6. LeetCode() Binary Tree Level Order Traversal

    感觉我这个思路好 先记录上一层有几个节点 /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeN ...

  7. ListView的item里面控件文本颜色修改

    @SuppressLint("InflateParams") @Override public View getChildView(int groupPosition, int c ...

  8. 王爽 <<汇编 语言>> 13.6 BIOS中断例程应用

    ;名称:ILOVEU程序 ;使用BIOS提供的中断例程 assume cs:code code segment main: ;显示背景22*80 ;dh中放行号 ;dl中放列号 bibi: push ...

  9. C++引用详解

    引用的概念 引用:就是某一变量(目标)的一个别名,对引用的操作与对变量直接操作完全一样. 引用的声明方法:类型标识符 &引用名=目标变量名: 如下:定义引用ra,它是变量a的引用,即别名. i ...

  10. php中判断字符串是否全是中文或含有中文的实现代码

    <?php header('Content-type:text/html; charset=utf-8'); $str = '你好'; if(preg_match('/^[\x{4e00}-\x ...