Merge pull request #1844 from jrw972/monterey
[ACE_TAO.git] / TAO / orbsvcs / tests / Time / Client_i.cpp
blobb6a9fc776d99f9d80e55e5522bb58d90b7eaf003
1 #include "Client_i.h"
3 #include "tao/debug.h"
5 #include "ace/Get_Opt.h"
6 #include "ace/Read_Buffer.h"
7 #include "ace/OS_NS_unistd.h"
8 #include "ace/OS_NS_string.h"
9 #include "ace/OS_NS_fcntl.h"
10 #include "ace/os_include/os_netdb.h"
14 // Constructor.
16 Client_i::Client_i (void)
17 : ior_ (0),
18 loop_count_ (10),
19 shutdown_ (0),
20 clerk_ ()
24 // The following test throws an exception that the operation
25 // <secure_universal_time> has not been currently implemented in the
26 // given server.
28 void
29 Client_i::test_for_secure_universal_time (void)
31 ACE_DEBUG ((LM_DEBUG,
32 "[CLIENT] Process/Thread Id : (%P/%t) Testing secure_time()\n"));
34 try
36 CosTime::UTO_var UTO_server =
37 this->clerk_->secure_universal_time ();
40 catch (const CORBA::SystemException& sysex)
42 sysex._tao_print_exception ("System Exception");
43 ACE_DEBUG ((LM_DEBUG,
44 "[CLIENT] Process/Thread Id : (%P/%t) test_for_secure_universal_time() successful !!\n"));
48 // The following test retrieves the current universal time as a UTO
49 // from the clerk_ and prints out the various attributes of the UTO.
51 void
52 Client_i::test_for_universal_time (void)
54 ACE_DEBUG ((LM_DEBUG,
55 "[CLIENT] Process/Thread Id : (%P/%t) Testing universal_time()\n"));
56 try
58 CosTime::UTO_var UTO_server =
59 this->clerk_->universal_time ();
61 ACE_DEBUG ((LM_DEBUG,
62 "\nTime = %Q\nInaccuracy = %Q\nTimeDiff = %d\nstruct.time = %Q"
63 "\nstruct.inacclo = %d\nstruct.inacchi = %d\nstruct.Tdf = %d\n",
64 UTO_server->time (),
65 UTO_server->inaccuracy (),
66 UTO_server->tdf (),
67 (UTO_server->utc_time ()).time,
68 (UTO_server->utc_time ()).inacclo,
69 (UTO_server->utc_time ()).inacchi,
70 (UTO_server->utc_time ()).tdf));
72 catch (const CORBA::Exception& ex)
74 ex._tao_print_exception ("Error:");
75 ACE_DEBUG ((LM_DEBUG,
76 "[CLIENT] Process/Thread Id : (%P/%t) test_for_universal_time()\n"));
80 void
81 Client_i::test_for_new_universal_time (void)
83 TimeBase::TimeT time = 999999999;
84 TimeBase::InaccuracyT inaccuracy = 9999;
85 TimeBase::TdfT tdf = 99;
87 ACE_DEBUG ((LM_DEBUG,
88 "[CLIENT] Process/Thread Id : (%P/%t) Testing new_universal_time()\n"));
90 try
92 CosTime::UTO_var UTO_server = this->clerk_->new_universal_time (time,
93 inaccuracy,
94 tdf);
96 ACE_ASSERT (UTO_server->time () == 999999999);
97 ACE_ASSERT (UTO_server->inaccuracy () == 9999);
98 ACE_ASSERT (UTO_server->tdf () == 99);
99 ACE_ASSERT ((UTO_server->utc_time ()).time == 999999999);
100 ACE_ASSERT ((UTO_server->utc_time ()).inacchi == 0);
101 ACE_ASSERT ((UTO_server->utc_time ()).inacclo == 9999);
102 ACE_ASSERT ((UTO_server->utc_time ()).tdf == 99);
105 catch (const CORBA::Exception& ex)
107 ACE_DEBUG ((LM_DEBUG,
108 "[CLIENT] Process/Thread Id : (%P/%t) Test new_universal_time () fails.\n"));
109 ex._tao_print_exception ("Exception :\n");
113 void
114 Client_i::test_for_uto_from_utc (void)
116 TimeBase::UtcT utc_struct;
118 utc_struct.time = 999999999;
119 utc_struct.inacclo = 50;
120 utc_struct.inacchi = 50;
121 utc_struct.tdf = 99;
123 ACE_DEBUG ((LM_DEBUG,
124 "[CLIENT] Process/Thread Id : (%P/%t) Testing uto_from_utc ()\n"));
128 CosTime::UTO_var UTO_server = this->clerk_->uto_from_utc (utc_struct);
130 TimeBase::InaccuracyT inaccuracy = utc_struct.inacchi;
131 inaccuracy <<= 32;
132 inaccuracy |= utc_struct.inacclo;
134 ACE_ASSERT (UTO_server->time () == 999999999);
135 ACE_ASSERT (UTO_server->inaccuracy () == inaccuracy);
136 ACE_ASSERT (UTO_server->tdf () == 99);
137 ACE_ASSERT ((UTO_server->utc_time ()).time == 999999999);
138 ACE_ASSERT ((UTO_server->utc_time ()).inacclo == 50);
139 ACE_ASSERT ((UTO_server->utc_time ()).inacchi == 50);
140 ACE_ASSERT ((UTO_server->utc_time ()).tdf == 99);
143 catch (const CORBA::Exception& ex)
145 ACE_DEBUG ((LM_DEBUG,
146 "[CLIENT] Process/Thread Id : (%P/%t) Test uto_from_utc () fails.\n"));
147 ex._tao_print_exception ("Exception :\n");
151 void
152 Client_i::test_for_new_interval (void)
154 TimeBase::TimeT lower = 666666666;
155 TimeBase::TimeT upper = 999999999;
157 ACE_DEBUG ((LM_DEBUG,
158 "[CLIENT] Process/Thread Id : (%P/%t) Testing new_interval ()\n"));
162 CosTime::TIO_var TIO_server = this->clerk_->new_interval (lower,
163 upper);
165 ACE_ASSERT ((TIO_server->time_interval ()).lower_bound == 666666666);
166 ACE_ASSERT ((TIO_server->time_interval ()).upper_bound == 999999999);
169 catch (const CORBA::Exception& ex)
171 ACE_DEBUG ((LM_DEBUG,
172 "[CLIENT] Process/Thread Id : (%P/%t) Test new_interval () fails.\n"));
173 ex._tao_print_exception ("Exception :\n");
176 return;
179 // Reads the Time Service Server ior from a file
182 Client_i::read_ior (ACE_TCHAR *filename)
184 // Open the file for reading.
185 ACE_HANDLE f_handle = ACE_OS::open (filename, 0);
187 if (f_handle == ACE_INVALID_HANDLE)
188 ACE_ERROR_RETURN ((LM_ERROR,
189 "[CLIENT] Process/Thread Id : (%P/%t) Unable to open %s for writing: %p\n",
190 filename),
191 -1);
193 ACE_Read_Buffer ior_buffer (f_handle);
194 char *data = ior_buffer.read ();
196 if (data == 0)
197 ACE_ERROR_RETURN ((LM_ERROR,
198 "[CLIENT] Process/Thread Id : (%P/%t) Unable to read ior: %p\n"),
199 -1);
201 this->ior_ = ACE_OS::strdup (data);
202 ior_buffer.alloc ()->free (data);
204 ACE_OS::close (f_handle);
206 return 0;
209 // Parses the command line arguments and returns an error status.
212 Client_i::parse_args (void)
214 ACE_Get_Opt get_opts (argc_, argv_, ACE_TEXT("dn:f:xk:"));
215 int c;
216 int result;
218 while ((c = get_opts ()) != -1)
219 // ACE_DEBUG((LM_DEBUG,"bal =1%c",c
220 // ));
221 switch (c)
223 case 'd': // debug flag
224 TAO_debug_level++;
225 break;
226 case 'n': // loop count
227 this->loop_count_ = (u_int) ACE_OS::atoi (get_opts.opt_arg ());
228 break;
229 case 'k': // ior provide on command line
230 this->ior_ = ACE_OS::strdup (ACE_TEXT_ALWAYS_CHAR(get_opts.opt_arg ()));
231 break;
232 case 'f': // read the IOR from the file.
233 result = this->read_ior (get_opts.opt_arg ());
234 if (result < 0)
235 ACE_ERROR_RETURN ((LM_ERROR,
236 "[CLIENT] Process/Thread Id : (%P/%t) Unable to read ior from %s : %p\n",
237 get_opts.opt_arg ()),
238 -1);
239 break;
240 case 'x':
241 this->shutdown_ = 1;
242 break;
243 case '?':
244 default:
245 ACE_ERROR_RETURN ((LM_ERROR,
246 "usage: %s"
247 " [-d]"
248 " [-n loopcount]"
249 " [-f ior-file]"
250 " [-k ior]"
251 " [-x]"
252 "\n",
253 this->argv_ [0]),
254 -1);
257 // Indicates successful parsing of command line.
258 return 0;
261 // Execute client example code.
264 Client_i::run (void)
266 // Retrieves the current universal time.
267 test_for_universal_time ();
269 // Asserts the validity of the new_universal_time.
270 test_for_new_universal_time ();
272 // Asserts the validity of the UTO created from a given UTC.
273 test_for_uto_from_utc ();
275 // Asserts the validity of the TIO created using the given
276 // bounds.
277 test_for_new_interval ();
279 // Throws an exception because the method has not been
280 // implemented currently.
281 test_for_secure_universal_time ();
283 return 0;
286 Client_i::~Client_i (void)
288 ACE_OS::free (this->ior_);
292 Client_i::obtain_initial_references (void)
297 // Initialize the naming services.
298 if (my_name_client_.init (orb_.in ()) != 0)
299 ACE_ERROR_RETURN ((LM_ERROR,
300 "[CLIENT] Process/Thread Id : (%P/%t) Unable to initialize "
301 "the TAO_Naming_Client.\n"),
302 -1);
304 ACE_DEBUG ((LM_DEBUG,
305 "DONE\n"));
307 char host_name[MAXHOSTNAMELEN];
308 char name[MAXHOSTNAMELEN];
310 ACE_OS::hostname (host_name, MAXHOSTNAMELEN);
312 CosNaming::Name clerk_name;
313 clerk_name.length (2);
314 clerk_name[0].id = CORBA::string_dup ("ClerkContext");
315 ACE_OS::strcpy (name, "Clerk:");
316 ACE_OS::strcat (name, host_name);
317 clerk_name[1].id = CORBA::string_dup (name);
319 ACE_DEBUG ((LM_DEBUG,
320 "%C|\n",
321 name));
323 CORBA::Object_var temp_object =
324 my_name_client_->resolve (clerk_name);
326 clerk_ = CosTime::TimeService::_narrow (temp_object.in ());
328 if (CORBA::is_nil (clerk_.in ()))
329 ACE_ERROR_RETURN ((LM_ERROR,
330 "[CLIENT] Process/Thread Id : (%P/%t) Unable to Resolve "
331 "Clerk Reference\n"),
332 -1);
334 catch (const CORBA::Exception& ex)
336 ex._tao_print_exception (
337 "Client :: obtain_initial_references\n");
338 return -1;
341 return 0;
344 // This method uses the IOR if specified on the command line or in a
345 // file, else it uses the Naming Service
348 Client_i::init (int argc, ACE_TCHAR **argv)
350 this->argc_ = argc;
351 this->argv_ = argv;
355 // Retrieve the ORB.
356 this->orb_ = CORBA::ORB_init (this->argc_, this->argv_);
358 // Parse command line and verify parameters.
359 if (this->parse_args () == -1)
360 return -1;
362 if (this->ior_)
364 // An ior is specified for the client through a commandline
365 // option or a file.
367 CORBA::Object_var server_object =
368 this->orb_->string_to_object (this->ior_);
370 if (CORBA::is_nil (server_object.in ()))
371 ACE_ERROR_RETURN ((LM_ERROR,
372 "invalid ior <%C>\n",
373 this->ior_),
374 -1);
375 this->clerk_ =
376 CosTime::TimeService::_narrow (server_object.in ());
378 ACE_DEBUG ((LM_DEBUG,
379 "[CLIENT] Process/Thread Id : (%P/%t) Using the IOR provided\n"));
381 else
382 { // No IOR specified. Use the Naming Service
383 ACE_DEBUG((LM_DEBUG,
384 "[CLIENT] Process/Thread Id : (%P/%t) Using the Naming Service\n"));
386 if (this->obtain_initial_references () == -1)
387 return -1;
391 catch (const CORBA::Exception& ex)
393 ex._tao_print_exception ("Client_i::init\n");
394 return -1;
397 return 0;