1、客戶端環境調用
#include<Ice/Ice.h>
int main(int argc, char* argv[])
{
Ice::CommunicatorPtr ic;
try
{
ic = Ice::initialize(argc, argv);
Ice::ObjectPrx base = ic->stringToProxy("MyProxy:default -p 10000");
if (!base)
{
throw "Can not find proxy";
}
MyProxyPrx proxy = MyProxyPrx::checkCast(base);
if (!proxy)
{
throw "Invalid Proxy";
}
proxy->op();
}
catch(const Ice::Exception& ex)
{
}
catch(const std::string& msg)
{
}
catch(const char* msg)
{
}
if (ic)
{
ic->destory();
}
return 0;
}
2、服務器端環境調用
#include<Ice/Ice.h>
int main(int argc, char* argv[])
{
Ice::CommunicatorPtr ic;
try
{
ic = Ice::initialize(argc, argv);
Ice::ObjectAdapterPtr adapter = ic->createObjectAdapterWithEndpoints("MyAdapter", "default -p 10000");
MyProxyPtr proxy = new MyProxyI();
adapter->add(proxy, communicator()->stringToIdentity("MyProxy"));
adapter->activate();
ic->waitForShutdown();
}
catch(const Ice::Exception& ex)
{
}
catch(const std::string& msg)
{
}
catch(const char* msg)
{
}
if (ic)
{
ic->destory();
}
return 0;
}
3、Application對象
class Application
{
public:
Application(SignalPolicy = HandleSignals);
public:
int main(int, char*[], const Ice::InitializationData& = Ice::InitializationData());
int main(int, char*[], const char*);
public:
virtual int run(int, char*[]) = 0;
virtual void interruptCallback(int);
public:
static const char* appName();
static CommunicatorPtr communicator();
static bool interrupted();
};
定義類,繼承至Ice::Application,實現run方法,在main函數中調用Application::main()即可。需要知道Application的main方法幫你完成了如下事情
A、創建並設置了一個異常句柄,用來捕獲程序運行過程中異常並打印到標準輸出
B、調用Ice::initialize初始化來創建通信器,並在主循環結束後調用通信器的destory方法
C、掃描輸入參數並將ICE支持的參數剝離掉後返回給run方法
D、提供appName方法獲取應用名稱,即argv[0],提供communicator方法獲取通信器
E、創建信號處理器,用來捕獲系統信號並處理
F、創建一個日誌處理對象,用來輸出系統日誌到錯誤輸出
在run方法的實現中,可以通過調用interrupted方法判斷是否是外部信號中斷了程序運行
4、Service對象
class Service
{
public:
virtual bool shutdown();
virtual void interrupt();
public:
int main(int&, char*[], const InitializationData& = InitializationData());
Ice::CommunicatorPtr communicator() const;
static Service* instance();
bool service() const;
std::string name() const;
virtual void handleInterrupt(int);
protected:
virtual bool start(int, char*[], int&) = 0;
virtual void waitForShutdown();
virtual bool stop();
virtual Ice::CommunicatorPtr initializeCommunicator(int&, char*[], const InitializationData&);
protected:
virtual void syserror(const std::string&);
virtual void error(const std::string&);
virtual void warning(const std::string&);
virtual void trace(const std::string&);
virtual void print(const std::string&);
};
定義類,繼承至Ice:Service,實現start方法,在main函數中調用Service::main()即可。需要知道Service的run方法幫你完成了如下事情:
A、創建一個信號處理對象,用來捕獲系統信號並處理
B、調用initializeCommunicator方法創建ICE的通信對象,並在主循環結束後調用通信器的destory方法
C、調用start方法,如果start方法返回false則結束
D、調用waitForShutdown方法等待主循環結束,主循環可通過調用shutdown方法結束
E、調用stop方法
F、回收系統資源
Service程序運行支持如下命令行參數:--daemon、--pidfile file、--noclose、--nochdir
Service由於在--daemon啓動後關閉了標準輸出的描述符,因此日誌不起作用,如果需要自定義日誌,需要子類重新定義initializeCommunicator
方法並安裝一個日誌對象來記錄運行日誌
ice環境初始化和slice方法調用
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章
ICE的AMI方法調用
followshake
2020-02-25 16:53:11
ICE的服務器對象實現
followshake
2020-02-25 16:53:10
slice的AMD方法實現與調用
followshake
2020-02-25 16:53:10
slice的c++映射
followshake
2020-02-25 16:53:10
ICE的屬性設置
followshake
2020-02-25 16:53:10
ICE的slice定義注意事項
followshake
2020-02-25 16:53:10
ICE運行環境備忘二
followshake
2020-02-25 16:53:10
ICE運行時環境備忘一
followshake
2020-02-25 16:53:10
ICE的AMI方法調用
followshake
2020-02-25 16:53:11
ICE的服務器對象實現
followshake
2020-02-25 16:53:10
slice的AMD方法實現與調用
followshake
2020-02-25 16:53:10
slice的c++映射
followshake
2020-02-25 16:53:10
ICE的屬性設置
followshake
2020-02-25 16:53:10
ICE的工具集基本功能
followshake
2020-02-25 16:53:10
ICE的slice定義注意事項
followshake
2020-02-25 16:53:10