ICE是ZEROC的開源通訊協議產品,它的全稱是:The Internet Communications Engine,翻譯爲中文是互聯網通訊引擎,是一個面向對象的中間件,它封裝並實現了底層的通信邏輯,使咱們可以方便的構建分佈式應用程序。相對於只面向WINDOWS系統微軟的.NET(以及原來的DCOM)、複雜的CORBA及性能較差的WEB SERVICE等分佈式方案,ICE很好的解決了這些中間件的不足:它支持不一樣的系統,如WINDOWS、LINUX等,支持在多種開發語言上使用,如C++、C、JAVA、RUBY、PYTHON、VB等。服務端能夠是上面提到的任何一種語言實現的,客戶端也能夠根據本身的實際狀況選擇不一樣的語言實現,如服務端採用C語言實現,而客戶端採用JAVA語言實現等。
java
ICE官網:https://zeroc.com,點擊download後選擇合適的平臺/開發環境便可,Ubuntu18.04的安裝以下圖。python
notes: 注意ICE3.7.2與3.6.x版本差別較大,頭文件不互相兼容,選用時需注意客戶端/服務端ICE版本的一致性,避免代碼沒法編譯經過。c++
官網的helloworld程序,詳見官網目錄https://doc.zeroc.com/ice/3.7/hello-world-applicationgit
首先建立一個slice文件Printer.ice
,並將程序須要遠程調用的接口寫入其中。對於hello world程序,slice文件可按以下來寫: github
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也能完美的解決,詳見以下代碼及註釋。session
完整可運行的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(); }