最近学习了异步通信这一章:异步包括服务端的异步分派(AMD)以及客户端的异步调用(AMI)。
下面介绍一下如何实现简单的异步通信(主要实现了客户端的异步调用,AMD暂未体现):
1.首先编写Ice文件:Demo.ice
module Demo{
interface Employee{
["amd"]void add(int a, int b, out int sum);
};
};
["amd"]表示的是元数据,指定服务器端将提供异步分派的方法函数。该add函数中有两个in参数;一个out参数,作为回传参数。
2.映射到C++代码:使用命令 slice2cpp Demo.ice
经过映射之后,程序将生成Demo.h与Demo.cpp文件,如下面部分代码所示Demo.h
// ....More code before
namespace Demo
{
class Employee : virtual public ::Ice::Object
{
public:
typedef EmployeePrx ProxyType;
typedef EmployeePtr PointerType;
virtual ::Ice::ObjectPtr ice_clone() const;
virtual bool ice_isA(const ::std::string&, const ::Ice::Current& = ::Ice::Current()) const;
virtual ::std::vector< ::std::string> ice_ids(const ::Ice::Current& = ::Ice::Current()) const;
virtual const ::std::string& ice_id(const ::Ice::Current& = ::Ice::Current()) const;
static const ::std::string& ice_staticId();
virtual void add_async(const ::Demo::AMD_Employee_addPtr&, ::Ice::Int, ::Ice::Int, const ::Ice::Current& = ::Ice::Current()) = 0;
// ... More code here
};
// ...
3. 实现服务端程序
新建Server.cpp文件,将Demo.h中的虚方法add_async()实现。并新建servant类来继承Ice::application类,初始化Ice run time ,同时将servant注册到对象适配器中去。
#include <Demo.h>
#include <Ice/Application.h>
#include <IceUtil/Monitor.h>
#include <iostream>
using namespace std;
using namespace Demo;
class Manager : virtual public Employee,public IceUtil::Monitor<IceUtil::Mutex>
{
public:
Manager():_sum(0){}
~Manager(){}
virtual void add_async(const ::Demo::AMD_Employee_addPtr& add_ptr, int a, int b, const ::Ice::Current& =::Ice::Current())
{
_sum = a+b;
sleep(1);
cout<< "The sum is " <<_sum<<"."<<endl;
add_ptr->ice_response(_sum); //数据处理完之后,回调函数,调用客户端实现的方法
}
private:
int _sum;
};
class myApplication : virtual public Ice::Application
{
public:
virtual int run(int argc , char* argv[])
{
Ice::ObjectAdapterPtr adapter =
communicator()->createObjectAdapterWithEndpoints("GameServer", "default -p 10000");
Ice::ObjectPtr Manptr = new Manager;
adapter->add(Manptr, communicator()->stringToIdentity("ManServer"));//将servant类对象指针ManPtr注册到适配器中
adapter->activate(); //激活适配器,使客户端能够访问适配器中已添加的servant活动表
communicator()->waitForShutdown();
}
};
int main(int argc , char * argv[])
{
myApplication app;
app.main(argc , argv);
return 0;
}
4.实现客户端程序
新建Client.cpp文件,实现回调函数,创建访问指定servant的代理。
#include<Ice/Ice.h>
#include<Demo.h>
#include<iostream>
#include<IceUtil/IceUtil.h>using namespace std;
using namespace Demo;class Client_add:public AMD_Employee_add
{
public:
virtual void ice_response(int sum)
{
cout << "receive from server--The sum is "<<sum<<"."<<endl;
}
virtual void ice_exception(){}
virtual void ice_exception(const std::exception& e){}
void failed_operation(const Ice::Exception &e){}
};
typedef IceUtil::Handle<Client_add> ClientAddPtr;class ClientApp : virtual public Ice::Application
{
public:
virtual int run(int argc , char* argv[])
{
Ice::ObjectPrx base = communicator()->stringToProxy("ManServer:default -p 10000"); EmployeePrx emp = EmployeePrx::checkedCast(base); if(!emp)throw "Invalid Proxy!"; ClientAddPtr amd_add = new Client_add;
Callback_Employee_addPtr addPtr=newCallback_Employee_add(amd_add,&Client_add::ice_response,&Client_add::failed_operation); //实现回调对象指针
emp->begin_add(10,20,addPtr); //第三个参数为回调类的智能指针 cout<<"I'm waiting for the data from,but not blocked."<<endl; }
};int main(int argc , char* argv[])
{
ClientApp app;
app.main(argc , argv); return 0;
}
5. 编译Server.cpp: g++ Demo.cpp Server.cpp -o Server -lIce -lIceUtil -I./ -I/opt/Ice-3.4.2/include -L/opt/Ice-3.4.2/lib
编译Client.cpp: g++ Demo.cpp Client.cpp -o Client -lIce -lIceUtil -I./ -I/opt/Ice-3.4.2/include -L/opt/Ice-3.4.2/lib
运行服务端:./Server 运行客户端:./Client