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