Merge pull request #2301 from sonndinh/remove-dup-reactor-functions
[ACE_TAO.git] / ACE / examples / C++NPv2 / Service_Reporter.cpp
blobf14bf0a8cc0eb039c5653b6dde90e7e1e90350c2
1 /*
2 ** Copyright 2002 Addison Wesley. All Rights Reserved.
3 */
5 #include "ace/OS_NS_stdio.h"
6 #include "ace/OS_NS_string.h"
7 #include "ace/Get_Opt.h"
8 #include "ace/INET_Addr.h"
9 #include "ace/Log_Msg.h"
10 #include "ace/Service_Config.h"
11 #include "ace/Service_Repository.h"
12 #include "ace/Service_Types.h"
13 #include "ace/SOCK_Stream.h"
14 #include "ace/Truncate.h"
15 #include "Service_Reporter.h"
17 int Service_Reporter::init (int argc, ACE_TCHAR *argv[]) {
18 ACE_INET_Addr local_addr (Service_Reporter::DEFAULT_PORT);
20 // Start at argv[0].
21 ACE_Get_Opt get_opt (argc, argv, ACE_TEXT ("p:"), 0);
22 get_opt.long_option (ACE_TEXT ("port"),
23 'p',
24 ACE_Get_Opt::ARG_REQUIRED);
26 for (int c; (c = get_opt ()) != -1; )
27 switch (c) {
28 case 'p':
29 local_addr.set_port_number
30 (ACE_OS::atoi (get_opt.opt_arg ()));
33 acceptor_.open (local_addr);
34 return reactor ()->register_handler
35 (this,
36 ACE_Event_Handler::ACCEPT_MASK);
40 int Service_Reporter::handle_input (ACE_HANDLE) {
41 ACE_SOCK_Stream peer_stream;
42 acceptor_.accept (peer_stream);
44 ACE_Service_Repository_Iterator iterator
45 (*ACE_Service_Repository::instance (), 0);
47 for (const ACE_Service_Type *st;
48 iterator.next (st) != 0;
49 iterator.advance ()) {
50 iovec iov[3];
51 iov[0].iov_base = const_cast<ACE_TCHAR *> (st->name ());
53 iov[0].iov_len =
54 ACE_Utils::truncate_cast<u_long> (
55 ACE_OS::strlen (st->name ()) * sizeof (ACE_TCHAR));
57 const ACE_TCHAR *state = st->active () ?
58 ACE_TEXT (" (active) ") : ACE_TEXT (" (paused) ");
59 iov[1].iov_base = const_cast<ACE_TCHAR *> (state);
61 iov[1].iov_len =
62 ACE_Utils::truncate_cast<u_long> (
63 ACE_OS::strlen (state) * sizeof (ACE_TCHAR));
65 ACE_TCHAR *report = 0; // Ask info() to allocate buffer
66 int len = st->type ()->info (&report, 0);
67 iov[2].iov_base = static_cast<ACE_TCHAR *> (report);
69 iov[2].iov_len = static_cast<u_long> (len);
70 iov[2].iov_len *= sizeof (ACE_TCHAR);
71 peer_stream.sendv_n (iov, 3);
72 ACE::strdelete (report);
75 peer_stream.close ();
76 return 0;
80 int Service_Reporter::info (ACE_TCHAR **bufferp,
81 size_t length) const {
82 ACE_INET_Addr local_addr;
83 acceptor_.get_local_addr (local_addr);
85 ACE_TCHAR buf[BUFSIZ];
86 ACE_OS::sprintf
87 (buf, ACE_TEXT ("%hu"), local_addr.get_port_number ());
88 ACE_OS::strcat
89 (buf, ACE_TEXT ("/tcp # lists services in daemon\n"));
90 if (*bufferp == 0)
91 *bufferp = ACE::strnew (buf);
92 else
93 ACE_OS::strncpy (*bufferp, buf, length);
95 return ACE_Utils::truncate_cast<int> (ACE_OS::strlen (*bufferp));
99 int Service_Reporter::suspend ()
100 { return reactor ()->suspend_handler (this); }
102 int Service_Reporter::resume ()
103 { return reactor ()->resume_handler (this); }
106 int Service_Reporter::fini () {
107 reactor ()->remove_handler
108 (this,
109 ACE_Event_Handler::ACCEPT_MASK
110 | ACE_Event_Handler::DONT_CALL);
111 return acceptor_.close ();
114 // Define the service object make and gobble functions.
115 ACE_FACTORY_DEFINE (ACE_Local_Service, Service_Reporter)
117 // Define the ACE_Static_Svc_Descriptor that conveys the service information
118 // to the ACE_Service_Config.
119 ACE_STATIC_SVC_DEFINE (
120 Reporter_Descriptor,
121 ACE_TEXT ("Service_Reporter"),
122 ACE_SVC_OBJ_T,
123 &ACE_SVC_NAME (Service_Reporter),
124 ACE_Service_Type::DELETE_THIS
125 | ACE_Service_Type::DELETE_OBJ,
126 0 // This object is not initially active.
129 // Define the class that will register this service with ACE_Service_Config
130 // at program startup.
131 ACE_STATIC_SVC_REQUIRE (Reporter_Descriptor)