Revert "Use a variable on the stack to not have a temporary in the call"
[ACE_TAO.git] / TAO / tao / Strategies / UIOP_Acceptor.cpp
blob3d1b80571b8fd9e0ae71bf9587f427737ae6297b
1 #include "tao/Strategies/UIOP_Acceptor.h"
3 #if TAO_HAS_UIOP == 1
5 #include "tao/Strategies/UIOP_Profile.h"
6 #include "tao/MProfile.h"
7 #include "tao/ORB_Core.h"
8 #include "tao/Server_Strategy_Factory.h"
9 #include "tao/debug.h"
10 #include "tao/Protocols_Hooks.h"
11 #include "tao/Codeset_Manager.h"
12 #include "tao/CDR.h"
14 #include "ace/OS_NS_stdio.h"
15 #include "ace/OS_NS_string.h"
16 #include "ace/OS_NS_unistd.h"
18 TAO_BEGIN_VERSIONED_NAMESPACE_DECL
20 TAO_UIOP_Acceptor::TAO_UIOP_Acceptor ()
21 : TAO_Acceptor (TAO_TAG_UIOP_PROFILE),
22 base_acceptor_ (this),
23 creation_strategy_ (0),
24 concurrency_strategy_ (0),
25 accept_strategy_ (0),
26 version_ (TAO_DEF_GIOP_MAJOR, TAO_DEF_GIOP_MINOR),
27 orb_core_ (0),
28 unlink_on_close_ (true)
32 TAO_UIOP_Acceptor::~TAO_UIOP_Acceptor ()
34 // Make sure we are closed before we start destroying the
35 // strategies.
36 this->close ();
38 delete this->creation_strategy_;
39 delete this->concurrency_strategy_;
40 delete this->accept_strategy_;
43 int
44 TAO_UIOP_Acceptor::create_profile (const TAO::ObjectKey &object_key,
45 TAO_MProfile &mprofile,
46 CORBA::Short priority)
48 // Check if multiple endpoints should be put in one profile or
49 // if they should be spread across multiple profiles.
50 if (priority == TAO_INVALID_PRIORITY)
51 return this->create_new_profile (object_key,
52 mprofile,
53 priority);
54 else
55 return this->create_shared_profile (object_key,
56 mprofile,
57 priority);
60 int
61 TAO_UIOP_Acceptor::create_new_profile (const TAO::ObjectKey &object_key,
62 TAO_MProfile &mprofile,
63 CORBA::Short priority)
65 ACE_UNIX_Addr addr;
67 if (this->base_acceptor_.acceptor ().get_local_addr (addr) == -1)
68 return 0;
70 int count = mprofile.profile_count ();
71 if ((mprofile.size () - count) < 1
72 && mprofile.grow (count + 1) == -1)
73 return -1;
75 TAO_UIOP_Profile *pfile = 0;
76 ACE_NEW_RETURN (pfile,
77 TAO_UIOP_Profile (addr,
78 object_key,
79 this->version_,
80 this->orb_core_),
81 -1);
82 pfile->endpoint ()->priority (priority);
84 if (mprofile.give_profile (pfile) == -1)
86 pfile->_decr_refcnt ();
87 pfile = 0;
88 return -1;
91 // Do not add any tagged components to the profile if configured
92 // by the user not to do so, or if an UIOP 1.0 endpoint is being
93 // created (IIOP 1.0 did not support tagged components, so we follow
94 // the same convention for UIOP).
95 if (this->orb_core_->orb_params ()->std_profile_components () == 0
96 || (this->version_.major == 1 && this->version_.minor == 0))
97 return 0;
99 pfile->tagged_components ().set_orb_type (TAO_ORB_TYPE);
100 TAO_Codeset_Manager *csm = this->orb_core_->codeset_manager();
101 if (csm)
102 csm->set_codeset(pfile->tagged_components());
103 return 0;
107 TAO_UIOP_Acceptor::create_shared_profile (const TAO::ObjectKey &object_key,
108 TAO_MProfile &mprofile,
109 CORBA::Short priority)
111 TAO_Profile *pfile = 0;
112 TAO_UIOP_Profile *uiop_profile = 0;
114 // First see if <mprofile> already contains a UIOP profile.
115 for (TAO_PHandle i = 0; i != mprofile.profile_count (); ++i)
117 pfile = mprofile.get_profile (i);
118 if (pfile->tag () == TAO_TAG_UIOP_PROFILE)
120 uiop_profile = dynamic_cast<TAO_UIOP_Profile *> (pfile);
121 break;
125 if (uiop_profile == 0)
127 // If <mprofile> doesn't contain UIOP_Profile, we need to create
128 // one.
129 return create_new_profile (object_key,
130 mprofile,
131 priority);
133 else
135 // A UIOP_Profile already exists - just add our endpoint to it.
136 ACE_UNIX_Addr addr;
138 if (this->base_acceptor_.acceptor ().get_local_addr (addr) == -1)
139 return 0;
141 TAO_UIOP_Endpoint *endpoint = 0;
142 ACE_NEW_RETURN (endpoint,
143 TAO_UIOP_Endpoint (addr),
144 -1);
145 endpoint->priority (priority);
146 uiop_profile->add_endpoint (endpoint);
148 return 0;
153 TAO_UIOP_Acceptor::is_collocated (const TAO_Endpoint *endpoint)
155 const TAO_UIOP_Endpoint *endp =
156 dynamic_cast<const TAO_UIOP_Endpoint *> (endpoint);
158 // Make sure the dynamically cast pointer is valid.
159 if (endp == 0)
160 return 0;
162 // For UNIX Files this is relatively cheap.
163 ACE_UNIX_Addr address;
164 if (this->base_acceptor_.acceptor ().get_local_addr (address) == -1)
165 return 0;
167 return endp->object_addr () == address;
171 TAO_UIOP_Acceptor::close ()
173 if (this->unlink_on_close_)
175 ACE_UNIX_Addr addr;
177 if (this->base_acceptor_.acceptor ().get_local_addr (addr) == 0)
178 (void) ACE_OS::unlink (addr.get_path_name ());
180 this->unlink_on_close_ = false;
183 return this->base_acceptor_.close ();
187 TAO_UIOP_Acceptor::open (TAO_ORB_Core *orb_core,
188 ACE_Reactor *reactor,
189 int major,
190 int minor,
191 const char *address,
192 const char *options)
194 this->orb_core_ = orb_core;
196 if (address == 0)
197 return -1;
199 if (major >= 0 && minor >= 0)
200 this->version_.set_version (static_cast<CORBA::Octet> (major),
201 static_cast<CORBA::Octet> (minor));
202 // Parse options
203 if (this->parse_options (options) == -1)
204 return -1;
205 else
206 return this->open_i (address,
207 reactor);
211 TAO_UIOP_Acceptor::open_default (TAO_ORB_Core *orb_core,
212 ACE_Reactor *reactor,
213 int major,
214 int minor,
215 const char *options)
217 this->orb_core_ = orb_core;
219 if (major >= 0 && minor >= 0)
220 this->version_.set_version (static_cast<CORBA::Octet> (major),
221 static_cast<CORBA::Octet> (minor));
223 // Parse options
224 if (this->parse_options (options) == -1)
225 return -1;
227 ACE_Auto_String_Free tempname (ACE_OS::tempnam (0, "TAO"));
229 if (tempname.get () == 0)
230 return -1;
232 return this->open_i (tempname.get (),
233 reactor);
237 TAO_UIOP_Acceptor::open_i (const char *rendezvous,
238 ACE_Reactor *reactor)
240 ACE_NEW_RETURN (this->creation_strategy_,
241 TAO_UIOP_CREATION_STRATEGY (this->orb_core_),
242 -1);
244 ACE_NEW_RETURN (this->concurrency_strategy_,
245 TAO_UIOP_CONCURRENCY_STRATEGY (this->orb_core_),
246 -1);
248 ACE_NEW_RETURN (this->accept_strategy_,
249 TAO_UIOP_ACCEPT_STRATEGY (this->orb_core_),
250 -1);
252 ACE_UNIX_Addr addr;
254 this->rendezvous_point (addr, rendezvous);
256 if (this->base_acceptor_.open (addr,
257 reactor,
258 this->creation_strategy_,
259 this->accept_strategy_,
260 this->concurrency_strategy_) == -1)
262 // Don't unlink an existing rendezvous point since it may be in
263 // use by another UIOP server/client.
264 if (errno == EADDRINUSE)
265 this->unlink_on_close_ = false;
267 return -1;
270 (void) this->base_acceptor_.acceptor().enable (ACE_CLOEXEC);
271 // This avoids having child processes acquire the listen socket thereby
272 // denying the server the opportunity to restart on a well-known endpoint.
273 // This does not affect the aberrent behavior on Win32 platforms.
275 // @@ If Profile creation is slow we may need to cache the
276 // rendezvous point here
278 if (TAO_debug_level > 5)
279 TAOLIB_DEBUG ((LM_DEBUG,
280 "\nTAO (%P|%t) - UIOP_Acceptor::open_i - "
281 "listening on: <%C>\n",
282 addr.get_path_name ()));
284 // In the event that an accept() fails, we can examine the reason. If
285 // the reason warrants it, we can try accepting again at a later time.
286 // The amount of time we wait to accept again is governed by this orb
287 // parameter.
288 this->set_error_retry_delay (
289 this->orb_core_->orb_params ()->accept_error_delay());
291 return 0;
294 void
295 TAO_UIOP_Acceptor::rendezvous_point (ACE_UNIX_Addr &addr,
296 const char *rendezvous)
298 // To guarantee portability, local IPC rendezvous points (including
299 // the path and filename) should not be longer than 99 characters
300 // long. Some platforms may support longer rendezvous points,
301 // usually 108 characters including the null terminator, but
302 // Posix.1g only requires that local IPC rendezvous point arrays
303 // contain a maximum of at least 100 characters, including the null
304 // terminator. If an endpoint is longer than what the platform
305 // supports then it will be truncated so that it fits, and a warning
306 // will be issued.
308 // Avoid using relative paths in your UIOP endpoints. If possible,
309 // use absolute paths instead. Imagine that the server is given an
310 // endpoint to create using -ORBEndpoint uiop://foobar. A local IPC
311 // rendezvous point called foobar will be created in the current
312 // working directory. If the client is not started in the directory
313 // where the foobar rendezvous point exists then the client will not
314 // be able to communicate with the server since its point of
315 // communication, the rendezvous point, was not found. On the other
316 // hand, if an absolute path was used, the client would know exactly
317 // where to find the rendezvous point. It is up to the user to make
318 // sure that a given UIOP endpoint is accessible by both the server
319 // and the client.
321 addr.set (rendezvous);
323 const size_t length = ACE_OS::strlen (addr.get_path_name ());
325 // Check if rendezvous point was truncated by ACE_UNIX_Addr since
326 // most UNIX domain socket rendezvous points can only be less than
327 // 108 characters long.
328 if (length < ACE_OS::strlen (rendezvous))
329 TAOLIB_DEBUG ((LM_WARNING,
330 "TAO (%P|%t) - UIOP rendezvous point was truncated to <%s>\n"
331 "since it was longer than %d characters long.\n",
332 addr.get_path_name (),
333 length));
336 CORBA::ULong
337 TAO_UIOP_Acceptor::endpoint_count ()
339 return 1;
343 TAO_UIOP_Acceptor::object_key (IOP::TaggedProfile &profile,
344 TAO::ObjectKey &object_key)
346 // Create the decoding stream from the encapsulation in the buffer,
347 #if (TAO_NO_COPY_OCTET_SEQUENCES == 1)
348 TAO_InputCDR cdr (profile.profile_data.mb ());
349 #else
350 TAO_InputCDR cdr (reinterpret_cast<char*> (profile.profile_data.get_buffer ()),
351 profile.profile_data.length ());
352 #endif /* TAO_NO_COPY_OCTET_SEQUENCES == 1 */
354 CORBA::Octet major = 0;
355 CORBA::Octet minor = 0;
357 // Read the version. We just read it here. We don't *do any*
358 // processing.
359 if (!(cdr.read_octet (major) && cdr.read_octet (minor)))
361 if (TAO_debug_level > 0)
363 TAOLIB_DEBUG ((LM_DEBUG,
364 ACE_TEXT ("TAO (%P|%t) - UIOP_Profile::decode - v%d.%d\n"),
365 major,
366 minor));
369 return -1;
372 char * rendezvous = 0;
374 // Get rendezvous_point
375 if (cdr.read_string (rendezvous) == 0)
377 TAOLIB_ERROR ((LM_ERROR, "error decoding UIOP rendezvous_point"));
379 return -1;
382 // delete the rendezvous point. We don't do any processing.
383 delete [] rendezvous;
385 // ... and object key.
386 if ((cdr >> object_key) == 0)
387 return -1;
389 return 1;
393 TAO_UIOP_Acceptor::parse_options (const char *str)
395 if (str == 0)
396 return 0; // No options to parse. Not a problem.
398 // Use an option format similar to the one used for CGI scripts in
399 // HTTP URLs.
400 // e.g.: option1=foo&option2=bar
402 ACE_CString options (str);
404 const size_t len = options.length ();
406 static const char option_delimiter = '&';
408 // Count the number of options.
410 CORBA::ULong option_count = 1;
411 // Number of endpoints in the string (initialized to 1).
413 // Only check for endpoints after the protocol specification and
414 // before the object key.
415 for (size_t i = 0; i < len; ++i)
416 if (options[i] == option_delimiter)
417 ++option_count;
419 // The idea behind the following loop is to split the options into
420 // (option, name) pairs.
421 // For example,
422 // `option1=foo&option2=bar'
423 // will be parsed into:
424 // `option1=foo'
425 // `option2=bar'
427 ACE_CString::size_type begin = 0;
428 ACE_CString::size_type end = 0;
430 for (CORBA::ULong j = 0; j < option_count; ++j)
432 if (j < option_count - 1)
433 end = options.find (option_delimiter, begin);
434 else
435 end = len;
437 if (end == begin)
438 TAOLIB_ERROR_RETURN ((LM_ERROR,
439 "TAO (%P|%t) Zero length UIOP option.\n"),
440 -1);
441 else if (end != ACE_CString::npos)
443 ACE_CString opt =
444 options.substring (begin, end - begin);
446 ACE_CString::size_type const slot = opt.find ("=");
448 if (slot == len - 1
449 || slot == ACE_CString::npos)
450 TAOLIB_ERROR_RETURN ((LM_ERROR,
451 "TAO (%P|%t) - UIOP option <%C> is "
452 "missing a value.\n",
453 opt.c_str ()),
454 -1);
456 const ACE_CString name (opt.substring (0, slot));
457 ACE_CString value = opt.substring (slot + 1);
459 begin = end + 1;
461 if (name.length () == 0)
462 TAOLIB_ERROR_RETURN ((LM_ERROR,
463 "TAO (%P|%t) - Zero length UIOP "
464 "option name.\n"),
465 -1);
467 if (name == "priority")
469 TAOLIB_ERROR_RETURN ((LM_ERROR,
470 ACE_TEXT ("TAO (%P|%t) - Invalid UIOP endpoint format: ")
471 ACE_TEXT ("endpoint priorities no longer supported.\n")),
472 -1);
474 else
475 TAOLIB_ERROR_RETURN ((LM_ERROR,
476 "TAO (%P|%t) - Invalid UIOP option: <%C>\n",
477 name.c_str ()),
478 -1);
480 else
481 break; // No other options.
483 return 0;
486 TAO_END_VERSIONED_NAMESPACE_DECL
488 #endif /* TAO_HAS_UIOP == 1 */