ACE+TAO-6_5_17
[ACE_TAO.git] / TAO / orbsvcs / FT_Naming_Service / FT_Naming_Service.cpp
blob9ebeba5d3dc4d854726a05ed583ad56d75ecb55c
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 (void)
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;
55 catch (const CORBA::Exception& ex)
57 ex._tao_print_exception ("TAO_FT_Naming_Service::init");
58 return -1;
61 return 0;
64 int
65 TAO_FT_Naming_Service::parse_args (int &argc, ACE_TCHAR* argv[])
67 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("-t:n:"));
68 int c;
70 while ((c = get_opts ()) != -1)
72 switch (c)
74 case 't':
76 int const time = ACE_OS::atoi (get_opts.opt_arg ());
77 if (time >= 0)
78 this->time_ = time;
80 // Remove the option '-t' from argv []
81 // to avoid any confusion that might result.
82 for (int i = get_opts.opt_ind (); i != argc; ++i)
83 argv [i-2 ] = argv [i];
85 // Decrement the value of argc to reflect the removal
86 // of '-t' option.
87 argc = argc - 2;
88 break;
90 case 'n':
92 int const nt = ACE_OS::atoi (get_opts.opt_arg ());
93 if (nt >= 1)
94 this->num_threads_ = nt;
96 // Remove the option '-n' from argv []
97 // to avoid any confusion that might result.
98 for (int i = get_opts.opt_ind (); i != argc; ++i)
99 argv [i-2 ] = argv [i];
101 // Decrement the value of argc to reflect the removal
102 // of '-n' option.
103 argc = argc - 2;
104 break;
107 case '?':
108 default:
109 // Don't do anything. The TAO_Naming_Server::parse_args ()
110 // takes care of indicating an error in case of error.
111 break;
114 return 0;
117 // Run the ORB event loop.
119 class ORB_Runner : public ACE_Task_Base
121 public:
122 ORB_Runner (CORBA::ORB_ptr o, long t)
123 : orb_(CORBA::ORB::_duplicate (o)),
124 time_(t)
127 int svc (void)
129 if (!CORBA::is_nil (orb_.in ()))
131 if (time_ == 0)
133 this->orb_->run ();
135 else
137 ACE_Time_Value tv (time_);
138 this->orb_->run (tv);
142 return 0;
145 private:
146 CORBA::ORB_var orb_;
147 long time_;
151 TAO_FT_Naming_Service::run (void)
153 ORB_Runner runner (this->orb_.in(), time_);
154 if (this->num_threads_ == 1)
155 return runner.svc();
156 else
158 int grpid = runner.activate ( THR_NEW_LWP | THR_JOINABLE | THR_INHERIT_SCHED,
159 this->num_threads_);
160 if (grpid == -1)
162 ORBSVCS_ERROR ((LM_ERROR,
163 ACE_TEXT ("FT_Naming_Service(%P)::run %p\n"),
164 ACE_TEXT ("thread acitvation")));
165 return -1;
167 runner.wait();
169 return 0;
172 void
173 TAO_FT_Naming_Service::shutdown (void)
175 if (!CORBA::is_nil (orb_.in ()))
177 this->orb_->shutdown (0);
182 TAO_FT_Naming_Service::fini (void)
184 this->my_naming_server_.fini();
188 // destroy implies shutdown
189 if (!CORBA::is_nil (orb_.in ()))
191 this->orb_->destroy ();
194 catch (const CORBA::Exception& ex)
196 ex._tao_print_exception ("TAO_FT_Naming_Service::fini");
197 return -1;
199 return 0;
202 // Destructor.
203 TAO_FT_Naming_Service::~TAO_FT_Naming_Service (void)