ACE_Service_Manager Class Reference

Provide a standard ACE service for managing all the services configured in an ACE_Service_Repository. More...

#include <Service_Manager.h>

Inheritance diagram for ACE_Service_Manager:

Inheritance graph
[legend]
Collaboration diagram for ACE_Service_Manager:

Collaboration graph
[legend]
List of all members.

Public Member Functions

 ACE_Service_Manager (void)
 Constructor.
virtual ~ACE_Service_Manager (void)
 Destructor.

Protected Member Functions

virtual int reconfigure_services (void)
virtual int list_services (void)
virtual int init (int argc, ACE_TCHAR *argv[])
 Initializes object when dynamic linking occurs.
virtual int info (ACE_TCHAR **info_string, size_t length) const
 Returns information on a service object.
virtual int fini (void)
 Terminates object when dynamic unlinking occurs.
virtual int suspend (void)
 Temporarily disable a service without removing it completely.
virtual int resume (void)
 Re-enable a previously suspended service.
void dump (void) const
 Dump the state of an object.
int open (const ACE_INET_Addr &sia)
virtual ACE_HANDLE get_handle (void) const
 Get the I/O handle.
virtual int handle_input (ACE_HANDLE fd)
 Called when input events occur (e.g., connection or data).
virtual int handle_close (ACE_HANDLE fd, ACE_Reactor_Mask)
virtual int handle_signal (int signum, siginfo_t *, ucontext_t *)
virtual void process_request (ACE_TCHAR *request)
 Handle one request.

Protected Attributes

 ACE_ALLOC_HOOK_DECLARE
 Declare the dynamic allocation hooks.
ACE_SOCK_Stream client_stream_
ACE_SOCK_Acceptor acceptor_
 Acceptor instance.
bool debug_
 Keep track whether we debug or not.
int signum_
 The signal used to trigger reconfiguration.

Static Protected Attributes

static u_short DEFAULT_PORT_ = 10000
 Default port for the Acceptor to listen on.

Detailed Description

Provide a standard ACE service for managing all the services configured in an ACE_Service_Repository.

This implementation is simple and just handles each client request one at a time. There are currently 3 types of requests:

Each request is associated with a new connection, which is closed when the request is processed. In addition, you must be using the singleton <ACE_Reactor::instance> in order to trigger reconfigurations.

Definition at line 52 of file Service_Manager.h.


Constructor & Destructor Documentation

ACE_Service_Manager::ACE_Service_Manager ( void   ) 

Constructor.

Definition at line 35 of file Service_Manager.cpp.

References ACE_TRACE.

00036   : debug_ (false),
00037     signum_ (SIGHUP)
00038 {
00039   ACE_TRACE ("ACE_Service_Manager::ACE_Service_Manager");
00040 }

ACE_Service_Manager::~ACE_Service_Manager ( void   )  [virtual]

Destructor.

Definition at line 42 of file Service_Manager.cpp.

References ACE_TRACE.

00043 {
00044   ACE_TRACE ("ACE_Service_Manager::~ACE_Service_Manager");
00045 }


Member Function Documentation

ACE_BEGIN_VERSIONED_NAMESPACE_DECL void ACE_Service_Manager::dump ( void   )  const [protected]

Dump the state of an object.

Definition at line 24 of file Service_Manager.cpp.

References ACE_TRACE.

00025 {
00026 #if defined (ACE_HAS_DUMP)
00027   ACE_TRACE ("ACE_Service_Manager::dump");
00028 #endif /* ACE_HAS_DUMP */
00029 }

int ACE_Service_Manager::fini ( void   )  [protected, virtual]

Terminates object when dynamic unlinking occurs.

Reimplemented from ACE_Shared_Object.

Definition at line 142 of file Service_Manager.cpp.

References ACE_Event_Handler::ACCEPT_MASK, ACE_TRACE, ACE_Event_Handler::DONT_CALL, handle_close(), ACE_Reactor::instance(), ACE_Event_Handler::NULL_MASK, and ACE_Reactor::remove_handler().

00143 {
00144   ACE_TRACE ("ACE_Service_Manager::fini");
00145 
00146   int retv = 0;
00147   if (this->get_handle () != ACE_INVALID_HANDLE)
00148     {
00149       retv = ACE_Reactor::instance ()->remove_handler
00150         (this,
00151          ACE_Event_Handler::ACCEPT_MASK |
00152          ACE_Event_Handler::DONT_CALL);
00153       this->handle_close (ACE_INVALID_HANDLE,
00154                           ACE_Event_Handler::NULL_MASK);
00155     }
00156   return retv;
00157 }

ACE_HANDLE ACE_Service_Manager::get_handle ( void   )  const [protected, virtual]

Get the I/O handle.

Reimplemented from ACE_Event_Handler.

Definition at line 160 of file Service_Manager.cpp.

References acceptor_, ACE_TRACE, and ACE_IPC_SAP::get_handle().

00161 {
00162   ACE_TRACE ("ACE_Service_Manager::get_handle");
00163   return this->acceptor_.get_handle ();
00164 }

int ACE_Service_Manager::handle_close ( ACE_HANDLE  fd,
ACE_Reactor_Mask   
) [protected, virtual]

Called when a <handle_*()> method returns -1 or when the <remove_handler> method is called on an ACE_Reactor. The close_mask indicates which event has triggered the <handle_close> method callback on a particular handle.

Reimplemented from ACE_Event_Handler.

Definition at line 135 of file Service_Manager.cpp.

References acceptor_, ACE_TRACE, and ACE_SOCK_Acceptor::close().

Referenced by fini().

00136 {
00137   ACE_TRACE ("ACE_Service_Manager::handle_close");
00138   return this->acceptor_.close ();
00139 }

int ACE_Service_Manager::handle_input ( ACE_HANDLE  fd  )  [protected, virtual]

Called when input events occur (e.g., connection or data).

Reimplemented from ACE_Event_Handler.

Definition at line 276 of file Service_Manager.cpp.

References ACE_DEBUG, ACE_TEXT, ACE_TRACE, client_stream_, debug_, ACE_INET_Addr::get_host_name(), ACE_INET_Addr::get_port_number(), ACE_Reactor::instance(), LM_DEBUG, LM_ERROR, process_request(), ACE_SOCK_IO::recv(), ACE_Reactor::register_handler(), SIGPIPE, ACE_OS::strchr(), and ACE_Reactor::uses_event_associations().

00277 {
00278   ACE_TRACE ("ACE_Service_Manager::handle_input");
00279 
00280   // Try to find out if the implementation of the reactor that we are
00281   // using requires us to reset the event association for the newly
00282   // created handle. This is because the newly created handle will
00283   // inherit the properties of the listen handle, including its event
00284   // associations.
00285   int reset_new_handle =
00286     ACE_Reactor::instance ()->uses_event_associations ();
00287 
00288   if (this->acceptor_.accept (this->client_stream_, // stream
00289                               0, // remote address
00290                               0, // timeout
00291                               1, // restart
00292                               reset_new_handle  // reset new handler
00293                               ) == -1)
00294     return -1;
00295 
00296   if (this->debug_)
00297     {
00298       ACE_DEBUG ((LM_DEBUG,
00299                   ACE_TEXT ("client_stream fd = %d\n"),
00300                  this->client_stream_.get_handle ()));
00301       ACE_INET_Addr sa;
00302       if (this->client_stream_.get_remote_addr (sa) == -1)
00303         return -1;
00304 
00305       ACE_DEBUG ((LM_DEBUG,
00306                   ACE_TEXT ("accepted from host %C at port %d\n"),
00307                   sa.get_host_name (),
00308                   sa.get_port_number ()));
00309     }
00310 
00311   ACE_TCHAR request[BUFSIZ];
00312   ACE_TCHAR* offset = request;
00313   ssize_t remaining = sizeof (request);
00314 
00315   // Read service request from client.
00316 
00317   ssize_t result;
00318 
00319   // Keep looping until we actually get the request.  Note that Win32
00320   // sets the socket into non-blocking mode, so we may need to loop if
00321   // the system is heavily loaded.  Read bytes into the buffer until a
00322   // '\n' or '\r' is found in the buffer, otherwise the buffer
00323   // contains an incomplete string.
00324 
00325   int error;
00326   do
00327     {
00328       result = client_stream_.recv (offset, remaining);
00329       error = errno;
00330       if (result == 0 && error != EWOULDBLOCK)
00331         remaining = 0;
00332 
00333       if (result >= 0)
00334         {
00335           if ((remaining -= result) <= 0)
00336             {
00337               ACE_DEBUG ((LM_ERROR,
00338                           ACE_TEXT ("Request buffer overflow.\n")));
00339               result = 0;
00340               break;
00341             }
00342 
00343           offset += result;
00344           *offset = 0;
00345 
00346           if (ACE_OS::strchr (request, '\r') != 0
00347               || ACE_OS::strchr (request, '\n') != 0)
00348             remaining = 0;
00349         }
00350     }
00351   while (result == -1 && error == EWOULDBLOCK || remaining > 0);
00352 
00353   switch (result)
00354     {
00355     case -1:
00356       if (this->debug_)
00357         ACE_DEBUG ((LM_ERROR,
00358                     ACE_TEXT ("%p\n"),
00359                     ACE_TEXT ("recv")));
00360       break;
00361     case 0:
00362       return 0;
00363       /* NOTREACHED */
00364     default:
00365       {
00366         ACE_Event_Handler *old_signal_handler = 0;
00367         ACE_Reactor::instance ()->register_handler (SIGPIPE,
00368                                                     this,
00369                                                     0,
00370                                                     &old_signal_handler);
00371 
00372         this->process_request (request);
00373 
00374         // Restore existing SIGPIPE handler
00375         ACE_Reactor::instance ()->register_handler (SIGPIPE,
00376                                                     old_signal_handler);
00377       }
00378     }
00379 
00380   if (this->client_stream_.close () == -1 && this->debug_)
00381     ACE_DEBUG ((LM_ERROR,
00382                 ACE_TEXT ("%p\n"),
00383                 ACE_TEXT ("close")));
00384   return 0;
00385 }

int ACE_Service_Manager::handle_signal ( int  signum,
siginfo_t ,
ucontext_t  
) [protected, virtual]

Called when object is signaled by OS (either via UNIX signals or when a Win32 object becomes signaled).

Reimplemented from ACE_Event_Handler.

Definition at line 167 of file Service_Manager.cpp.

00168 {
00169   return 0;
00170 }

int ACE_Service_Manager::info ( ACE_TCHAR **  info_string,
size_t  length 
) const [protected, virtual]

Returns information on a service object.

Reimplemented from ACE_Shared_Object.

Definition at line 73 of file Service_Manager.cpp.

References ACE_TEXT, ACE_TRACE, ACE_INET_Addr::get_port_number(), ACE_OS::sprintf(), ACE_OS::strdup(), ACE_OS::strlen(), and ACE_OS::strsncpy().

00074 {
00075   ACE_TRACE ("ACE_Service_Manager::info");
00076   ACE_INET_Addr sa;
00077   ACE_TCHAR buf[BUFSIZ];
00078 
00079   if (this->acceptor_.get_local_addr (sa) == -1)
00080     return -1;
00081 
00082   ACE_OS::sprintf (buf,
00083                    ACE_TEXT ("%d/%s %s"),
00084                    sa.get_port_number (),
00085                    ACE_TEXT ("tcp"),
00086                    ACE_TEXT ("# lists all services in the daemon\n"));
00087   if (*strp == 0 && (*strp = ACE_OS::strdup (buf)) == 0)
00088     return -1;
00089   else
00090     ACE_OS::strsncpy (*strp, buf, length);
00091   return static_cast<int> (ACE_OS::strlen (buf));
00092 }

int ACE_Service_Manager::init ( int  argc,
ACE_TCHAR argv[] 
) [protected, virtual]

Initializes object when dynamic linking occurs.

Reimplemented from ACE_Shared_Object.

Definition at line 95 of file Service_Manager.cpp.

References ACE_Event_Handler::ACCEPT_MASK, ACE_ERROR_RETURN, ACE_TEXT, ACE_TRACE, ACE_OS::atoi(), debug_, DEFAULT_PORT_, ACE_OS::getopt(), ACE_Reactor::instance(), LM_ERROR, open(), ACE_INET_Addr::set(), and signum_.

00096 {
00097   ACE_TRACE ("ACE_Service_Manager::init");
00098   ACE_INET_Addr local_addr (ACE_Service_Manager::DEFAULT_PORT_);
00099 
00100   //FUZZ: disable check_for_lack_ACE_OS
00101   ACE_Get_Opt getopt (argc, argv, ACE_TEXT ("dp:s:"), 0); // Start at argv[0]
00102 
00103   for (int c; (c = getopt ()) != -1; )
00104   //FUZZ: enable check_for_lack_ACE_OS
00105      switch (c)
00106        {
00107        case 'd':
00108          this->debug_ = true;
00109          break;
00110        case 'p':
00111          local_addr.set ((u_short) ACE_OS::atoi (getopt.opt_arg ()));
00112          break;
00113        case 's':
00114          this->signum_ = ACE_OS::atoi (getopt.opt_arg ());
00115          break;
00116        default:
00117          break;
00118        }
00119 
00120   if (this->get_handle () == ACE_INVALID_HANDLE &&
00121       this->open (local_addr) == -1)
00122     ACE_ERROR_RETURN ((LM_ERROR,
00123                        ACE_TEXT ("%p\n"),
00124                        ACE_TEXT ("open")), -1);
00125   else if (ACE_Reactor::instance ()->register_handler
00126            (this,
00127             ACE_Event_Handler::ACCEPT_MASK) == -1)
00128     ACE_ERROR_RETURN ((LM_ERROR,
00129                        ACE_TEXT ("registering service with ACE_Reactor\n")),
00130                       -1);
00131   return 0;
00132 }

int ACE_Service_Manager::list_services ( void   )  [protected, virtual]

Determine all the services offered by this daemon and return the information back to the client.

Definition at line 176 of file Service_Manager.cpp.

References ACE_DEBUG, ACE_ERROR, ACE_TEXT, ACE_TRACE, ACE_Service_Repository_Iterator::advance(), client_stream_, ACE_Service_Repository::instance(), LM_DEBUG, LM_ERROR, ACE_Service_Repository_Iterator::next(), ACE_SOCK_Stream::send_n(), ACE_OS::strcat(), ACE_OS::strcpy(), and ACE_OS::strlen().

00177 {
00178   ACE_TRACE ("ACE_Service_Manager::list_services");
00179   ACE_Service_Repository_Iterator sri (*ACE_Service_Repository::instance (), 0);
00180 
00181   for (const ACE_Service_Type *sr;
00182        sri.next (sr) != 0;
00183        sri.advance ())
00184     {
00185       ssize_t len = static_cast<ssize_t> (ACE_OS::strlen (sr->name ())) + 11;
00186       ACE_TCHAR buf[BUFSIZ];
00187       ACE_TCHAR *p = buf + len;
00188 
00189       ACE_OS::strcpy (buf, sr->name ());
00190       ACE_OS::strcat (buf, (sr->active ()) ?
00191                       ACE_TEXT (" (active) ") :
00192                       ACE_TEXT (" (paused) "));
00193 
00194       p[-1] = ' ';
00195       p[0]  = '\0';
00196 
00197       len += sr->type ()->info (&p, sizeof buf - len);
00198 
00199       if (this->debug_)
00200         ACE_DEBUG ((LM_DEBUG,
00201                     ACE_TEXT ("len = %d, info = %s%s"),
00202                     len,
00203                     buf,
00204                     buf[len - 1] == '\n' ? ACE_TEXT ("") : ACE_TEXT ("\n")));
00205 
00206       if (len > 0)
00207         {
00208           ssize_t n = this->client_stream_.send_n (buf, len);
00209           if (n <= 0 && errno != EPIPE)
00210             ACE_ERROR ((LM_ERROR,
00211                         ACE_TEXT ("%p\n"),
00212                         ACE_TEXT ("send_n")));
00213         }
00214     }
00215 
00216   return 0;
00217 }

int ACE_Service_Manager::open ( const ACE_INET_Addr sia  )  [protected]

Definition at line 62 of file Service_Manager.cpp.

References ACE_TRACE.

Referenced by init().

00063 {
00064   ACE_TRACE ("ACE_Service_Manager::open");
00065 
00066   // Reuse the listening address, even if it's already in use!
00067   if (this->acceptor_.open (sia, 1) == -1)
00068     return -1;
00069   return 0;
00070 }

void ACE_Service_Manager::process_request ( ACE_TCHAR request  )  [protected, virtual]

Handle one request.

Definition at line 245 of file Service_Manager.cpp.

References ACE_TEXT, ACE_TRACE, ACE_Service_Config::process_directive(), reconfigure_services(), and ACE_OS::strcmp().

Referenced by handle_input().

00246 {
00247   ACE_TRACE("ACE_Service_Manager::process_request");
00248   ACE_TCHAR *p;
00249 
00250   // Kill trailing newlines.
00251   for (p = request;
00252        (*p != '\0') && (*p != '\r') && (*p != '\n');
00253        p++)
00254     continue;
00255 
00256   *p = '\0';
00257 
00258   if (ACE_OS::strcmp (request, ACE_TEXT ("help")) == 0)
00259     // Return a list of the configured services.
00260     this->list_services ();
00261   else if (ACE_OS::strcmp (request, ACE_TEXT ("reconfigure") )== 0)
00262     // Trigger a reconfiguration by re-reading the local <svc.conf> file.
00263     this->reconfigure_services ();
00264   else
00265     // Just process a single request passed in via the socket
00266     // remotely.
00267     ACE_Service_Config::process_directive (request);
00268 
00269   // Additional management services may be handled here...
00270 }

int ACE_Service_Manager::reconfigure_services ( void   )  [protected, virtual]

Trigger a reconfiguration of the Service Configurator by re-reading its local <svc.conf> file.

Definition at line 223 of file Service_Manager.cpp.

References ACE_TRACE, client_stream_, ACE_Service_Config::reconfig_occurred(), and ACE_SOCK_Stream::send_n().

Referenced by process_request().

00224 {
00225   ACE_TRACE ("ACE_Service_Manager::reconfigure_services");
00226 
00227 #if 0
00228 // Send ourselves a signal!  ACE_OS::kill (ACE_OS::getpid (),
00229 // this->signum_);
00230 #endif /* 0 */
00231 
00232   // Flag the main event loop that a reconfiguration should occur.
00233   // The next trip through the <ACE_Reactor::run_event_loop> should
00234   // pick this up and cause a reconfiguration.  Note that we can't
00235   // trigger the reconfiguration automatically since that might "pull
00236   // the rug" out from underneath the existing services in a
00237   // problematic way.
00238   ACE_Service_Config::reconfig_occurred ((sig_atomic_t) 1);
00239   return static_cast<int> (this->client_stream_.send_n ("done\n",
00240                                                         sizeof ("done\n")));
00241 }

int ACE_Service_Manager::resume ( void   )  [protected, virtual]

Re-enable a previously suspended service.

Reimplemented from ACE_Service_Object.

Definition at line 55 of file Service_Manager.cpp.

References ACE_TRACE, ACE_Reactor::instance(), and ACE_Reactor::resume_handler().

00056 {
00057   ACE_TRACE ("ACE_Service_Manager::resume");
00058   return ACE_Reactor::instance ()->resume_handler (this);
00059 }

int ACE_Service_Manager::suspend ( void   )  [protected, virtual]

Temporarily disable a service without removing it completely.

Reimplemented from ACE_Service_Object.

Definition at line 48 of file Service_Manager.cpp.

References ACE_TRACE, ACE_Reactor::instance(), and ACE_Reactor::suspend_handler().

00049 {
00050   ACE_TRACE ("ACE_Service_Manager::suspend");
00051   return ACE_Reactor::instance ()->suspend_handler (this);
00052 }


Member Data Documentation

ACE_SOCK_Acceptor ACE_Service_Manager::acceptor_ [protected]

Acceptor instance.

Definition at line 105 of file Service_Manager.h.

Referenced by get_handle(), and handle_close().

ACE_Service_Manager::ACE_ALLOC_HOOK_DECLARE [protected]

Declare the dynamic allocation hooks.

Definition at line 86 of file Service_Manager.h.

ACE_SOCK_Stream ACE_Service_Manager::client_stream_ [protected]

Connection to the client (we only support one client connection at a time).

Definition at line 102 of file Service_Manager.h.

Referenced by handle_input(), list_services(), and reconfigure_services().

bool ACE_Service_Manager::debug_ [protected]

Keep track whether we debug or not.

Definition at line 108 of file Service_Manager.h.

Referenced by handle_input(), and init().

u_short ACE_Service_Manager::DEFAULT_PORT_ = 10000 [static, protected]

Default port for the Acceptor to listen on.

Definition at line 114 of file Service_Manager.h.

Referenced by init().

int ACE_Service_Manager::signum_ [protected]

The signal used to trigger reconfiguration.

Definition at line 111 of file Service_Manager.h.

Referenced by init().


The documentation for this class was generated from the following files:
Generated on Tue Feb 2 17:35:34 2010 for ACE by  doxygen 1.4.7