Changes to attempt to silence bcc64x
[ACE_TAO.git] / TAO / orbsvcs / tests / Time / Client_i.cpp
blobc312319341558f1b0ebf2d5bf4ec984b5beaeaed
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"
13 // Constructor.
15 Client_i::Client_i ()
16 : ior_ (0),
17 loop_count_ (10),
18 shutdown_ (0),
19 clerk_ ()
23 // The following test throws an exception that the operation
24 // <secure_universal_time> has not been currently implemented in the
25 // given server.
27 void
28 Client_i::test_for_secure_universal_time ()
30 ACE_DEBUG ((LM_DEBUG,
31 "[CLIENT] Process/Thread Id : (%P/%t) Testing secure_time()\n"));
33 try
35 CosTime::UTO_var UTO_server =
36 this->clerk_->secure_universal_time ();
38 catch (const CORBA::SystemException& sysex)
40 sysex._tao_print_exception ("System Exception");
41 ACE_DEBUG ((LM_DEBUG,
42 "[CLIENT] Process/Thread Id : (%P/%t) test_for_secure_universal_time() successful !!\n"));
46 // The following test retrieves the current universal time as a UTO
47 // from the clerk_ and prints out the various attributes of the UTO.
49 void
50 Client_i::test_for_universal_time ()
52 ACE_DEBUG ((LM_DEBUG,
53 "[CLIENT] Process/Thread Id : (%P/%t) Testing universal_time()\n"));
54 try
56 CosTime::UTO_var UTO_server =
57 this->clerk_->universal_time ();
59 ACE_DEBUG ((LM_DEBUG,
60 "\nTime = %Q\nInaccuracy = %Q\nTimeDiff = %d\nstruct.time = %Q"
61 "\nstruct.inacclo = %d\nstruct.inacchi = %d\nstruct.Tdf = %d\n",
62 UTO_server->time (),
63 UTO_server->inaccuracy (),
64 UTO_server->tdf (),
65 (UTO_server->utc_time ()).time,
66 (UTO_server->utc_time ()).inacclo,
67 (UTO_server->utc_time ()).inacchi,
68 (UTO_server->utc_time ()).tdf));
70 catch (const CORBA::Exception& ex)
72 ex._tao_print_exception ("Error:");
73 ACE_DEBUG ((LM_DEBUG,
74 "[CLIENT] Process/Thread Id : (%P/%t) test_for_universal_time()\n"));
78 void
79 Client_i::test_for_new_universal_time ()
81 TimeBase::TimeT time = 999999999;
82 TimeBase::InaccuracyT inaccuracy = 9999;
83 TimeBase::TdfT tdf = 99;
85 ACE_DEBUG ((LM_DEBUG,
86 "[CLIENT] Process/Thread Id : (%P/%t) Testing new_universal_time()\n"));
88 try
90 CosTime::UTO_var UTO_server = this->clerk_->new_universal_time (time,
91 inaccuracy,
92 tdf);
94 ACE_ASSERT (UTO_server->time () == 999999999);
95 ACE_ASSERT (UTO_server->inaccuracy () == 9999);
96 ACE_ASSERT (UTO_server->tdf () == 99);
97 ACE_ASSERT ((UTO_server->utc_time ()).time == 999999999);
98 ACE_ASSERT ((UTO_server->utc_time ()).inacchi == 0);
99 ACE_ASSERT ((UTO_server->utc_time ()).inacclo == 9999);
100 ACE_ASSERT ((UTO_server->utc_time ()).tdf == 99);
102 catch (const CORBA::Exception& ex)
104 ACE_DEBUG ((LM_DEBUG,
105 "[CLIENT] Process/Thread Id : (%P/%t) Test new_universal_time () fails.\n"));
106 ex._tao_print_exception ("Exception :\n");
110 void
111 Client_i::test_for_uto_from_utc ()
113 TimeBase::UtcT utc_struct;
115 utc_struct.time = 999999999;
116 utc_struct.inacclo = 50;
117 utc_struct.inacchi = 50;
118 utc_struct.tdf = 99;
120 ACE_DEBUG ((LM_DEBUG,
121 "[CLIENT] Process/Thread Id : (%P/%t) Testing uto_from_utc ()\n"));
125 CosTime::UTO_var UTO_server = this->clerk_->uto_from_utc (utc_struct);
127 TimeBase::InaccuracyT inaccuracy = utc_struct.inacchi;
128 inaccuracy <<= 32;
129 inaccuracy |= utc_struct.inacclo;
131 ACE_ASSERT (UTO_server->time () == 999999999);
132 ACE_ASSERT (UTO_server->inaccuracy () == inaccuracy);
133 ACE_ASSERT (UTO_server->tdf () == 99);
134 ACE_ASSERT ((UTO_server->utc_time ()).time == 999999999);
135 ACE_ASSERT ((UTO_server->utc_time ()).inacclo == 50);
136 ACE_ASSERT ((UTO_server->utc_time ()).inacchi == 50);
137 ACE_ASSERT ((UTO_server->utc_time ()).tdf == 99);
139 catch (const CORBA::Exception& ex)
141 ACE_DEBUG ((LM_DEBUG,
142 "[CLIENT] Process/Thread Id : (%P/%t) Test uto_from_utc () fails.\n"));
143 ex._tao_print_exception ("Exception :\n");
147 void
148 Client_i::test_for_new_interval ()
150 TimeBase::TimeT lower = 666666666;
151 TimeBase::TimeT upper = 999999999;
153 ACE_DEBUG ((LM_DEBUG,
154 "[CLIENT] Process/Thread Id : (%P/%t) Testing new_interval ()\n"));
158 CosTime::TIO_var TIO_server = this->clerk_->new_interval (lower,
159 upper);
161 ACE_ASSERT ((TIO_server->time_interval ()).lower_bound == 666666666);
162 ACE_ASSERT ((TIO_server->time_interval ()).upper_bound == 999999999);
164 catch (const CORBA::Exception& ex)
166 ACE_DEBUG ((LM_DEBUG,
167 "[CLIENT] Process/Thread Id : (%P/%t) Test new_interval () fails.\n"));
168 ex._tao_print_exception ("Exception :\n");
171 return;
174 // Reads the Time Service Server ior from a file
177 Client_i::read_ior (ACE_TCHAR *filename)
179 // Open the file for reading.
180 ACE_HANDLE f_handle = ACE_OS::open (filename, 0);
182 if (f_handle == ACE_INVALID_HANDLE)
183 ACE_ERROR_RETURN ((LM_ERROR,
184 "[CLIENT] Process/Thread Id : (%P/%t) Unable to open %s for writing: %p\n",
185 filename),
186 -1);
188 ACE_Read_Buffer ior_buffer (f_handle);
189 char *data = ior_buffer.read ();
191 if (data == 0)
192 ACE_ERROR_RETURN ((LM_ERROR,
193 "[CLIENT] Process/Thread Id : (%P/%t) Unable to read ior: %p\n"),
194 -1);
196 this->ior_ = ACE_OS::strdup (data);
197 ior_buffer.alloc ()->free (data);
199 ACE_OS::close (f_handle);
201 return 0;
204 // Parses the command line arguments and returns an error status.
207 Client_i::parse_args ()
209 ACE_Get_Opt get_opts (argc_, argv_, ACE_TEXT("dn:f:xk:"));
210 int c;
211 int result;
213 while ((c = get_opts ()) != -1)
214 // ACE_DEBUG((LM_DEBUG,"bal =1%c",c
215 // ));
216 switch (c)
218 case 'd': // debug flag
219 TAO_debug_level++;
220 break;
221 case 'n': // loop count
222 this->loop_count_ = (u_int) ACE_OS::atoi (get_opts.opt_arg ());
223 break;
224 case 'k': // ior provide on command line
225 this->ior_ = ACE_OS::strdup (ACE_TEXT_ALWAYS_CHAR(get_opts.opt_arg ()));
226 break;
227 case 'f': // read the IOR from the file.
228 result = this->read_ior (get_opts.opt_arg ());
229 if (result < 0)
230 ACE_ERROR_RETURN ((LM_ERROR,
231 "[CLIENT] Process/Thread Id : (%P/%t) Unable to read ior from %s : %p\n",
232 get_opts.opt_arg ()),
233 -1);
234 break;
235 case 'x':
236 this->shutdown_ = 1;
237 break;
238 case '?':
239 default:
240 ACE_ERROR_RETURN ((LM_ERROR,
241 "usage: %s"
242 " [-d]"
243 " [-n loopcount]"
244 " [-f ior-file]"
245 " [-k ior]"
246 " [-x]"
247 "\n",
248 this->argv_ [0]),
249 -1);
252 // Indicates successful parsing of command line.
253 return 0;
256 // Execute client example code.
259 Client_i::run ()
261 // Retrieves the current universal time.
262 test_for_universal_time ();
264 // Asserts the validity of the new_universal_time.
265 test_for_new_universal_time ();
267 // Asserts the validity of the UTO created from a given UTC.
268 test_for_uto_from_utc ();
270 // Asserts the validity of the TIO created using the given
271 // bounds.
272 test_for_new_interval ();
274 // Throws an exception because the method has not been
275 // implemented currently.
276 test_for_secure_universal_time ();
278 return 0;
281 Client_i::~Client_i ()
283 ACE_OS::free (this->ior_);
287 Client_i::obtain_initial_references ()
291 // Initialize the naming services.
292 if (my_name_client_.init (orb_.in ()) != 0)
293 ACE_ERROR_RETURN ((LM_ERROR,
294 "[CLIENT] Process/Thread Id : (%P/%t) Unable to initialize "
295 "the TAO_Naming_Client.\n"),
296 -1);
298 ACE_DEBUG ((LM_DEBUG,
299 "DONE\n"));
301 char host_name[MAXHOSTNAMELEN];
302 char name[MAXHOSTNAMELEN];
304 ACE_OS::hostname (host_name, MAXHOSTNAMELEN);
306 CosNaming::Name clerk_name;
307 clerk_name.length (2);
308 clerk_name[0].id = CORBA::string_dup ("ClerkContext");
309 ACE_OS::strcpy (name, "Clerk:");
310 ACE_OS::strcat (name, host_name);
311 clerk_name[1].id = CORBA::string_dup (name);
313 ACE_DEBUG ((LM_DEBUG,
314 "%C|\n",
315 name));
317 CORBA::Object_var temp_object =
318 my_name_client_->resolve (clerk_name);
320 clerk_ = CosTime::TimeService::_narrow (temp_object.in ());
322 if (CORBA::is_nil (clerk_.in ()))
323 ACE_ERROR_RETURN ((LM_ERROR,
324 "[CLIENT] Process/Thread Id : (%P/%t) Unable to Resolve "
325 "Clerk Reference\n"),
326 -1);
328 catch (const CORBA::Exception& ex)
330 ex._tao_print_exception (
331 "Client :: obtain_initial_references\n");
332 return -1;
335 return 0;
338 // This method uses the IOR if specified on the command line or in a
339 // file, else it uses the Naming Service
342 Client_i::init (int argc, ACE_TCHAR **argv)
344 this->argc_ = argc;
345 this->argv_ = argv;
349 // Retrieve the ORB.
350 this->orb_ = CORBA::ORB_init (this->argc_, this->argv_);
352 // Parse command line and verify parameters.
353 if (this->parse_args () == -1)
354 return -1;
356 if (this->ior_)
358 // An ior is specified for the client through a commandline
359 // option or a file.
361 CORBA::Object_var server_object =
362 this->orb_->string_to_object (this->ior_);
364 if (CORBA::is_nil (server_object.in ()))
365 ACE_ERROR_RETURN ((LM_ERROR,
366 "invalid ior <%C>\n",
367 this->ior_),
368 -1);
369 this->clerk_ =
370 CosTime::TimeService::_narrow (server_object.in ());
372 ACE_DEBUG ((LM_DEBUG,
373 "[CLIENT] Process/Thread Id : (%P/%t) Using the IOR provided\n"));
375 else
376 { // No IOR specified. Use the Naming Service
377 ACE_DEBUG((LM_DEBUG,
378 "[CLIENT] Process/Thread Id : (%P/%t) Using the Naming Service\n"));
380 if (this->obtain_initial_references () == -1)
381 return -1;
384 catch (const CORBA::Exception& ex)
386 ex._tao_print_exception ("Client_i::init\n");
387 return -1;
390 return 0;