Merge pull request #2309 from mitza-oci/warnings
[ACE_TAO.git] / TAO / tao / Strategies / SHMIOP_Acceptor.cpp
blob0074fa175339c6044bfe1b878d32433b974917c0
1 #include "tao/Strategies/SHMIOP_Acceptor.h"
3 #if defined (TAO_HAS_SHMIOP) && (TAO_HAS_SHMIOP != 0)
5 #include "tao/Strategies/SHMIOP_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/Codeset_Manager.h"
11 #include "tao/CDR.h"
13 #include "ace/os_include/os_netdb.h"
14 #include "ace/OS_NS_ctype.h"
16 TAO_BEGIN_VERSIONED_NAMESPACE_DECL
18 TAO_SHMIOP_Acceptor::TAO_SHMIOP_Acceptor ()
19 : TAO_Acceptor (TAO_TAG_SHMEM_PROFILE),
20 version_ (TAO_DEF_GIOP_MAJOR, TAO_DEF_GIOP_MINOR),
21 orb_core_ (0),
22 base_acceptor_ (this),
23 creation_strategy_ (0),
24 concurrency_strategy_ (0),
25 accept_strategy_ (0),
26 mmap_file_prefix_ (0),
27 mmap_size_ (1024 * 1024)
31 TAO_SHMIOP_Acceptor::~TAO_SHMIOP_Acceptor ()
33 // Make sure we are closed before we start destroying the
34 // strategies.
35 this->close ();
37 delete this->creation_strategy_;
38 delete this->concurrency_strategy_;
39 delete this->accept_strategy_;
42 // TODO =
43 // 2) For V1.[1,2] there are tagged components
44 // 3) Create multiple profiles for wild carded endpoints (may be multiple
45 // interfaces over which we can receive requests. Thus a profile
46 // must be made for each one.
47 int
48 TAO_SHMIOP_Acceptor::create_profile (const TAO::ObjectKey &object_key,
49 TAO_MProfile &mprofile,
50 CORBA::Short priority)
52 // Check if multiple endpoints should be put in one profile or
53 // if they should be spread across multiple profiles.
54 if (priority == TAO_INVALID_PRIORITY)
55 return this->create_new_profile (object_key, mprofile, priority);
56 else
57 return this->create_shared_profile (object_key, mprofile, priority);
60 int
61 TAO_SHMIOP_Acceptor::create_new_profile (const TAO::ObjectKey &object_key,
62 TAO_MProfile &mprofile,
63 CORBA::Short priority)
65 // @@ we only make one for now
66 int count = mprofile.profile_count ();
67 if ((mprofile.size () - count) < 1
68 && mprofile.grow (count + 1) == -1)
69 return -1;
71 TAO_SHMIOP_Profile *pfile = 0;
72 ACE_NEW_RETURN (pfile,
73 TAO_SHMIOP_Profile (this->host_.c_str (),
74 this->address_.get_port_number (),
75 object_key,
76 this->address_.get_remote_addr (),
77 this->version_,
78 this->orb_core_),
79 -1);
80 pfile->endpoint ()->priority (priority);
82 if (mprofile.give_profile (pfile) == -1)
84 pfile->_decr_refcnt ();
85 pfile = 0;
86 return -1;
89 // Do not add any tagged components to the profile if configured
90 // by the user not to do so, or if an SHMIOP 1.0 endpoint is being
91 // created (IIOP 1.0 did not support tagged components, so we follow
92 // the same convention for SHMIOP).
93 if (this->orb_core_->orb_params ()->std_profile_components () == 0
94 || (this->version_.major == 1 && this->version_.minor == 0))
95 return 0;
97 pfile->tagged_components ().set_orb_type (TAO_ORB_TYPE);
98 TAO_Codeset_Manager *csm = this->orb_core_->codeset_manager();
99 if (csm)
100 csm->set_codeset(pfile->tagged_components());
101 return 0;
105 TAO_SHMIOP_Acceptor::create_shared_profile (const TAO::ObjectKey &object_key,
106 TAO_MProfile &mprofile,
107 CORBA::Short priority)
109 TAO_Profile *pfile = 0;
110 TAO_SHMIOP_Profile *shmiop_profile = 0;
112 // First see if <mprofile> already contains a SHMIOP profile.
113 for (TAO_PHandle i = 0; i != mprofile.profile_count (); ++i)
115 pfile = mprofile.get_profile (i);
116 if (pfile->tag () == TAO_TAG_SHMEM_PROFILE)
118 shmiop_profile = dynamic_cast <TAO_SHMIOP_Profile *>(pfile);
119 break;
123 if (shmiop_profile == 0)
125 // If <mprofile> doesn't contain SHMIOP_Profile, we need to create
126 // one.
127 return create_new_profile (object_key, mprofile, priority);
129 else
131 // A SHMIOP_Profile already exists - just add our endpoint to it.
133 TAO_SHMIOP_Endpoint *endpoint = 0;
134 ACE_NEW_RETURN (endpoint,
135 TAO_SHMIOP_Endpoint (this->host_.c_str (),
136 this->address_.get_port_number (),
137 this->address_.get_remote_addr ()),
138 -1);
139 endpoint->priority (priority);
140 shmiop_profile->add_endpoint (endpoint);
142 return 0;
147 TAO_SHMIOP_Acceptor::is_collocated (const TAO_Endpoint *endpoint)
149 const TAO_SHMIOP_Endpoint *endp =
150 dynamic_cast <const TAO_SHMIOP_Endpoint *> (endpoint);
152 // Make sure the dynamically cast pointer is valid.
153 if (endp == 0)
154 return 0;
156 if (endp->port () != this->address_.get_port_number ())
157 return 0;
159 return this->address_.same_host (endp->object_addr ());
163 TAO_SHMIOP_Acceptor::close ()
165 return this->base_acceptor_.close ();
169 TAO_SHMIOP_Acceptor::open (TAO_ORB_Core *orb_core,
170 ACE_Reactor *reactor,
171 int major,
172 int minor,
173 const char *port,
174 const char *options)
176 if (major >=0 && minor >= 0)
177 this->version_.set_version (static_cast <CORBA::Octet>(major),
178 static_cast <CORBA::Octet>(minor));
179 // Parse options
180 if (this->parse_options (options) == -1)
181 return -1;
183 if (port)
185 if (ACE_OS::ace_isdigit (*port) == 0)
186 return -1; // Port number must consist of digits
188 this->address_.set (ACE_TEXT_CHAR_TO_TCHAR(port));
191 return this->open_i (orb_core, reactor);
195 TAO_SHMIOP_Acceptor::open_default (TAO_ORB_Core *orb_core,
196 ACE_Reactor *reactor,
197 int major,
198 int minor,
199 const char *options)
201 if (major >=0 && minor >= 0)
202 this->version_.set_version (static_cast <CORBA::Octet>(major),
203 static_cast <CORBA::Octet>(minor));
205 // Parse options
206 if (this->parse_options (options) == -1)
207 return -1;
209 // @@ Until we can support multihomed machines correctly we must
210 // pick the "default interface" and only listen on that IP
211 // address.
212 this->host_ = this->address_.get_host_name ();
214 return this->open_i (orb_core, reactor);
218 TAO_SHMIOP_Acceptor::set_mmap_options (const ACE_TCHAR *prefix, ACE_OFF_T size)
220 this->mmap_file_prefix_ = prefix;
221 this->mmap_size_ = size;
223 return 0;
227 TAO_SHMIOP_Acceptor::open_i (TAO_ORB_Core* orb_core, ACE_Reactor *reactor)
229 this->orb_core_ = orb_core;
231 ACE_NEW_RETURN (this->creation_strategy_,
232 TAO_SHMIOP_CREATION_STRATEGY (this->orb_core_),
233 -1);
235 ACE_NEW_RETURN (this->concurrency_strategy_,
236 TAO_SHMIOP_CONCURRENCY_STRATEGY (this->orb_core_),
237 -1);
239 ACE_NEW_RETURN (this->accept_strategy_,
240 TAO_SHMIOP_ACCEPT_STRATEGY (this->orb_core_),
241 -1);
243 // We only accept connection on localhost.
244 if (this->base_acceptor_.open (this->address_,
245 reactor,
246 this->creation_strategy_,
247 this->accept_strategy_,
248 this->concurrency_strategy_) == -1)
250 if (TAO_debug_level > 0)
251 TAOLIB_ERROR ((LM_ERROR,
252 ACE_TEXT ("TAO (%P|%t) - SHMIOP_Acceptor::open_i, %p\n\n"),
253 ACE_TEXT ("cannot open acceptor")));
254 return -1;
257 this->base_acceptor_.acceptor().mmap_prefix (this->mmap_file_prefix_);
258 this->base_acceptor_.acceptor().init_buffer_size (this->mmap_size_);
260 if (orb_core->server_factory ()->activate_server_connections () != 0)
261 this->base_acceptor_.acceptor().preferred_strategy (ACE_MEM_IO::MT);
263 // @@ Should this be a catastrophic error???
264 if (this->base_acceptor_.acceptor ().get_local_addr (this->address_) != 0)
266 if (TAO_debug_level > 0)
267 TAOLIB_ERROR ((LM_ERROR,
268 ACE_TEXT ("TAO (%P|%t) - SHMIOP_Acceptor::open_i, %p\n\n"),
269 ACE_TEXT ("cannot get local addr\n")));
270 return -1;
273 // If the ORB is instructed to use dotted decimal addresses, we respect that
274 // also for shmiop
275 if (orb_core->orb_params ()->use_dotted_decimal_addresses ())
277 // Get the ip address, we get the remote addr to put in the IOR, don't
278 // calls get_host_addr() directly on address_, we then get the internal
279 // address back
280 const char *tmp = this->address_.get_remote_addr().get_host_addr ();
282 if (tmp == 0)
284 if (TAO_debug_level > 0)
285 TAOLIB_ERROR ((LM_ERROR,
286 ACE_TEXT ("TAO (%P|%t) - ")
287 ACE_TEXT ("SHMIOP_Acceptor::open_i, ")
288 ACE_TEXT ("- %p, "),
289 ACE_TEXT ("cannot determine hostname\n")));
290 return -1;
293 this->host_ = tmp;
295 else
297 // This will be the actualy host name of the original endpoint.
298 ACE_TCHAR tmp_host[MAXHOSTNAMELEN+1];
300 if (this->address_.get_host_name (tmp_host, sizeof tmp_host) != 0)
302 if (TAO_debug_level > 0)
303 TAOLIB_ERROR ((LM_ERROR,
304 ACE_TEXT ("TAO (%P|%t) - SHMIOP_Acceptor::open_i, - %p\n"),
305 ACE_TEXT ("cannot cache hostname\n")));
306 return -1;
308 this->host_ = ACE_TEXT_ALWAYS_CHAR(tmp_host);
311 // This avoids having child processes acquire the listen socket thereby
312 // denying the server the opportunity to restart on a well-known endpoint.
313 // This does not affect the aberrent behavior on Win32 platforms.
314 (void) this->base_acceptor_.acceptor().enable (ACE_CLOEXEC);
316 if (TAO_debug_level > 5)
318 TAOLIB_DEBUG ((LM_DEBUG,
319 ACE_TEXT ("TAO (%P|%t) - SHMIOP_Acceptor::open_i, ")
320 ACE_TEXT ("listening on : <%C:%u>\n"),
321 this->host_.c_str (),
322 this->address_.get_port_number ()));
325 // In the event that an accept() fails, we can examine the reason. If
326 // the reason warrants it, we can try accepting again at a later time.
327 // The amount of time we wait to accept again is governed by this orb
328 // parameter.
329 this->set_error_retry_delay (
330 this->orb_core_->orb_params ()->accept_error_delay());
332 return 0;
337 TAO_SHMIOP_Acceptor::object_key (IOP::TaggedProfile &profile,
338 TAO::ObjectKey &object_key)
340 // Create the decoding stream from the encapsulation in the buffer,
341 #if (TAO_NO_COPY_OCTET_SEQUENCES == 1)
342 TAO_InputCDR cdr (profile.profile_data.mb ());
343 #else
344 TAO_InputCDR cdr (reinterpret_cast<char*>(profile.profile_data.get_buffer ()),
345 profile.profile_data.length ());
346 #endif /* TAO_NO_COPY_OCTET_SEQUENCES == 1 */
348 CORBA::Octet major = 0;
349 CORBA::Octet minor = 0;
351 // Read the version. We just read it here. We don't*do any*
352 // processing.
353 if (!(cdr.read_octet (major) && cdr.read_octet (minor)))
355 if (TAO_debug_level > 0)
357 TAOLIB_DEBUG ((LM_DEBUG,
358 ACE_TEXT ("TAO (%P|%t) - SHMIOP_Profile::decode, v%d.%d\n"),
359 major,
360 minor));
362 return -1;
365 CORBA::String_var host;
366 CORBA::UShort port = 0;
368 // Get host and port. No processing here too..
369 if (cdr.read_string (host.out ()) == 0
370 || cdr.read_ushort (port) == 0)
372 if (TAO_debug_level > 0)
374 TAOLIB_ERROR ((LM_ERROR,
375 ACE_TEXT ("TAO (%P|%t) - SHMIOP_Acceptor::object_key, ")
376 ACE_TEXT ("error while decoding host/port\n")));
378 return -1;
381 // ... and object key.
382 if ((cdr >> object_key) == 0)
383 return -1;
385 // We are NOT bothered about the rest.
387 return 1;
391 CORBA::ULong
392 TAO_SHMIOP_Acceptor::endpoint_count ()
394 // @@ for now just assume one!
395 // we should take a look at the local address, if it is zero then
396 // get the list of available IP interfaces and return this number.
397 return 1;
401 TAO_SHMIOP_Acceptor::parse_options (const char *str)
403 if (str == 0)
404 return 0; // No options to parse. Not a problem.
406 // Use an option format similar to the one used for CGI scripts in
407 // HTTP URLs.
408 // e.g.: option1=foo&option2=bar
410 ACE_CString options (str);
412 size_t len = options.length ();
414 const char option_delimiter = '&';
416 // Count the number of options.
418 CORBA::ULong option_count = 1;
419 // Number of endpoints in the string (initialized to 1).
421 // Only check for endpoints after the protocol specification and
422 // before the object key.
423 for (size_t i = 0; i < len; ++i)
424 if (options[i] == option_delimiter)
425 ++option_count;
427 // The idea behind the following loop is to split the options into
428 // (option, name) pairs.
429 // For example,
430 // `option1=foo&option2=bar'
431 // will be parsed into:
432 // `option1=foo'
433 // `option2=bar'
435 ACE_CString::size_type begin = 0;
436 ACE_CString::size_type end = 0;
438 for (CORBA::ULong j = 0; j < option_count; ++j)
440 if (j < option_count - 1)
441 end = options.find (option_delimiter, begin);
442 else
443 end = len;
445 if (end == begin)
446 TAOLIB_ERROR_RETURN ((LM_ERROR,
447 ACE_TEXT ("TAO (%P|%t) Zero length SHMIOP option.\n")),
448 -1);
449 else if (end != ACE_CString::npos)
451 ACE_CString opt = options.substring (begin, end - begin);
453 ACE_CString::size_type const slot = opt.find ("=");
455 if (slot == len - 1
456 || slot == ACE_CString::npos)
457 TAOLIB_ERROR_RETURN ((LM_ERROR,
458 ACE_TEXT ("TAO (%P|%t) SHMIOP option <%C> is ")
459 ACE_TEXT ("missing a value.\n"),
460 opt.c_str ()),
461 -1);
463 ACE_CString name = opt.substring (0, slot);
464 ACE_CString value = opt.substring (slot + 1);
466 begin = end + 1;
468 if (name.length () == 0)
469 TAOLIB_ERROR_RETURN ((LM_ERROR,
470 ACE_TEXT ("TAO (%P|%t) Zero length SHMIOP ")
471 ACE_TEXT ("option name.\n")),
472 -1);
474 if (name == "priority")
476 TAOLIB_ERROR_RETURN ((LM_ERROR,
477 ACE_TEXT ("TAO (%P|%t) Invalid SHMIOP endpoint format: ")
478 ACE_TEXT ("endpoint priorities no longer supported.\n")),
479 -1);
481 else
482 TAOLIB_ERROR_RETURN ((LM_ERROR,
483 ACE_TEXT ("TAO (%P|%t) Invalid SHMIOP option: <%C>\n"),
484 name.c_str ()),
485 -1);
487 else
488 break; // No other options.
490 return 0;
493 TAO_END_VERSIONED_NAMESPACE_DECL
495 #endif /* TAO_HAS_SHMIOP && TAO_HAS_SHMIOP != 0 */