本文主要介绍一种构建ZeroC Ice demo的方法,该demo的构建过程中使用到了Ice提供的Ice::Application类。
1 Ice::Application介绍
“Ice::Application”类封装了所有的初始化和终止操作,用户在使用Ice::Application类时,就不需要过分关注初始化及相关异常,而将重点放在服务代码的实现上。
要使用Ice::Application的相关功能,需要继承Ice::Application类、并在派生类中对Ice::Application类中的纯虚方法run进行具体实现,最终的Ice服务接口都是在run方法定义的。
2 demo构建
说明:本文介绍的demo程序是在Centos 7上、使用C++编程语言开发的,对应的ice版本为“3.6.4”。
沿用博客ZeroC Ice介绍中已编写的基础框架文件(即:Hello.ice、Hello.h、Hello.cpp、HelloI.h、HelloI.cpp),我们继续编写继承Ice::Application类的demo中的剩余文件,这些剩余文件包括:1个继承Ice::Application类的服务端程序、服务端程序的配置文件、1个普通的客户端文件。
2.5 编写Ice服务器代码
说明:本节继承了博客ZeroC Ice介绍的部分内容,所以本节直接以2.5小节开始计数。
Ice的服务端代码继承了Ice::Application类。服务端代码(server.cpp)如下:
#include <Ice/Ice.h>
#include <HelloI.h>
using namespace std;
class HelloServer : public Ice::Application
{
public:
virtual int run(int, char*[]);
};
int HelloServer::run(int argc, char*[])
{
if(argc > 1)
{
cerr << appName() << ": too many arguments" << endl;
return EXIT_FAILURE;
}
Ice::ObjectAdapterPtr adapter = communicator()->createObjectAdapter("HelloAdapter");
Demo::HelloPtr hello = new HelloI;
adapter->add(hello, communicator()->stringToIdentity("hello"));
adapter->activate();
communicator()->waitForShutdown();
return EXIT_SUCCESS;
}
int main(int argc, char* argv[])
{
HelloServer app;
return app.main(argc, argv, "config.server");
}
服务端程序的配置文件(config.server)内容如下:
#
# The server creates one single object adapter with the name
# "HelloAdapter". The following line sets the endpoints for this
# adapter.
#
# When no -h <host> option is specified in the endpoints, the default
# value from the Ice.Default.Host property is used. If this property
# isn't set, the endpoints will listen on all available network
# interfaces.
#
HelloAdapter.Endpoints=tcp -p 10000
#
# Only listen on the localhost interface by default. You can comment
# out this property to allow listening on all available interfaces.
#
Ice.Default.Host=localhost
注意:本文到现在为止,介绍的几个文件(Hello.ice、Hello.h、Hello.cpp、HelloI.h、HelloI.cpp、server.cpp、config.server)为搭建继承Ice::Application类的Ice demo时需要使用的基础框架文件,对于其他需要继承Ice::Application类进行demo搭建的文章,如无特殊说明,均沿用了这七个基础文件。相关的demo搭建链接如下:
- 只是继承Ice::Application类搭建Ice demo,不涉及其他技术,请参考本文后续部分。
- 继承Ice::Application类搭建IceGrid的demo环境,相关文章点击此处。
2.6 编写客户端代码
客户端代码(client.cpp)如下:
#include <Ice/Ice.h>
#include <Hello.h>
using namespace std;
using namespace Demo;
int main(int argc, char* argv[])
{
int status = 0;
Ice::CommunicatorPtr ic;
try
{
ic = Ice::initialize(argc, argv);
Ice::ObjectPrx base = ic->stringToProxy("hello:tcp -h localhost -p 10000");
HelloPrx hello = HelloPrx::checkedCast(base);
if (!hello)
{
throw "Invalid proxy";
}
string result = "";
result = hello->SayHello("liitdar");
cout << "[ice_with_application] client's result: " << result << endl;
}
catch (const Ice::Exception& ex)
{
cerr << ex << endl;
status = 1;
}
catch (const char* msg)
{
cerr << msg << endl;
status = 1;
}
if (ic)
{
ic->destroy();
}
return status;
}
2.7 编译生成客户端和服务端的应用程序
编译生成服务端应用程序(server),命令如下:
g++ -o server -I. server.cpp Hello.cpp HelloI.cpp -lIce -lIceUtil -lpthread
编译生成客户端应用程序(client),命令如下:
g++ -o client -I. client.cpp Hello.cpp -lIce -lIceUtil -lpthread
2.8 运行服务端与客户端程序
在一个终端运行服务端程序,如下:
./server
新打开一个终端,运行客户端程序,如下:
./client
正常情况下,我们能够在上面的两个终端中看到服务端与客户端的信息交互情况,如下:
【服务端】:
【客户端】:
如果两个终端中出现了上述信息,说明demo程序部署成功了。