Merge pull request #2216 from jwillemsen/jwi-cxxversionchecks
[ACE_TAO.git] / TAO / orbsvcs / FT_Naming_Service / FT_Naming_Service.cpp
blob6958151e8dccbfd90ab9acb7d19a4f9289633a1e
1 #include "FT_Naming_Service.h"
2 #include "orbsvcs/Naming/FaultTolerant/FT_Naming_Server.h"
3 #include "orbsvcs/Naming/Persistent_Naming_Context_Factory.h"
4 #include "orbsvcs/Daemon_Utilities.h"
5 #include "orbsvcs/Log_Macros.h"
7 #include "ace/Get_Opt.h"
8 #include "ace/Argv_Type_Converter.h"
9 #include "ace/Task.h"
11 // Default Constructor.
12 TAO_FT_Naming_Service::TAO_FT_Naming_Service ()
13 : time_ (0),
14 num_threads_ (1)
18 // Constructor taking command-line arguments.
19 TAO_FT_Naming_Service::TAO_FT_Naming_Service (int argc, ACE_TCHAR* argv[])
20 : time_ (0),
21 num_threads_ (1)
23 this->init (argc, argv);
27 // Initialize the state of the TAO_FT_Naming_Service object
28 int
29 TAO_FT_Naming_Service::init (int argc, ACE_TCHAR* argv[])
31 try
33 // Check if -ORBDaemon is specified and if so, daemonize at this moment,
34 // -ORBDaemon in the ORB core is faulty, see bugzilla 3335
35 TAO_Daemon_Utility::check_for_daemon (argc, argv);
37 // Initialize the ORB
38 this->orb_ = CORBA::ORB_init (argc, argv);
40 // Parse the args for '-t' option. If '-t' option is passed, do
41 // the needful and then remove the option from the list of
42 // arguments.
43 this->parse_args (argc, argv);
45 // This function call initializes the naming service and returns
46 // '-1' in case of an exception.
47 int const result = this->my_naming_server_.init_with_orb (argc,
48 argv,
49 this->orb_.in ());
51 if (result == -1)
52 return result;
54 catch (const CORBA::Exception& ex)
56 ex._tao_print_exception ("TAO_FT_Naming_Service::init");
57 return -1;
60 return 0;
63 int
64 TAO_FT_Naming_Service::parse_args (int &argc, ACE_TCHAR* argv[])
66 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("-t:n:"));
67 int c;
69 while ((c = get_opts ()) != -1)
71 switch (c)
73 case 't':
75 int const time = ACE_OS::atoi (get_opts.opt_arg ());
76 if (time >= 0)
77 this->time_ = time;
79 // Remove the option '-t' from argv []
80 // to avoid any confusion that might result.
81 for (int i = get_opts.opt_ind (); i != argc; ++i)
82 argv [i-2 ] = argv [i];
84 // Decrement the value of argc to reflect the removal
85 // of '-t' option.
86 argc = argc - 2;
87 break;
89 case 'n':
91 int const nt = ACE_OS::atoi (get_opts.opt_arg ());
92 if (nt >= 1)
93 this->num_threads_ = nt;
95 // Remove the option '-n' from argv []
96 // to avoid any confusion that might result.
97 for (int i = get_opts.opt_ind (); i != argc; ++i)
98 argv [i-2 ] = argv [i];
100 // Decrement the value of argc to reflect the removal
101 // of '-n' option.
102 argc = argc - 2;
103 break;
106 case '?':
107 default:
108 // Don't do anything. The TAO_Naming_Server::parse_args ()
109 // takes care of indicating an error in case of error.
110 break;
113 return 0;
116 // Run the ORB event loop.
118 class ORB_Runner : public ACE_Task_Base
120 public:
121 ORB_Runner (CORBA::ORB_ptr o, long t)
122 : orb_(CORBA::ORB::_duplicate (o)),
123 time_(t)
126 int svc ()
128 if (!CORBA::is_nil (orb_.in ()))
130 if (time_ == 0)
132 this->orb_->run ();
134 else
136 ACE_Time_Value tv (time_);
137 this->orb_->run (tv);
141 return 0;
144 private:
145 CORBA::ORB_var orb_;
146 long time_;
150 TAO_FT_Naming_Service::run ()
152 ORB_Runner runner (this->orb_.in(), time_);
153 if (this->num_threads_ == 1)
154 return runner.svc();
155 else
157 int grpid = runner.activate ( THR_NEW_LWP | THR_JOINABLE | THR_INHERIT_SCHED,
158 this->num_threads_);
159 if (grpid == -1)
161 ORBSVCS_ERROR ((LM_ERROR,
162 ACE_TEXT ("FT_Naming_Service(%P)::run %p\n"),
163 ACE_TEXT ("thread acitvation")));
164 return -1;
166 runner.wait();
168 return 0;
171 void
172 TAO_FT_Naming_Service::shutdown ()
174 if (!CORBA::is_nil (orb_.in ()))
176 this->orb_->shutdown (false);
181 TAO_FT_Naming_Service::fini ()
183 this->my_naming_server_.fini();
187 // destroy implies shutdown
188 if (!CORBA::is_nil (orb_.in ()))
190 this->orb_->destroy ();
193 catch (const CORBA::Exception& ex)
195 ex._tao_print_exception ("TAO_FT_Naming_Service::fini");
196 return -1;
198 return 0;
201 // Destructor.
202 TAO_FT_Naming_Service::~TAO_FT_Naming_Service ()