IIOP_SSL_Connector.cpp

Go to the documentation of this file.
00001 #include "orbsvcs/SSLIOP/IIOP_SSL_Connector.h"
00002 
00003 #include "tao/debug.h"
00004 #include "tao/ORB_Core.h"
00005 #include "tao/Environment.h"
00006 #include "tao/IIOP_Endpoint.h"
00007 #include "tao/Transport_Cache_Manager.h"
00008 #include "tao/Thread_Lane_Resources.h"
00009 #include "tao/Connect_Strategy.h"
00010 #include "tao/Wait_Strategy.h"
00011 #include "tao/Profile_Transport_Resolver.h"
00012 #include "tao/Transport.h"
00013 
00014 #include "ace/Strategies_T.h"
00015 
00016 
00017 ACE_RCSID (SSLIOP,
00018            IIOP_SSL_Connector,
00019            "IIOP_SSL_Connector.cpp,v 1.44 2006/03/14 06:14:35 jtc Exp")
00020 
00021 TAO_BEGIN_VERSIONED_NAMESPACE_DECL
00022 
00023 TAO::IIOP_SSL_Connector::IIOP_SSL_Connector (CORBA::Boolean flag)
00024   : TAO_IIOP_Connector (flag),
00025     connect_strategy_ (),
00026     base_connector_ ()
00027 {
00028 }
00029 
00030 TAO::IIOP_SSL_Connector::~IIOP_SSL_Connector (void)
00031 {
00032 }
00033 
00034 int
00035 TAO::IIOP_SSL_Connector::open (TAO_ORB_Core *orb_core)
00036 {
00037   this->orb_core (orb_core);
00038 
00039   // Create our connect strategy
00040   if (this->create_connect_strategy () == -1)
00041     return -1;
00042 
00043   // Our connect creation strategy
00044   CONNECT_CREATION_STRATEGY *connect_creation_strategy = 0;
00045 
00046   ACE_NEW_RETURN (connect_creation_strategy,
00047                   CONNECT_CREATION_STRATEGY (orb_core->thr_mgr (),
00048                                              orb_core,
00049                                              this->lite_flag_),
00050                   -1);
00051 
00052   // Our activation strategy
00053   CONNECT_CONCURRENCY_STRATEGY *concurrency_strategy = 0;
00054 
00055   ACE_NEW_RETURN (concurrency_strategy,
00056                   CONNECT_CONCURRENCY_STRATEGY (orb_core),
00057                   -1);
00058 
00059 
00060   return this->base_connector_.open (this->orb_core ()->reactor (),
00061                                      connect_creation_strategy,
00062                                      &this->connect_strategy_,
00063                                      concurrency_strategy);
00064 }
00065 
00066 int
00067 TAO::IIOP_SSL_Connector::close (void)
00068 {
00069   delete this->base_connector_.creation_strategy ();
00070   delete this->base_connector_.concurrency_strategy ();
00071   return this->base_connector_.close ();
00072 }
00073 
00074 int
00075 TAO::IIOP_SSL_Connector::set_validate_endpoint (TAO_Endpoint *endpoint)
00076 {
00077   if (endpoint->tag () != IOP::TAG_INTERNET_IOP)
00078     return -1;
00079 
00080   TAO_IIOP_Endpoint *iiop_endpoint =
00081     dynamic_cast<TAO_IIOP_Endpoint *> (endpoint);
00082 
00083   if (iiop_endpoint == 0)
00084     return -1;
00085 
00086   const ACE_INET_Addr &remote_address =
00087     iiop_endpoint->object_addr ();
00088 
00089   // Verify that the remote ACE_INET_Addr was initialized properly.
00090   // Failure can occur if hostname lookup failed when initializing the
00091   // remote ACE_INET_Addr.
00092   if (remote_address.get_type () != AF_INET)
00093     {
00094       if (TAO_debug_level > 0)
00095         {
00096           ACE_DEBUG ((LM_DEBUG,
00097                       ACE_TEXT ("TAO (%P|%t) IIOP_SSL connection failed.\n")
00098                       ACE_TEXT ("TAO (%P|%t) This is most likely ")
00099                       ACE_TEXT ("due to a hostname lookup ")
00100                       ACE_TEXT ("failure.\n")));
00101         }
00102 
00103       return -1;
00104     }
00105 
00106   return 0;
00107 }
00108 
00109 TAO_Transport *
00110 TAO::IIOP_SSL_Connector::make_connection (
00111   TAO::Profile_Transport_Resolver *r,
00112   TAO_Transport_Descriptor_Interface &desc,
00113   ACE_Time_Value *max_wait_time)
00114 {
00115   TAO_IIOP_Endpoint *iiop_endpoint =
00116     dynamic_cast<TAO_IIOP_Endpoint *> (desc.endpoint ());
00117 
00118   if (iiop_endpoint == 0)
00119     return 0;
00120 
00121   const ACE_INET_Addr &remote_address =
00122     iiop_endpoint->object_addr ();
00123 
00124   if (TAO_debug_level > 4)
00125     ACE_DEBUG ((LM_DEBUG,
00126                 ACE_TEXT ("(%P|%t) IIOP_SSL_Connector::connect ")
00127                 ACE_TEXT ("making a new connection \n")));
00128 
00129   // Get the right synch options
00130   ACE_Synch_Options synch_options;
00131 
00132   this->active_connect_strategy_->synch_options (max_wait_time,
00133                                                  synch_options);
00134 
00135   // If we don't need to block for a transport just set the timeout to
00136   // be zero.
00137   ACE_Time_Value tmp_zero (ACE_Time_Value::zero);
00138   if (!r->blocked_connect ())
00139     {
00140       synch_options.timeout (ACE_Time_Value::zero);
00141       max_wait_time = &tmp_zero;
00142     }
00143 
00144 
00145   IIOP_SSL_Connection_Handler *svc_handler = 0;
00146 
00147   // Connect.
00148   int result =
00149     this->base_connector_.connect (svc_handler,
00150                                    remote_address,
00151                                    synch_options);
00152 
00153   // The connect() method creates the service handler and bumps the
00154   // #REFCOUNT# up one extra.  There are three possibilities from
00155   // calling connect(): (a) connection succeeds immediately - in this
00156   // case, the #REFCOUNT# on the handler is two; (b) connection
00157   // completion is pending - in this case, the #REFCOUNT# on the
00158   // handler is also two; (c) connection fails immediately - in this
00159   // case, the #REFCOUNT# on the handler is one since close() gets
00160   // called on the handler.
00161   //
00162   // The extra reference count in
00163   // TAO_Connect_Creation_Strategy::make_svc_handler() is needed in
00164   // the case when connection completion is pending and we are going
00165   // to wait on a variable in the handler to changes, signifying
00166   // success or failure.  Note, that this increment cannot be done
00167   // once the connect() returns since this might be too late if
00168   // another thread pick up the completion and potentially deletes the
00169   // handler before we get a chance to increment the reference count.
00170 
00171   // Make sure that we always do a remove_reference
00172   ACE_Event_Handler_var svc_handler_auto_ptr (svc_handler);
00173 
00174   TAO_Transport *transport =
00175     svc_handler->transport ();
00176 
00177   if (result == -1)
00178     {
00179       // No immediate result, wait for completion
00180       if (errno == EWOULDBLOCK)
00181         {
00182           // Try to wait until connection completion. Incase we block, then we
00183           // get a connected transport or not. In case of non block we get
00184           // a connected or not connected transport
00185           if (!this->wait_for_connection_completion (r,
00186                                                      transport,
00187                                                      max_wait_time))
00188             {
00189               if (TAO_debug_level > 2)
00190                 ACE_ERROR ((LM_ERROR, "TAO (%P|%t) - IIOP_SSL_Connector::"
00191                                       "make_connection, "
00192                                       "wait for completion failed\n"));
00193             }
00194         }
00195       else
00196         {
00197           // Transport is not usable
00198           transport = 0;
00199         }
00200     }
00201 
00202   // In case of errors transport is zero
00203   if (transport == 0)
00204     {
00205       // Give users a clue to the problem.
00206       if (TAO_debug_level)
00207         {
00208           ACE_DEBUG ((LM_ERROR,
00209                       "TAO (%P|%t) - IIOP_SSL_Connector::make_connection, "
00210                       "connection to <%s:%d> failed (%p)\n",
00211                       iiop_endpoint->host (), iiop_endpoint->port (),
00212                       "errno"));
00213         }
00214 
00215       return 0;
00216     }
00217 
00218   // At this point, the connection has be successfully connected.
00219   // #REFCOUNT# is one.
00220   if (TAO_debug_level > 2)
00221     ACE_DEBUG ((LM_DEBUG,
00222                 "TAO (%P|%t) - IIOP_SSL_Connector::make_connection, "
00223                 "new connection to <%s:%d> on Transport[%d]\n",
00224                 iiop_endpoint->host (), iiop_endpoint->port (),
00225                 svc_handler->peer ().get_handle ()));
00226 
00227   // Add the handler to Cache
00228   int retval =
00229     this->orb_core ()->lane_resources ().transport_cache ().cache_transport (
00230       &desc,
00231       transport);
00232 
00233   // Failure in adding to cache.
00234   if (retval != 0)
00235     {
00236       // Close the handler.
00237       svc_handler->close ();
00238 
00239       if (TAO_debug_level > 0)
00240         {
00241           ACE_ERROR ((LM_ERROR,
00242                       "TAO (%P|%t) - IIOP_SSL_Connector::make_connection, "
00243                       "could not add the new connection to cache\n"));
00244         }
00245 
00246       return 0;
00247     }
00248 
00249   if (transport->is_connected () &&
00250       transport->wait_strategy ()->register_handler () != 0)
00251     {
00252       // Registration failures.
00253 
00254       // Purge from the connection cache, if we are not in the cache, this
00255       // just does nothing.
00256       (void) transport->purge_entry ();
00257 
00258       // Close the handler.
00259       (void) transport->close_connection ();
00260 
00261       if (TAO_debug_level > 0)
00262         ACE_ERROR ((LM_ERROR,
00263                     "TAO (%P|%t) - IIOP_SSL_Connector [%d]::make_connection, "
00264                     "could not register the transport "
00265                     "in the reactor.\n",
00266                     transport->id ()));
00267 
00268       return 0;
00269     }
00270 
00271   return transport;
00272 }
00273 
00274 int
00275 TAO::IIOP_SSL_Connector::cancel_svc_handler (
00276   TAO_Connection_Handler * svc_handler)
00277 {
00278   IIOP_SSL_Connection_Handler* handler=
00279     dynamic_cast<IIOP_SSL_Connection_Handler*> (svc_handler);
00280 
00281   if (handler)
00282     // Cancel from the connector
00283     return this->base_connector_.cancel (handler);
00284 
00285   return -1;
00286 }
00287 
00288 TAO_END_VERSIONED_NAMESPACE_DECL

Generated on Thu Nov 9 13:54:12 2006 for TAO_SSLIOP by doxygen 1.3.6