ice库c语言例子,很不多的ICE架构入门学习例子
雖然使用傳統(tǒng)的SOCKET編程,我們可以更為清楚程序的性能,能夠更直接的操控SOCKET的設(shè)置,比如發(fā)送超時(shí)時(shí)間,接受BUFFER的大小,以及進(jìn)行自己的協(xié)議加密。但是由于其調(diào)試成本較高,且不易于分布式部署
ICE 作為一個(gè)中間件技術(shù),已經(jīng)得到越來越廣泛的應(yīng)用。在 ICE3.2 以后,由于采用了epoll 模型,其通信層的性能提升較為明顯。而且其相較于SOCKET編程一個(gè)很明顯的優(yōu)勢是便于調(diào)試。這對(duì)于程序員來說,無疑是很大的誘惑。
網(wǎng)上介紹ICE原理的文獻(xiàn)很多,我就不廢話。先從一個(gè)簡單的實(shí)例開始,進(jìn)行我們的ICE編程入門之旅:
建立一個(gè)testice.ice文件
#ifndef _H_TEST_WG_ICE_H_
#define _H_TEST_WG_ICE_H_
module WG
{
interface WGTestIce
{
int ping();
int GetTime(out string strTime);
};
};
#endif
我們需要建立一個(gè)客戶端和一個(gè)服務(wù)端。
服務(wù)端文件如下:
testiceImpl.h
#include #include #include “testice.h”
#include using namespace WG;
class CTestIceImpl: public WGTestIce
{
public:
CTestIceImpl(){}
~CTestIceImpl(){}
int ping(const Ice::Current& = Ice::Current())
{
std::cout << “ping successfully.” << std::endl;
return 0;
}
int GetTime(std::string& strTime, const Ice::Current& = Ice::Current())
{
time_t tt = time(NULL);
struct tm tm1;
struct tm* pNow;
pNow = localtime_r(&tt,&tm1);
if (!pNow)
{
strTime = “error”;
return -1;
}
char buf[32] = {0};
sprintf(buf,”%04d-%02d-%02d %02d:%02d:%02d”,
pNow->tm_year+1900,
pNow->tm_mon+1,
pNow->tm_mday,
pNow->tm_hour,
pNow->tm_min,
pNow->tm_sec);
strTime = buf;
return 0;
}
};
#endif
上面這個(gè)類派生了接口類,用做服務(wù)端的實(shí)現(xiàn)。
再建立一個(gè)主函數(shù)文件testServer.cpp’
#include #include “testice.h”
#include “testiceImpl.h”
int createSrv()
try
{
int tmpargc = 0;
char** tmpargv = NULL;
Ice::CommunicatorPtr ic;
ic = Ice::initialize(tmpargc,tmpargv);
Ice::ObjectAdapterPtr adapter = ic->createObjectAdapterWithEndpoints(”WGSrv”,”default -p 7788″);
Ice::ObjectPtr object = new CTestIceImpl();
adapter->add(object, ic->stringToIdentity(”WGInterfaceAgentId”));
adapter->activate();
std::cout << “now server start successfully.” << std::endl;
ic->waitForShutdown();
if (ic)
{
std::cout << “now ice server exit.” << std::endl;
ic->destroy();
}
}
catch(const Ice::Exception& ex)
{
std::cout << “catch ice exception, ” << ex << std::endl;
return -1;
}
catch(…)
{
std::cout << “catch unknown exception.” << std::endl;
return -1;
}
int main(int argc, char** argv)
{
std::cout << “before to create Srv” << std::endl;
return createSrv();
}
接下來我們建立客戶端測試文件 testClient.cpp
#include #include #include “testice.h”
Ice::CommunicatorPtr??? pCommunicator;
Ice::ObjectPrx????????? pObject;
WG::WGTestIcePrx??????? pProxy;
const char* menu =
“*****************test ice client ***********************/n”
“/t 1 ping/n”
“/t 2 getTime/n”
“/t q quit/n”
“********************************************************/n”;
void showMenu()
{
std::cout << menu << std::endl;
return;
}
int doMenu()
{
int ret = 0;
char c;
std::string strTime;
showMenu();
c = getchar();
while(c != ‘q’)
{
switch (c)
{
case ‘1′:
ret = pProxy->ping();
std::cout << “ping ret is ” << ret << std::endl;
break;
case ‘2′:
ret = pProxy->GetTime(strTime);
std::cout << “GetTime ret is ” << ret << “retTime is ” << strTime << std::endl;
break;
case ‘q’:
return 0;
break;
default:
break;
}
sleep(2);
showMenu();
//system(”clear”);
c = getchar();
}
return 0;
}
int main(int argc, char** argv)
try
{
if (argc < 4)
{
std::cout << “argc less than 4. example is : testClient WGInterfaceAgentId 127.0.0.1 7788″ << std::endl;
return -1;
}
char buf[128] = {0};
snprintf(buf,sizeof(buf)-1,”%s:default -h %s -p %s”,argv[1], argv[2],argv[3]);
int tmpargc = 0;
char** tmpargv = NULL;
pCommunicator = Ice::initialize(tmpargc,tmpargv);
pObject = pCommunicator->stringToProxy(buf);
pProxy = WG::WGTestIcePrx::checkedCast(pObject);
return doMenu();
}
catch(const Ice::Exception& ex)
{
std::cout << “catch ice exception, ” << ex << std::endl;
return -1;
}
catch(…)
{
std::cout << “catch unknown exception. ” << std::endl;
return -1;
}
然后編譯SERVER_OBJS=testice.o/
testServer.o
CLIENT_OBJS=testice.o/
testClient.o
就可以測試了。
先啟動(dòng)server,再啟動(dòng)客戶端, 效果如下:
*****************test ice client ***********************
1 ping
2 getTime
q quit
********************************************************
2
GetTime ret is 0retTime is 2008-12-29 20:15:30
好,那么入門第一步就完成了。
總結(jié)
以上是生活随笔為你收集整理的ice库c语言例子,很不多的ICE架构入门学习例子的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 毛肚多少钱一斤啊?
- 下一篇: 高效快速中值滤波算法c语言,快速中值滤波