ACE_Service_Manager
是可用于对服务配置器进行远程管理的服务。它目前可以接受两种类型的请求。其一,你可以向它发送“help”消息,列出当前被加载进应用的所有服务。其二,你可以向服务管理器发送“reconfigure”消息,从而使得服务配置器重新配置它自己。下面的例子演示了一个客户,它向服务管理器发送这两种类型的命令。
例
8-2#include "ace/OS.h"
#include "ace/SOCK_Stream.h"
#include "ace/SOCK_Connector.h"
#include "ace/Event_Handler.h"
#include "ace/Get_Opt.h"
#include "ace/Reactor.h"
#include "ace/Thread_Manager.h"
#define BUFSIZE 128
class Client: public ACE_Event_Handler
{
public:
~Client()
{
ACE_DEBUG((LM_DEBUG,"Destructor \n"));
}
//Constructor
Client(int argc, char *argv[]): connector_(), stream_()
{
//The user must specify address and port number
ACE_Get_Opt get_opt(argc,argv,"a:p:");
for(int c;(c=get_opt())!=-1;)
{
switch(c)
{
case ’a’:
addr_=get_opt.optarg;
break;
case ’p’:
port_= ((u_short)ACE_OS::atoi(get_opt.optarg));
break;
default:
break;
}
}
address_.set(port_,addr_);
}
//Connect to the remote machine
int connect()
{
connector_.connect(stream_,address_);
ACE_Reactor::instance()->
register_handler(this,ACE_Event_Handler::READ_MASK);
return 0;
}
//Send a list_services command
int list_services()
{
stream_.send_n("help",5);
return 0;
}
//Send the reconfiguration command
int reconfigure()
{
stream_.send_n("reconfigure",12);
return 0;
}
//Handle both standard input and remote data from the
//ACE_Service_Manager
int handle_input(ACE_HANDLE h)
{
"Error in receiving from remote\n"));char buf[BUFSIZE];
//Got command from the user
if(h== ACE_STDIN)
{
int result = ACE_OS::read (h, buf, BUFSIZ);
if (result == -1)
ACE_ERROR((LM_ERROR,"can’t read from STDIN"));
else if (result > 0)
{
//Connect to the Service Manager
this->connect();
if(ACE_OS::strncmp(buf,"list",4)==0)
this->list_services();
else if(ACE_OS::strncmp(buf,"reconfigure",11)==0)
this->reconfigure();
}
return 0;
}
//We got input from remote
else
{
switch(stream_.recv(buf,BUFSIZE))
{
case -1:
//ACE_ERROR((LM_ERROR,
ACE_Reactor::instance()->remove_handler(this,
ACE_Event_Handler::READ_MASK);
return 0;
case 0:
return 0;
default:
ACE_OS::printf("%s",buf);
return 0;
}
}
}
//Used by the Reactor Framework
ACE_HANDLE get_handle() const
{
return stream_.get_handle();
}
//Close down the underlying stream
int handle_close(ACE_HANDLE,ACE_Reactor_Mask)
{
return stream_.close();
}
private:
ACE_SOCK_Connector connector_;
ACE_SOCK_Stream stream_;
ACE_INET_Addr address_;
char *addr_;
u_short port_;
};
int main(int argc, char *argv[])
{
Client client(argc,argv);
//Register the the client event handler as the standard
//input handler
ACE::register_stdin_handler(&client,
ACE_Reactor::instance(),
ACE_Thread_Manager::instance());
ACE_Reactor::run_event_loop();
}
在此例中,
Client类是一个事件处理器,它处理两种类型的事件:来自用户的标准输入事件和来自ACE_Service_Manager的回复。如果用户输入“list”或“reconfigure”命令,相应的消息就被发送到远地的ACE_Service_manager。服务管理器随之回复以当前配置的服务的列表或是“done”(指示服务的重配置已经完成)。因为ACE_Service_Manager是一个服务,你可以在svc.conf文件中指定你是想要静态还是动态地加载此服务,并使用服务配置构架来将它加载入应用中。例如,下面的命令指定在
9876端口静态地启动服务管理器:
static ACE_Service_Manager “-p 9876”
