2 //=============================================================================
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"
25 NContextExt_Client_i::NContextExt_Client_i ()
29 NContextExt_Client_i::~NContextExt_Client_i ()
34 // Parses the command line arguments and returns an
37 NContextExt_Client_i::parse_args ()
39 ACE_Get_Opt
get_opts (argc_
, argv_
, ACE_TEXT("dvs"));
44 while ((c
= get_opts ()) != -1)
47 case 'd': // debug flag
50 case 'v': // output needed
57 ACE_ERROR_RETURN ((LM_ERROR
,
66 // Indicates successful parsing of command line.
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)));
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;
88 *name_componentPtr
= '/';
93 *name_componentPtr
= '.';
98 *name_componentPtr
= '\\';
103 *name_componentPtr
= '<';
108 *name_componentPtr
= '>';
113 *name_componentPtr
= ' ';
118 *name_componentPtr
= '%';
125 *name_componentPtr
= 'A' + ( ACE_OS::rand () % 26);
130 ACE_ERROR ((LM_ERROR
, "shouldnt come here"));
135 *name_componentPtr
= '\0';
137 return name_component
;
141 NContextExt_Client_i::run ()
145 CosNaming::Name name
;
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
163 this->naming_context_
->resolve_str (str_name
.in ());
165 catch (const CosNaming::NamingContext::NotFound
&)
170 Web_Server::Iterator_Factory_var factory
=
171 Web_Server::Iterator_Factory::_narrow (factory_object
.in ());
175 CosNaming::BindingIterator_var iter
;
176 CosNaming::BindingList_var bindings_list
;
178 this->naming_context_
->list (2,
179 bindings_list
.out (),
182 // Convert the stringified name back as CosNaming::Name and print
184 CosNaming::Name
*nam
=
185 this->naming_context_
->to_name (str_name
.in ());
187 // Declare a CosNaming::Name variable and assign length to it.
189 nm
.length (nam
->length ());
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
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
,
219 catch (const CORBA::NO_MEMORY
& ex
)
221 ex
._tao_print_exception ("A system exception oc client side");
224 catch (const CORBA::SystemException
& ex
)
226 ex
._tao_print_exception ("A system exception oc client side");
229 catch (const CORBA::Exception
& ex
)
231 ex
._tao_print_exception ("client");
239 NContextExt_Client_i::init (int argc
, ACE_TCHAR
*argv
[])
246 // First initialize the ORB, that will remove some arguments...
248 CORBA::ORB_init (this->argc_
, this->argv_
);
250 // There must be at least one argument, the file that has to be
252 if (this->parse_args () == -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");
283 NContextExt_Client_i::print_values (CosNaming::Name name
,
284 CORBA::String_var str_name
,
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"),
296 name
[1].kind
.in ()));
298 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("The string form of the input name is: \n%C\n\n"),
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"),
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"),
316 ACE_DEBUG ((LM_DEBUG
,ACE_TEXT ("The URL form of the string is \n %C\n"),