Merge pull request #1844 from jrw972/monterey
[ACE_TAO.git] / TAO / orbsvcs / tests / Interoperable_Naming / ncontextext_client_i.cpp
blobd2ab16ff3a57d69f5885836fe750315fbe7c9171
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 (void)
29 NContextExt_Client_i::~NContextExt_Client_i (void)
34 // Parses the command line arguments and returns an
35 // error status
36 int
37 NContextExt_Client_i::parse_args (void)
40 ACE_Get_Opt get_opts (argc_, argv_, ACE_TEXT("dvs"));
41 int c;
43 this->view_ = 1;
45 while ((c = get_opts ()) != -1)
46 switch (c)
48 case 'd': // debug flag
49 TAO_debug_level++;
50 break;
51 case 'v': // output needed
52 this->view_ = 0;
53 break;
54 case 's':
55 break;
56 case '?':
57 default:
58 ACE_ERROR_RETURN ((LM_ERROR,
59 "usage: %s"
60 " [-d]"
61 " [-v]"
62 "\n",
63 this->argv_ [0]),
64 -1);
67 // Indicates successful parsing of command line.
68 return 0;
71 char *
72 NContextExt_Client_i::get_name ()
75 // USe time (NULL) to produce the seed:
76 ACE_OS::srand (static_cast<u_int> (ACE_OS::time (0)));
78 const int len = 10;
79 char *name_component = CORBA::string_alloc (len);
80 char *name_componentPtr = name_component;
83 for (int i = 0; i < len; ++i)
86 int rand_value = ACE_OS::rand () % 10;
88 switch (rand_value)
90 case 0:
91 *name_componentPtr = '/';
92 ++name_componentPtr;
93 break;
95 case 1:
96 *name_componentPtr = '.';
97 ++name_componentPtr;
98 break;
100 case 2:
101 *name_componentPtr = '\\';
102 ++name_componentPtr;
103 break;
105 case 3:
106 *name_componentPtr = '<';
107 ++name_componentPtr;
108 break;
110 case 4:
111 *name_componentPtr = '>';
112 ++name_componentPtr;
113 break;
115 case 5:
116 *name_componentPtr = ' ';
117 ++name_componentPtr;
118 break;
120 case 6:
121 *name_componentPtr = '%';
122 ++name_componentPtr;
123 break;
125 case 7:
126 case 8:
127 case 9:
128 *name_componentPtr = 'A' + ( ACE_OS::rand () % 26 );
129 ++name_componentPtr;
130 break;
132 default:
133 ACE_ERROR ((LM_ERROR, "shouldnt come here"));
134 break;
138 *name_componentPtr = '\0';
140 return name_component;
145 NContextExt_Client_i::run (void)
150 CosNaming::Name name;
152 name.length (2);
153 name[0].id = CORBA::string_dup (this->get_name ());
154 name[0].kind = CORBA::string_dup (this->get_name ());
155 name[1].id = CORBA::string_dup ("Iterator_Factory");
156 name[1].kind = CORBA::string_dup ("factory");
158 // Get the stringified form of the name
159 CORBA::String_var str_name =
160 this->naming_context_->to_string (name);
162 CORBA::Object_var factory_object;
166 // Resolve the name using the stringified form of the name
167 factory_object =
168 this->naming_context_->resolve_str (str_name.in ());
170 catch (const CosNaming::NamingContext::NotFound&)
174 // Narrow
175 Web_Server::Iterator_Factory_var factory =
176 Web_Server::Iterator_Factory::_narrow (factory_object.in ());
179 // Create bindings
180 CosNaming::BindingIterator_var iter;
181 CosNaming::BindingList_var bindings_list;
183 this->naming_context_->list (2,
184 bindings_list.out (),
185 iter.out ());
187 // Convert the stringified name back as CosNaming::Name and print
188 // them out.
189 CosNaming::Name *nam =
190 this->naming_context_->to_name (str_name.in ());
192 // Declare a CosNaming::Name variable and assign length to it.
193 CosNaming::Name nm;
194 nm.length (nam->length ());
196 nm = *nam;
198 // Test the to_url function:
199 // For the address, we are assigning
200 // some random address now. But, in real applications, the address
201 // denotes the address of the NamingContext possibly returned from
202 // the LocateReply or LOCATION_FORWARD reply by an agent listening
203 // at that address
205 CORBA::String_var address =
206 CORBA::string_dup (":myhost.555xyz.com:9999");
208 // Since we are just testing the functionality of the to_url
209 // function, use a random object name.
210 CORBA::String_var obj_name = get_name ();
212 CORBA::String_var url_string =
213 this->naming_context_->to_url (address.in (), obj_name.in());
215 if (this->view_ == 0)
217 this->print_values (name,
218 str_name,
220 obj_name,
221 url_string);
224 catch (const CORBA::NO_MEMORY& ex)
226 ex._tao_print_exception ("A system exception oc client side");
227 return -1;
229 catch (const CORBA::SystemException& ex)
231 ex._tao_print_exception ("A system exception oc client side");
232 return -1;
234 catch (const CORBA::Exception& ex)
236 ex._tao_print_exception ("client");
237 return -1;
240 return 0;
244 NContextExt_Client_i::init (int argc, ACE_TCHAR *argv[])
246 this->argc_ = argc;
247 this->argv_ = argv;
251 // First initialize the ORB, that will remove some arguments...
252 CORBA::ORB_var orb =
253 CORBA::ORB_init (this->argc_, this->argv_);
255 // There must be at least one argument, the file that has to be
256 // retrieved
257 if (this->parse_args () == -1)
258 return 1;
260 // Get a reference to the Naming Service
261 CORBA::Object_var naming_context_object =
262 orb->resolve_initial_references ("NameService");
264 if (CORBA::is_nil (naming_context_object.in ()))
265 ACE_ERROR_RETURN ((LM_ERROR,
266 "Cannot resolve Naming Service\n"),
269 // Narrow to get the correct reference
270 this->naming_context_ =
271 CosNaming::NamingContextExt::_narrow (naming_context_object.in ());
273 if (CORBA::is_nil (this->naming_context_.in ()))
274 ACE_ERROR_RETURN ((LM_ERROR,
275 "Cannot narrow Naming Service\n"),
278 catch (const CORBA::Exception& ex)
280 ex._tao_print_exception ("client");
281 return 1;
284 return 0;
287 void
288 NContextExt_Client_i::print_values (CosNaming::Name name,
289 CORBA::String_var str_name,
290 CosNaming::Name nm,
291 CORBA::String_var obj_name,
292 CORBA::String_var url_string)
295 ACE_DEBUG((LM_DEBUG, ACE_TEXT ("The first component id is %C,")
296 ACE_TEXT ("The first component kind is %C,")
297 ACE_TEXT ("The second component id is %C,")
298 ACE_TEXT ("The second component kind is %C\n\n"),
299 name[0].id.in (),
300 name[0].kind.in (),
301 name[1].id.in (),
302 name[1].kind.in ()));
304 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("The string form of the input name is: \n%C\n\n"),
305 str_name.in ()));
307 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("The unstringified version of the name components are:,")
308 ACE_TEXT ("The first component id is %C,")
309 ACE_TEXT ("The first component kind is %C,")
310 ACE_TEXT ("The second component id is %C,")
311 ACE_TEXT ("The second component kind is %C\n\n"),
312 nm[0].id.in (),
313 nm[0].kind.in (),
314 nm[1].id.in (),
315 nm[1].kind.in ()));
317 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("When the address of the NamingContext is:")
318 ACE_TEXT ("myhost.555xyz.com:9999")
319 ACE_TEXT ("and the Object name is \n%C\n"),
320 obj_name.in ()));
322 ACE_DEBUG ((LM_DEBUG,ACE_TEXT ("The URL form of the string is \n %C\n"),
323 url_string.in ()));