1
// Event_Service.cpp,v 1.47 2003/11/04 05:21:30 dhinton Exp
3
#include "Event_Service.h"
5
#include "ace/Get_Opt.h"
6
#include "ace/Auto_Ptr.h"
7
#include "ace/Argv_Type_Converter.h"
8
#include "ace/OS_main.h"
10
#include "orbsvcs/CosNamingC.h"
11
#include "orbsvcs/Event_Utilities.h"
12
#include "orbsvcs/Sched/Config_Scheduler.h"
13
#include "orbsvcs/Event/Module_Factory.h"
14
#include "orbsvcs/Event/Event_Channel.h"
16
#include "orbsvcs/Event/EC_Default_Factory.h"
17
#include "orbsvcs/Event/EC_Event_Channel.h"
19
#include "tao/BiDir_GIOP/BiDirGIOP.h"
20
#include "ace/OS_NS_strings.h"
22
ACE_RCSID(Event_Service, Event_Service, "Event_Service.cpp,v 1.47 2003/11/04 05:21:30 dhinton Exp")
24
int ACE_TMAIN (int argc, ACE_TCHAR* argv[])
26
TAO_EC_Default_Factory::init_svcs ();
28
Event_Service event_service;
29
return event_service.run (argc, argv);
32
// ****************************************************************
34
Event_Service::Event_Service (void)
35
: module_factory_ (0),
38
scheduler_type_ (ES_SCHED_NONE),
39
event_service_type_ (ES_NEW),
44
Event_Service::~Event_Service (void)
46
delete this->ec_impl_;
48
delete this->sched_impl_;
49
this->sched_impl_ = 0;
50
delete this->module_factory_;
51
this->module_factory_ = 0;
55
Event_Service::run (int argc, ACE_TCHAR* argv[])
59
// Make a copy of command line parameter.
60
ACE_Argv_Type_Converter command(argc, argv);
64
CORBA::ORB_init (command.get_argc(), command.get_ASCII_argv(), "" ACE_ENV_ARG_PARAMETER);
67
if (this->parse_args (command.get_argc(), command.get_TCHAR_argv()) == -1)
70
CORBA::Object_var root_poa_object =
71
this->orb_->resolve_initial_references("RootPOA"
72
ACE_ENV_ARG_PARAMETER);
74
if (CORBA::is_nil (root_poa_object.in ()))
75
ACE_ERROR_RETURN ((LM_ERROR,
76
" (%P|%t) Unable to initialize the root POA.\n"),
79
PortableServer::POA_var root_poa =
80
PortableServer::POA::_narrow (root_poa_object.in () ACE_ENV_ARG_PARAMETER);
83
PortableServer::POAManager_var poa_manager =
84
root_poa->the_POAManager (ACE_ENV_SINGLE_ARG_PARAMETER);
87
poa_manager->activate (ACE_ENV_SINGLE_ARG_PARAMETER);
90
CORBA::Object_var naming_obj =
91
this->orb_->resolve_initial_references ("NameService" ACE_ENV_ARG_PARAMETER);
93
if (CORBA::is_nil (naming_obj.in ()))
94
ACE_ERROR_RETURN ((LM_ERROR,
95
" (%P|%t) Unable to initialize the Naming Service.\n"),
98
CosNaming::NamingContext_var naming_context =
99
CosNaming::NamingContext::_narrow (naming_obj.in () ACE_ENV_ARG_PARAMETER);
102
RtecScheduler::Scheduler_var scheduler;
103
// This is the name we (potentially) register the Scheduling
104
// Service in the Naming Service.
105
CosNaming::Name schedule_name (1);
106
schedule_name.length (1);
107
schedule_name[0].id = CORBA::string_dup ("ScheduleService");
109
// The old EC always needs a scheduler. If none is
110
// specified, we default to a local scheduler
111
if (this->scheduler_type_ == ES_SCHED_LOCAL ||
112
(this->scheduler_type_ == ES_SCHED_NONE &&
113
this->event_service_type_ != ES_NEW))
115
// Create a local scheduler instance
116
ACE_NEW_RETURN (this->sched_impl_,
117
ACE_Config_Scheduler,
120
scheduler = this->sched_impl_->_this (ACE_ENV_SINGLE_ARG_PARAMETER);
123
// Register the servant with the Naming Context....
124
naming_context->rebind (schedule_name, scheduler.in ()
125
ACE_ENV_ARG_PARAMETER);
128
else if (this->scheduler_type_ == ES_SCHED_GLOBAL)
130
// Get reference to a scheduler from naming service
131
CORBA::Object_var tmp =
132
naming_context->resolve (schedule_name ACE_ENV_ARG_PARAMETER);
135
scheduler = RtecScheduler::Scheduler::_narrow (tmp.in ()
136
ACE_ENV_ARG_PARAMETER);
139
if (CORBA::is_nil (scheduler.in ()))
140
ACE_ERROR_RETURN ((LM_ERROR,
141
" (%P|%t) Unable to resolve the Scheduling Service.\n"),
145
switch (this->event_service_type_)
149
TAO_EC_Event_Channel_Attributes attr (root_poa.in (),
152
if (this->scheduler_type_ != ES_SCHED_NONE)
154
attr.scheduler = scheduler.in ();
157
TAO_EC_Event_Channel* ec;
159
TAO_EC_Event_Channel (attr),
162
ec->activate (ACE_ENV_SINGLE_ARG_PARAMETER);
167
case ES_OLD_REACTIVE:
169
ACE_NEW_RETURN (this->module_factory_,
170
TAO_Reactive_Module_Factory,
172
ACE_NEW_RETURN (this->ec_impl_,
173
ACE_EventChannel (scheduler.in (),
175
ACE_DEFAULT_EVENT_CHANNEL_TYPE,
176
this->module_factory_),
182
ACE_NEW_RETURN (this->module_factory_,
183
TAO_Default_Module_Factory,
186
ACE_NEW_RETURN (this->ec_impl_,
187
ACE_EventChannel (scheduler.in (),
189
ACE_DEFAULT_EVENT_CHANNEL_TYPE,
190
this->module_factory_),
197
RtecEventChannelAdmin::EventChannel_var ec;
199
// If the servant name is empty and we don't use BiDIR GIOP, activate the
200
// servant under the default POA, else create a new child POA with persistent policies
201
// the needed policies
202
int persistent = ACE_OS::strcmp(this->servant_name_.c_str(), "");
203
if ((persistent == 0) && (this->use_bidir_giop_ == 0))
205
// Notice that we activate *this* object with the POA, but we
206
// forward all the requests to the underlying EC
208
ec = this->_this (ACE_ENV_SINGLE_ARG_PARAMETER);
216
CORBA::PolicyList policies (3);
221
root_poa->create_id_assignment_policy (PortableServer::USER_ID
222
ACE_ENV_ARG_PARAMETER);
226
root_poa->create_lifespan_policy (PortableServer::PERSISTENT
227
ACE_ENV_ARG_PARAMETER);
231
if (this->use_bidir_giop_ == 1)
234
pol <<= BiDirPolicy::BOTH;
236
this->orb_->create_policy (BiDirPolicy::BIDIRECTIONAL_POLICY_TYPE,
238
ACE_ENV_ARG_PARAMETER);
242
policies.length (index);
244
ACE_CString child_poa_name = "childPOA";
245
PortableServer::POA_var child_poa =
246
root_poa->create_POA (child_poa_name.c_str (),
249
ACE_ENV_ARG_PARAMETER);
252
// Creation of persistentPOA is over. Destroy the Policy objects.
253
for (CORBA::ULong i = 0;
254
i < policies.length ();
257
policies[i]->destroy (ACE_ENV_SINGLE_ARG_PARAMETER);
261
if (CORBA::is_nil (child_poa.in ()))
262
ACE_ERROR_RETURN ((LM_ERROR,
263
" (%P|%t) Unable to initialize the child POA.\n"),
266
PortableServer::ObjectId_var ec_object_id =
267
PortableServer::string_to_ObjectId(servant_name_.c_str());
269
child_poa->activate_object_with_id(ec_object_id.in(),
271
ACE_ENV_ARG_PARAMETER);
274
CORBA::Object_var ec_obj =
275
child_poa->id_to_reference(ec_object_id.in()
276
ACE_ENV_ARG_PARAMETER);
280
RtecEventChannelAdmin::EventChannel::_narrow(ec_obj.in()
281
ACE_ENV_ARG_PARAMETER);
285
CORBA::String_var str =
286
this->orb_->object_to_string (ec.in () ACE_ENV_ARG_PARAMETER);
289
if (ACE_OS::strcmp(this->ior_file_name_.c_str(), "") != 0)
292
ACE_OS::fopen (ACE_TEXT_CHAR_TO_TCHAR(this->ior_file_name_.c_str()),
294
if (output_file == 0)
295
ACE_ERROR_RETURN ((LM_ERROR,
296
"Cannot open output file for writing IOR: %s",
297
this->ior_file_name_.c_str()),
299
ACE_OS::fprintf (output_file, "%s", str.in ());
300
ACE_OS::fclose (output_file);
303
if (ACE_OS::strcmp(this->pid_file_name_.c_str(), "") != 0)
306
ACE_OS::fopen (ACE_TEXT_CHAR_TO_TCHAR(this->pid_file_name_.c_str()),
310
ACE_OS::fprintf (pidf,
312
ACE_static_cast (long, ACE_OS::getpid ()));
313
ACE_OS::fclose (pidf);
317
ACE_DEBUG ((LM_DEBUG,
318
ACE_LIB_TEXT("The EC IOR is <%s>\n"), ACE_TEXT_CHAR_TO_TCHAR(str.in ())));
320
CosNaming::Name channel_name (1);
321
channel_name.length (1);
322
channel_name[0].id = CORBA::string_dup (this->service_name_.c_str());
323
naming_context->rebind (channel_name, ec.in () ACE_ENV_ARG_PARAMETER);
326
ACE_DEBUG ((LM_DEBUG,
327
ACE_LIB_TEXT("%s; running event service\n"),
328
ACE_TEXT_CHAR_TO_TCHAR(__FILE__)));
329
this->orb_->run (ACE_ENV_SINGLE_ARG_PARAMETER);
332
naming_context->unbind (channel_name ACE_ENV_ARG_PARAMETER);
335
if (!CORBA::is_nil (scheduler.in ()))
337
naming_context->unbind (schedule_name ACE_ENV_ARG_PARAMETER);
344
ACE_PRINT_EXCEPTION (ACE_ANY_EXCEPTION, "EC");
353
Event_Service::parse_args (int argc, ACE_TCHAR* argv [])
356
this->service_name_ = "EventService";
358
ACE_Get_Opt get_opt (argc, argv, ACE_LIB_TEXT("n:o:p:s:t:q:b"));
361
while ((opt = get_opt ()) != EOF)
366
this->service_name_ = ACE_TEXT_ALWAYS_CHAR(get_opt.opt_arg ());
370
this->ior_file_name_ = ACE_TEXT_ALWAYS_CHAR(get_opt.opt_arg ());
374
this->pid_file_name_ = ACE_TEXT_ALWAYS_CHAR(get_opt.opt_arg ());
378
this->servant_name_ = ACE_TEXT_ALWAYS_CHAR(get_opt.opt_arg ());
382
this->use_bidir_giop_ = 1;
386
// It could be just a flag (i.e. no "global" or "local"
387
// argument, but this is consistent with the EC_Multiple
388
// test and also allows for a runtime scheduling service.
390
if (ACE_OS::strcasecmp (get_opt.opt_arg (), ACE_LIB_TEXT("global")) == 0)
392
this->scheduler_type_ = ES_SCHED_GLOBAL;
394
else if (ACE_OS::strcasecmp (get_opt.opt_arg (), ACE_LIB_TEXT("local")) == 0)
396
this->scheduler_type_ = ES_SCHED_LOCAL;
398
else if (ACE_OS::strcasecmp (get_opt.opt_arg (), ACE_LIB_TEXT("none")) == 0)
400
this->scheduler_type_ = ES_SCHED_NONE;
404
ACE_DEBUG ((LM_DEBUG,
405
ACE_LIB_TEXT("Unknown scheduling type <%s> ")
406
ACE_LIB_TEXT("defaulting to none\n"),
407
get_opt.opt_arg ()));
408
this->scheduler_type_ = ES_SCHED_NONE;
413
if (ACE_OS::strcasecmp (get_opt.opt_arg (), ACE_LIB_TEXT("NEW")) == 0)
415
this->event_service_type_ = ES_NEW;
417
else if (ACE_OS::strcasecmp (get_opt.opt_arg (), ACE_LIB_TEXT("OLD_REACTIVE")) == 0)
419
this->event_service_type_ = ES_OLD_REACTIVE;
421
else if (ACE_OS::strcasecmp (get_opt.opt_arg (), ACE_LIB_TEXT("OLD_MT")) == 0)
423
this->event_service_type_ = ES_OLD_MT;
427
ACE_DEBUG ((LM_DEBUG,
428
ACE_LIB_TEXT("Unknown event service type <%s> ")
429
ACE_LIB_TEXT("defaulting to NEW\n"),
430
get_opt.opt_arg ()));
431
this->event_service_type_ = ES_NEW;
437
ACE_DEBUG ((LM_DEBUG,
438
ACE_LIB_TEXT("Usage: %s ")
439
ACE_LIB_TEXT("-n service_name ")
440
ACE_LIB_TEXT("-o ior_file_name ")
441
ACE_LIB_TEXT("-p pid_file_name ")
442
ACE_LIB_TEXT("-s <global|local|none> ")
443
ACE_LIB_TEXT("-t <new|old_reactive|old_mt> ")
444
ACE_LIB_TEXT("-q servant_name for persistent IOR ")
445
ACE_LIB_TEXT("-b use bidir giop ")
456
RtecEventChannelAdmin::ConsumerAdmin_ptr
457
Event_Service::for_consumers (ACE_ENV_SINGLE_ARG_DECL)
458
ACE_THROW_SPEC ((CORBA::SystemException))
460
return this->ec_impl_->for_consumers (ACE_ENV_SINGLE_ARG_PARAMETER);
463
RtecEventChannelAdmin::SupplierAdmin_ptr
464
Event_Service::for_suppliers (ACE_ENV_SINGLE_ARG_DECL)
465
ACE_THROW_SPEC ((CORBA::SystemException))
467
return this->ec_impl_->for_suppliers (ACE_ENV_SINGLE_ARG_PARAMETER);
471
Event_Service::destroy (ACE_ENV_SINGLE_ARG_DECL)
472
ACE_THROW_SPEC ((CORBA::SystemException))
474
this->ec_impl_->destroy (ACE_ENV_SINGLE_ARG_PARAMETER);
476
this->orb_->shutdown ();
479
RtecEventChannelAdmin::Observer_Handle
480
Event_Service::append_observer (RtecEventChannelAdmin::Observer_ptr observer
483
CORBA::SystemException,
484
RtecEventChannelAdmin::EventChannel::SYNCHRONIZATION_ERROR,
485
RtecEventChannelAdmin::EventChannel::CANT_APPEND_OBSERVER))
487
return this->ec_impl_->append_observer (observer ACE_ENV_ARG_PARAMETER);
491
Event_Service::remove_observer (RtecEventChannelAdmin::Observer_Handle handle
494
CORBA::SystemException,
495
RtecEventChannelAdmin::EventChannel::SYNCHRONIZATION_ERROR,
496
RtecEventChannelAdmin::EventChannel::CANT_REMOVE_OBSERVER))
498
this->ec_impl_->remove_observer (handle ACE_ENV_ARG_PARAMETER);
501
#if defined (ACE_HAS_EXPLICIT_TEMPLATE_INSTANTIATION)
502
template class ACE_Auto_Basic_Ptr<POA_RtecScheduler::Scheduler>;
503
template class auto_ptr<POA_RtecScheduler::Scheduler>;
504
#elif defined(ACE_HAS_TEMPLATE_INSTANTIATION_PRAGMA)
505
#pragma instantiate ACE_Auto_Basic_Ptr<POA_RtecScheduler::Scheduler>
506
#pragma instantiate auto_ptr<POA_RtecScheduler::Scheduler>
507
#endif /* ACE_HAS_EXPLICIT_TEMPLATE_INSTANTIATION */