SHMIOP_Acceptor.cpp

Go to the documentation of this file.
00001 // SHMIOP_Acceptor.cpp,v 1.35 2006/03/10 07:19:18 jtc Exp
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   // Make sure we are closed before we start destroying the
00041   // strategies.
00042   this->close ();
00043 
00044   delete this->creation_strategy_;
00045   delete this->concurrency_strategy_;
00046   delete this->accept_strategy_;
00047 }
00048 
00049 // TODO =
00050 //    2) For V1.[1,2] there are tagged components
00051 //    3) Create multiple profiles for wild carded endpoints (may be multiple
00052 //       interfaces over which we can receive requests.  Thus a profile
00053 //       must be made for each one.
00054 int
00055 TAO_SHMIOP_Acceptor::create_profile (const TAO::ObjectKey &object_key,
00056                                      TAO_MProfile &mprofile,
00057                                      CORBA::Short priority)
00058 {
00059   // Check if multiple endpoints should be put in one profile or
00060   // if they should be spread across multiple profiles.
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   // @@ we only make one for now
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   // Do not add any tagged components to the profile if configured
00101   // by the user not to do so, or if an SHMIOP 1.0 endpoint is being
00102   // created (IIOP 1.0 did not support tagged components, so we follow
00103   // the same convention for SHMIOP).
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   // First see if <mprofile> already contains a SHMIOP profile.
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       // If <mprofile> doesn't contain SHMIOP_Profile, we need to create
00137       // one.
00138       return create_new_profile (object_key,
00139                                  mprofile,
00140                                  priority);
00141     }
00142   else
00143     {
00144       // A SHMIOP_Profile already exists - just add our endpoint to it.
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   // Make sure the dynamically cast pointer is valid.
00166   if (endp == 0)
00167     return 0;
00168 
00169       // @@ TODO The following code looks funky, why only the host
00170       //    name is compared?  What if there are multiple SHMIOP
00171       //    servers in the same address?  Why do SHMIOP_Endpoints keep
00172       //    a INET_Addr but not a MEM_Addr?  And why is there no lazy
00173       //    evaluation of IP-addresses for SHMIOP endpoints?  Is it
00174       //    because it is always 'localhost'?  We need answers to
00175       //    these questions to solve:
00176       //
00177       // http://deuce.doc.wustl.edu/bugzilla/show_bug.cgi?id=1220
00178       //
00179   // The following code is suspec
00180   // compare the port and sin_addr (numeric host address)
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   // Parse options
00202   if (this->parse_options (options) == -1)
00203     return -1;
00204 
00205   if (isdigit (*port) == 0)
00206     return -1;                  // Port number must consist of digits
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   // Parse options
00227   if (this->parse_options (options) == -1)
00228     return -1;
00229 
00230   // @@ Until we can support multihomed machines correctly we must
00231   //    pick the "default interface" and only listen on that IP
00232   //    address.
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   // We only accept connection on localhost.
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   // @@ Should this be a catastrophic error???
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   // If the ORB is instructed to use dotted decimal addresses, we respect that
00299   // also for shmiop
00300   if (orb_core->orb_params ()->use_dotted_decimal_addresses ())
00301     {
00302       // Get the ip address, we get the remote addr to put in the IOR, don't
00303       // calls get_host_addr() directly on address_, we then get the internal
00304       // address back
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       // This will be the actualy host name of the original endpoint.
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   // This avoids having child processes acquire the listen socket thereby
00338   // denying the server the opportunity to restart on a well-known endpoint.
00339   // This does not affect the aberrent behavior on Win32 platforms.
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   // Create the decoding stream from the encapsulation in the buffer,
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 /* TAO_NO_COPY_OCTET_SEQUENCES == 1 */
00365 
00366   CORBA::Octet major = 0;
00367   CORBA::Octet minor = 0;
00368 
00369   // Read the version. We just read it here. We don't*do any*
00370   // processing.
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   // Get host and port. No processing here too..
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   // ... and object key.
00401   if ((cdr >> object_key) == 0)
00402     return -1;
00403 
00404   // We are NOT bothered about the rest.
00405 
00406   return 1;
00407 }
00408 
00409 
00410 CORBA::ULong
00411 TAO_SHMIOP_Acceptor::endpoint_count (void)
00412 {
00413   // @@ for now just assume one!
00414   // we should take a look at the local address, if it is zero then
00415   // get the list of available IP interfaces and return this number.
00416   return 1;
00417 }
00418 
00419 int
00420 TAO_SHMIOP_Acceptor::parse_options (const char *str)
00421 {
00422   if (str == 0)
00423     return 0;  // No options to parse.  Not a problem.
00424 
00425   // Use an option format similar to the one used for CGI scripts in
00426   // HTTP URLs.
00427   // e.g.:  option1=foo&option2=bar
00428 
00429   ACE_CString options (str);
00430 
00431   size_t len = options.length ();
00432 
00433   const char option_delimiter = '&';
00434 
00435   // Count the number of options.
00436 
00437   CORBA::ULong option_count = 1;
00438   // Number of endpoints in the string  (initialized to 1).
00439 
00440   // Only check for endpoints after the protocol specification and
00441   // before the object key.
00442   for (size_t i = 0; i < len; ++i)
00443     if (options[i] == option_delimiter)
00444       option_count++;
00445 
00446   // The idea behind the following loop is to split the options into
00447   // (option, name) pairs.
00448   // For example,
00449   //    `option1=foo&option2=bar'
00450   // will be parsed into:
00451   //    `option1=foo'
00452   //    `option2=bar'
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); // Handle last endpoint differently
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 /* TAO_HAS_SHMIOP && TAO_HAS_SHMIOP != 0 */

Generated on Thu Nov 9 13:39:29 2006 for TAO_Strategies by doxygen 1.3.6