Merge pull request #2301 from sonndinh/remove-dup-reactor-functions
[ACE_TAO.git] / TAO / orbsvcs / LifeCycle_Service / LifeCycle_Service.cpp
blob0bd674008d490bb901a026dea916aa46bb10cd2d
2 //=============================================================================
3 /**
4 * @file LifeCycle_Service.cpp
6 * The server for the LifeCycleService of the quoter example.
8 * @author Michael Kircher (mk1@cs.wustl.edu)
9 */
10 //=============================================================================
13 #include "orbsvcs/Log_Macros.h"
14 #include "LifeCycle_Service.h"
16 #include "ace/Argv_Type_Converter.h"
17 #include "ace/OS_main.h"
19 Life_Cycle_Service_Server::Life_Cycle_Service_Server ()
20 : debug_level_ (1)
24 Life_Cycle_Service_Server::~Life_Cycle_Service_Server ()
26 try
28 // Unbind the Factory Finder.
29 CosNaming::Name generic_Factory_Name (2);
30 generic_Factory_Name.length (2);
31 generic_Factory_Name[0].id = CORBA::string_dup ("LifeCycle_Service");
32 this->namingContext_var_->unbind (generic_Factory_Name);
34 catch (const CORBA::Exception& ex)
36 ex._tao_print_exception ("User Exception");
40 int
41 Life_Cycle_Service_Server::init (int argc,
42 ACE_TCHAR *argv[])
44 int retval = 0;
46 // Copy command line parameter.
47 ACE_Argv_Type_Converter command(argc, argv);
49 retval = this->orb_manager_.init (command.get_argc(),
50 command.get_TCHAR_argv());
52 if (retval == -1)
53 ORBSVCS_ERROR_RETURN ((LM_ERROR,
54 ACE_TEXT("%p\n"),
55 ACE_TEXT("init")),
56 -1);
58 // Activate the POA manager
59 retval = this->orb_manager_.activate_poa_manager ();
61 if (retval == -1)
62 ORBSVCS_ERROR_RETURN ((LM_ERROR, "%p\n", "activate_poa_manager"), -1);
65 this->parse_args (command.get_argc(), command.get_TCHAR_argv());
67 ACE_NEW_RETURN (this->life_Cycle_Service_i_ptr_,
68 Life_Cycle_Service_i(this->debug_level_),
69 -1);
71 // Activate the object.
72 CORBA::String_var str =
73 this->orb_manager_.activate (this->life_Cycle_Service_i_ptr_);
75 if (this->debug_level_ >= 2)
76 ORBSVCS_DEBUG ((LM_DEBUG, "LifeCycle_Service: IOR is: <%C>\n", str.in ()));
78 // Register the LifeCycle Service with the Naming Service.
79 try
81 if (this->debug_level_ >= 2)
82 ORBSVCS_DEBUG ((LM_DEBUG,
83 ACE_TEXT("LifeCycle_Service: Trying to get a reference to the Naming Service.\n")));
85 // Get the Naming Service object reference.
86 CORBA::Object_var namingObj_var =
87 orb_manager_.orb()->resolve_initial_references ("NameService");
89 if (CORBA::is_nil (namingObj_var.in ()))
90 ORBSVCS_ERROR ((LM_ERROR,
91 " LifeCycle_Service: Unable get the Naming Service.\n"));
93 // Narrow the object reference to a Naming Context.
94 namingContext_var_ = CosNaming::NamingContext::_narrow (namingObj_var.in ());
97 if (CORBA::is_nil (namingContext_var_.in ()))
98 ORBSVCS_ERROR ((LM_ERROR,
99 "LifeCycle_Service: Unable get the Naming Service.\n"));
101 if (this->debug_level_ >= 2)
102 ORBSVCS_DEBUG ((LM_DEBUG,
103 ACE_TEXT("LifeCycle_Service: Have a proper reference to the Naming Service.\n")));
105 CosNaming::Name life_Cycle_Service_Name (1);
106 life_Cycle_Service_Name.length (1);
107 life_Cycle_Service_Name[0].id = CORBA::string_dup ("Life_Cycle_Service");
109 CORBA::Object_ptr tmp = this->life_Cycle_Service_i_ptr_->_this();
111 namingContext_var_->bind (life_Cycle_Service_Name,
112 tmp);
114 if (this->debug_level_ >= 2)
115 ORBSVCS_DEBUG ((LM_DEBUG,
116 ACE_TEXT("LifeCycle_Service: Bound the LifeCycle Service to the Naming Context.\n")));
118 catch (const CORBA::Exception& ex)
120 ex._tao_print_exception (
121 "Life_Cycle_Service_Server::init");
124 return 0;
129 Life_Cycle_Service_Server::run ()
131 if (this->debug_level_ >= 1)
132 ORBSVCS_DEBUG ((LM_DEBUG,
133 ACE_TEXT("\nLifeCycle Service: Life_Cycle_Service_Server is running\n")));
135 orb_manager_.orb()->run ();
137 return 0;
141 // Function get_options.
143 u_int
144 Life_Cycle_Service_Server::parse_args (int argc,
145 ACE_TCHAR* argv[])
147 ACE_Get_Opt get_opt (argc, argv, ACE_TEXT("?d:"));
148 int opt;
149 int exit_code = 0;
151 while ((opt = get_opt ()) != EOF)
152 switch (opt)
154 case 'd': // debug flag.
155 this->debug_level_ = ACE_OS::atoi (get_opt.opt_arg ());
156 break;
157 default:
158 exit_code = 1;
159 ORBSVCS_ERROR ((LM_ERROR,
160 "%s: unknown arg, -%c\n",
161 argv[0], char(opt)));
162 ACE_FALLTHROUGH;
163 case '?':
164 ORBSVCS_DEBUG ((LM_DEBUG,
165 ACE_TEXT("usage: %s")
166 ACE_TEXT(" [-d] <debug level> - Set the debug level\n")
167 ACE_TEXT(" [-?] - Prints this message\n")
168 ACE_TEXT("\n"),
169 argv[0]));
170 ACE_OS::exit (exit_code);
171 break;
173 return 0;
176 // function main
179 ACE_TMAIN (int argc, ACE_TCHAR* argv [])
181 Life_Cycle_Service_Server life_Cycle_Service_Server;
185 int check = life_Cycle_Service_Server.init (argc,
186 argv);
188 if (check)
189 return 1;
190 else
192 life_Cycle_Service_Server.run ();
195 catch (const CORBA::Exception& ex)
197 ex._tao_print_exception ("LifeCycleService::main");
198 return -1;
200 return 0;