2 ** Copyright 2002 Addison Wesley. All Rights Reserved.
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
);
21 ACE_Get_Opt
get_opt (argc
, argv
, ACE_TEXT ("p:"), 0);
22 get_opt
.long_option (ACE_TEXT ("port"),
24 ACE_Get_Opt::ARG_REQUIRED
);
26 for (int c
; (c
= get_opt ()) != -1; )
29 local_addr
.set_port_number
30 (ACE_OS::atoi (get_opt
.opt_arg ()));
33 acceptor_
.open (local_addr
);
34 return reactor ()->register_handler
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 ()) {
51 iov
[0].iov_base
= const_cast<ACE_TCHAR
*> (st
->name ());
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
);
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
);
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
];
87 (buf
, ACE_TEXT ("%hu"), local_addr
.get_port_number ());
89 (buf
, ACE_TEXT ("/tcp # lists services in daemon\n"));
91 *bufferp
= ACE::strnew (buf
);
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
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 (
121 ACE_TEXT ("Service_Reporter"),
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
)