Merge pull request #1844 from jrw972/monterey
[ACE_TAO.git] / TAO / orbsvcs / Concurrency_Service / Concurrency_Service.cpp
blob70e52c70f6c619ba0de3af24616d92c1fd9babd6
2 //=============================================================================
3 /**
4 * @file Concurrency_Service.cpp
6 * This class implements a Concurrency_Service object.
8 * @author Torben Worm <tworm@cs.wustl.edu>
9 */
10 //=============================================================================
13 #include "orbsvcs/Log_Macros.h"
14 #include "Concurrency_Service.h"
16 #include "orbsvcs/Daemon_Utilities.h"
18 #include "ace/Argv_Type_Converter.h"
19 #include "tao/debug.h"
20 #include "ace/OS_main.h"
21 #include "ace/OS_NS_stdio.h"
22 #include "ace/OS_NS_unistd.h"
24 // Default Constructor.
26 Concurrency_Service::Concurrency_Service (void)
27 : use_naming_service_ (1),
28 ior_file_name_ (0),
29 pid_file_name_ (0)
31 ORBSVCS_DEBUG ((LM_DEBUG,
32 ACE_TEXT("Concurrency_Service::Concurrency_Service (void)\n")));
35 // Constructor taking command-line arguments.
37 Concurrency_Service::Concurrency_Service (int argc,
38 ACE_TCHAR** argv)
40 ORBSVCS_DEBUG ((LM_DEBUG,
41 ACE_TEXT("Concurrency_Service::Concurrency_Service (...)\n")));
42 this->init (argc, argv);
45 int
46 Concurrency_Service::parse_args (int argc, ACE_TCHAR** argv)
48 ORBSVCS_DEBUG ((LM_DEBUG,
49 ACE_TEXT("Concurrency_Service::parse_args\n")));
51 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("do:p:s"));
52 int c;
54 while ((c = get_opts ()) != -1)
55 switch (c)
57 case 'd': // debug flag
58 ++TAO_debug_level;
59 break;
60 case 'o': // output the IOR to a file
61 this->ior_file_name_ = get_opts.opt_arg();
62 break;
63 case 'p':
64 this->pid_file_name_ = get_opts.opt_arg();
65 break;
66 case 's':
67 this->use_naming_service_ = 0;
68 break;
69 default:
70 ORBSVCS_ERROR_RETURN ((LM_ERROR,
71 ACE_TEXT("usage: %s")
72 ACE_TEXT(" [-d]")
73 ACE_TEXT(" [-o] <ior_output_file>")
74 ACE_TEXT("\n"),
75 argv[0]),
76 1);
78 // Indicates successful persing of command line.
79 return 0;
82 // Initialize the state of the Concurrency_Service object.
84 int
85 Concurrency_Service::init (int argc,
86 ACE_TCHAR **argv)
88 ORBSVCS_DEBUG ((LM_DEBUG,
89 "Concurrency_Service::init\n"));
91 // Check if -ORBDaemon is specified and if so, daemonize at this moment,
92 // -ORBDaemon in the ORB core is faulty, see bugzilla 3335
93 TAO_Daemon_Utility::check_for_daemon (argc, argv);
95 // Copy command line parameter.
96 ACE_Argv_Type_Converter command_line(argc, argv);
98 if (this->orb_manager_.init_child_poa (command_line.get_argc(),
99 command_line.get_TCHAR_argv(),
100 "child_poa") == -1)
101 ORBSVCS_ERROR_RETURN ((LM_ERROR,
102 ACE_TEXT("%p\n"),
103 ACE_TEXT("init_child_poa")),
104 -1);
106 if (this->parse_args (command_line.get_argc(), command_line.get_TCHAR_argv())!=0)
107 ORBSVCS_ERROR_RETURN ((LM_ERROR,
108 ACE_TEXT("Could not parse command line\n")),
109 -1);
110 CORBA::String_var str =
111 this->orb_manager_.activate (this->my_concurrency_server_.GetLockSetFactory ());
112 ORBSVCS_DEBUG ((LM_DEBUG,
113 "The IOR is: <%C>\n",
114 str.in ()));
116 if (this->ior_file_name_ != 0)
118 FILE* iorf = ACE_OS::fopen (ior_file_name_, ACE_TEXT("w"));
119 if (iorf == 0)
121 ORBSVCS_ERROR_RETURN ((LM_ERROR,
122 "Cannot open output file for writing IOR: %s",
123 ior_file_name_),
124 -1);
127 ACE_OS::fprintf (iorf, "%s\n", str.in ());
128 ACE_OS::fclose (iorf);
131 if (this->pid_file_name_ != 0)
133 FILE* pidf = ACE_OS::fopen (pid_file_name_, ACE_TEXT("w"));
134 if (pidf != 0)
136 ACE_OS::fprintf (pidf,
137 "%ld\n",
138 static_cast<long> (ACE_OS::getpid ()));
139 ACE_OS::fclose (pidf);
143 if (this->use_naming_service_)
144 return this->init_naming_service ();
146 return 0;
150 Concurrency_Service::init_naming_service (void)
152 ORBSVCS_DEBUG ((LM_DEBUG, "Concurrency_Service::init_naming_service (...)\n"));
153 CORBA::ORB_var orb;
154 PortableServer::POA_var child_poa;
156 orb = this->orb_manager_.orb ();
158 int result = this->naming_client_.init (orb.in ());
160 if (result == -1)
161 return result;
162 lockset_factory_ =
163 this->my_concurrency_server_.GetLockSetFactory ()->_this ();
165 CosNaming::Name concurrency_context_name (1);
166 concurrency_context_name.length (1);
167 concurrency_context_name[0].id = CORBA::string_dup ("CosConcurrency");
169 this->concurrency_context_ =
170 this->naming_client_->bind_new_context (concurrency_context_name);
172 CosNaming::Name lockset_name (1);
173 lockset_name.length (1);
174 lockset_name[0].id = CORBA::string_dup ("LockSetFactory");
175 this->concurrency_context_->bind (lockset_name,
176 lockset_factory_.in ());
177 return 0;
180 // Run the ORB event loop.
183 Concurrency_Service::run (void)
185 ORBSVCS_DEBUG ((LM_DEBUG,
186 "Concurrency_Service::run (...)\n"));
188 int retval = this->orb_manager_.run ();
189 if (retval == -1)
190 ORBSVCS_ERROR_RETURN ((LM_ERROR,
191 "Concurrency_Service::run"),
192 -1);
193 return 0;
196 // Destructor.
198 Concurrency_Service::~Concurrency_Service (void)
200 ORBSVCS_DEBUG ((LM_DEBUG,
201 "Concurrency_Service::~Concurrency_Service (void)\n"));
205 ACE_TMAIN (int argc, ACE_TCHAR ** argv)
207 Concurrency_Service concurrency_service;
209 ORBSVCS_DEBUG ((LM_DEBUG,
210 "\n \t Concurrency Service:SERVER \n\n"));
214 int r = concurrency_service.init (argc, argv);
216 if (r == -1)
217 return 1;
219 concurrency_service.run ();
222 catch (const CORBA::SystemException& sysex)
224 sysex._tao_print_exception ("System Exception");
225 return -1;
227 catch (const CORBA::UserException& userex)
229 userex._tao_print_exception ("User Exception");
230 return -1;
233 return 0;