00001
00002
00003 #include "tao/Strategies/SHMIOP_Acceptor.h"
00004
00005 #if defined (TAO_HAS_SHMIOP) && (TAO_HAS_SHMIOP != 0)
00006
00007 #include "tao/Strategies/SHMIOP_Profile.h"
00008 #include "tao/MProfile.h"
00009 #include "tao/ORB_Core.h"
00010 #include "tao/Server_Strategy_Factory.h"
00011 #include "tao/debug.h"
00012 #include "tao/Codeset_Manager.h"
00013 #include "tao/CDR.h"
00014
00015 #include "ace/os_include/os_netdb.h"
00016 #include "ace/OS_NS_ctype.h"
00017
00018 ACE_RCSID (Strategies,
00019 SHMIOP_Acceptor,
00020 "$Id: SHMIOP_Acceptor.cpp 83166 2008-10-12 20:55:37Z johnnyw $")
00021
00022 TAO_BEGIN_VERSIONED_NAMESPACE_DECL
00023
00024 TAO_SHMIOP_Acceptor::TAO_SHMIOP_Acceptor (void)
00025 : TAO_Acceptor (TAO_TAG_SHMEM_PROFILE),
00026 version_ (TAO_DEF_GIOP_MAJOR, TAO_DEF_GIOP_MINOR),
00027 orb_core_ (0),
00028 base_acceptor_ (this),
00029 creation_strategy_ (0),
00030 concurrency_strategy_ (0),
00031 accept_strategy_ (0),
00032 mmap_file_prefix_ (0),
00033 mmap_size_ (1024 * 1024)
00034 {
00035 }
00036
00037 TAO_SHMIOP_Acceptor::~TAO_SHMIOP_Acceptor (void)
00038 {
00039
00040
00041 this->close ();
00042
00043 delete this->creation_strategy_;
00044 delete this->concurrency_strategy_;
00045 delete this->accept_strategy_;
00046 }
00047
00048
00049
00050
00051
00052
00053 int
00054 TAO_SHMIOP_Acceptor::create_profile (const TAO::ObjectKey &object_key,
00055 TAO_MProfile &mprofile,
00056 CORBA::Short priority)
00057 {
00058
00059
00060 if (priority == TAO_INVALID_PRIORITY)
00061 return this->create_new_profile (object_key, mprofile, priority);
00062 else
00063 return this->create_shared_profile (object_key, mprofile, priority);
00064 }
00065
00066 int
00067 TAO_SHMIOP_Acceptor::create_new_profile (const TAO::ObjectKey &object_key,
00068 TAO_MProfile &mprofile,
00069 CORBA::Short priority)
00070 {
00071
00072 int count = mprofile.profile_count ();
00073 if ((mprofile.size () - count) < 1
00074 && mprofile.grow (count + 1) == -1)
00075 return -1;
00076
00077 TAO_SHMIOP_Profile *pfile = 0;
00078 ACE_NEW_RETURN (pfile,
00079 TAO_SHMIOP_Profile (this->host_.c_str (),
00080 this->address_.get_port_number (),
00081 object_key,
00082 this->address_.get_remote_addr (),
00083 this->version_,
00084 this->orb_core_),
00085 -1);
00086 pfile->endpoint ()->priority (priority);
00087
00088 if (mprofile.give_profile (pfile) == -1)
00089 {
00090 pfile->_decr_refcnt ();
00091 pfile = 0;
00092 return -1;
00093 }
00094
00095
00096
00097
00098
00099 if (this->orb_core_->orb_params ()->std_profile_components () == 0
00100 || (this->version_.major == 1 && this->version_.minor == 0))
00101 return 0;
00102
00103 pfile->tagged_components ().set_orb_type (TAO_ORB_TYPE);
00104 TAO_Codeset_Manager *csm = this->orb_core_->codeset_manager();
00105 if (csm)
00106 csm->set_codeset(pfile->tagged_components());
00107 return 0;
00108 }
00109
00110 int
00111 TAO_SHMIOP_Acceptor::create_shared_profile (const TAO::ObjectKey &object_key,
00112 TAO_MProfile &mprofile,
00113 CORBA::Short priority)
00114 {
00115 TAO_Profile *pfile = 0;
00116 TAO_SHMIOP_Profile *shmiop_profile = 0;
00117
00118
00119 for (TAO_PHandle i = 0; i != mprofile.profile_count (); ++i)
00120 {
00121 pfile = mprofile.get_profile (i);
00122 if (pfile->tag () == TAO_TAG_SHMEM_PROFILE)
00123 {
00124 shmiop_profile = dynamic_cast <TAO_SHMIOP_Profile *>(pfile);
00125 break;
00126 }
00127 }
00128
00129 if (shmiop_profile == 0)
00130 {
00131
00132
00133 return create_new_profile (object_key, mprofile, priority);
00134 }
00135 else
00136 {
00137
00138
00139 TAO_SHMIOP_Endpoint *endpoint = 0;
00140 ACE_NEW_RETURN (endpoint,
00141 TAO_SHMIOP_Endpoint (this->host_.c_str (),
00142 this->address_.get_port_number (),
00143 this->address_.get_remote_addr ()),
00144 -1);
00145 endpoint->priority (priority);
00146 shmiop_profile->add_endpoint (endpoint);
00147
00148 return 0;
00149 }
00150 }
00151
00152 int
00153 TAO_SHMIOP_Acceptor::is_collocated (const TAO_Endpoint *endpoint)
00154 {
00155 const TAO_SHMIOP_Endpoint *endp =
00156 dynamic_cast <const TAO_SHMIOP_Endpoint *> (endpoint);
00157
00158
00159 if (endp == 0)
00160 return 0;
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174 return this->address_.same_host (endp->object_addr ());
00175 }
00176
00177 int
00178 TAO_SHMIOP_Acceptor::close (void)
00179 {
00180 return this->base_acceptor_.close ();
00181 }
00182
00183 int
00184 TAO_SHMIOP_Acceptor::open (TAO_ORB_Core *orb_core,
00185 ACE_Reactor *reactor,
00186 int major,
00187 int minor,
00188 const char *port,
00189 const char *options)
00190 {
00191 if (major >=0 && minor >= 0)
00192 this->version_.set_version (static_cast <CORBA::Octet>(major),
00193 static_cast <CORBA::Octet>(minor));
00194
00195 if (this->parse_options (options) == -1)
00196 return -1;
00197
00198 if (ACE_OS::ace_isdigit (*port) == 0)
00199 return -1;
00200
00201 if (port)
00202 this->address_.set (ACE_TEXT_CHAR_TO_TCHAR(port));
00203
00204 return this->open_i (orb_core, reactor);
00205 }
00206
00207 int
00208 TAO_SHMIOP_Acceptor::open_default (TAO_ORB_Core *orb_core,
00209 ACE_Reactor *reactor,
00210 int major,
00211 int minor,
00212 const char *options)
00213 {
00214 if (major >=0 && minor >= 0)
00215 this->version_.set_version (static_cast <CORBA::Octet>(major),
00216 static_cast <CORBA::Octet>(minor));
00217
00218
00219 if (this->parse_options (options) == -1)
00220 return -1;
00221
00222
00223
00224
00225 this->host_ = this->address_.get_host_name ();
00226
00227 return this->open_i (orb_core, reactor);
00228 }
00229
00230 int
00231 TAO_SHMIOP_Acceptor::set_mmap_options (const ACE_TCHAR *prefix, ACE_OFF_T size)
00232 {
00233 this->mmap_file_prefix_ = prefix;
00234 this->mmap_size_ = size;
00235
00236 return 0;
00237 }
00238
00239 int
00240 TAO_SHMIOP_Acceptor::open_i (TAO_ORB_Core* orb_core, ACE_Reactor *reactor)
00241 {
00242 this->orb_core_ = orb_core;
00243
00244 ACE_NEW_RETURN (this->creation_strategy_,
00245 TAO_SHMIOP_CREATION_STRATEGY (this->orb_core_),
00246 -1);
00247
00248 ACE_NEW_RETURN (this->concurrency_strategy_,
00249 TAO_SHMIOP_CONCURRENCY_STRATEGY (this->orb_core_),
00250 -1);
00251
00252 ACE_NEW_RETURN (this->accept_strategy_,
00253 TAO_SHMIOP_ACCEPT_STRATEGY (this->orb_core_),
00254 -1);
00255
00256
00257 if (this->base_acceptor_.open (this->address_,
00258 reactor,
00259 this->creation_strategy_,
00260 this->accept_strategy_,
00261 this->concurrency_strategy_) == -1)
00262 {
00263 if (TAO_debug_level > 0)
00264 ACE_ERROR ((LM_ERROR,
00265 ACE_TEXT ("TAO (%P|%t) - SHMIOP_Acceptor::open_i, %p\n\n"),
00266 ACE_TEXT ("cannot open acceptor")));
00267 return -1;
00268 }
00269
00270 this->base_acceptor_.acceptor().mmap_prefix (this->mmap_file_prefix_);
00271 this->base_acceptor_.acceptor().init_buffer_size (this->mmap_size_);
00272
00273 if (orb_core->server_factory ()->activate_server_connections () != 0)
00274 this->base_acceptor_.acceptor().preferred_strategy (ACE_MEM_IO::MT);
00275
00276
00277 if (this->base_acceptor_.acceptor ().get_local_addr (this->address_) != 0)
00278 {
00279 if (TAO_debug_level > 0)
00280 ACE_ERROR ((LM_ERROR,
00281 ACE_TEXT ("TAO (%P|%t) - SHMIOP_Acceptor::open_i, %p\n\n"),
00282 ACE_TEXT ("cannot get local addr\n")));
00283 return -1;
00284 }
00285
00286
00287
00288 if (orb_core->orb_params ()->use_dotted_decimal_addresses ())
00289 {
00290
00291
00292
00293 const char *tmp = this->address_.get_remote_addr().get_host_addr ();
00294
00295 if (tmp == 0)
00296 {
00297 if (TAO_debug_level > 0)
00298 ACE_ERROR ((LM_ERROR,
00299 ACE_TEXT ("TAO (%P|%t) - ")
00300 ACE_TEXT ("SHMIOP_Acceptor::open_i, ")
00301 ACE_TEXT ("- %p, "),
00302 ACE_TEXT ("cannot determine hostname\n")));
00303 return -1;
00304 }
00305
00306 this->host_ = tmp;
00307 }
00308 else
00309 {
00310
00311 ACE_TCHAR tmp_host[MAXHOSTNAMELEN+1];
00312
00313 if (this->address_.get_host_name (tmp_host, sizeof tmp_host) != 0)
00314 {
00315 if (TAO_debug_level > 0)
00316 ACE_ERROR ((LM_ERROR,
00317 ACE_TEXT ("TAO (%P|%t) - SHMIOP_Acceptor::open_i, - %p\n"),
00318 ACE_TEXT ("cannot cache hostname\n")));
00319 return -1;
00320 }
00321 this->host_ = ACE_TEXT_ALWAYS_CHAR(tmp_host);
00322 }
00323
00324
00325
00326
00327 (void) this->base_acceptor_.acceptor().enable (ACE_CLOEXEC);
00328
00329 if (TAO_debug_level > 5)
00330 {
00331 ACE_DEBUG ((LM_DEBUG,
00332 ACE_TEXT ("TAO (%P|%t) - SHMIOP_Acceptor::open_i, ")
00333 ACE_TEXT ("listening on : <%C:%u>\n"),
00334 this->host_.c_str (),
00335 this->address_.get_port_number ()));
00336 }
00337
00338
00339
00340
00341
00342 this->set_error_retry_delay (
00343 this->orb_core_->orb_params ()->accept_error_delay());
00344
00345 return 0;
00346 }
00347
00348
00349 int
00350 TAO_SHMIOP_Acceptor::object_key (IOP::TaggedProfile &profile,
00351 TAO::ObjectKey &object_key)
00352 {
00353
00354 #if (TAO_NO_COPY_OCTET_SEQUENCES == 1)
00355 TAO_InputCDR cdr (profile.profile_data.mb ());
00356 #else
00357 TAO_InputCDR cdr (reinterpret_cast<char*>(profile.profile_data.get_buffer ()),
00358 profile.profile_data.length ());
00359 #endif
00360
00361 CORBA::Octet major = 0;
00362 CORBA::Octet minor = 0;
00363
00364
00365
00366 if (!(cdr.read_octet (major) && cdr.read_octet (minor)))
00367 {
00368 if (TAO_debug_level > 0)
00369 {
00370 ACE_DEBUG ((LM_DEBUG,
00371 ACE_TEXT ("TAO (%P|%t) - SHMIOP_Profile::decode, v%d.%d\n"),
00372 major,
00373 minor));
00374 }
00375 return -1;
00376 }
00377
00378 CORBA::String_var host;
00379 CORBA::UShort port = 0;
00380
00381
00382 if (cdr.read_string (host.out ()) == 0
00383 || cdr.read_ushort (port) == 0)
00384 {
00385 if (TAO_debug_level > 0)
00386 {
00387 ACE_ERROR ((LM_ERROR,
00388 ACE_TEXT ("TAO (%P|%t) - SHMIOP_Acceptor::object_key, ")
00389 ACE_TEXT ("error while decoding host/port\n")));
00390 }
00391 return -1;
00392 }
00393
00394
00395 if ((cdr >> object_key) == 0)
00396 return -1;
00397
00398
00399
00400 return 1;
00401 }
00402
00403
00404 CORBA::ULong
00405 TAO_SHMIOP_Acceptor::endpoint_count (void)
00406 {
00407
00408
00409
00410 return 1;
00411 }
00412
00413 int
00414 TAO_SHMIOP_Acceptor::parse_options (const char *str)
00415 {
00416 if (str == 0)
00417 return 0;
00418
00419
00420
00421
00422
00423 ACE_CString options (str);
00424
00425 size_t len = options.length ();
00426
00427 const char option_delimiter = '&';
00428
00429
00430
00431 CORBA::ULong option_count = 1;
00432
00433
00434
00435
00436 for (size_t i = 0; i < len; ++i)
00437 if (options[i] == option_delimiter)
00438 ++option_count;
00439
00440
00441
00442
00443
00444
00445
00446
00447
00448 ACE_CString::size_type begin = 0;
00449 ACE_CString::size_type end = 0;
00450
00451 for (CORBA::ULong j = 0; j < option_count; ++j)
00452 {
00453 if (j < option_count - 1)
00454 end = options.find (option_delimiter, begin);
00455 else
00456 end = len;
00457
00458 if (end == begin)
00459 ACE_ERROR_RETURN ((LM_ERROR,
00460 ACE_TEXT ("TAO (%P|%t) Zero length SHMIOP option.\n")),
00461 -1);
00462 else if (end != ACE_CString::npos)
00463 {
00464 ACE_CString opt = options.substring (begin, end - begin);
00465
00466 ACE_CString::size_type const slot = opt.find ("=");
00467
00468 if (slot == len - 1
00469 || slot == ACE_CString::npos)
00470 ACE_ERROR_RETURN ((LM_ERROR,
00471 ACE_TEXT ("TAO (%P|%t) SHMIOP option <%C> is ")
00472 ACE_TEXT ("missing a value.\n"),
00473 opt.c_str ()),
00474 -1);
00475
00476 ACE_CString name = opt.substring (0, slot);
00477 ACE_CString value = opt.substring (slot + 1);
00478
00479 begin = end + 1;
00480
00481 if (name.length () == 0)
00482 ACE_ERROR_RETURN ((LM_ERROR,
00483 ACE_TEXT ("TAO (%P|%t) Zero length SHMIOP ")
00484 ACE_TEXT ("option name.\n")),
00485 -1);
00486
00487 if (name == "priority")
00488 {
00489 ACE_ERROR_RETURN ((LM_ERROR,
00490 ACE_TEXT ("TAO (%P|%t) Invalid SHMIOP endpoint format: ")
00491 ACE_TEXT ("endpoint priorities no longer supported.\n")),
00492 -1);
00493 }
00494 else
00495 ACE_ERROR_RETURN ((LM_ERROR,
00496 ACE_TEXT ("TAO (%P|%t) Invalid SHMIOP option: <%C>\n"),
00497 name.c_str ()),
00498 -1);
00499 }
00500 else
00501 break;
00502 }
00503 return 0;
00504 }
00505
00506 TAO_END_VERSIONED_NAMESPACE_DECL
00507
00508 #endif