2 #include /**/ "NT_Notify_Service.h"
4 #if defined (ACE_WIN32) && !defined (ACE_LACKS_WIN32_SERVICES)
6 #include /**/ "Notify_Service.h"
7 #include "tao/ORB_Core.h"
9 #include "ace/Reactor.h"
10 #include "orbsvcs/Log_Macros.h"
12 #define REGISTRY_KEY_ROOT HKEY_LOCAL_MACHINE
13 #define TAO_REGISTRY_SUBKEY ACE_TEXT ("SOFTWARE\\ACE\\TAO")
14 #define TAO_NOTIFY_SERVICE_OPTS_NAME ACE_TEXT ("TaoNotifyServiceOptions")
15 #define TAO_SERVICE_PARAM_COUNT ACE_TEXT ("TaoServiceParameterCount")
17 TAO_NT_Notify_Service::TAO_NT_Notify_Service ()
25 TAO_NT_Notify_Service::~TAO_NT_Notify_Service ()
29 for (int i
= 0; i
< argc_save_
; i
++)
30 ACE_OS::free (argv_save_
[i
]);
32 ACE_OS::free (argv_save_
);
37 TAO_NT_Notify_Service::handle_control (DWORD control_code
)
39 if (control_code
== SERVICE_CONTROL_SHUTDOWN
40 || control_code
== SERVICE_CONTROL_STOP
)
42 report_status (SERVICE_STOP_PENDING
);
43 TAO_ORB_Core_instance ()->reactor ()->end_reactor_event_loop ();
44 TAO_ORB_Core_instance ()->orb ()->shutdown (1);
45 report_status (SERVICE_STOPPED
);
48 ACE_NT_Service::handle_control (control_code
);
52 TAO_NT_Notify_Service::handle_exception (ACE_HANDLE
)
58 TAO_NT_Notify_Service::report_error (const ACE_TCHAR
*format
,
63 ACE_TEXT_FormatMessage (FORMAT_MESSAGE_FROM_SYSTEM
,
69 ORBSVCS_DEBUG ((LM_DEBUG
, format
, val
, msg
));
73 TAO_NT_Notify_Service::arg_manip (char *args
, DWORD arglen
, bool query
)
77 // It looks in the NT Registry under
78 // \\HKEY_LOCAL_MACHINE\SOFTWARE\ACE\TAO for the value of
79 // "TaoNotifyServiceOptions" for any Notify Service options such as
80 // "-ORBListenEndpoints".
82 // Get/Set Notify Service options from the NT Registry.
84 LONG result
= ACE_TEXT_RegOpenKeyEx (REGISTRY_KEY_ROOT
,
87 query
? KEY_READ
: KEY_WRITE
,
90 DWORD type
= REG_EXPAND_SZ
;
95 if (result
== ERROR_SUCCESS
)
97 result
= ACE_TEXT_RegQueryValueEx (hkey
,
98 TAO_NOTIFY_SERVICE_OPTS_NAME
,
103 if (result
!= ERROR_SUCCESS
)
105 this->report_error (ACE_TEXT ("Could not query %s, %s\n"),
106 TAO_NOTIFY_SERVICE_OPTS_NAME
, result
);
111 this->report_error (ACE_TEXT ("No key for %s, %s\n"),
112 TAO_REGISTRY_SUBKEY
, result
);
117 if (result
!= ERROR_SUCCESS
)
119 result
= ACE_TEXT_RegCreateKeyEx (REGISTRY_KEY_ROOT
,
129 DWORD bufSize
= static_cast<DWORD
>(ACE_OS::strlen (args
) + 1);
130 if (result
== ERROR_SUCCESS
)
132 result
= ACE_TEXT_RegSetValueEx (hkey
,
133 TAO_NOTIFY_SERVICE_OPTS_NAME
,
138 if (result
!= ERROR_SUCCESS
)
140 this->report_error (ACE_TEXT ("Could not set %s, %s\n"),
141 TAO_NOTIFY_SERVICE_OPTS_NAME
, result
);
146 this->report_error (ACE_TEXT ("Could not create key %s, %s\n"),
147 TAO_REGISTRY_SUBKEY
, result
);
155 TAO_NT_Notify_Service::set_args (const ACE_TCHAR
*args
)
157 char argbuf
[ACE_DEFAULT_ARGV_BUFSIZ
];
160 this->arg_manip (argbuf
, ACE_DEFAULT_ARGV_BUFSIZ
, true);
161 ACE_OS::printf ("%s\n", argbuf
);
165 ACE_OS::strcpy (argbuf
, ACE_TEXT_ALWAYS_CHAR (args
));
166 this->arg_manip (argbuf
, 0, false);
172 TAO_NT_Notify_Service::init (int argc
,
176 BYTE buf
[ACE_DEFAULT_ARGV_BUFSIZ
];
180 // This solution is very kludgy. It looks in the NT Registry under
181 // \\HKEY_LOCAL_MACHINE\SOFTWARE\ACE\TAO for the value of
182 // "TaoNotifyServiceOptions" for any Notify Service options such as
185 // Get Notify Service options from the NT Registry.
187 ACE_TEXT_RegOpenKeyEx (REGISTRY_KEY_ROOT
,
194 DWORD bufSize
= sizeof (buf
);
196 ACE_TEXT_RegQueryValueEx (hkey
,
197 TAO_NOTIFY_SERVICE_OPTS_NAME
,
205 // Add options to the args list (if any).
207 if (ACE_OS::strlen ((char *) buf
) > 0)
209 ACE_ARGV
args ((const char*) buf
);
210 // Allocate the internal args list to be one bigger than the
211 // args list passed into the function. We use a 'save' list in
212 // case we use a 'destructive' args list processor - this way we
213 // maintain the correct argv and argc for memory freeing
214 // operations in the destructor.
215 argv_save_
= (ACE_TCHAR
**) ACE_OS::malloc (sizeof (ACE_TCHAR
*) * (argc
+ args
.argc ()));
217 // Copy the values into the internal args buffer.
219 for (i
= 0; i
< argc
; i
++)
220 argv_save_
[i
] = ACE_OS::strdup (argv
[i
]);
223 for (i
= argc
; i
< static_cast<int> ((args
.argc () + argc
)); i
++)
224 argv_save_
[i
] = ACE_OS::strdup (args
.argv ()[j
++]);
226 // Set the arg counter.
227 argc_save_
= argc
+ args
.argc ();
241 TAO_NT_Notify_Service::svc ()
243 TAO_Notify_Service_Driver notify_service
;
247 if (notify_service
.init (argc_
, argv_
) == -1)
250 report_status (SERVICE_RUNNING
);
251 notify_service
.run ();
253 catch (const CORBA::Exception
& ex
)
255 ex
._tao_print_exception (ACE_TEXT ("TAO NT Notify Service"));
262 #endif /* ACE_WIN32 && !ACE_LACKS_WIN32_SERVICES */