Merge pull request #2309 from mitza-oci/warnings
[ACE_TAO.git] / TAO / orbsvcs / Scheduling_Service / Scheduling_Service.cpp
blobe19c59b7b05c4ce2dab2d29a5fab16ebec6a2f11
1 #include "Scheduling_Service.h"
3 #include "ace/Get_Opt.h"
4 #include <memory>
5 #include "ace/Argv_Type_Converter.h"
6 #include "orbsvcs/CosNamingC.h"
7 #include "ace/OS_main.h"
8 #include "ace/OS_NS_stdio.h"
9 #include "ace/OS_NS_strings.h"
10 #include "ace/OS_NS_unistd.h"
12 TAO_Scheduling_Service::TAO_Scheduling_Service ()
13 : scheduler_impl_ (0),
14 service_name_ ("ScheduleService"),
15 scheduler_type_ (CONFIG)
19 // Constructor taking the command-line arguments.
20 TAO_Scheduling_Service::TAO_Scheduling_Service (int argc, ACE_TCHAR* argv[])
21 : scheduler_impl_ (0),
22 service_name_ ("ScheduleService"),
23 scheduler_type_ (CONFIG)
25 this->init (argc, argv);
28 TAO_Scheduling_Service::~TAO_Scheduling_Service ()
32 // Initialize the Scheduling Service with the arguments.
33 int
34 TAO_Scheduling_Service::init (int argc, ACE_TCHAR* argv[])
36 int result;
37 CORBA::ORB_var orb;
38 PortableServer::POAManager_ptr poa_manager;
40 try
42 // Copy command line parameter.
43 ACE_Argv_Type_Converter command_line(argc, argv);
45 // Initialize ORB manager.
46 this->orb_manager_.init (command_line.get_argc(), command_line.get_TCHAR_argv());
48 orb = this->orb_manager_.orb ();
50 poa_manager = this->orb_manager_.poa_manager ();
52 poa_manager->activate ();
54 // Check the non-ORB arguments. this needs to come before we
55 // initialize the scheduler implementation so that we know which
56 // type of scheduler to use.
58 result = this->parse_args (command_line.get_argc(), command_line.get_TCHAR_argv());
59 if (result < 0)
60 return result;
62 // Construct a scheduler implementation of the specified type.
63 switch (this->scheduler_type_)
65 case RECONFIG:
66 ACE_NEW_THROW_EX (scheduler_impl_,
67 RECONFIG_SCHED_TYPE,
68 CORBA::NO_MEMORY ());
69 break;
71 case CONFIG:
72 ACE_NEW_THROW_EX (scheduler_impl_,
73 CONFIG_SCHED_TYPE,
74 CORBA::NO_MEMORY ());
75 break;
77 default:
78 ORBSVCS_ERROR_RETURN ((LM_ERROR,
79 "TAO_Scheduling_Service::init: "
80 "unrecognized Scheduler_Type"), -1);
83 // Locate the naming service.
84 CORBA::Object_var naming_obj =
85 orb->resolve_initial_references ("NameService");
87 if (CORBA::is_nil (naming_obj.in ()))
88 ORBSVCS_ERROR_RETURN ((LM_ERROR,
89 " (%P|%t) Unable to locate the Naming Service.\n"),
90 -1);
91 CosNaming::NamingContext_var naming_context =
92 CosNaming::NamingContext::_narrow (naming_obj.in ());
94 RtecScheduler::Scheduler_var scheduler =
95 this->scheduler_impl_->_this ();
97 CORBA::String_var scheduler_ior_string =
98 orb->object_to_string (scheduler.in ());
100 ORBSVCS_DEBUG ((LM_DEBUG, ACE_TEXT("The scheduler IOR is <%C>\n"),
101 scheduler_ior_string.in ()));
103 // Register the servant with the Naming Context....
104 CosNaming::Name schedule_name (1);
105 schedule_name.length (1);
106 schedule_name[0].id = CORBA::string_dup (this->service_name_.rep());
107 naming_context->rebind (schedule_name, scheduler.in ());
109 if (this->ior_file_name_.rep() != 0)
111 FILE *iorf = ACE_OS::fopen (this->ior_file_name_.rep(), "w");
112 if (iorf != 0)
114 ACE_OS::fprintf (iorf,
115 "%s\n",
116 scheduler_ior_string.in ());
117 ACE_OS::fclose (iorf);
121 if (this->pid_file_name_.rep() != 0)
123 FILE *pidf = ACE_OS::fopen (this->pid_file_name_.rep(), "w");
124 if (pidf != 0)
126 ACE_OS::fprintf (pidf,
127 ACE_TEXT("%ld\n"),
128 static_cast<long> (ACE_OS::getpid ()));
129 ACE_OS::fclose (pidf);
133 catch (const CORBA::Exception& ex)
135 ex._tao_print_exception ("TAO_Scheduling_Service::init");
136 return -1;
139 return 0;
143 // Runs the TAO_Scheduling_Service.
146 TAO_Scheduling_Service::run ()
148 // Run the ORB manager.
149 return this->orb_manager_.run ();
153 // Parses the command line arguments.
156 TAO_Scheduling_Service::parse_args (int argc, ACE_TCHAR* argv[])
158 ACE_Get_Opt get_opt (argc, argv, ACE_TEXT("n:p:o:s:"));
159 int opt;
161 while ((opt = get_opt ()) != EOF)
163 switch (opt)
165 case 'n':
166 this->service_name_ = ACE_TEXT_ALWAYS_CHAR(get_opt.opt_arg ());
167 break;
169 case 'p':
170 this->pid_file_name_ = ACE_TEXT_ALWAYS_CHAR(get_opt.opt_arg ());
171 break;
173 case 'o':
174 this->ior_file_name_ = ACE_TEXT_ALWAYS_CHAR(get_opt.opt_arg ());
175 break;
177 case 's':
178 if (ACE_OS::strcasecmp (ACE_TEXT("CONFIG"), get_opt.optarg) == 0)
180 this->scheduler_type_ = CONFIG;
182 else if (ACE_OS::strcasecmp (ACE_TEXT("RECONFIG"), get_opt.optarg) == 0)
184 this->scheduler_type_ = RECONFIG;
186 else
188 ORBSVCS_DEBUG ((LM_DEBUG,
189 "Usage: %s "
190 "[-n service_name] "
191 "[-p pid_file_name] "
192 "[-o ior_file_name] "
193 "[-s <CONFIG | reconfig>]"
194 "\n",
195 argv[0]));
197 return -1;
199 break;
201 case '?':
202 default:
204 ORBSVCS_DEBUG ((LM_DEBUG,
205 "Usage: %s "
206 "[-n service_name] "
207 "[-p pid_file_name] "
208 "[-o ior_file_name] "
209 "[-s <CONFIG | reconfig>]"
210 "\n",
211 argv[0]));
213 return -1;
217 return 0;
220 int ACE_TMAIN (int argc, ACE_TCHAR* argv[])
224 TAO_Scheduling_Service scheduling_service;
226 ORBSVCS_DEBUG ((LM_DEBUG,
227 "%s; initializing scheduling service\n", __FILE__));
229 if (scheduling_service.init (argc, argv) < 0)
230 ORBSVCS_ERROR_RETURN ((LM_ERROR, "%p\n", "init"), 1);
232 ORBSVCS_DEBUG ((LM_DEBUG,
233 "%s; running scheduling service\n", __FILE__));
235 scheduling_service.run ();
237 catch (const CORBA::Exception& ex)
239 ex._tao_print_exception ("schedule_service");
240 return 1;
243 return 0;