IORManip_IIOP_Filter.cpp

Go to the documentation of this file.
00001 // $Id: IORManip_IIOP_Filter.cpp 80140 2007-11-30 00:40:17Z jtc $
00002 
00003 #include "tao/IORManipulation/IORManip_IIOP_Filter.h"
00004 
00005 #if defined (TAO_HAS_IIOP) && (TAO_HAS_IIOP != 0)
00006 
00007 #include "tao/IORManipulation/IORManip_Loader.h"
00008 #include "tao/IIOP_Profile.h"
00009 #include "tao/MProfile.h"
00010 #include "tao/CDR.h"
00011 
00012 ACE_RCSID (IORManip_IIOP_Filter, IORManip_IIOP_Filter, "$Id: IORManip_IIOP_Filter.cpp 80140 2007-11-30 00:40:17Z jtc $")
00013 
00014 TAO_BEGIN_VERSIONED_NAMESPACE_DECL
00015 
00016 TAO_IORManip_IIOP_Filter::TAO_IORManip_IIOP_Filter (void)
00017 {
00018 }
00019 
00020 
00021 TAO_IORManip_IIOP_Filter::~TAO_IORManip_IIOP_Filter (void)
00022 {
00023 }
00024 
00025 
00026 CORBA::Boolean
00027 TAO_IORManip_IIOP_Filter::compare_profile_info (
00028                         const TAO_IORManip_IIOP_Filter::Profile_Info& left,
00029                         const TAO_IORManip_IIOP_Filter::Profile_Info& right)
00030 {
00031   return (left.version_.major == right.version_.major &&
00032           left.version_.minor == right.version_.minor &&
00033           left.port_          == right.port_          &&
00034           left.host_name_     == right.host_name_);
00035 }
00036 
00037 
00038 CORBA::Boolean
00039 TAO_IORManip_IIOP_Filter::profile_info_matches (
00040                             const TAO_IORManip_IIOP_Filter::Profile_Info&)
00041 {
00042   return true;
00043 }
00044 
00045 
00046 void
00047 TAO_IORManip_IIOP_Filter::filter_and_add (TAO_Profile* profile,
00048                                           TAO_MProfile& new_profiles,
00049                                           TAO_Profile* guideline)
00050 {
00051   TAO_IORManip_IIOP_Filter::Profile_Info ginfo;
00052   TAO_IORManip_IIOP_Filter::Profile_Info pinfo;
00053   TAO::IIOPEndpointSequence endpoints;
00054 
00055   this->fill_profile_info (guideline, ginfo);
00056   this->get_endpoints (profile, endpoints);
00057   if (endpoints.length () == 0)
00058     {
00059       CORBA::Boolean matches = false;
00060       this->fill_profile_info (profile, pinfo);
00061 
00062       if (guideline == 0)
00063         {
00064           matches = this->profile_info_matches (pinfo);
00065         }
00066       else
00067         {
00068           // Compare the current profile with the guideline profile
00069           matches = this->compare_profile_info (pinfo, ginfo);
00070         }
00071 
00072       if (matches)
00073         {
00074           if (new_profiles.add_profile (profile) == -1)
00075             {
00076               throw CORBA::NO_MEMORY ();
00077             }
00078         }
00079     }
00080   else
00081     {
00082       // Create a new profile with just the
00083       // components we are looking for
00084       TAO_IIOP_Profile* new_profile =
00085           this->create_profile (profile);
00086 
00087       // Set pinfo to match the current profile
00088       this->fill_profile_info (profile, pinfo);
00089 
00090       // Add each endpoint if it matches.  Begin from the end
00091       // of the sequence to preserve endpoint order, since <add_endpoint>
00092       // method reverses the order of endpoints in the list.
00093       for (CORBA::Long i = endpoints.length () - 1; i >= 0; i--) {
00094         // Set pinfo host name to the current endpoint host name
00095         pinfo.host_name_ = endpoints[i].host.in ();
00096         pinfo.port_ = endpoints[i].port;
00097 
00098         CORBA::Boolean matches = false;
00099         if (guideline == 0)
00100           {
00101             matches = this->profile_info_matches (pinfo);
00102           }
00103         else
00104           {
00105             // Compare the current profile with the guideline profile
00106             matches = this->compare_profile_info (pinfo, ginfo);
00107           }
00108 
00109         if (matches)
00110           {
00111             // Set the main endpoint on the profile
00112             if (i == 0)
00113               {
00114                 TAO_IIOP_Endpoint* ep = dynamic_cast<TAO_IIOP_Endpoint*> (
00115                                           new_profile->endpoint ());
00116                 if (ep == 0)
00117                   {
00118                     new_profile->_decr_refcnt ();
00119                     return;
00120                   }
00121                 else
00122                   {
00123                     ep->host (CORBA::string_dup (endpoints[i].host));
00124                     ep->port (endpoints[i].port);
00125                     ep->priority (endpoints[i].priority);
00126                   }
00127               }
00128             else
00129               {
00130                 TAO_IIOP_Endpoint *endpoint = 0;
00131                 ACE_NEW (endpoint,
00132                          TAO_IIOP_Endpoint (endpoints[i].host,
00133                                             endpoints[i].port,
00134                                             endpoints[i].priority));
00135                 if (endpoint == 0)
00136                   {
00137                     new_profile->_decr_refcnt ();
00138                     return;
00139                   }
00140 
00141                 new_profile->add_endpoint (endpoint);
00142               }
00143           }
00144       }
00145 
00146       if (new_profiles.add_profile (new_profile) == -1)
00147         {
00148           throw CORBA::NO_MEMORY ();
00149         }
00150 
00151       new_profile->encode_endpoints ();
00152 
00153       // After adding the profile to the MProfile,
00154       // we must decrement the reference to avoid a memory leak
00155       new_profile->_decr_refcnt ();
00156     }
00157 }
00158 
00159 
00160 int
00161 TAO_IORManip_IIOP_Filter::fill_profile_info (
00162                              TAO_Profile* profile,
00163                              TAO_IORManip_IIOP_Filter::Profile_Info& pinfo)
00164 {
00165   static const int host_length = 384;
00166   int status = 0;
00167   if (profile != 0)
00168     {
00169       char host[host_length] = "";
00170       if (profile->endpoint ()->addr_to_string (host, host_length) != -1)
00171         {
00172           char* delim = ACE_OS::strchr (host, ':');
00173           if (delim != 0)
00174             {
00175               *delim = '\0';
00176               pinfo.port_ = ACE_OS::atoi (delim + 1);
00177               status = 1;
00178             }
00179         }
00180 
00181       pinfo.host_name_ = host;
00182       pinfo.version_ = profile->version ();
00183     }
00184 
00185   return status;
00186 }
00187 
00188 
00189 int
00190 TAO_IORManip_IIOP_Filter::get_endpoints (TAO_Profile* profile,
00191                                          TAO::IIOPEndpointSequence& endpoints)
00192 {
00193   // Reset the endpoints
00194   endpoints.length (0);
00195 
00196   // Get the endpoints tagged component
00197   const TAO_Tagged_Components& comps = profile->tagged_components ();
00198   IOP::TaggedComponent tagged_component;
00199   tagged_component.tag = TAO_TAG_ENDPOINTS;
00200   comps.get_component (tagged_component);
00201 
00202   // Prepare the CDR for endpoint extraction
00203   const CORBA::Octet *buf =
00204     tagged_component.component_data.get_buffer ();
00205 
00206   TAO_InputCDR in_cdr (reinterpret_cast<const char*> (buf),
00207                        tagged_component.component_data.length ());
00208 
00209   // Extract the Byte Order.
00210   CORBA::Boolean byte_order;
00211   if ((in_cdr >> ACE_InputCDR::to_boolean (byte_order)) == 0)
00212     return 0;
00213   in_cdr.reset_byte_order (static_cast<int> (byte_order));
00214 
00215   // Extract endpoints sequence.
00216   if ((in_cdr >> endpoints) == 0)
00217     return 0;
00218 
00219   return 1;
00220 }
00221 
00222 
00223 TAO_IIOP_Profile*
00224 TAO_IORManip_IIOP_Filter::create_profile (TAO_Profile* profile)
00225 {
00226   ACE_INET_Addr addr;
00227   TAO_IIOP_Profile* new_profile = 0;
00228   ACE_NEW_THROW_EX (new_profile,
00229                    TAO_IIOP_Profile (addr,
00230                                      profile->object_key (),
00231                                      profile->version (),
00232                                      profile->orb_core ()),
00233                    CORBA::NO_MEMORY (
00234                      CORBA::SystemException::_tao_minor_code (
00235                        0,
00236                        ENOMEM),
00237                      CORBA::COMPLETED_NO));
00238 
00239   // Copy all of the tagged components
00240   const TAO_Tagged_Components& comps = profile->tagged_components ();
00241   new_profile->tagged_components () = comps;
00242 
00243   // Set the endpoints component to an empty list
00244   IOP::TaggedComponent tagged_component;
00245   tagged_component.tag = TAO_TAG_ENDPOINTS;
00246   new_profile->tagged_components ().set_component (tagged_component);
00247 
00248   return new_profile;
00249 }
00250 
00251 TAO_END_VERSIONED_NAMESPACE_DECL
00252 
00253 #endif /* TAO_HAS_IIOP && TAO_HAS_IIOP != 0 */

Generated on Tue Feb 2 17:43:35 2010 for TAO_IORManipulation by  doxygen 1.4.7