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 (void)
29 NContextExt_Client_i::~NContextExt_Client_i (void)
34 // Parses the command line arguments and returns an
37 NContextExt_Client_i::parse_args (void)
40 ACE_Get_Opt
get_opts (argc_
, argv_
, ACE_TEXT("dvs"));
45 while ((c
= get_opts ()) != -1)
48 case 'd': // debug flag
51 case 'v': // output needed
58 ACE_ERROR_RETURN ((LM_ERROR
,
67 // Indicates successful parsing of command line.
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)));
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;
91 *name_componentPtr
= '/';
96 *name_componentPtr
= '.';
101 *name_componentPtr
= '\\';
106 *name_componentPtr
= '<';
111 *name_componentPtr
= '>';
116 *name_componentPtr
= ' ';
121 *name_componentPtr
= '%';
128 *name_componentPtr
= 'A' + ( ACE_OS::rand () % 26 );
133 ACE_ERROR ((LM_ERROR
, "shouldnt come here"));
138 *name_componentPtr
= '\0';
140 return name_component
;
145 NContextExt_Client_i::run (void)
150 CosNaming::Name name
;
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
168 this->naming_context_
->resolve_str (str_name
.in ());
170 catch (const CosNaming::NamingContext::NotFound
&)
175 Web_Server::Iterator_Factory_var factory
=
176 Web_Server::Iterator_Factory::_narrow (factory_object
.in ());
180 CosNaming::BindingIterator_var iter
;
181 CosNaming::BindingList_var bindings_list
;
183 this->naming_context_
->list (2,
184 bindings_list
.out (),
187 // Convert the stringified name back as CosNaming::Name and print
189 CosNaming::Name
*nam
=
190 this->naming_context_
->to_name (str_name
.in ());
192 // Declare a CosNaming::Name variable and assign length to it.
194 nm
.length (nam
->length ());
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
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
,
224 catch (const CORBA::NO_MEMORY
& ex
)
226 ex
._tao_print_exception ("A system exception oc client side");
229 catch (const CORBA::SystemException
& ex
)
231 ex
._tao_print_exception ("A system exception oc client side");
234 catch (const CORBA::Exception
& ex
)
236 ex
._tao_print_exception ("client");
244 NContextExt_Client_i::init (int argc
, ACE_TCHAR
*argv
[])
251 // First initialize the ORB, that will remove some arguments...
253 CORBA::ORB_init (this->argc_
, this->argv_
);
255 // There must be at least one argument, the file that has to be
257 if (this->parse_args () == -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");
288 NContextExt_Client_i::print_values (CosNaming::Name name
,
289 CORBA::String_var str_name
,
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"),
302 name
[1].kind
.in ()));
304 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("The string form of the input name is: \n%C\n\n"),
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"),
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"),
322 ACE_DEBUG ((LM_DEBUG
,ACE_TEXT ("The URL form of the string is \n %C\n"),