Merge pull request #1844 from jrw972/monterey
[ACE_TAO.git] / TAO / tests / Policies / Manipulation.cpp
blob84f75a7854c309baeb73bde1b2545761740839b0
1 #include "testC.h"
3 #include "tao/Messaging/Messaging.h"
4 #include "tao/AnyTypeCode/Any.h"
5 #include "tao/AnyTypeCode/TAOA.h"
6 #include "tao/TAOC.h"
7 #include "tao/Object_T.h"
8 #include "ace/Get_Opt.h"
9 #include "ace/Task.h"
10 #include "ace/OS_NS_time.h"
12 int nthreads = 5;
13 int niterations = 100;
15 int
16 parse_args (int argc, ACE_TCHAR *argv[])
18 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("n:i:"));
19 int c;
21 while ((c = get_opts ()) != -1)
22 switch (c)
24 case 'n':
25 nthreads = ACE_OS::atoi (get_opts.opt_arg ());
26 break;
27 case 'i':
28 niterations = ACE_OS::atoi (get_opts.opt_arg ());
29 break;
30 case '?':
31 default:
32 ACE_ERROR_RETURN ((LM_ERROR,
33 "usage: %s "
34 "-n <nthreads> "
35 "-i <niterations> "
36 "\n",
37 argv [0]),
38 -1);
40 // Indicates successful parsing of the command line
41 return 0;
44 /**
45 * Run the client thread
47 * Use the ACE_Task_Base class to run the client threads.
49 class Manipulation : public ACE_Task_Base
51 public:
52 Manipulation (CORBA::ORB_ptr orb,
53 Test_ptr test,
54 int niterations);
56 /// The thread entry point.
57 virtual int svc (void);
59 private:
60 void perform_iteration (unsigned int *seed,
61 CORBA::PolicyList_var &policies,
62 CORBA::PolicyManager_ptr policy_manager,
63 CORBA::PolicyCurrent_ptr policy_current);
65 private:
66 /// The ORB pointer
67 CORBA::ORB_var orb_;
69 /// The test object reference
70 Test_var test_;
72 /// The number of iterations on this thread
73 int niterations_;
76 int
77 ACE_TMAIN(int argc, ACE_TCHAR *argv[])
79 try
81 CORBA::ORB_var orb =
82 CORBA::ORB_init (argc, argv);
84 if (parse_args (argc, argv) != 0)
85 return 1;
87 CORBA::Object_var object =
88 orb->string_to_object ("corbaloc:iiop:localhost:12345/FakeIOR");
90 Test_var test =
91 TAO::Narrow_Utils<Test>::unchecked_narrow (object.in ());
93 if (CORBA::is_nil (test.in ()))
95 ACE_ERROR_RETURN ((LM_ERROR,
96 "Object reference is nil\n"),
97 1);
100 Manipulation manipulation (orb.in (),
101 test.in (),
102 niterations);
103 if (manipulation.activate (THR_NEW_LWP | THR_JOINABLE,
104 nthreads) != 0)
105 ACE_ERROR_RETURN ((LM_ERROR,
106 "Cannot activate manipulation threads\n"),
109 manipulation.thr_mgr ()->wait ();
111 ACE_DEBUG ((LM_DEBUG, "threads finished\n"));
113 orb->destroy ();
115 catch (const CORBA::Exception& ex)
117 ex._tao_print_exception ("Caught exception:");
118 return 1;
121 return 0;
124 // ****************************************************************
126 Manipulation::Manipulation (CORBA::ORB_ptr orb,
127 Test_ptr test,
128 int niterations)
129 : orb_ (CORBA::ORB::_duplicate (orb)),
130 test_ (Test::_duplicate (test)),
131 niterations_ (niterations)
135 void
136 Manipulation::perform_iteration (unsigned int *seed,
137 CORBA::PolicyList_var &policies,
138 CORBA::PolicyManager_ptr policy_manager,
139 CORBA::PolicyCurrent_ptr policy_current)
143 int r = ACE_OS::rand_r (seed);
145 const int ADD_OBJECT_POLICY = 0;
146 const int ADD_CURRENT_POLICY = 1;
147 const int ADD_MANAGER_POLICY = 2;
149 const int SET_OBJECT_POLICY = 3;
150 const int SET_CURRENT_POLICY = 4;
151 const int SET_MANAGER_POLICY = 5;
153 const int SAVE_CURRENT_POLICIES = 6;
154 const int RESTORE_CURRENT_POLICIES = 7;
155 const int SAVE_MANAGER_POLICIES = 8;
156 // const int RESTORE_MANAGER_POLICIES = 9;
157 const int LAST_OPERATION = 10;
158 int operation = r % LAST_OPERATION;
160 if (operation == ADD_OBJECT_POLICY
161 || operation == ADD_CURRENT_POLICY
162 || operation == ADD_MANAGER_POLICY
163 || operation == SET_OBJECT_POLICY
164 || operation == SET_CURRENT_POLICY
165 || operation == SET_MANAGER_POLICY)
167 CORBA::Policy_var policy;
169 CORBA::Any any;
170 CORBA::ULong policy_type = 0;
172 int type = ACE_OS::rand_r (seed) % 3;
173 if (type == 0)
175 TimeBase::TimeT value = 0;
176 any <<= value;
178 policy_type =
179 Messaging::RELATIVE_RT_TIMEOUT_POLICY_TYPE;
181 else if (type == 1)
183 TAO::BufferingConstraint value;
184 any <<= value;
186 policy_type =
187 TAO::BUFFERING_CONSTRAINT_POLICY_TYPE;
189 else // type == 2 (or something else)
191 Messaging::SyncScope value = 0;
192 any <<= value;
194 policy_type =
195 Messaging::SYNC_SCOPE_POLICY_TYPE;
198 policy = this->orb_->create_policy (policy_type,
199 any);
201 CORBA::SetOverrideType override_type = CORBA::SET_OVERRIDE;
202 if (operation == ADD_OBJECT_POLICY
203 || operation == ADD_CURRENT_POLICY
204 || operation == ADD_MANAGER_POLICY)
206 override_type = CORBA::ADD_OVERRIDE;
209 CORBA::PolicyList policy_list (1);
210 policy_list.length (1);
211 policy_list[0] = CORBA::Policy::_duplicate (policy.in ());
213 if (operation == ADD_OBJECT_POLICY
214 || operation == SET_OBJECT_POLICY)
216 CORBA::Object_var tmp =
217 this->test_->_set_policy_overrides (policy_list,
218 override_type);
220 else if (operation == ADD_CURRENT_POLICY
221 || operation == SET_CURRENT_POLICY)
223 policy_current->set_policy_overrides (policy_list,
224 override_type);
226 else
228 // operation == ADD_CURRENT_POLICY
229 // || operation == SET_CURRENT_POLICY)
230 policy_manager->set_policy_overrides (policy_list,
231 override_type);
233 policy_list[0]->destroy ();
235 else if (operation == SAVE_CURRENT_POLICIES)
237 CORBA::PolicyTypeSeq types;
238 policies =
239 policy_current->get_policy_overrides (types);
241 else if (operation == SAVE_MANAGER_POLICIES)
243 CORBA::PolicyTypeSeq types;
244 policies =
245 policy_manager->get_policy_overrides (types);
247 else if (operation == RESTORE_CURRENT_POLICIES)
249 if (policies.ptr () != 0)
251 policy_current->set_policy_overrides (policies.in (),
252 CORBA::SET_OVERRIDE);
255 else // operation == RESTORE_MANAGER_POLICIES)
257 if (policies.ptr () != 0)
259 policy_manager->set_policy_overrides (policies.in (),
260 CORBA::SET_OVERRIDE);
264 catch (const CORBA::Exception&)
266 // Ignore all exceptions
272 Manipulation::svc (void)
276 unsigned int seed =
277 static_cast<unsigned int> (ACE_OS::gethrtime ());
278 CORBA::Object_var object =
279 this->orb_->resolve_initial_references ("ORBPolicyManager");
281 CORBA::PolicyManager_var policy_manager =
282 CORBA::PolicyManager::_narrow (object.in ());
284 object =
285 this->orb_->resolve_initial_references ("PolicyCurrent");
287 CORBA::PolicyCurrent_var policy_current =
288 CORBA::PolicyCurrent::_narrow (object.in ());
290 for (int i = 0; i != this->niterations_; ++i)
292 CORBA::PolicyList_var policies;
293 this->perform_iteration (&seed,
294 policies,
295 policy_manager.in (),
296 policy_current.in ());
299 catch (const CORBA::Exception& ex)
301 ex._tao_print_exception ("Manipulation: exception raised");
303 return 0;