SHMIOP_Acceptor.cpp

Go to the documentation of this file.
00001 // $Id: SHMIOP_Acceptor.cpp 78931 2007-07-18 09:59:36Z johnnyw $
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 78931 2007-07-18 09:59:36Z 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_ (),
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   return 0;
00338 }
00339 
00340 
00341 int
00342 TAO_SHMIOP_Acceptor::object_key (IOP::TaggedProfile &profile,
00343                                  TAO::ObjectKey &object_key)
00344 {
00345   // Create the decoding stream from the encapsulation in the buffer,
00346 #if (TAO_NO_COPY_OCTET_SEQUENCES == 1)
00347   TAO_InputCDR cdr (profile.profile_data.mb ());
00348 #else
00349   TAO_InputCDR cdr (reinterpret_cast<char*>(profile.profile_data.get_buffer ()),
00350                     profile.profile_data.length ());
00351 #endif /* TAO_NO_COPY_OCTET_SEQUENCES == 1 */
00352 
00353   CORBA::Octet major = 0;
00354   CORBA::Octet minor = 0;
00355 
00356   // Read the version. We just read it here. We don't*do any*
00357   // processing.
00358   if (!(cdr.read_octet (major) && cdr.read_octet (minor)))
00359   {
00360     if (TAO_debug_level > 0)
00361       {
00362         ACE_DEBUG ((LM_DEBUG,
00363                     ACE_TEXT ("TAO (%P|%t) - SHMIOP_Profile::decode, v%d.%d\n"),
00364                     major,
00365                     minor));
00366       }
00367     return -1;
00368   }
00369 
00370   CORBA::String_var host;
00371   CORBA::UShort port = 0;
00372 
00373   // Get host and port. No processing here too..
00374   if (cdr.read_string (host.out ()) == 0
00375       || cdr.read_ushort (port) == 0)
00376     {
00377       if (TAO_debug_level > 0)
00378         {
00379           ACE_ERROR ((LM_ERROR,
00380                       ACE_TEXT ("TAO (%P|%t) - SHMIOP_Acceptor::object_key, ")
00381                       ACE_TEXT ("error while decoding host/port\n")));
00382         }
00383       return -1;
00384     }
00385 
00386   // ... and object key.
00387   if ((cdr >> object_key) == 0)
00388     return -1;
00389 
00390   // We are NOT bothered about the rest.
00391 
00392   return 1;
00393 }
00394 
00395 
00396 CORBA::ULong
00397 TAO_SHMIOP_Acceptor::endpoint_count (void)
00398 {
00399   // @@ for now just assume one!
00400   // we should take a look at the local address, if it is zero then
00401   // get the list of available IP interfaces and return this number.
00402   return 1;
00403 }
00404 
00405 int
00406 TAO_SHMIOP_Acceptor::parse_options (const char *str)
00407 {
00408   if (str == 0)
00409     return 0;  // No options to parse.  Not a problem.
00410 
00411   // Use an option format similar to the one used for CGI scripts in
00412   // HTTP URLs.
00413   // e.g.:  option1=foo&option2=bar
00414 
00415   ACE_CString options (str);
00416 
00417   size_t len = options.length ();
00418 
00419   const char option_delimiter = '&';
00420 
00421   // Count the number of options.
00422 
00423   CORBA::ULong option_count = 1;
00424   // Number of endpoints in the string  (initialized to 1).
00425 
00426   // Only check for endpoints after the protocol specification and
00427   // before the object key.
00428   for (size_t i = 0; i < len; ++i)
00429     if (options[i] == option_delimiter)
00430       ++option_count;
00431 
00432   // The idea behind the following loop is to split the options into
00433   // (option, name) pairs.
00434   // For example,
00435   //    `option1=foo&option2=bar'
00436   // will be parsed into:
00437   //    `option1=foo'
00438   //    `option2=bar'
00439 
00440   ACE_CString::size_type begin = 0;
00441   ACE_CString::size_type end = 0;
00442 
00443   for (CORBA::ULong j = 0; j < option_count; ++j)
00444     {
00445       if (j < option_count - 1)
00446         end = options.find (option_delimiter, begin);
00447       else
00448         end = len;
00449 
00450       if (end == begin)
00451         ACE_ERROR_RETURN ((LM_ERROR,
00452                            ACE_TEXT ("TAO (%P|%t) Zero length SHMIOP option.\n")),
00453                           -1);
00454       else if (end != ACE_CString::npos)
00455         {
00456           ACE_CString opt = options.substring (begin, end - begin);
00457 
00458           ACE_CString::size_type const slot = opt.find ("=");
00459 
00460           if (slot == len - 1
00461               || slot == ACE_CString::npos)
00462             ACE_ERROR_RETURN ((LM_ERROR,
00463                                ACE_TEXT ("TAO (%P|%t) SHMIOP option <%s> is ")
00464                                ACE_TEXT ("missing a value.\n"),
00465                                ACE_TEXT_CHAR_TO_TCHAR(opt.c_str ())),
00466                               -1);
00467 
00468           ACE_CString name = opt.substring (0, slot);
00469           ACE_CString value = opt.substring (slot + 1);
00470 
00471           begin = end + 1;
00472 
00473           if (name.length () == 0)
00474             ACE_ERROR_RETURN ((LM_ERROR,
00475                                ACE_TEXT ("TAO (%P|%t) Zero length SHMIOP ")
00476                                ACE_TEXT ("option name.\n")),
00477                               -1);
00478 
00479           if (name == "priority")
00480             {
00481               ACE_ERROR_RETURN ((LM_ERROR,
00482                                  ACE_TEXT ("TAO (%P|%t) Invalid SHMIOP endpoint format: ")
00483                                  ACE_TEXT ("endpoint priorities no longer supported.\n")),
00484                                 -1);
00485             }
00486           else
00487             ACE_ERROR_RETURN ((LM_ERROR,
00488                                ACE_TEXT ("TAO (%P|%t) Invalid SHMIOP option: <%s>\n"),
00489                                ACE_TEXT_CHAR_TO_TCHAR(name.c_str ())),
00490                               -1);
00491         }
00492       else
00493         break;  // No other options.
00494     }
00495   return 0;
00496 }
00497 
00498 TAO_END_VERSIONED_NAMESPACE_DECL
00499 
00500 #endif /* TAO_HAS_SHMIOP && TAO_HAS_SHMIOP != 0 */

Generated on Sun Jan 27 15:59:50 2008 for TAO_Strategies by doxygen 1.3.6