Merge pull request #2218 from jwillemsen/jwi-pthreadsigmask
[ACE_TAO.git] / TAO / tao / Strategies / COIOP_Acceptor.cpp
blob3e3be97e9c5cd3eaa473f09662027f1e0dbd51f5
1 #include "tao/Strategies/COIOP_Acceptor.h"
3 #if defined (TAO_HAS_COIOP) && (TAO_HAS_COIOP != 0)
5 #include "tao/Strategies/COIOP_Profile.h"
6 #include "tao/MProfile.h"
7 #include "tao/ORB_Core.h"
8 #include "tao/debug.h"
9 #include "tao/Protocols_Hooks.h"
10 #include "tao/Codeset_Manager.h"
11 #include "tao/CDR.h"
13 #include <memory>
14 #include "ace/OS_NS_string.h"
16 TAO_BEGIN_VERSIONED_NAMESPACE_DECL
18 TAO_COIOP_Acceptor::TAO_COIOP_Acceptor ()
19 : TAO_Acceptor (TAO_TAG_COIOP_PROFILE),
20 uuid_ (*ACE_Utils::UUID_GENERATOR::instance ()->generate_UUID ()),
21 version_ (TAO_DEF_GIOP_MAJOR, TAO_DEF_GIOP_MINOR),
22 orb_core_ (0)
26 TAO_COIOP_Acceptor::~TAO_COIOP_Acceptor ()
28 // Make sure we are closed before we start destroying the
29 // strategies.
30 this->close ();
33 // TODO =
34 // 2) For V1.[1,2] there are tagged components
36 int
37 TAO_COIOP_Acceptor::create_profile (const TAO::ObjectKey & object_key,
38 TAO_MProfile &mprofile,
39 CORBA::Short priority)
41 // Check if multiple endpoints should be put in one profile or if
42 // they should be spread across multiple profiles.
43 if (priority == TAO_INVALID_PRIORITY)
44 return this->create_new_profile (object_key, mprofile, priority);
45 else
46 return this->create_shared_profile (object_key, mprofile, priority);
49 int
50 TAO_COIOP_Acceptor::create_new_profile (const TAO::ObjectKey &object_key,
51 TAO_MProfile &mprofile,
52 CORBA::Short priority)
54 // Adding this->endpoint_count_ to the TAO_MProfile.
55 if (mprofile.grow (1) == -1)
56 return -1;
58 TAO_COIOP_Profile *pfile = 0;
59 ACE_NEW_RETURN (pfile,
60 TAO_COIOP_Profile (uuid_,
61 object_key,
62 this->version_,
63 this->orb_core_),
64 -1);
65 pfile->endpoint ()->priority (priority);
67 if (mprofile.give_profile (pfile) == -1)
69 pfile->_decr_refcnt ();
70 pfile = 0;
71 return -1;
74 // Do not add any tagged components to the profile if configured
75 // by the user not to do so, or if an IIOP 1.0 endpoint is being
76 // created (IIOP 1.0 did not support tagged components).
77 if (this->orb_core_->orb_params ()->std_profile_components () != 0
78 && (this->version_.major >= 1 && this->version_.minor > 0))
80 pfile->tagged_components ().set_orb_type (TAO_ORB_TYPE);
82 TAO_Codeset_Manager *csm = this->orb_core_->codeset_manager();
83 if (csm)
84 csm->set_codeset(pfile->tagged_components());
87 return 0;
90 int
91 TAO_COIOP_Acceptor::create_shared_profile (const TAO::ObjectKey &object_key,
92 TAO_MProfile &mprofile,
93 CORBA::Short priority)
95 TAO_Profile *pfile = 0;
96 TAO_COIOP_Profile *coiop_profile = 0;
98 // First see if <mprofile> already contains a COIOP profile.
99 for (TAO_PHandle i = 0; i != mprofile.profile_count (); ++i)
101 pfile = mprofile.get_profile (i);
102 if (pfile->tag () == TAO_TAG_COIOP_PROFILE)
104 coiop_profile = dynamic_cast<TAO_COIOP_Profile *> (pfile);
105 break;
109 // If <mprofile> doesn't contain a COIOP_Profile, we need to create
110 // one.
111 if (coiop_profile == 0)
113 ACE_NEW_RETURN (coiop_profile,
114 TAO_COIOP_Profile (uuid_,
115 object_key,
116 this->version_,
117 this->orb_core_),
118 -1);
119 coiop_profile->endpoint ()->priority (priority);
121 if (mprofile.give_profile (coiop_profile) == -1)
123 coiop_profile->_decr_refcnt ();
124 coiop_profile = 0;
125 return -1;
128 if (this->orb_core_->orb_params ()->std_profile_components () != 0
129 && (this->version_.major >= 1 && this->version_.minor >= 1))
131 coiop_profile->tagged_components ().set_orb_type (TAO_ORB_TYPE);
132 TAO_Codeset_Manager *csm = this->orb_core_->codeset_manager();
133 if (csm)
134 csm->set_codeset(pfile->tagged_components());
138 TAO_COIOP_Endpoint *endpoint = 0;
139 ACE_NEW_RETURN (endpoint,
140 TAO_COIOP_Endpoint (uuid_),
141 -1);
142 endpoint->priority (priority);
143 coiop_profile->add_endpoint (endpoint);
145 return 0;
149 TAO_COIOP_Acceptor::is_collocated (const TAO_Endpoint *endpoint)
151 const TAO_COIOP_Endpoint *endp =
152 dynamic_cast<const TAO_COIOP_Endpoint *> (endpoint);
154 // Make sure the dynamically cast pointer is valid.
155 if (endp == 0)
156 return false;
158 return (endp->uuid() == uuid_);
162 TAO_COIOP_Acceptor::close ()
164 return 0;
168 TAO_COIOP_Acceptor::open (TAO_ORB_Core *orb_core,
169 ACE_Reactor *,
170 int major,
171 int minor,
172 const char *address,
173 const char *options)
175 this->orb_core_ = orb_core;
177 if (major >=0 && minor >= 0)
178 this->version_.set_version (static_cast<CORBA::Octet> (major),
179 static_cast<CORBA::Octet> (minor));
180 // Parse options
181 if (this->parse_options (options) == -1)
182 return -1;
184 uuid_.from_string (address);
186 return 0;
190 TAO_COIOP_Acceptor::open_default (TAO_ORB_Core *orb_core,
191 ACE_Reactor *,
192 int major,
193 int minor,
194 const char *options)
196 this->orb_core_ = orb_core;
198 if (major >=0 && minor >= 0)
199 this->version_.set_version (static_cast<CORBA::Octet> (major),
200 static_cast<CORBA::Octet> (minor));
202 // Parse options
203 if (this->parse_options (options) == -1)
204 return -1;
206 return 0;
209 CORBA::ULong
210 TAO_COIOP_Acceptor::endpoint_count ()
212 return 1;
216 TAO_COIOP_Acceptor::object_key (IOP::TaggedProfile &profile,
217 TAO::ObjectKey &object_key)
219 // Create the decoding stream from the encapsulation in the buffer,
220 #if (TAO_NO_COPY_OCTET_SEQUENCES == 1)
221 TAO_InputCDR cdr (profile.profile_data.mb ());
222 #else
223 TAO_InputCDR cdr (reinterpret_cast<char*> (profile.profile_data.get_buffer ()),
224 profile.profile_data.length ());
225 #endif /* TAO_NO_COPY_OCTET_SEQUENCES == 1 */
227 CORBA::Octet major = 0;
228 CORBA::Octet minor = 0;
230 // Read the version. We just read it here. We don't*do any*
231 // processing.
232 if (!(cdr.read_octet (major) && cdr.read_octet (minor)))
234 if (TAO_debug_level > 0)
236 TAOLIB_DEBUG ((LM_DEBUG,
237 ACE_TEXT ("TAO (%P|%t) COIOP_Profile::decode - v%d.%d\n"),
238 major,
239 minor));
241 return -1;
244 CORBA::String_var uuid;
246 // Get host and port. No processing here too..
247 if (cdr.read_string (uuid.out ()) == 0)
249 if (TAO_debug_level > 0)
251 TAOLIB_DEBUG ((LM_DEBUG,
252 ACE_TEXT ("TAO (%P|%t) TAO_COIOP_Acceptor::object_key - ")
253 ACE_TEXT ("error while decoding host/port")));
255 return -1;
258 // ... and object key.
259 if ((cdr >> object_key) == 0)
260 return -1;
262 // We are NOT bothered about the rest.
264 return 1;
268 TAO_COIOP_Acceptor::parse_options (const char *str)
270 if (str == 0)
271 return 0; // No options to parse. Not a problem.
273 // Use an option format similar to the one used for CGI scripts in
274 // HTTP URLs.
275 // e.g.: option1=foo&option2=bar
277 ACE_CString options (str);
279 size_t len = options.length ();
281 const char option_delimiter = '&';
283 // Count the number of options.
285 CORBA::ULong option_count = 1;
286 // Number of endpoints in the string (initialized to 1).
288 // Only check for endpoints after the protocol specification and
289 // before the object key.
290 for (size_t i = 0; i < len; ++i)
291 if (options[i] == option_delimiter)
292 option_count++;
294 // The idea behind the following loop is to split the options into
295 // (option, name) pairs.
296 // For example,
297 // `option1=foo&option2=bar'
298 // will be parsed into:
299 // `option1=foo'
300 // `option2=bar'
302 ACE_CString::size_type begin = 0;
303 ACE_CString::size_type end = 0;
305 for (CORBA::ULong j = 0; j < option_count; ++j)
307 if (j < option_count - 1)
308 end = options.find (option_delimiter, begin);
309 else
310 end = len;
312 if (end == begin)
314 TAOLIB_ERROR_RETURN ((LM_ERROR,
315 ACE_TEXT ("TAO (%P|%t) Zero length COIOP option.\n")),
316 -1);
318 else if (end != ACE_CString::npos)
320 ACE_CString opt = options.substring (begin, end);
322 ACE_CString::size_type const slot = opt.find ("=");
324 if (slot == len - 1
325 || slot == ACE_CString::npos)
326 TAOLIB_ERROR_RETURN ((LM_ERROR,
327 ACE_TEXT ("TAO (%P|%t) COIOP option <%C> is ")
328 ACE_TEXT ("missing a value.\n"),
329 opt.c_str ()),
330 -1);
332 ACE_CString name = opt.substring (0, slot);
333 ACE_CString value = opt.substring (slot + 1);
335 begin = end + 1;
337 if (name.length () == 0)
338 TAOLIB_ERROR_RETURN ((LM_ERROR,
339 ACE_TEXT ("TAO (%P|%t) Zero length COIOP ")
340 ACE_TEXT ("option name.\n")),
341 -1);
343 if (name == "priority")
345 TAOLIB_ERROR_RETURN ((LM_ERROR,
346 ACE_TEXT ("TAO (%P|%t) Invalid COIOP endpoint format: ")
347 ACE_TEXT ("endpoint priorities no longer supported.\n")),
348 -1);
350 else
352 TAOLIB_ERROR_RETURN ((LM_ERROR,
353 ACE_TEXT ("TAO (%P|%t) Invalid COIOP option: <%C>\n"),
354 name.c_str ()),
355 -1);
358 else
360 break; // No other options.
363 return 0;
366 TAO_END_VERSIONED_NAMESPACE_DECL
368 #endif /* TAO_HAS_COIOP && TAO_HAS_COIOP != 0 */