ICE Service使用方法简介
最近研究了一下网络通信中间件ICE的使用,粗通其皮毛,按照官方手册依葫芦画瓢写了一个程序员都喜闻乐见的“Hello World”程序,服务端和客户端均用C++开发,通讯协议使用默认的TCP。感觉ICE的大致好处有以下两点:
1. 平台无关性。无论客户端或者服务端均可用现在流行的开发语言(C++ /JAVA/C#/php)进行开发,并且屏蔽语言差异性。现在比较流行的方式是客户端用C#开发,与用C++开发的服务端直接通讯。
2. 通讯协议多样性。现在可选择TCP、UDP、HTTP进行通讯,如果对安全要求较高,可选择SSL对传输的数据进行加密。
另外ICE还提供一些扩展组件,实现网络通讯的负载均衡(ICEGrid),通讯节点统一管理(ICEBOX),程序自动更新(ICEPatch)等,方便应用扩展。
如果使用过ICE的人都知道,在实现服务端或者客户端的时候通常都要写一些“公式化”的代码,负责Ice通信器初始化、异常捕获,以及应用终止后的销毁。如下所示:
1
int status=0;
2
3
Ice::CommunicatorPtr ic;
4
5
try
6
7
{
8
9
ic = Ice::initialize(argc,argv);
10
11
Ice::ObjectAdapterPtr adapter =
12
13
ic->createObjectAdapterWithEndpoints("SayHelloAdapter","tcp -h 127.0.0.1 -p 10000");
14
15
Ice::ObjectPtr object = new HelloICEI;
16
17
adapter->add(object,ic->stringToIdentity("SimpleHello"));
18
19
adapter->activate();
20
21
ic->waitForShutdown();
22
23
}
24
25
catch (const Ice::Exception & e)
26
27
{
28
29
cerr << e << endl;
30
31
status = 1;
32
33
}
34
35
catch (const char * msg)
36
37
{
38
39
cerr << msg << endl;
40
41
status = 1;
42
43
}
44
45
if ( ic )
46
47
{
48
49
try
50
51
{
52
53
ic->destroy();//关闭ICE
54
55
}
56
57
catch (const Ice::Exception & e)
58
59
{
60
61
cerr << e << endl;
62
63
status = 1;
64
65
}
66
67
}
68
69
如果每次都要写这么多的话,第一浪费时间和精力,第二不能将注意力集中到业务逻辑上。还有一点就是对代码的整洁和易读方面做的不够好(本人略微有些代码“洁癖”)。
阅读官方手册,发现ICE提供两个工具类封装了这些“公式化”逻辑,分别是“Application”和“Service”。
Ice::Application本身是一个抽象类,其run()函数为纯虚函数,因此必须被继承后使用。
Ice::Application 是一个单体(singleton)类,会创建单个通信器。如果你要使用多个通信器,不能使用Ice::Application来定义多个App。而至多定义一个App的实例。
其它通信器需要使用Ice::initialize()手工生成。
一般而言,Ice::Application 类对于Ice 客户和服务器来说已经非常方便,但在有些情况下,应用可能需要作为Unix 看守(daemon)或Win32 服务运行在系统一级。对于这样的情况,Ice 提供了Ice::Service。一个可与Ice::Application 相比的单体类,但它还封装了低级的、 针对特定平台的初始化和关闭步骤――系统服务常常需要使用这样的步骤。
下面介绍Ice::Service的使用方法,仅介绍开发步骤,后面附上具体例子下载。
1. 需要在服务端中引用Ice/Service.h头文件。本人曾在此浪费了2个小时。
2. 新建一个类继承Ice::Service,并实现其中的三个虚函数。代码如下:
1
class MyService : public Ice::Service
2
3

{
4
5
protected:
6
7
virtual bool start(int, char *[],int&);
8
9
virtual bool stop();
10
11
virtual void interrupt();
12
13
private:
14
15
Ice::ObjectAdapterPtr m_adapter;
16
17
};
18
19
void MyService::interrupt()
20
21

{
22
23
std::cout << "Receive signal
" << std::endl;
24
25
Ice::Service::interrupt();
26
27
}
28
29
bool MyService::stop()
30
31

{
32
33
std::cout << "Stop running
" << std::endl;
34
35
return true;
36
37
}
38
39
bool MyService::start(int argc, char * argv[],int& status)
40
41

{
42
43
std::string endpoint = "tcp -h localhost -p 10000";
44
45
m_adapter = communicator()->createObjectAdapterWi thEndpoints("SimpleHelloAdapter", endpoint);
46
47
Ice::ObjectPtr object = new HelloICEI;
48
49
m_adapter->add(object,communicator()->stringToIdentity("SimpleHello"));
50
51
m_adapter->activate();
52
53
return true;
54
55
}
56
57
3. 在main函数中启动服务。此处需要注意对于服务来说最常见的有安装服务、卸载服务、启动服务、停止服务四个操作。这四个操作分别通过启动参数来控制。以Win32平台为例:
--service NAME
作为名叫NAME 的Windows 服务启动。在传给start 成员函数的参数向量中,这个选项会被移除。
但是,在应用作为Windows 服务运行之前,它必须先被安装,因此,Ice::Service 类还支持另外一些的命令行选项,用于执行管理活动:
--install NAME [--display DISP] [--executable EXEC][ARG ...]
安装NAME 服务。如果指定了--display 选项,就把DISP 用作服务的显示名,否则就使用NAME。如果指定了--executable 选项,就把EXEC 用作服务的可执行路径名,否则就使用可执行文件的路径名来调用--install。其他任何参数都会不加改变地传给Service::start 成员函数。注意,在启动时传给服务的参数集中,这个命令会自动增加命令行参数--service NAME,因此,你不需要显式地指定这些选项。
--uninstall NAME
移除NAME 服务。如果服务目前是活动的,在反安装之前,必须先使它停止。
--start NAME [ARG ...]
启动NAME 服务。其他任何参数都会不加改变地传给Service::start 成员函数。
--stop NAME
停止NAME 服务。如果指定的管理命令不止一个,或者在使用--service 的同时还使用了管理命令,就会发生错误。在执行了管理命令之后,程序会立即终止。Ice::Service 类支持Windows 服务控制代码SERVICE_CONTROL_INTERROGATE 和SERVICE_CONTROL_STOP。在收到SERVICE_CONTROL_STOP 时,Ice::Service 会调用shutdown 成员函数。
在Visual Studio中设置启动参数—install MyService,可在系统服务中注册名为“MyService”的服务。
启动服务端程序可以在系统服务列表中看到MyService已经作为服务注册进去了。
4. 至此ICE的服务端即可作为系统服务的形式存在与客户端进行通讯了。
示例代码下载
posted on 2011-07-10 13:10
三夏健 阅读(1946)
评论(3) 编辑 收藏 引用 所属分类:
ICE