Merge pull request #1844 from jrw972/monterey
[ACE_TAO.git] / TAO / tests / RTCORBA / Thread_Pool / client.cpp
blob6b3ab9377b1424701a3179fcd20190437635d336
1 #include "ace/Get_Opt.h"
2 #include "testC.h"
3 #include "tao/RTCORBA/RTCORBA.h"
4 #include "tao/ORB_Core.h"
5 #include "ace/Task.h"
6 #include "ace/OS_NS_unistd.h"
7 #include "../check_supported_priorities.cpp"
9 const ACE_TCHAR *ior = ACE_TEXT("file://ior_1");
10 int iterations = 6;
11 int shutdown_server = 0;
13 int
14 parse_args (int argc, ACE_TCHAR *argv[])
16 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("xk:i:"));
17 int c;
19 while ((c = get_opts ()) != -1)
20 switch (c)
22 case 'x':
23 shutdown_server = 1;
24 break;
26 case 'k':
27 ior = get_opts.opt_arg ();
28 break;
30 case 'i':
31 iterations = ACE_OS::atoi (get_opts.opt_arg ());
32 break;
34 case '?':
35 default:
36 ACE_ERROR_RETURN ((LM_ERROR,
37 "usage: %s "
38 "-k <ior> "
39 "-i <iterations> "
40 "-x [shutdown server] "
41 "\n",
42 argv [0]),
43 -1);
46 // Indicates successful parsing of the command line
47 return 0;
50 class Task : public ACE_Task_Base
52 public:
54 Task (ACE_Thread_Manager &thread_manager,
55 CORBA::ORB_ptr orb);
57 int svc (void);
59 CORBA::ORB_var orb_;
63 Task::Task (ACE_Thread_Manager &thread_manager,
64 CORBA::ORB_ptr orb)
65 : ACE_Task_Base (&thread_manager),
66 orb_ (CORBA::ORB::_duplicate (orb))
70 int
71 Task::svc (void)
73 try
75 CORBA::Object_var object =
76 this->orb_->string_to_object (ior);
78 test_var test =
79 test::_narrow (object.in ());
81 object =
82 this->orb_->resolve_initial_references ("RTCurrent");
84 RTCORBA::Current_var current =
85 RTCORBA::Current::_narrow (object.in ());
87 // We need to set the client thread CORBA priority
88 current->the_priority (get_implicit_thread_CORBA_priority (this->orb_.in ()));
90 pid_t pid =
91 ACE_OS::getpid ();
93 for (int i = 0; i != iterations; ++i)
95 CORBA::Long r =
96 test->method (pid,
97 i);
99 ACE_ASSERT (r == i);
100 // Assert disappears on with optimizations on.
101 ACE_UNUSED_ARG (r);
104 if (shutdown_server)
106 test->shutdown ();
109 catch (const CORBA::Exception& ex)
111 ex._tao_print_exception ("Exception caught:");
112 return -1;
115 return 0;
119 ACE_TMAIN(int argc, ACE_TCHAR *argv[])
123 CORBA::ORB_var orb =
124 CORBA::ORB_init (argc, argv);
126 int result =
127 parse_args (argc, argv);
128 if (result != 0)
129 return result;
131 // Thread Manager for managing task.
132 ACE_Thread_Manager thread_manager;
134 // Create task.
135 Task task (thread_manager,
136 orb.in ());
138 // Task activation flags.
139 long flags =
140 THR_NEW_LWP |
141 THR_JOINABLE |
142 orb->orb_core ()->orb_params ()->thread_creation_flags ();
144 // Activate task.
145 result =
146 task.activate (flags);
147 if (result == -1)
149 if (errno == EPERM)
151 ACE_ERROR_RETURN ((LM_ERROR,
152 "Cannot create thread with scheduling policy %s\n"
153 "because the user does not have the appropriate privileges, terminating program....\n"
154 "Check svc.conf options and/or run as root\n",
155 sched_policy_name (orb->orb_core ()->orb_params ()->ace_sched_policy ())),
158 else
159 // Unexpected error.
160 ACE_ASSERT (0);
163 // Wait for task to exit.
164 result =
165 thread_manager.wait ();
166 ACE_ASSERT (result != -1);
168 catch (const CORBA::Exception& ex)
170 ex._tao_print_exception ("Exception caught");
171 return -1;
174 return 0;