00001
00002
00003
00004 #include "tao/Strategies/SHMIOP_Connector.h"
00005
00006 #if defined (TAO_HAS_SHMIOP) && (TAO_HAS_SHMIOP != 0)
00007
00008 #include "tao/Strategies/SHMIOP_Profile.h"
00009 #include "tao/Strategies/SHMIOP_Endpoint.h"
00010 #include "tao/debug.h"
00011 #include "tao/Base_Transport_Property.h"
00012 #include "tao/ORB_Core.h"
00013 #include "tao/Client_Strategy_Factory.h"
00014 #include "tao/SystemException.h"
00015 #include "tao/Transport_Cache_Manager.h"
00016 #include "tao/Thread_Lane_Resources.h"
00017 #include "tao/Blocked_Connect_Strategy.h"
00018 #include "ace/OS_NS_strings.h"
00019
00020 ACE_RCSID (Strategies,
00021 SHMIOP_Connector,
00022 "$Id: SHMIOP_Connector.cpp 86593 2009-09-02 14:58:00Z vzykov $")
00023
00024 TAO_BEGIN_VERSIONED_NAMESPACE_DECL
00025
00026 TAO_SHMIOP_Connector::TAO_SHMIOP_Connector (void)
00027 : TAO_Connector (TAO_TAG_SHMEM_PROFILE),
00028 connect_strategy_ (),
00029 base_connector_ ()
00030 {
00031 }
00032
00033 TAO_SHMIOP_Connector::~TAO_SHMIOP_Connector (void)
00034 {
00035 }
00036
00037 int
00038 TAO_SHMIOP_Connector::open (TAO_ORB_Core *orb_core)
00039 {
00040 this->orb_core (orb_core);
00041
00042
00043
00044
00045 ACE_NEW_RETURN (this->active_connect_strategy_,
00046 TAO_Blocked_Connect_Strategy (orb_core),
00047 -1);
00048
00049
00050 TAO_SHMIOP_CONNECT_CREATION_STRATEGY *connect_creation_strategy = 0;
00051
00052 ACE_NEW_RETURN (connect_creation_strategy,
00053 TAO_SHMIOP_CONNECT_CREATION_STRATEGY
00054 (orb_core->thr_mgr (),
00055 orb_core),
00056 -1);
00057
00058
00059 TAO_SHMIOP_CONNECT_CONCURRENCY_STRATEGY *concurrency_strategy = 0;
00060
00061 ACE_NEW_RETURN (concurrency_strategy,
00062 TAO_SHMIOP_CONNECT_CONCURRENCY_STRATEGY (orb_core),
00063 -1);
00064
00065 if (this->base_connector_.open (this->orb_core ()->reactor (),
00066 connect_creation_strategy,
00067 &this->connect_strategy_,
00068 concurrency_strategy) == -1)
00069 return -1;
00070
00071
00072 else if (orb_core->client_factory ()->allow_callback () == 0)
00073
00074 {
00075 this->base_connector_.connector ().preferred_strategy (ACE_MEM_IO::MT);
00076 this->connect_strategy_.connector ().preferred_strategy (ACE_MEM_IO::MT);
00077 }
00078 return 0;
00079 }
00080
00081 int
00082 TAO_SHMIOP_Connector::close (void)
00083 {
00084 delete this->base_connector_.concurrency_strategy ();
00085 delete this->base_connector_.creation_strategy ();
00086 return this->base_connector_.close ();
00087 }
00088
00089 int
00090 TAO_SHMIOP_Connector::set_validate_endpoint (TAO_Endpoint *endpoint)
00091 {
00092 if (endpoint->tag () != TAO_TAG_SHMEM_PROFILE)
00093 return -1;
00094
00095 TAO_SHMIOP_Endpoint *shmiop_endpoint =
00096 dynamic_cast <TAO_SHMIOP_Endpoint *>(endpoint);
00097 if (shmiop_endpoint == 0)
00098 return -1;
00099
00100 const ACE_INET_Addr &remote_address =
00101 shmiop_endpoint->object_addr ();
00102
00103
00104
00105
00106 if (remote_address.get_type () != AF_INET)
00107 {
00108 if (TAO_debug_level > 0)
00109 {
00110 ACE_ERROR ((LM_ERROR,
00111 ACE_TEXT ("TAO (%P|%t) SHMIOP connection failed.\n")
00112 ACE_TEXT ("TAO (%P|%t) This is most likely ")
00113 ACE_TEXT ("due to a hostname lookup ")
00114 ACE_TEXT ("failure.\n")));
00115 }
00116
00117 return -1;
00118 }
00119
00120 return 0;
00121
00122 }
00123
00124 TAO_Transport *
00125 TAO_SHMIOP_Connector::make_connection (TAO::Profile_Transport_Resolver *,
00126 TAO_Transport_Descriptor_Interface &desc,
00127 ACE_Time_Value *timeout)
00128 {
00129 if (TAO_debug_level > 0)
00130 ACE_DEBUG ((LM_DEBUG,
00131 ACE_TEXT ("TAO (%P|%t) - SHMIOP_Connector::make_connection, ")
00132 ACE_TEXT ("looking for SHMIOP connection.\n")));
00133
00134 TAO_SHMIOP_Endpoint *shmiop_endpoint =
00135 this->remote_endpoint (desc.endpoint ());
00136
00137 if (shmiop_endpoint == 0)
00138 return 0;
00139
00140 const ACE_INET_Addr &remote_address =
00141 shmiop_endpoint->object_addr ();
00142
00143 if (TAO_debug_level > 2)
00144 ACE_DEBUG ((LM_DEBUG,
00145 "TAO (%P|%t) - SHMIOP_Connector::make_connection, "
00146 "making a new connection to <%C:%d>\n",
00147 shmiop_endpoint->host (),
00148 shmiop_endpoint->port ()));
00149
00150
00151 ACE_Synch_Options synch_options;
00152
00153 this->active_connect_strategy_->synch_options (timeout,
00154 synch_options);
00155
00156 TAO_SHMIOP_Connection_Handler *svc_handler = 0;
00157
00158
00159 int result = this->base_connector_.connect (svc_handler,
00160 remote_address,
00161 synch_options);
00162
00163
00164 ACE_Event_Handler_var svc_handler_auto_ptr (svc_handler);
00165
00166
00167 if (result == -1)
00168 {
00169
00170 if (TAO_debug_level > 0)
00171 {
00172 ACE_ERROR ((LM_ERROR,
00173 ACE_TEXT ("TAO (%P|%t) - SHMIOP_Connector::make_connection, ")
00174 ACE_TEXT ("connection to <%C:%u> failed (%p)\n"),
00175 shmiop_endpoint->host (),
00176 shmiop_endpoint->port (),
00177 ACE_TEXT ("errno")));
00178 }
00179
00180 return 0;
00181 }
00182
00183 if (svc_handler->keep_waiting ())
00184 {
00185 svc_handler->connection_pending ();
00186 }
00187
00188 if (svc_handler->error_detected ())
00189 {
00190 svc_handler->cancel_pending_connection ();
00191 }
00192
00193 TAO_Transport *transport =
00194 svc_handler->transport ();
00195
00196
00197
00198 if (TAO_debug_level > 2)
00199 ACE_DEBUG ((LM_DEBUG,
00200 "TAO (%P|%t) - SHMIOP_Connector::make_connection, "
00201 "new %C connection to <%C:%d> on Transport[%d]\n",
00202 transport->is_connected() ? "connected" : "not connected",
00203 shmiop_endpoint->host (),
00204 shmiop_endpoint->port (),
00205 svc_handler->peer ().get_handle ()));
00206
00207
00208 int retval =
00209 this->orb_core ()->lane_resources ().transport_cache ().cache_transport (&desc,
00210 transport);
00211
00212
00213 if (retval == -1)
00214 {
00215
00216 svc_handler->close ();
00217
00218 if (TAO_debug_level > 0)
00219 {
00220 ACE_ERROR ((LM_ERROR,
00221 ACE_TEXT("TAO (%P|%t) - SHMIOP_Connector::make_connection, ")
00222 ACE_TEXT("could not add the new connection to cache\n")));
00223 }
00224
00225 return 0;
00226 }
00227
00228 if (svc_handler->error_detected ())
00229 {
00230 svc_handler->cancel_pending_connection ();
00231 transport->purge_entry();
00232 return 0;
00233 }
00234
00235 if (transport->is_connected () &&
00236 transport->wait_strategy ()->register_handler () != 0)
00237 {
00238
00239
00240
00241
00242 (void) transport->purge_entry ();
00243
00244
00245 (void) transport->close_connection ();
00246
00247 if (TAO_debug_level > 0)
00248 ACE_ERROR ((LM_ERROR,
00249 "TAO (%P|%t) - SHMIOP_Connector [%d]::make_connection, "
00250 "could not register the transport in the reactor.\n",
00251 transport->id ()));
00252
00253 return 0;
00254 }
00255
00256 svc_handler_auto_ptr.release ();
00257 return transport;
00258 }
00259
00260 TAO_Profile *
00261 TAO_SHMIOP_Connector::create_profile (TAO_InputCDR& cdr)
00262 {
00263 TAO_Profile *pfile = 0;
00264 ACE_NEW_RETURN (pfile,
00265 TAO_SHMIOP_Profile (this->orb_core ()),
00266 0);
00267
00268 int r = pfile->decode (cdr);
00269 if (r == -1)
00270 {
00271 pfile->_decr_refcnt ();
00272 pfile = 0;
00273 }
00274
00275 return pfile;
00276 }
00277
00278 TAO_Profile *
00279 TAO_SHMIOP_Connector::make_profile (void)
00280 {
00281
00282
00283
00284
00285
00286 TAO_Profile *profile = 0;
00287 ACE_NEW_THROW_EX (profile,
00288 TAO_SHMIOP_Profile (this->orb_core ()),
00289 CORBA::NO_MEMORY (
00290 CORBA::SystemException::_tao_minor_code (
00291 TAO::VMCID,
00292 ENOMEM),
00293 CORBA::COMPLETED_NO));
00294
00295 return profile;
00296 }
00297
00298 int
00299 TAO_SHMIOP_Connector::check_prefix (const char *endpoint)
00300 {
00301
00302 if (!endpoint || !*endpoint)
00303 return -1;
00304
00305 const char *protocol[] = { "shmiop", "shmioploc" };
00306
00307 size_t slot = ACE_OS::strchr (endpoint, ':') - endpoint;
00308
00309 size_t len0 = ACE_OS::strlen (protocol[0]);
00310 size_t len1 = ACE_OS::strlen (protocol[1]);
00311
00312
00313
00314 if (slot == len0
00315 && ACE_OS::strncasecmp (endpoint, protocol[0], len0) == 0)
00316 return 0;
00317 else if (slot == len1
00318 && ACE_OS::strncasecmp (endpoint, protocol[1], len1) == 0)
00319 return 0;
00320
00321 return -1;
00322
00323
00324 }
00325
00326 char
00327 TAO_SHMIOP_Connector::object_key_delimiter (void) const
00328 {
00329 return TAO_SHMIOP_Profile::object_key_delimiter_;
00330 }
00331
00332 TAO_SHMIOP_Endpoint *
00333 TAO_SHMIOP_Connector::remote_endpoint (TAO_Endpoint *endpoint)
00334 {
00335 if (endpoint->tag () != TAO_TAG_SHMEM_PROFILE)
00336 return 0;
00337
00338 TAO_SHMIOP_Endpoint *shmiop_endpoint =
00339 dynamic_cast <TAO_SHMIOP_Endpoint *>(endpoint);
00340 if (shmiop_endpoint == 0)
00341 return 0;
00342
00343 return shmiop_endpoint;
00344 }
00345
00346 int
00347 TAO_SHMIOP_Connector::cancel_svc_handler (
00348 TAO_Connection_Handler * svc_handler)
00349 {
00350 TAO_SHMIOP_Connection_Handler* handler=
00351 dynamic_cast<TAO_SHMIOP_Connection_Handler*>(svc_handler);
00352
00353 if (handler)
00354
00355 return this->base_connector_.cancel (handler);
00356
00357 return -1;
00358 }
00359
00360 TAO_END_VERSIONED_NAMESPACE_DECL
00361
00362 #endif