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"
10 #include "tao/Codeset_Manager.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
),
22 base_acceptor_ (this),
23 creation_strategy_ (0),
24 concurrency_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
37 delete this->creation_strategy_
;
38 delete this->concurrency_strategy_
;
39 delete this->accept_strategy_
;
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.
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
);
57 return this->create_shared_profile (object_key
, mprofile
, priority
);
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)
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 (),
76 this->address_
.get_remote_addr (),
80 pfile
->endpoint ()->priority (priority
);
82 if (mprofile
.give_profile (pfile
) == -1)
84 pfile
->_decr_refcnt ();
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))
97 pfile
->tagged_components ().set_orb_type (TAO_ORB_TYPE
);
98 TAO_Codeset_Manager
*csm
= this->orb_core_
->codeset_manager();
100 csm
->set_codeset(pfile
->tagged_components());
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
);
123 if (shmiop_profile
== 0)
125 // If <mprofile> doesn't contain SHMIOP_Profile, we need to create
127 return create_new_profile (object_key
, mprofile
, priority
);
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 ()),
139 endpoint
->priority (priority
);
140 shmiop_profile
->add_endpoint (endpoint
);
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.
156 if (endp
->port () != this->address_
.get_port_number ())
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
,
176 if (major
>=0 && minor
>= 0)
177 this->version_
.set_version (static_cast <CORBA::Octet
>(major
),
178 static_cast <CORBA::Octet
>(minor
));
180 if (this->parse_options (options
) == -1)
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
,
201 if (major
>=0 && minor
>= 0)
202 this->version_
.set_version (static_cast <CORBA::Octet
>(major
),
203 static_cast <CORBA::Octet
>(minor
));
206 if (this->parse_options (options
) == -1)
209 // @@ Until we can support multihomed machines correctly we must
210 // pick the "default interface" and only listen on that IP
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
;
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_
),
235 ACE_NEW_RETURN (this->concurrency_strategy_
,
236 TAO_SHMIOP_CONCURRENCY_STRATEGY (this->orb_core_
),
239 ACE_NEW_RETURN (this->accept_strategy_
,
240 TAO_SHMIOP_ACCEPT_STRATEGY (this->orb_core_
),
243 // We only accept connection on localhost.
244 if (this->base_acceptor_
.open (this->address_
,
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")));
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")));
273 // If the ORB is instructed to use dotted decimal addresses, we respect that
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
280 const char *tmp
= this->address_
.get_remote_addr().get_host_addr ();
284 if (TAO_debug_level
> 0)
285 TAOLIB_ERROR ((LM_ERROR
,
286 ACE_TEXT ("TAO (%P|%t) - ")
287 ACE_TEXT ("SHMIOP_Acceptor::open_i, ")
289 ACE_TEXT ("cannot determine hostname\n")));
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")));
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
329 this->set_error_retry_delay (
330 this->orb_core_
->orb_params ()->accept_error_delay());
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 ());
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*
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"),
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")));
381 // ... and object key.
382 if ((cdr
>> object_key
) == 0)
385 // We are NOT bothered about the rest.
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.
401 TAO_SHMIOP_Acceptor::parse_options (const char *str
)
404 return 0; // No options to parse. Not a problem.
406 // Use an option format similar to the one used for CGI scripts in
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
)
427 // The idea behind the following loop is to split the options into
428 // (option, name) pairs.
430 // `option1=foo&option2=bar'
431 // will be parsed into:
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
);
446 TAOLIB_ERROR_RETURN ((LM_ERROR
,
447 ACE_TEXT ("TAO (%P|%t) Zero length SHMIOP option.\n")),
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 ("=");
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"),
463 ACE_CString name
= opt
.substring (0, slot
);
464 ACE_CString value
= opt
.substring (slot
+ 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")),
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")),
482 TAOLIB_ERROR_RETURN ((LM_ERROR
,
483 ACE_TEXT ("TAO (%P|%t) Invalid SHMIOP option: <%C>\n"),
488 break; // No other options.
493 TAO_END_VERSIONED_NAMESPACE_DECL
495 #endif /* TAO_HAS_SHMIOP && TAO_HAS_SHMIOP != 0 */