Merge pull request #1844 from jrw972/monterey
[ACE_TAO.git] / TAO / tests / Connection_Timeout / client.cpp
blobc0a0e8389d84ec45f548020586d543233bfc33f3
1 #include "TestC.h"
2 #include "ace/Get_Opt.h"
3 #include "ace/Profile_Timer.h"
4 #include "tao/Messaging/Messaging.h"
5 #include "tao/AnyTypeCode/Any.h"
7 const ACE_TCHAR *ior =
8 ACE_TEXT("corbaloc:iiop:192.3.47/10007/RandomObject");
10 ACE_Profile_Timer profile_timer;
11 TimeBase::TimeT timeout_period = 1000000;
13 int
14 parse_args (int argc, ACE_TCHAR *argv[])
16 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("k:"));
17 int c;
19 while ((c = get_opts ()) != -1)
20 switch (c)
22 case 'k':
23 ior = get_opts.opt_arg ();
24 break;
26 case '?':
27 default:
28 ACE_ERROR_RETURN ((LM_ERROR,
29 "usage: %s "
30 "-k <ior> "
31 "\n",
32 argv [0]),
33 -1);
35 // Indicates successful parsing of the command line
36 return 0;
39 int
40 test_timeout (CORBA::Object_ptr object)
42 // Start the timer
43 profile_timer.start ();
45 try
47 // First connection happens here..
48 Test::Hello_var hello =
49 Test::Hello::_narrow(object);
51 if (CORBA::is_nil (hello.in ()))
53 ACE_ERROR_RETURN ((LM_DEBUG,
54 "Nil Test::Hello reference <%s>\n",
55 ior),
56 1);
59 CORBA::String_var the_string =
60 hello->get_string ();
62 ACE_DEBUG ((LM_DEBUG, "(%P|%t) - string returned <%C>\n",
63 the_string.in ()));
65 hello->shutdown ();
67 catch (const CORBA::Exception&)
69 // Stop the timer
70 profile_timer.stop ();
72 // Get the elampsed time
73 ACE_Profile_Timer::ACE_Elapsed_Time el;
74 profile_timer.elapsed_time (el);
76 // Give a 30% error margin for handling exceptions etc. It is a
77 // high margin, though!. But the timeout is too small and wider
78 // range would help.
79 // The elapsed time is in secs
80 if (el.real_time > 0.200)
82 ACE_ERROR_RETURN ((LM_ERROR,
83 "(%P|%t) ERROR: Too long to timeout: %F\n",
84 el.real_time),
85 1);
87 else
88 ACE_DEBUG ((LM_DEBUG,
89 "(%P|%t) Success, timeout: %F\n",
90 el.real_time));
93 return 0;
96 int
97 ACE_TMAIN(int argc, ACE_TCHAR *argv[])
99 int retval = 1;
103 CORBA::ORB_var orb =
104 CORBA::ORB_init (argc, argv);
106 if (parse_args (argc, argv) != 0)
107 return 1;
109 CORBA::Object_var tmp =
110 orb->string_to_object(ior);
112 CORBA::Object_var object =
113 orb->resolve_initial_references ("PolicyCurrent");
115 CORBA::PolicyCurrent_var policy_current =
116 CORBA::PolicyCurrent::_narrow (object.in ());
118 CORBA::Any timeout_as_any;
119 timeout_as_any <<= timeout_period;
121 CORBA::PolicyList policy_list (1);
122 policy_list.length (1);
123 policy_list[0] =
124 orb->create_policy (TAO::CONNECTION_TIMEOUT_POLICY_TYPE,
125 timeout_as_any);
127 policy_current->set_policy_overrides (policy_list,
128 CORBA::ADD_OVERRIDE);
130 for (CORBA::ULong l = 0;
131 l != policy_list.length ();
132 ++l)
134 policy_list[l]->destroy ();
137 retval = test_timeout (tmp.in ());
139 orb->destroy ();
141 catch (const CORBA::Exception& ex)
143 ex._tao_print_exception ("Exception caught:");
144 return 1;
147 return retval;