【
colorado
】
说明文档位于DPWI第3章,程序位于Ice 3.3.1发布${ICE_HOME}/demo/book/printer。
1、Slice文件
Printer.ice 定义如下:
#ifndef SIMPLE_ICE
#define SIMPLE_ICE
module Demo
{
interface Printer
{
void printString(string s);
};
};
#endif
本Slice文件定义了模块Demo,它含有Printer接口,定义了printString操作,printString 操作接受一个字符串。程序功能是客户端调用printString发送字符串给服务器,服务器将字符串打印出来。
编译本slice文件:
slice2cpp Printer.ice
生成 Printer.h/Printer.cpp 文件,定义了接口的规范,实现了运行时支持的类型以及类型的整编与解编。在我们编写的C/S程序中引用了这些文件,以便使用接口。
2、服务器端
Server.cpp:
//必须包含Ice.h文件,以支持Ice运行时
#include <Ice/Ice.h>
//必须包含Printer.h,以使用Slice规范
#include <Printer.h>
using namespace std;
using namespace Demo;
/*
骨架类Printer根据Printer.ice定义由Slice2cpp编译器生成,
它的printString是纯虚方法,骨架类不能被实例化。
服务者PrinterI类继承自骨架类Printer,提供了printString纯虚方法的实现。
*/
class PrinterI : public Printer {
public:
virtual void printString(const string & s,const Ice::Current &);
};
/*
Ice:Current对象为服务器中操作的实现提供了有关当前执行请求的信息,
如适配器,连接,标识,层面,操作,操作模式,上下文,请求ID等。
*/
void PrinterI::printString(const string & s, const Ice::Current &)
{
cout << s << endl;
}
int
main(int argc, char* argv[])
{
int status = 0;
Ice::CommunicatorPtr ic;
try {
//初始化Ice,获取通讯器,智能指针
ic = Ice::initialize(argc, argv);
//在通讯器ic上创建对象适配器SimplePrinterAdapter,缺省协议(TCP),在端口10000上监听
Ice::ObjectAdapterPtr adapter
= ic->createObjectAdapterWithEndpoints("SimplePrinterAdapter", "default -p 10000");
//创建服务者类PrinterI
Ice::ObjectPtr object = new PrinterI;
//将服务者类PrinterI加入对象适配器,服务者类的名称是SimplePrinter,
//客户端必须使用此名称的请求标识来请求服务者的操作。
adapter->add(object,ic->stringToIdentity("SimplePrinter"));
//激活对象适配器,客户端请求现在可以分派给服务者类PrinterI。
adapter->activate();
//挂起当前主函数所在线程,等待通讯器关闭
ic->waitForShutdown();
} catch (const Ice::Exception & e) { //捕捉Ice运行时抛出的异常
cerr << e << endl;
status = 1;
} catch (const char * msg) {//捕捉致命错误,输出错误串
cerr << msg << endl;
status = 1;
}
//通讯器必须初始化过,否则不能调用destroy
if (ic) {
try {
ic->destroy();//销毁通讯器
} catch (const Ice::Exception & e) {
cerr << e << endl;
status = 1;
}
}
return status;
}
3、客户端
#include <Ice/Ice.h>
#include <Printer.h>
using namespace std;
using namespace Demo;
int
main(int argc, char * argv[])
{
int status = 0;
Ice::CommunicatorPtr ic;
try {
//初始化Ice,获取通讯器,智能指针
ic = Ice::initialize(argc, argv);
//创建通讯器的代理基类,该代理使用的请求标识为SimplePrinter,端口号:10000,协议:默认(TCP)
Ice::ObjectPrx base = ic->stringToProxy("SimplePrinter:default -p 10000");
//代理基类向下转型为PrinterPrx代理类,调用本操作会向服务器发送请求。
//如果转型成功就返回Printer接口的代理,如果转型失败就返回null代理。
PrinterPrx printer = PrinterPrx::checkedCast(base);
//不是Printer接口的代理,抛出异常
if (!printer)
throw "Invalid proxy";
//是Printer接口的代理,调用代理的操作printString
printer->printString("Hello World!");
} catch (const Ice::Exception & ex) {//捕获Ice运行时异常
cerr << ex << endl;
status = 1;
} catch (const char * msg) {//捕捉致命错误,输出错误串
cerr << msg << endl;
status = 1;
}
//通讯器必须初始化过,否则不能调用destroy
if (ic) {
try {
ic->destroy();//销毁通讯器
} catch (const Ice::Exception & ex) {
cerr << ex << endl;
status = 1;
}
}
return status;
}
4、运行
客户端和服务器使用VC++的工程文件完成编译,然后先运行Server,再运行Client。关闭时,以Ctrl+C结束Server程序。
5、说明
${ICE_HOME}/demo/Ice/minimal是一个与此例非常类似的最小Ice应用程序。不同之处在于minimal的服务器定义了对强制关闭信号的处理,如对Ctrl+C定义了:IceUtil::CtrlCHandler ctrCHandler(onCtrlC);然后实现了static void onCtrlC(int)函数,对接收到Ctrl+C信号进行了处理。另外minimal程序用单独的helloI文件实现了hello接口的sayHello操作规范。
本文所展示的程序比马维达先生中译本(Ice 1.3.0)的程序在异常处理方面有所增强,请读者注意。
分享到:
相关推荐
ICE-BA: Incremental, Consistent and Efficient Bundle Adjustment for Visual-Inertial SLAM的pdf文档
iCE40 Ultra:移动设备中的杀手级芯片.pdf
iCE40系列:超低功耗FPGA评估和开发方案.pdf
一个简单的ice hello world,对于刚刚入手的ice的人有帮助,运行起来,了解其中的原理
fy_iceworld_2002.zip
冰淇淋商店在线网站,使用以下语言构建:* Django框架* python languadge * upslash随机图片API * CSS3 * HTML5免费图片hd ::: with API
icecream-cpp::soft_ice_cream:永远不要使用coutprintf进行调试
ICE文档作者写的关于实例程序的开发过程。很基础,不是很深,建议刚入门的同志可以看看,学学怎么使用ICE编程。
ICE-3.7.4 最新安装文件msi文件,windows版 ICE常见报错 Exception in thread Ice.ConnectionRefusedException error = 0 at IceInternal.Network.doFinishConnect(Network.java:417) at IceInternal....
MC ^ 2 = E 协作式数学方程式编辑软件。... 通过运行docker run -p <port>:8080 -d ice-mc2启动映像。 Node.js 您必须安装Node.js和MongoDB。 运行npm install && node deployment.js && node app.js
Ice 是一种面向对象的中间件平台。从根本上说,这意味着Ice 为构建面 向对象的客户-服务器应用提供了工具、API 和库支持。Ice 应用适合在异 种环境中使用:客户和服务器可以... 本例子是用java写的ice实现的HelloWorld
xmake-idea::ice_cream:IntelliJ IDEA中的XMake集成
public abstract class _HelloWorldDisp extends Ice.ObjectImpl implements HelloWorld{} servant类是ice所定义的接口,在服务器端的实现类。我们在该类中可以编写服务器端对请求的具体处理。 代码如下: package ...
ZeroC ICE集群搭建 ICE安装目录: /home/apps/cpplibs/Ice-3.6.4/ ICE集群基本由registry(master, replication, ...) 和 icegridnode(多)组成。 registry: registry的master和replication区别只是IceGrid.Registry....
ICE3PCS02G 英飞凌芯片 INFINEON 中文版规格书手册
雪地地图改版 草地 f
ice4j 交互式连接建立(ICE)协议结合了各种NAT遍历实用程序(例如STUN和TURN协议),以提供一种强大的机制,该机制允许基于SIP和XMPP的基于Offer / Answer的协议遍历NAT。 该项目提供了ICE协议的Java实现,该协议...
Ice Ice-3.7.0 linux windows mac os docker java js python ruby source Download Ice 3.7 Version: 3.7.0 - July 21, 2017 You are licensing Ice under GPLv2 unless you purchase a commercial license.
详细介绍了ICE的各种知识功能的使用以及一些c++和java的代码例子
进程查看工具.