00001 #include "tao/Strategies/SCIOP_Endpoint.h"
00002
00003 #if TAO_HAS_SCIOP == 1
00004
00005 #include "tao/ORB_Constants.h"
00006 #include "tao/debug.h"
00007
00008 #include "ace/os_include/os_netdb.h"
00009 #include "ace/Synch_Traits.h"
00010 #include "ace/Thread_Mutex.h"
00011 #include "ace/OS_NS_string.h"
00012 #include "ace/Log_Msg.h"
00013 #include "ace/Synch.h"
00014 #include "ace/OS_NS_stdio.h"
00015 #include "tao/ORB_Core.h"
00016
00017 ACE_RCSID (Strategies,
00018 SCIOP_Endpoint,
00019 "$Id: SCIOP_Endpoint.cpp 84443 2009-02-12 20:26:35Z johnnyw $")
00020
00021
00022 #if !defined (__ACE_INLINE__)
00023 # include "tao/Strategies/SCIOP_Endpoint.inl"
00024 #endif
00025
00026 TAO_BEGIN_VERSIONED_NAMESPACE_DECL
00027
00028 TAO_SCIOP_Endpoint::TAO_SCIOP_Endpoint (const ACE_INET_Addr &addr,
00029 int use_dotted_decimal_addresses)
00030 : TAO_Endpoint (TAO_TAG_SCIOP_PROFILE)
00031 , host_ ()
00032 , port_ (683)
00033 , object_addr_ (addr)
00034 , object_addr_set_ (0)
00035 , preferred_path_ ()
00036 , is_encodable_ (true)
00037 , next_ (0)
00038 {
00039 this->set (addr, use_dotted_decimal_addresses);
00040 }
00041
00042 TAO_SCIOP_Endpoint::TAO_SCIOP_Endpoint (const char *host,
00043 CORBA::UShort port,
00044 const ACE_INET_Addr &addr,
00045 CORBA::Short priority)
00046 : TAO_Endpoint (TAO_TAG_SCIOP_PROFILE,
00047 priority)
00048 , host_ (host)
00049 , port_ (port)
00050 , object_addr_ (addr)
00051 , object_addr_set_ (0)
00052 , preferred_path_ ()
00053 , is_encodable_ (true)
00054 , next_ (0)
00055 {
00056 }
00057
00058 TAO_SCIOP_Endpoint::TAO_SCIOP_Endpoint (void)
00059 : TAO_Endpoint (TAO_TAG_SCIOP_PROFILE)
00060 , host_ ()
00061 , port_ (683)
00062 , object_addr_ ()
00063 , object_addr_set_ (0)
00064 , preferred_path_ ()
00065 , is_encodable_ (true)
00066 , next_ (0)
00067 {
00068 }
00069
00070 TAO_SCIOP_Endpoint::TAO_SCIOP_Endpoint (const char *host,
00071 CORBA::UShort port,
00072 CORBA::Short priority)
00073 : TAO_Endpoint (TAO_TAG_SCIOP_PROFILE)
00074 , host_ (host)
00075 , port_ (port)
00076 , object_addr_ ()
00077 , object_addr_set_ (0)
00078 , preferred_path_ ()
00079 , is_encodable_ (true)
00080 , next_ (0)
00081 {
00082 this->priority (priority);
00083 }
00084
00085 TAO_SCIOP_Endpoint::~TAO_SCIOP_Endpoint (void)
00086 {
00087 }
00088
00089 TAO_SCIOP_Endpoint::TAO_SCIOP_Endpoint (const TAO_SCIOP_Endpoint &rhs)
00090 : TAO_Endpoint (rhs.tag_,
00091 rhs.priority_)
00092 , host_ (rhs.host_)
00093 , port_ (rhs.port_)
00094 , object_addr_ (rhs.object_addr_)
00095 , object_addr_set_ (rhs.object_addr_set_)
00096 , preferred_path_ (rhs.preferred_path_)
00097 , is_encodable_ (rhs.is_encodable_)
00098 , next_ (0)
00099 {
00100 }
00101
00102 int
00103 TAO_SCIOP_Endpoint::set (const ACE_INET_Addr &addr,
00104 int use_dotted_decimal_addresses)
00105 {
00106 char tmp_host[MAXHOSTNAMELEN + 1];
00107
00108 if (use_dotted_decimal_addresses
00109 || addr.get_host_name (tmp_host, sizeof (tmp_host)) != 0)
00110 {
00111 const char *tmp = addr.get_host_addr ();
00112 if (tmp == 0)
00113 {
00114 if (TAO_debug_level > 0)
00115 ACE_DEBUG ((LM_DEBUG,
00116 ACE_TEXT ("\n\nTAO (%P|%t) ")
00117 ACE_TEXT ("SCIOP_Endpoint::set ")
00118 ACE_TEXT ("- %p\n\n"),
00119 ACE_TEXT ("cannot determine hostname")));
00120 return -1;
00121 }
00122 else
00123 this->host_ = tmp;
00124 }
00125 else
00126 this->host_ = CORBA::string_dup (tmp_host);
00127
00128 this->port_ = addr.get_port_number();
00129
00130 return 0;
00131 }
00132
00133 int
00134 TAO_SCIOP_Endpoint::addr_to_string (char *buffer, size_t length)
00135 {
00136 size_t actual_len =
00137 ACE_OS::strlen (this->host_.in ())
00138 + sizeof (':')
00139 + ACE_OS::strlen ("65536")
00140 + sizeof ('\0');
00141
00142 if (length < actual_len)
00143 return -1;
00144
00145 ACE_OS::sprintf (buffer, "%s:%d",
00146 this->host_.in (), this->port_);
00147
00148 return 0;
00149 }
00150
00151 const char *
00152 TAO_SCIOP_Endpoint::host (const char *h)
00153 {
00154 this->host_ = h;
00155
00156 return this->host_.in ();
00157 }
00158
00159 TAO_Endpoint *
00160 TAO_SCIOP_Endpoint::next (void)
00161 {
00162 return this->next_;
00163 }
00164
00165 TAO_Endpoint *
00166 TAO_SCIOP_Endpoint::duplicate (void)
00167 {
00168 TAO_SCIOP_Endpoint *endpoint = 0;
00169
00170
00171 ACE_NEW_RETURN (endpoint,
00172 TAO_SCIOP_Endpoint (*this),
00173 0);
00174
00175 return endpoint;
00176 }
00177
00178 CORBA::Boolean
00179 TAO_SCIOP_Endpoint::is_equivalent (const TAO_Endpoint *other_endpoint)
00180 {
00181 TAO_Endpoint *endpt =
00182 const_cast<TAO_Endpoint *> (other_endpoint);
00183
00184 TAO_SCIOP_Endpoint *endpoint =
00185 dynamic_cast<TAO_SCIOP_Endpoint *> (endpt);
00186 if (endpoint == 0)
00187 return 0;
00188
00189 return (this->port_ == endpoint->port_
00190 && (ACE_OS::strcmp(this->host(), endpoint->host()) == 0));
00191 }
00192
00193 CORBA::ULong
00194 TAO_SCIOP_Endpoint::hash (void)
00195 {
00196 if (this->hash_val_ != 0)
00197 return this->hash_val_;
00198
00199 {
00200 ACE_GUARD_RETURN (TAO_SYNCH_MUTEX,
00201 guard,
00202 this->addr_lookup_lock_,
00203 this->hash_val_);
00204
00205 if (this->hash_val_ != 0)
00206 return this->hash_val_;
00207
00208
00209
00210
00211 if (!this->object_addr_set_)
00212 {
00213
00214 (void) this->object_addr_i ();
00215 }
00216
00217 this->hash_val_ =
00218 this->object_addr_.get_ip_address () + this->port ();
00219 }
00220
00221 return this->hash_val_;
00222 }
00223
00224 const ACE_INET_Addr &
00225 TAO_SCIOP_Endpoint::object_addr (void) const
00226 {
00227
00228
00229
00230
00231
00232
00233
00234 if (!this->object_addr_set_)
00235 {
00236 ACE_GUARD_RETURN (TAO_SYNCH_MUTEX,
00237 guard,
00238 this->addr_lookup_lock_,
00239 this->object_addr_);
00240
00241 if (!this->object_addr_set_)
00242 {
00243 (void) this->object_addr_i ();
00244 }
00245 }
00246
00247 return this->object_addr_;
00248 }
00249
00250 void
00251 TAO_SCIOP_Endpoint::object_addr_i (void) const
00252 {
00253 if (this->object_addr_.set (this->port_, this->host_.in ()) == -1)
00254 {
00255
00256
00257
00258
00259
00260
00261
00262
00263 this->object_addr_.set_type (-1);
00264 }
00265 else
00266 {
00267 this->object_addr_set_ = true;
00268 }
00269 }
00270
00271 CORBA::ULong
00272 TAO_SCIOP_Endpoint::preferred_interfaces (TAO_ORB_Core *oc)
00273 {
00274 ACE_CString tmp (
00275 oc->orb_params ()->preferred_interfaces ());
00276
00277 ACE_CString::size_type pos = 0;
00278
00279 pos = tmp.find (this->host_.in ());
00280
00281 TAO_SCIOP_Endpoint *latest = this;
00282
00283 CORBA::ULong count = 0;
00284
00285 while (pos != ACE_CString::npos)
00286 {
00287
00288 ACE_CString::size_type new_pos = tmp.find (",", pos + 1);
00289
00290
00291 ACE_CString::size_type length = 0;
00292
00293 if (new_pos == ACE_CString::npos)
00294 length = tmp.length () - pos;
00295 else
00296 length = new_pos - pos;
00297
00298 ACE_CString rem_tmp = tmp.substr (pos, length);
00299
00300
00301 ACE_CString::size_type col_pos = rem_tmp.find (":");
00302
00303 if (col_pos == ACE_CString::npos)
00304 {
00305 pos = tmp.find (latest->host_.in (),
00306 pos + length);
00307 continue;
00308 }
00309
00310 ACE_CString path = rem_tmp.substr (col_pos + 1);
00311
00312 latest->preferred_path_.host =
00313 CORBA::string_dup (path.c_str ());
00314
00315 if (TAO_debug_level > 3)
00316 ACE_DEBUG ((LM_DEBUG,
00317 "(%P|%t) Adding path [%C] "
00318 " as preferred path for [%C]\n",
00319 path.c_str (), this->host_.in ()));
00320
00321 pos = tmp.find (latest->host_.in (),
00322 pos + length);
00323
00324 if (pos != ACE_CString::npos)
00325 {
00326 TAO_Endpoint *tmp_ep =
00327 latest->duplicate ();
00328
00329 latest->next_ = dynamic_cast<TAO_SCIOP_Endpoint *> (tmp_ep);
00330
00331 if (latest->next_ == 0) return count;
00332
00333 latest = latest->next_;
00334 ++count;
00335 }
00336 }
00337
00338 if (tmp.length () != 0 &&
00339 !oc->orb_params ()->enforce_pref_interfaces ())
00340 {
00341 TAO_Endpoint *tmp_ep = latest->duplicate ();
00342
00343 latest->next_ =
00344 dynamic_cast<TAO_SCIOP_Endpoint *> (tmp_ep);
00345
00346 if (latest->next_ == 0) return count;
00347
00348 latest->next_->preferred_path_.host = (const char *) 0;
00349 ++count;
00350 }
00351
00352 return count;
00353 }
00354
00355 bool
00356 TAO_SCIOP_Endpoint::is_preferred_network (void) const
00357 {
00358 return (this->preferred_path_.host.in () != 0);
00359 }
00360
00361 const char *
00362 TAO_SCIOP_Endpoint::preferred_network (void) const
00363 {
00364 return this->preferred_path_.host.in ();
00365 }
00366
00367 TAO_END_VERSIONED_NAMESPACE_DECL
00368
00369 #endif