Merge pull request #2216 from jwillemsen/jwi-cxxversionchecks
[ACE_TAO.git] / TAO / orbsvcs / tests / Interoperable_Naming / ncontextext_client_i.cpp
blobdcbbda028a574cb9a1e9d65233b2e31b18d76edc
2 //=============================================================================
3 /**
4 * @file ncontextext_client_i.cpp
6 * This class implements a simple CORBA client which
7 * converts a Name to a string and viceversa, forms a IIOPNAME
8 * url address and can resolve a stringified name.
10 * @author Priyanka Gontla <pgontla@ece.uci.edu>
12 //=============================================================================
15 #include "ncontextext_client_i.h"
16 #include "tao/debug.h"
17 #include "ace/Get_Opt.h"
18 #include "ace/Read_Buffer.h"
19 #include "ace/OS_NS_time.h"
21 // FUZZ: disable check_for_streams_include
22 #include "ace/streams.h"
24 // Constructor
25 NContextExt_Client_i::NContextExt_Client_i ()
29 NContextExt_Client_i::~NContextExt_Client_i ()
34 // Parses the command line arguments and returns an
35 // error status
36 int
37 NContextExt_Client_i::parse_args ()
39 ACE_Get_Opt get_opts (argc_, argv_, ACE_TEXT("dvs"));
40 int c;
42 this->view_ = 1;
44 while ((c = get_opts ()) != -1)
45 switch (c)
47 case 'd': // debug flag
48 TAO_debug_level++;
49 break;
50 case 'v': // output needed
51 this->view_ = 0;
52 break;
53 case 's':
54 break;
55 case '?':
56 default:
57 ACE_ERROR_RETURN ((LM_ERROR,
58 "usage: %s"
59 " [-d]"
60 " [-v]"
61 "\n",
62 this->argv_ [0]),
63 -1);
66 // Indicates successful parsing of command line.
67 return 0;
70 char *
71 NContextExt_Client_i::get_name ()
73 // USe time (NULL) to produce the seed:
74 ACE_OS::srand (static_cast<u_int> (ACE_OS::time (0)));
76 const int len = 10;
77 char *name_component = CORBA::string_alloc (len);
78 char *name_componentPtr = name_component;
81 for (int i = 0; i < len; ++i)
83 int rand_value = ACE_OS::rand () % 10;
85 switch (rand_value)
87 case 0:
88 *name_componentPtr = '/';
89 ++name_componentPtr;
90 break;
92 case 1:
93 *name_componentPtr = '.';
94 ++name_componentPtr;
95 break;
97 case 2:
98 *name_componentPtr = '\\';
99 ++name_componentPtr;
100 break;
102 case 3:
103 *name_componentPtr = '<';
104 ++name_componentPtr;
105 break;
107 case 4:
108 *name_componentPtr = '>';
109 ++name_componentPtr;
110 break;
112 case 5:
113 *name_componentPtr = ' ';
114 ++name_componentPtr;
115 break;
117 case 6:
118 *name_componentPtr = '%';
119 ++name_componentPtr;
120 break;
122 case 7:
123 case 8:
124 case 9:
125 *name_componentPtr = 'A' + ( ACE_OS::rand () % 26);
126 ++name_componentPtr;
127 break;
129 default:
130 ACE_ERROR ((LM_ERROR, "shouldnt come here"));
131 break;
135 *name_componentPtr = '\0';
137 return name_component;
141 NContextExt_Client_i::run ()
145 CosNaming::Name name;
147 name.length (2);
148 name[0].id = CORBA::string_dup (this->get_name ());
149 name[0].kind = CORBA::string_dup (this->get_name ());
150 name[1].id = CORBA::string_dup ("Iterator_Factory");
151 name[1].kind = CORBA::string_dup ("factory");
153 // Get the stringified form of the name
154 CORBA::String_var str_name =
155 this->naming_context_->to_string (name);
157 CORBA::Object_var factory_object;
161 // Resolve the name using the stringified form of the name
162 factory_object =
163 this->naming_context_->resolve_str (str_name.in ());
165 catch (const CosNaming::NamingContext::NotFound&)
169 // Narrow
170 Web_Server::Iterator_Factory_var factory =
171 Web_Server::Iterator_Factory::_narrow (factory_object.in ());
174 // Create bindings
175 CosNaming::BindingIterator_var iter;
176 CosNaming::BindingList_var bindings_list;
178 this->naming_context_->list (2,
179 bindings_list.out (),
180 iter.out ());
182 // Convert the stringified name back as CosNaming::Name and print
183 // them out.
184 CosNaming::Name *nam =
185 this->naming_context_->to_name (str_name.in ());
187 // Declare a CosNaming::Name variable and assign length to it.
188 CosNaming::Name nm;
189 nm.length (nam->length ());
191 nm = *nam;
193 // Test the to_url function:
194 // For the address, we are assigning
195 // some random address now. But, in real applications, the address
196 // denotes the address of the NamingContext possibly returned from
197 // the LocateReply or LOCATION_FORWARD reply by an agent listening
198 // at that address
200 CORBA::String_var address =
201 CORBA::string_dup (":myhost.555xyz.com:9999");
203 // Since we are just testing the functionality of the to_url
204 // function, use a random object name.
205 CORBA::String_var obj_name = get_name ();
207 CORBA::String_var url_string =
208 this->naming_context_->to_url (address.in (), obj_name.in());
210 if (this->view_ == 0)
212 this->print_values (name,
213 str_name,
215 obj_name,
216 url_string);
219 catch (const CORBA::NO_MEMORY& ex)
221 ex._tao_print_exception ("A system exception oc client side");
222 return -1;
224 catch (const CORBA::SystemException& ex)
226 ex._tao_print_exception ("A system exception oc client side");
227 return -1;
229 catch (const CORBA::Exception& ex)
231 ex._tao_print_exception ("client");
232 return -1;
235 return 0;
239 NContextExt_Client_i::init (int argc, ACE_TCHAR *argv[])
241 this->argc_ = argc;
242 this->argv_ = argv;
246 // First initialize the ORB, that will remove some arguments...
247 CORBA::ORB_var orb =
248 CORBA::ORB_init (this->argc_, this->argv_);
250 // There must be at least one argument, the file that has to be
251 // retrieved
252 if (this->parse_args () == -1)
253 return 1;
255 // Get a reference to the Naming Service
256 CORBA::Object_var naming_context_object =
257 orb->resolve_initial_references ("NameService");
259 if (CORBA::is_nil (naming_context_object.in ()))
260 ACE_ERROR_RETURN ((LM_ERROR,
261 "Cannot resolve Naming Service\n"),
264 // Narrow to get the correct reference
265 this->naming_context_ =
266 CosNaming::NamingContextExt::_narrow (naming_context_object.in ());
268 if (CORBA::is_nil (this->naming_context_.in ()))
269 ACE_ERROR_RETURN ((LM_ERROR,
270 "Cannot narrow Naming Service\n"),
273 catch (const CORBA::Exception& ex)
275 ex._tao_print_exception ("client");
276 return 1;
279 return 0;
282 void
283 NContextExt_Client_i::print_values (CosNaming::Name name,
284 CORBA::String_var str_name,
285 CosNaming::Name nm,
286 CORBA::String_var obj_name,
287 CORBA::String_var url_string)
289 ACE_DEBUG((LM_DEBUG, ACE_TEXT ("The first component id is %C,")
290 ACE_TEXT ("The first component kind is %C,")
291 ACE_TEXT ("The second component id is %C,")
292 ACE_TEXT ("The second component kind is %C\n\n"),
293 name[0].id.in (),
294 name[0].kind.in (),
295 name[1].id.in (),
296 name[1].kind.in ()));
298 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("The string form of the input name is: \n%C\n\n"),
299 str_name.in ()));
301 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("The unstringified version of the name components are:,")
302 ACE_TEXT ("The first component id is %C,")
303 ACE_TEXT ("The first component kind is %C,")
304 ACE_TEXT ("The second component id is %C,")
305 ACE_TEXT ("The second component kind is %C\n\n"),
306 nm[0].id.in (),
307 nm[0].kind.in (),
308 nm[1].id.in (),
309 nm[1].kind.in ()));
311 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("When the address of the NamingContext is:")
312 ACE_TEXT ("myhost.555xyz.com:9999")
313 ACE_TEXT ("and the Object name is \n%C\n"),
314 obj_name.in ()));
316 ACE_DEBUG ((LM_DEBUG,ACE_TEXT ("The URL form of the string is \n %C\n"),
317 url_string.in ()));