zeroc-ice的全双工通信策略

简介: 在项目中,往往很多时候涉及全双工通信要求,zeroc-ice样例介绍很多异步通信的策略, 但我最近项目需求中,不仅是要全双工通信,还要求服务端需要明确每个客户端及区别对待, 所以需要给每个客户端做标记处理...

在项目中,往往很多时候涉及全双工通信要求,zeroc-ice样例介绍很多异步通信的策略,

但我最近项目需求中,不仅是要全双工通信,还要求服务端需要明确每个客户端及区别对待,

所以需要给每个客户端做标记处理

1)ice定义

#pragma once
#include <Ice/Identity.ice>

module TestIce
{

struct DateTimeI {
    int    isec;
    int imsec;
};

interface ClientAchieve
{

   //客户端实现

    void PValueChange_A(long devID,long pID, DateTimeI itime, float val);//A类客户端实现

    void PValueChange_B(long devID,long pID, DateTimeI itime, float val);//B类客户端实现

};

interface ServerAchieve
{

    //服务端实现

    void AddClient(::Ice::Identity ident, int ctype);
    void setPValue(long devID, long pID, float val);
};

};

2)接口实现:

在我的ice定义中

void AddClient(::Ice::Identity ident, int ctype);是很关键的一项,该函数是有服务端实现,

在客户端链接成功将其识别号及类型告知服务端,服务端将进行标记,该逻辑过程如下:

客户端链接及通告服务端代码样例

                Ice::ObjectAdapterPtr adapter = communicator()->createObjectAdapter("");
                Ice::Identity ident;
                ident.name = IceUtil::generateUUID();
                m_strUUID = ident.name;
                ident.category = "";
                ClientAchievePtr crtwoway = new ClientAchieveI(this);
                adapter->add(crtwoway, ident);
                adapter->activate();
                soneway->ice_getConnection()->setAdapter(adapter);
                soneway->AddClient(ident,ctype);//通知服务端

而在服务端,我们将进行标记每个客户端,示例代码:

void ServerAchieveI::AddClient(const ::Ice::Identity& ident,const ::Ice::Int &ctype, const ::Ice::Current& current)
{
      printf("adding client `%s(%d)'\n", _communicator->identityToString(ident).c_str(),ctype);
     TestIce::ClientAchievePrx client = PCS::ClientAchievePrx::uncheckedCast(current.con->createProxy(ident));

    //clients定义了一个客户端管理类,采用std::map<TestIce::ClientAchievePrx, ClientFlag> _clients;进行客户端标记

     clients->addCLient(client,ctype);
};


现在我们展示服务端接口代码示例:

class ClientManageThread;

class ServerAchieveI : public TestIce::ServerAchieve
{
public:
    ServerAchieveI(const Ice::CommunicatorPtr& communicator);
    virtual ~ServerAchieveI();
    //
    virtual void AddClient(const ::Ice::Identity&, const ::Ice::Int &ctype,const ::Ice::Current& = ::Ice::Current());
    virtual void setPValue(::Ice::Long devID, ::Ice::Long pID, float val, const ::Ice::Current&);
private:
    // Required to prevent compiler warnings with MSVC++
    ServerAchieveI& operator=(const ServerAchieveI&);

    Ice::CommunicatorPtr _communicator;
    ClientManageThread *clients;
};

/////////////////////////////////////
ServerAchieveI::ServerAchieveI(const Ice::CommunicatorPtr& communicator)
    : _communicator(communicator)
{
    clients = new ClientManageThread();
    clients->start();
};

ServerAchieveI::~ServerAchieveI()
{
    try
    {
        delete clients;
        clients = NULL;
    }
    catch (...)
    {
        
    }
};

void ServerAchieveI::AddClient(const ::Ice::Identity& ident, const ::Ice::Int &ctype,const ::Ice::Current& current)
{
      printf("adding client `%s(%d)'\n", _communicator->identityToString(ident).c_str(),ctype);
    TestIce::ClientAchievePrx client = TestIce::ClientAchievePrx::uncheckedCast(current.con->createProxy(ident));
    clients->addCLient(client,ctype);//clients根据ctype类型调用TestIce::ClientAchievePrx的客户端实现函数发送数据到指定客户端
};
//客户端调用该函数实现客户端到服务端的数据发送,soneway->setPValue(devID,pID,val);
void ServerAchieveI::setPValue(::Ice::Long devID, ::Ice::Long pID, float val, const ::Ice::Current& )
{
    std::cerr << " setPValue:"<< devID<<","<<pID<<","<<val << std::endl;
    PFrom _pfrom;
};

展示客户端代码样例,例如该客户端只需实现A类型函数:

///////////////////////////////h////////////////////////////////////////////////////////////////

class TestIceClient;

class ClientAchieveI : public TestIce::ClientAchieve
{
public:
    ClientAchieveI(TestIceClient* _client);
    ~ClientAchieveI();
    virtual void PValueChange_A(::Ice::Long devID
                              ,::Ice::Long pID
                              , const ::TestIce::DateTimeI& itime
                              , ::Ice::Float val
                              , const ::Ice::Current&);
private:
    TestIceClient* client;//真正实现类
};

/////////////////////////////////////////////cpp/////////////////////////////////////

ClientAchieveI::ClientAchieveI(TestIceClient* _client) : client(_client)
{
};

ClientAchieveI::~ClientAchieveI()
{

};

void ClientAchieveI::PValueChange(::Ice::Long devID
                              ,::Ice::Long pID
                              , const ::TestIce::DateTimeI& itime
                              , ::Ice::Float val
                              , const ::Ice::Current&)
{

    std::cerr <<" PValueChange:" <<devID<<","<<pID<<","<<val<<","<<itime.isec<<","<<itime.imsec<< std::endl;

    if(client)
        client->PValueChange(devID,pID,itime,val);
};

3)实现调用示例:

在我的ClientManageThread类中,实现数据从服务端发送各客户端:

            mutex_client.Lock();
            std::map<TestIce::ClientAchievePrx, int>::iterator itw = _clients.begin();
            while(itw != _clients.end())
            {
                try
                {

                   switch(it->second)
                    {
                        case 1:
                            itw->first->PValueChange_A(wdlc.devID, wdlc.pID, _itime, wdlc.val);
                        break;
                        case 2:
                            itw->first->PValueChange_B(wdlc.devID, wdlc.pID, _itime, wdlc.val);
                        break;
                        default:
                        break;
                    }
                    itw++;
                }
                catch (...)
                {
                    printf("PValueChange Error:%d\n",static_cast<int>(time(NULL)));
#ifdef WIN32
                    itw = _clients.erase(itw);
#else
                    std::map<TestIce::ClientAchievePrx, int>::iterator ittmp = itw++;
                    _clients.erase(ittmp);
#endif
                }
            }
            mutex_client.Unlock();

在客户端调用与已很多zeroc-ice的demoo一致:

//TestIce::ServerAchievePrx            soneway;

    if(connect())
    {
        try{            soneway->setPValue(devID,pID,val);
        }catch(...)
        {
            disconnect();
        }
    }


本文只是抛出一个zeroc-ice做全双工通信的思路,所展示代码只是具体代码中择取的代码,功能并不完善,大家可按自身需求修改完善适合自身项目诉求

目录
相关文章
|
7月前
数据通信方式
数据通信方式。
86 2
|
1月前
|
监控
嵌入式面试题:数据传输单工,半双工,全双工之间的区别
嵌入式面试题:数据传输单工,半双工,全双工之间的区别
14 0
|
2月前
A-B 通信模块如何与串行设备通信?
A-B 通信模块如何与串行设备通信?
|
10月前
串口通讯,三种数据传输方式介绍
串口通讯,三种数据传输方式介绍
129 0
|
8月前
|
安全 物联网
NFC通信基本原理 主动和被动通信
NFC通信基本原理 主动和被动通信
|
8月前
|
存储 域名解析 网络协议
LinuxUDP通讯
学习网络通讯时最主要的一个内容就是UDP通讯
104 0
|
10月前
|
芯片
|
11月前
|
存储 网络协议 物联网
LoRA转4G网关DLS11低功耗数据转发器工作原理
DLS11 是为 VS系列振弦采集仪研发的内置电池以及 LoRA、LTE(4G)无线的低功耗数据转发器。利用“实时在线”的 LoRA 收发器收集其它 LoRA 设备发送的数据并存储,定时启动将这些 存储的数据重新打包为标准的数据包经由 LTE 网络发送致远端服务器,数据发送方式有短信、TCP、邮件、FTP 等。DLS11 实 现了 VS振弦采集仪设备的现场组网,使用一张 SIM 卡即可实现多台 VS振弦采集仪设备的数据远传功能。
|
监控 网络协议 定位技术
完成端口技术在GPRS通信的应用
完成端口技术在GPRS通信的应用 .当具有接入GSM网络的设备是通过GPRS接入到INTERNET的。这个特性被用到了很多的行业,例如GPS卫星定位系统/远程抄表系统/电网监控系统中等等。
|
传感器 存储 编解码
通信专业英语精练
通信专业英语精练
134 0
通信专业英语精练