• ICE入门实例──ping和获取远程时间


    虽然使用传统的SOCKET编程,我们可以更为清楚程序的性能,能够更直接的操控SOCKET的设置,比如发送超时时间,接受BUFFER的大小,以及进行自己的协议加密。但是由于其调试成本较高,且不易于分布式部署

    ICE 作为一个中间件技术,已经得到越来越广泛的应用。在 ICE3.2 以后,由于采用了epoll 模型,其通信层的性能提升较为明显。而且其相较于SOCKET编程一个很明显的优势是便于调试。这对于程序员来说,无疑是很大的诱惑。

    网上介绍ICE原理的文献很多,我就不废话。先从一个简单的实例开始,进行我们的ICE编程入门之旅:

    建立一个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

    我们需要建立一个客户端和一个服务端。

    服务端文件如下:

    testiceImpl.h

    #include <iostream>
    #include <string>
    #include “testice.h”
    #include <sys/time.h>
    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

    上面这个类派生了接口类,用做服务端的实现。

    再建立一个主函数文件testServer.cpp’

    #include <Ice/Ice.h>
    #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 <iostream>
    #include <Ice/Ice.h>
    #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

    就可以测试了。

    先启动server,再启动客户端, 效果如下:

    *****************test ice client ***********************
             1 ping
             2 getTime
             q quit
    ********************************************************

    2
    GetTime ret is 0retTime is 2008-12-29 20:15:30

    好,那么入门第一步就完成了。

  • 相关阅读:
    [BZOJ3202][SDOI2013]项链
    Educational Codeforces Round 50
    [agc23E]Inversions
    [CF1016G]Appropriate Team
    [CF765F]Souvenirs
    [Luogu3733][HAOI2017]八纵八横
    [Luogu4609][FJOI2016]建筑师
    [BZOJ2159]Crash 的文明世界
    【学习笔记】Nim积
    PKUWC2020游记
  • 原文地址:https://www.cnblogs.com/buffer/p/1613581.html
Copyright © 2020-2023  润新知