SHMIOP_Acceptor.cpp

Go to the documentation of this file.
00001 // $Id: SHMIOP_Acceptor.cpp 81470 2008-04-28 12:40:23Z elliott_c $
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 81470 2008-04-28 12:40:23Z elliott_c $")
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   // Make sure we are closed before we start destroying the
00040   // strategies.
00041   this->close ();
00042 
00043   delete this->creation_strategy_;
00044   delete this->concurrency_strategy_;
00045   delete this->accept_strategy_;
00046 }
00047 
00048 // TODO =
00049 //    2) For V1.[1,2] there are tagged components
00050 //    3) Create multiple profiles for wild carded endpoints (may be multiple
00051 //       interfaces over which we can receive requests.  Thus a profile
00052 //       must be made for each one.
00053 int
00054 TAO_SHMIOP_Acceptor::create_profile (const TAO::ObjectKey &object_key,
00055                                      TAO_MProfile &mprofile,
00056                                      CORBA::Short priority)
00057 {
00058   // Check if multiple endpoints should be put in one profile or
00059   // if they should be spread across multiple profiles.
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   // @@ we only make one for now
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   // Do not add any tagged components to the profile if configured
00096   // by the user not to do so, or if an SHMIOP 1.0 endpoint is being
00097   // created (IIOP 1.0 did not support tagged components, so we follow
00098   // the same convention for SHMIOP).
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   // First see if <mprofile> already contains a SHMIOP profile.
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       // If <mprofile> doesn't contain SHMIOP_Profile, we need to create
00132       // one.
00133       return create_new_profile (object_key, mprofile, priority);
00134     }
00135   else
00136     {
00137       // A SHMIOP_Profile already exists - just add our endpoint to it.
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   // Make sure the dynamically cast pointer is valid.
00159   if (endp == 0)
00160     return 0;
00161 
00162       // @@ TODO The following code looks funky, why only the host
00163       //    name is compared?  What if there are multiple SHMIOP
00164       //    servers in the same address?  Why do SHMIOP_Endpoints keep
00165       //    a INET_Addr but not a MEM_Addr?  And why is there no lazy
00166       //    evaluation of IP-addresses for SHMIOP endpoints?  Is it
00167       //    because it is always 'localhost'?  We need answers to
00168       //    these questions to solve:
00169       //
00170       // http://deuce.doc.wustl.edu/bugzilla/show_bug.cgi?id=1220
00171       //
00172   // The following code is suspec
00173   // compare the port and sin_addr (numeric host address)
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   // Parse options
00195   if (this->parse_options (options) == -1)
00196     return -1;
00197 
00198   if (ACE_OS::ace_isdigit (*port) == 0)
00199     return -1;                  // Port number must consist of digits
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   // Parse options
00219   if (this->parse_options (options) == -1)
00220     return -1;
00221 
00222   // @@ Until we can support multihomed machines correctly we must
00223   //    pick the "default interface" and only listen on that IP
00224   //    address.
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   // We only accept connection on localhost.
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   // @@ Should this be a catastrophic error???
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   // If the ORB is instructed to use dotted decimal addresses, we respect that
00287   // also for shmiop
00288   if (orb_core->orb_params ()->use_dotted_decimal_addresses ())
00289     {
00290       // Get the ip address, we get the remote addr to put in the IOR, don't
00291       // calls get_host_addr() directly on address_, we then get the internal
00292       // address back
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       // This will be the actualy host name of the original endpoint.
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   // This avoids having child processes acquire the listen socket thereby
00325   // denying the server the opportunity to restart on a well-known endpoint.
00326   // This does not affect the aberrent behavior on Win32 platforms.
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 : <%s:%u>\n"),
00334                   ACE_TEXT_CHAR_TO_TCHAR(this->host_.c_str ()),
00335                   this->address_.get_port_number ()));
00336     }
00337 
00338   // In the event that an accept() fails, we can examine the reason.  If
00339   // the reason warrants it, we can try accepting again at a later time.
00340   // The amount of time we wait to accept again is governed by this orb
00341   // parameter.
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   // Create the decoding stream from the encapsulation in the buffer,
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 /* TAO_NO_COPY_OCTET_SEQUENCES == 1 */
00360 
00361   CORBA::Octet major = 0;
00362   CORBA::Octet minor = 0;
00363 
00364   // Read the version. We just read it here. We don't*do any*
00365   // processing.
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   // Get host and port. No processing here too..
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   // ... and object key.
00395   if ((cdr >> object_key) == 0)
00396     return -1;
00397 
00398   // We are NOT bothered about the rest.
00399 
00400   return 1;
00401 }
00402 
00403 
00404 CORBA::ULong
00405 TAO_SHMIOP_Acceptor::endpoint_count (void)
00406 {
00407   // @@ for now just assume one!
00408   // we should take a look at the local address, if it is zero then
00409   // get the list of available IP interfaces and return this number.
00410   return 1;
00411 }
00412 
00413 int
00414 TAO_SHMIOP_Acceptor::parse_options (const char *str)
00415 {
00416   if (str == 0)
00417     return 0;  // No options to parse.  Not a problem.
00418 
00419   // Use an option format similar to the one used for CGI scripts in
00420   // HTTP URLs.
00421   // e.g.:  option1=foo&option2=bar
00422 
00423   ACE_CString options (str);
00424 
00425   size_t len = options.length ();
00426 
00427   const char option_delimiter = '&';
00428 
00429   // Count the number of options.
00430 
00431   CORBA::ULong option_count = 1;
00432   // Number of endpoints in the string  (initialized to 1).
00433 
00434   // Only check for endpoints after the protocol specification and
00435   // before the object key.
00436   for (size_t i = 0; i < len; ++i)
00437     if (options[i] == option_delimiter)
00438       ++option_count;
00439 
00440   // The idea behind the following loop is to split the options into
00441   // (option, name) pairs.
00442   // For example,
00443   //    `option1=foo&option2=bar'
00444   // will be parsed into:
00445   //    `option1=foo'
00446   //    `option2=bar'
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 <%s> is ")
00472                                ACE_TEXT ("missing a value.\n"),
00473                                ACE_TEXT_CHAR_TO_TCHAR(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: <%s>\n"),
00497                                ACE_TEXT_CHAR_TO_TCHAR(name.c_str ())),
00498                               -1);
00499         }
00500       else
00501         break;  // No other options.
00502     }
00503   return 0;
00504 }
00505 
00506 TAO_END_VERSIONED_NAMESPACE_DECL
00507 
00508 #endif /* TAO_HAS_SHMIOP && TAO_HAS_SHMIOP != 0 */

Generated on Tue Feb 2 17:47:18 2010 for TAO_Strategies by  doxygen 1.4.7