1、ICE是什么?

ICE是ZEROC的开源通信协议产品,它的全称是:The Internet Communications Engine,翻译为中文是互联网通信引擎,是一个面向对象的中间件,它封装并实现了底层的通讯逻辑,使我们能够方便的构建分布式应用程序。相对于只面向WINDOWS系统微软的.NET(以及原来的DCOM)、复杂的CORBA及性能较差的WEB SERVICE等分布式方案,ICE很好的解决了这些中间件的不足:它支持不同的系统,如WINDOWS、LINUX等,支持在多种开发语言上使用,如C++、C、JAVA、RUBY、PYTHON、VB等。服务端可以是上面提到的任何一种语言实现的,客户端也可以根据自己的实际情况选择不同的语言实现,如服务端采用C语言实现,而客户端采用JAVA语言实现等。

2: ICE的安装和示例

2.1 ICE安装

   ICE官网:https://zeroc.com,点击download后选择合适的平台/开发环境即可,Ubuntu18.04的安装如下图。

notes: 注意ICE3.7.2与3.6.x版本差异较大,头文件不互相兼容,选用时需注意客户端/服务端ICE版本的一致性,避免代码无法编译通过。

2.2: hello world代码示例

官网的helloworld程序,详见官网目录https://doc.zeroc.com/ice/3.7/hello-world-application

首先创建一个slice文件Printer.ice,并将程序需要远程调用的接口写入其中。对于hello world程序,slice文件可按如下来写:

module Demo
{interface Printer{void printString(string s);}
}

下面我们以C++(c++11)为例,分别实现服务端和客户端。首先,我们使用slice2cpp将ice文件转换成C++可使用的.h和.cpp文件。其中.h文件中包含了我们需要远程调用的接口定义以及ICE封装,最主要的有Printer和PrinterPrx类,分别是服务端需要实现的具体对象和客户端使用的代理,其中PrinterPrx代理类中还提供了printStringAsync异步调用方法。

slice2cpp Printer.ice                                

服务端需要实现Printer类的接口,并创建本地创建,之后添加到ice适配器上,以便客户端远程调用。具体实现代码如下:

#include <Ice/Ice.h>
#include <Printer.h>using namespace std;
using namespace Demo;class PrinterI : public Printer
{
public:virtual void printString(string s, const Ice::Current&) override;
};void
PrinterI::printString(string s, const Ice::Current&)
{cout << s << endl;
}int
main(int argc, char* argv[])
{try{Ice::CommunicatorHolder ich(argc, argv);auto adapter = ich->createObjectAdapterWithEndpoints("SimplePrinterAdapter", "default -h localhost -p 10000");auto servant = make_shared<PrinterI>();adapter->add(servant, Ice::stringToIdentity("SimplePrinter"));adapter->activate();ich->waitForShutdown();}catch(const std::exception& e){cerr << e.what() << endl;return 1;}return 0;
}

客户端需要创建远程对象的代理,并通过代理进行远程调用,代码如下:

#include <Ice/Ice.h>
#include <Printer.h>
#include <stdexcept>using namespace std;
using namespace Demo;int
main(int argc, char* argv[])
{try{Ice::CommunicatorHolder ich(argc, argv);auto base = ich->stringToProxy("SimplePrinter:default -p 10000");auto printer = Ice::checkedCast<PrinterPrx>(base);if(!printer){throw std::runtime_error("Invalid proxy");}printer->printString("Hello World!");}catch(const std::exception& e){cerr << e.what() << endl;return 1;}return 0;
}

上面的demo演示了ice远程调用的基本工作方式,ICE接口的详细解释既可通过ICE官网查看,也可在安装ICE后查看相应的头文件注释。然而实际工程中我们需要对ice进行配置,处理网络异常,在服务端进行回调,穿透防火墙,进行线程调度等工作。虽然在ICE的chat demo中有介绍这些工作,然而其demo中引入了Glacier2 rooter中session的使用,而github中代码复杂度更高。相反,以上这些工作不通过Glacier2 rooter也能完美的解决,详见如下代码及注释。

完整可运行的Qt工程(可复用的ICE通信模板)可参考https://github.com/leaf-yyl/ice_template

ICE 文件: 定义了一个服务端需要提供的服务 以及一个客户端需要的回调

module Demo
{interface ServerService{void requireService(string ident, string s);}interface ClientCallback{void callback(string s);}
}

server端代码 : 服务器端worker对象提供具体的服务, ice_manager对象负责ic模块的管理,并接收客户端请求,这些请求会在不同的ICE线程中接收到,然后通过postevent函数最终全部转发到

worker的工作线程并依序处理,如果提供的服务是线程安全的且需要高并发,那么可以去除这一步以获得高性能。相反,如果worker提供的服务不是线程安全的,或者worker中存在线程相关的资源

(例如python解释器等),则必须通过事件循环或者消息队列将ICE线程收到的客户端请求汇总到worker线程统一处理。

#include <QEvent>
#include <QObject>
#include <QCoreApplication>#include <stdexcept>#include <Ice/Ice.h>
#include "Printer.h"using namespace std;class CustomEvent : public QEvent
{
public:explicit CustomEvent(Type type) :QEvent(type) {}enum PMAlgoEventType {CustomEvent_RequireService = 0x00000001,};string m_params;Demo::ClientCallbackPrxPtr m_callback;
};class ServerI : public Demo::ServerService
{
public:ServerI(){}/* Use event loop implement in QObject by Qt to post client requirements to user thread.* May be replaced by event loop in pure C++, handler in java and so on.*/void setImplement(QObject *implement) {m_implement = implement;}void requireService(string ident, string s, const ::Ice::Current& current) override{/* we donot generate the client requirement here, but post it to main thread as this function is called in ice server threads.* When the interface is not thread safe or time-consuming, or has thread associated context like python interpreter, we must post* it to a constant thread managered by ourself to avoid running exceptions.* ident : client object identification, used to build bidirectional connection to cross firewall and local network* s : params used for servcie*/CustomEvent *e = new CustomEvent(QEvent::Type(QEvent::User + CustomEvent::CustomEvent_RequireService));e->m_params   = s;e->m_callback = Ice::uncheckedCast<Demo::ClientCallbackPrx>(current.con->createProxy(Ice::stringToIdentity(ident)));QCoreApplication::postEvent(m_implement, e);}private:QObject *m_implement;
};class IceManager
{
public:IceManager() {/* set up global ice configurations, here we just set thread pool to 2 to avoid deadlock on ice callback,* other settings are configurable as the same.*/Ice::PropertiesPtr props0 = Ice::createProperties();props0->setProperty("Ice.ThreadPool.Server.Size", "2");props0->setProperty("Ice.ThreadPool.Server.SizeMax", "2");props0->setProperty("Ice.ThreadPool.Client.Size", "2");props0->setProperty("Ice.ThreadPool.Client.SizeMax", "2");props0->setProperty("Ice.Trace.ThreadPool", "1");Ice::InitializationData id;id.properties = props0;m_ich = Ice::CommunicatorHolder(id);}bool setImplement(QObject *implement) {try {/* create server object and add it to ice adapter to receive client requirements.* The adapter identification is used for ice pack service, we donot use it now.* The endpoints is the location url where client can access to require service.* The servant identification is used to identify servant as we can add multiple servants to one adapter.*/shared_ptr<ServerI> servant = make_shared<ServerI>();servant->setImplement(implement);auto adapter = m_ich->createObjectAdapterWithEndpoints("ServerAdapter", "default -h localhost -p 10000");adapter->add(servant, Ice::stringToIdentity("Server"));adapter->activate();} catch (const exception &e){cout << "Failed to create ice server object, error-->%s" << e.what() << endl;return false;}return true;}private:Ice::CommunicatorHolder m_ich;
};class MainWorker : public QObject
{
public:MainWorker(QObject *parent = nullptr) : QObject(parent) {}protected:/* Receive client requirements and deliver to associated servcie function */void customEvent(QEvent *e) {CustomEvent *event = (CustomEvent *)e;int type = event->type() - QEvent::User;if (CustomEvent::CustomEvent_RequireService == type) {renderService(event->m_params, event->m_callback);} else {cout << "Unrecognized event type-->" << type << "!" << endl;}}private:/* a simple implement */void renderService(const string& s, const Demo::ClientCallbackPrxPtr& callback) {cout << s << endl;callback->callback("Requirement done!");}
};int main(int argc, char *argv[])
{QCoreApplication a(argc, argv);MainWorker worker(&a);IceManager ice_manager;ice_manager.setImplement(&worker);return a.exec();
}

client端 头文件代码:客户端基本上与服务端类似, 但是多了一个主动发起请求的IceRequirer类,并在IceRequirer中设置链接参数等创建保活的ICE链接,达到与服务端的双向通信,具体原因

见代码注释。

#ifndef HELPER_H
#define HELPER_H#include <QTimer>
#include <QEvent>
#include <QObject>
#include <QThread>
#include <QCoreApplication>#include <stdexcept>#include <Ice/Ice.h>
#include "Printer.h"using namespace std;/* custom event used for event loop */
class CustomEvent : public QEvent
{
public:explicit CustomEvent(Type type) :QEvent(type) {}enum PMAlgoEventType {CustomEvent_RequireCallback = 0x00000001,};string m_params;
};class ClientI : public Demo::ClientCallback
{
public:ClientI(){}/* Use event loop implement in QObject by Qt to post client requirements to user thread.* May be replaced by event loop in pure C++, handler in java and so on.*/void setImplement(QObject *implement) {m_server_implement = implement;}void callback(string s, const ::Ice::Current&) override{/* we donot generate the client requirement here, but post it to main thread as this function is called in ice server threads.* When the interface is not thread safe or time-consuming, or has thread associated context like python interpreter, we must post* it to a constant thread managered by ourself to avoid running exceptions.* s : params used for servcie*/CustomEvent *e = new CustomEvent(QEvent::Type(QEvent::User + CustomEvent::CustomEvent_RequireCallback));e->m_params    = s;QCoreApplication::postEvent(m_server_implement, e);}private:QObject *m_server_implement;
};class ServerRequirer : public QObject
{Q_OBJECT
public:explicit ServerRequirer(const Ice::CommunicatorHolder &ich, const Ice::ObjectAdapterPtr &adapter, const string& ident) {m_ic        = ich.communicator();m_adapter   = adapter;m_ident     = ident;}public slots:void slot_requireServcie(const string& s) {if (nullptr == m_server_prx.get()) {/* connection is not established, try to establish connection first */try {auto base = m_ic->stringToProxy("Server:default -h localhost -p 10000");m_server_prx = Ice::checkedCast<Demo::ServerServicePrx>(base);if(nullptr != m_server_prx.get()){/* set up eseential configurations on Ice connection to keep this connection alive */m_server_prx->ice_getConnection()->setACM(Ice::nullopt, Ice::ACMClose::CloseOff, Ice::ACMHeartbeat::HeartbeatAlways);m_server_prx->ice_getConnection()->setAdapter(m_adapter);}}catch(const exception& e){cerr << e.what() << endl;}}if (nullptr != m_server_prx.get()) {try {/* require remote object call via object proxy */m_server_prx->requireService(m_ident, s);}catch(const exception& e){/* connection lost, reset it and re-establish it on next call */cerr << e.what() << endl;m_server_prx.reset();}}}private:string m_ident;Ice::CommunicatorPtr      m_ic;Ice::ObjectAdapterPtr     m_adapter;Demo::ServerServicePrxPtr m_server_prx;
};class IceManager : public QThread
{Q_OBJECT
public:/* Craete ice global communicator and manager ice requirement.* Server requirements are posted in seperate thread as they may be time-consuming* Local object that implements callback is identified through bidirectional connection instead of* creating a new connection fron server to client, as client may be defensed behind firewall or local network.*/IceManager() {/* register qt meta type */qRegisterMetaType<string>("string");/* set up global ice configurations, here we just set thread pool to 2 to avoid deadlock on ice callback,* other settings are configurable as the same.*/Ice::PropertiesPtr props0 = Ice::createProperties();props0->setProperty("Ice.ThreadPool.Server.Size", "2");props0->setProperty("Ice.ThreadPool.Server.SizeMax", "2");props0->setProperty("Ice.ThreadPool.Client.Size", "2");props0->setProperty("Ice.ThreadPool.Client.SizeMax", "2");props0->setProperty("Ice.Trace.ThreadPool", "1");/* create global ice communicator */Ice::InitializationData id;id.properties = props0;m_ich = Ice::CommunicatorHolder(id);}bool start(QObject *implement) {try {/* create client object and add it to ice adapter to receive server callbacks.* The adapter is created without identification as we donot access it by endpoints.* Instead, we pass it through the connection established with server to build a bidirectional connection.*/m_ident = "Client";shared_ptr<ClientI> servant = make_shared<ClientI>();servant->setImplement(implement);m_adapter = m_ich->createObjectAdapter("");m_adapter->add(servant, Ice::stringToIdentity(m_ident));m_adapter->activate();} catch (const exception &e) {cout << "Failed to create ice object, error-->%s" << e.what() << endl;return false;}/* create ice service requirer in seperate thread, as network request may be time-consuming */m_requirer = new ServerRequirer(m_ich, m_adapter, m_ident);m_requirer->moveToThread(this);connect(this, SIGNAL(signal_requireService(string)), m_requirer, SLOT(slot_requireServcie(string)));QThread::start();return true;}public slots:void slot_requireService() {string s("Hello world!");emit signal_requireService(s);}signals:void signal_requireService(string s);private:Ice::CommunicatorHolder m_ich;Ice::ObjectAdapterPtr   m_adapter;string          m_ident;        /* local object identification */ServerRequirer *m_requirer;
};class MainWorker : public QObject
{
public:MainWorker(QObject *parent = nullptr) : QObject(parent) {}protected:/* Receive callbacks from server and deliver to associated callback function */void customEvent(QEvent *e) {CustomEvent *event = (CustomEvent *)e;int type = event->type() - QEvent::User;if (CustomEvent::CustomEvent_RequireCallback == type) {renderCallback(event->m_params);} else {cout << "Unrecognized event type-->" << type << "!" << endl;}}private:/* a simple implement */void renderCallback(const string& s) {cout << s << endl;}
};#endif // HELPER_H

client main文件代码

#include "helper.h"int main(int argc, char *argv[])
{QCoreApplication a(argc, argv);/* local worker */MainWorker worker(&a);/* ice service manager */IceManager ice_manager;ice_manager.start(&worker);/* require serve service every 3 seconds */QTimer timer;QObject::connect(&timer, SIGNAL(timeout()), &ice_manager, SLOT(slot_requireService()));timer.start(3 * 1000);return a.exec();
}

转载于:https://www.cnblogs.com/leaf-yyl/p/10709389.html

Ice简介+Qt代码示例相关推荐

  1. 完整mes代码(含客户端和server端_Ice简介+Qt代码示例

    一.ICE是什么? ICE是ZEROC的开源通讯协议产品,它的全称是:The Internet Communications Engine,翻译为中文是互联网通讯引擎,是一个面向对象的中间件,它封装并 ...

  2. Zuul简介及代码示例

    zuul它是一种怪兽,可能是因为长相太可怕了,百度居然搜不到,我们打开Spring Cloud文档https://cloud.spring.io/spring-cloud-netflix/refere ...

  3. 每一个合格的家庭主妇都是生产厂长的有力竞争者——ERP库存管理pandas代码示例(面试题)

    欢迎关注,敬请点赞! 每一个合格的家庭主妇都是生产厂长的有力竞争者--ERP简介及代码示例 ERP简介 ERP小故事 ERP库存管理简单代码示例 导入数据 按销量排序的索引列表 分配库存量 最终结果 ...

  4. 【Groovy】集合遍历 ( 使用集合的 reverseEach 方法进行遍历 | 倒序集合迭代器 ReverseListIterator 类简介 | 代码示例 )

    文章目录 一.使用集合的 reverseEach 方法进行倒序遍历 二.倒序集合迭代器 ReverseListIterator 类简介 三.代码示例 一.使用集合的 reverseEach 方法进行倒 ...

  5. 【设计模式】建造者模式 ( 简介 | 适用场景 | 优缺点 | 代码示例 )

    文章目录 一.建造者模式简介 二.建造者模式适用场景 三.建造者模式优缺点 四.建造者模式与工厂模式 五.建造者模式代码示例 1.学生类 2.建造者抽象类 3.建造者实现类 4.教师类 ( 非必须 ) ...

  6. 【设计模式】抽象工厂模式 ( 简介 | 适用场景 | 优缺点 | 产品等级结构和产品族 | 代码示例 )

    文章目录 一.抽象工厂模式简介 二.抽象工厂模式适用场景 三.抽象工厂模式优缺点 四.产品等级结构和产品族 五.抽象工厂模式代码示例 1.冰箱抽象类 2.美的冰箱实现类 3.格力冰箱实现类 4.空调抽 ...

  7. 【设计模式】组合模式 ( 简介 | 适用场景 | 优缺点 | 代码示例 )

    文章目录 一.组合模式简介 二.组合模式适用场景 三.组合模式优缺点 四.组合模式和访问者模式 五.组合模式代码示例 1.书籍和目录的抽象父类 2.书籍类 3.目录类 4.测试类 一.组合模式简介 组 ...

  8. 【设计模式】模板方法模式 ( 简介 | 适用场景 | 优缺点 | 代码示例 )

    文章目录 一.模板方法模式简介 二.模板方法模式适用场景 三.模板方法模式优缺点 四.模板方法扩展 五.模板方法模式相关设计模式 六.模板方法模式代码示例 1.模板方法抽象类 2.模板方法实现类 1 ...

  9. 【设计模式】迭代器模式 ( 简介 | 适用场景 | 优缺点 | 代码示例 )

    文章目录 一.迭代器模式简介 二.迭代器模式适用场景 三.迭代器模式优缺点 四.迭代器模式和访问者模式 五.迭代器模式代码示例 1.迭代器接口 2.迭代器实现 3.集合元素实例类 4.集合管理接口 5 ...

  10. 【设计模式】策略模式 ( 简介 | 适用场景 | 优缺点 | 代码示例 )

    文章目录 一.策略模式简介 二.策略模式适用场景 三.策略模式优缺点 四.策略模式与其它设计模式 五.策略模式代码示例 1.促销策略接口 2.满减促销策略 3.返现促销策略 4.空促销策略 5.促销策 ...

最新文章

  1. BZOJ 4059: [Cerc2012]Non-boring sequences ( )
  2. EditText的各种属性
  3. 实战Nginx与PHP(FastCGI)的安装、配置与优化
  4. 黑科技揭秘:3分钟,轻松构建一张覆盖全球的企业专有网络
  5. python 3.6.0新语法,Python 3.6学习笔记(附2018最新python初学者入门视频教学)
  6. 7-3 复数四则运算 (15 分)
  7. 让VS2008对JQuery语法的智能感知更完美一点(转载)
  8. 教你编写高性能的mysql语法
  9. 2018人工智能发展盘点:国内各行业拥抱AI,总体呈现八大特点
  10. pandas统计个数
  11. Windows10更新错误显示0x8000ffff,易升更新0xc1900107
  12. Windows11右键桌面没新建
  13. c语言使用反三角函数,C语言中反三角函数怎样调用?
  14. 论文中et al.、etc.、e.g.、 i.e.的意思
  15. 10块钱闯荡深圳,如今身价935亿,超越李彦宏,他竟如此低调……
  16. OPPO员工年薪税后110万,租七百块农民房,被女友数落:太抠了!
  17. python 将彩色图片 黑白图片变换
  18. iQOO Z3、OPPOK9和小米11青春版的区别 哪个好
  19. Spark(一)Spark介绍
  20. 如何轻松搞定各种图形化展现

热门文章

  1. 不要轻率的问自己可以想明白可以确认的问题
  2. VirtualBox虚拟机如何选中“启用嵌套 VT-x/AMD-V”
  3. LINUX SHELL mkdir建立多级目录
  4. mod_signalwire.c:1009 Next SignalWire adoption
  5. 以后所有经济时事的点评都不在这里
  6. 检查了一下同事工作,非常不满意
  7. 几人同行时步伐总是整齐
  8. UBUNTU安装OpenOffice
  9. linux下用top命令查看,cpu利用率超过100%时怎么回事
  10. 管理感悟:听明白不容易