Merge pull request #2301 from sonndinh/remove-dup-reactor-functions
[ACE_TAO.git] / TAO / tests / RTCORBA / common_args.cpp
blobbeb292b7cad7ebf4f4bb7a5ee0bace184aa14ff3
1 #include "ace/Read_Buffer.h"
2 #include "ace/Array_Base.h"
3 #include "ace/OS_NS_stdio.h"
4 #include "ace/OS_NS_math.h"
5 #include "ace/OS_NS_stdlib.h"
7 typedef ACE_Array_Base<CORBA::ULong> ULong_Array;
9 int
10 get_priority_bands (const char *test_type,
11 const ACE_TCHAR *bands_file,
12 RTCORBA::RTORB_ptr rt_orb,
13 CORBA::PolicyList &policies,
14 int debug)
17 // Read bands from a file.
19 FILE* file =
20 ACE_OS::fopen (bands_file, "r");
22 if (file == 0)
23 ACE_ERROR_RETURN ((LM_ERROR,
24 "Cannot open file %s\n",
25 bands_file),
26 -1);
28 ACE_Read_Buffer reader (file, 1);
30 char *string =
31 reader.read (EOF, ' ', '\0');
33 // Check for empty bands file.
34 if (string == 0)
36 if (debug)
37 ACE_DEBUG ((LM_DEBUG,
38 "\n%s: No bands set!\n\n",
39 test_type));
40 return 0;
43 CORBA::ULong bands_length =
44 (reader.replaced () + 1) / 2;
46 RTCORBA::PriorityBands bands;
47 bands.length (bands_length);
49 if (debug)
50 ACE_DEBUG ((LM_DEBUG,
51 "\n%s: There are %d bands: ",
52 test_type,
53 bands_length));
55 int result = 1;
56 char* working_string = string;
57 for (CORBA::ULong i = 0; i < bands_length; ++i)
59 result = ::sscanf (working_string,
60 "%hd",
61 &bands[i].low);
62 if (result == 0 || result == EOF)
63 break;
65 working_string += ACE_OS::strlen (working_string);
66 working_string += 1;
68 result = ::sscanf (working_string,
69 "%hd",
70 &bands[i].high);
71 if (result == 0 || result == EOF)
72 break;
74 working_string += ACE_OS::strlen (working_string);
75 working_string += 1;
77 if (debug)
78 ACE_DEBUG ((LM_DEBUG,
79 "[%d %d] ",
80 bands[i].low,
81 bands[i].high));
84 reader.alloc ()->free (string);
86 if (result == 0 || result == EOF)
87 ACE_ERROR_RETURN ((LM_ERROR,
88 "Parsing error in file %s\n",
89 bands_file),
90 -1);
92 if (debug)
93 ACE_DEBUG ((LM_DEBUG,
94 "\n\n"));
96 CORBA::Policy_var banded_connection_policy =
97 rt_orb->create_priority_banded_connection_policy (bands);
99 policies.length (policies.length () + 1);
100 policies[policies.length () - 1] =
101 banded_connection_policy;
103 return 0;
107 get_values (const char *test_type,
108 const ACE_TCHAR *file_name,
109 const char *name,
110 ULong_Array &values,
111 int debug)
114 // Read lanes from a file.
116 FILE* file =
117 ACE_OS::fopen (file_name, "r");
119 if (file == 0)
120 ACE_ERROR_RETURN ((LM_ERROR,
121 "Cannot open file %s\n",
122 file_name),
123 -1);
125 ACE_Read_Buffer reader (file, 1);
127 char *string =
128 reader.read (EOF, ' ', '\0');
130 // Check for empty lanes file.
131 if (string == 0)
133 if (debug)
134 ACE_DEBUG ((LM_DEBUG,
135 "\n%s: No %s set!\n\n",
136 test_type,
137 name));
138 return 0;
141 size_t length =
142 reader.replaced () + 1;
144 values.size (length);
146 if (debug)
147 ACE_DEBUG ((LM_DEBUG,
148 "\n%s: There are %d %s: ",
149 test_type,
150 length,
151 name));
153 int result = 1;
154 char* working_string = string;
155 for (CORBA::ULong i = 0; i < length; ++i)
157 // sscanf with "%ul" doesn't seem to work properly on HP-UX. So,
158 // we will use strtoul instead.
159 char* endptr = 0;
160 values[i] = ACE_OS::strtoul (working_string, &endptr, 10);
162 if (endptr != working_string && endptr != 0 && *endptr != '\0')
164 result = 0;
165 break;
168 working_string += ACE_OS::strlen (working_string);
169 working_string += 1;
171 if (debug)
172 ACE_DEBUG ((LM_DEBUG,
173 "[%u] ",
174 values[i]));
177 reader.alloc ()->free (string);
179 if (result == 0 || result == EOF)
180 ACE_ERROR_RETURN ((LM_ERROR,
181 "Parsing error in file %s\n",
182 file_name),
183 -1);
185 if (debug)
186 ACE_DEBUG ((LM_DEBUG,
187 "\n\n"));
189 return 0;
193 get_priority_lanes (const char *test_type,
194 const ACE_TCHAR *lanes_file,
195 RTCORBA::RTORB_ptr rt_orb,
196 CORBA::ULong stacksize,
197 CORBA::ULong static_threads,
198 CORBA::ULong dynamic_threads,
199 CORBA::Boolean allow_request_buffering,
200 CORBA::ULong max_buffered_requests,
201 CORBA::ULong max_request_buffer_size,
202 CORBA::Boolean allow_borrowing,
203 CORBA::PolicyList &policies,
204 int debug)
206 ULong_Array priorities;
207 int result =
208 get_values (test_type,
209 lanes_file,
210 "lanes",
211 priorities,
212 debug);
213 if (result != 0 ||
214 priorities.size () == 0)
215 return result;
217 RTCORBA::ThreadpoolLanes lanes;
218 lanes.length (priorities.size ());
220 for (CORBA::ULong i = 0;
221 i < priorities.size ();
222 ++i)
224 lanes[i].lane_priority = priorities[i];
225 lanes[i].static_threads = static_threads;
226 lanes[i].dynamic_threads = dynamic_threads;
229 RTCORBA::ThreadpoolId threadpool_id =
230 rt_orb->create_threadpool_with_lanes (stacksize,
231 lanes,
232 allow_borrowing,
233 allow_request_buffering,
234 max_buffered_requests,
235 max_request_buffer_size);
237 CORBA::Policy_var threadpool_policy =
238 rt_orb->create_threadpool_policy (threadpool_id);
240 policies.length (policies.length () + 1);
241 policies[policies.length () - 1] =
242 threadpool_policy;
244 return 0;
248 get_protocols (const char *test_type,
249 const ACE_TCHAR *lanes_file,
250 RTCORBA::RTORB_ptr rt_orb,
251 CORBA::PolicyList &policies,
252 int debug)
254 ULong_Array protocol_values;
255 int result =
256 get_values (test_type,
257 lanes_file,
258 "protocols",
259 protocol_values,
260 debug);
261 if (result != 0 ||
262 protocol_values.size () == 0)
263 return result;
265 RTCORBA::ProtocolList protocols;
266 protocols.length (protocol_values.size ());
268 for (CORBA::ULong i = 0;
269 i < protocol_values.size ();
270 ++i)
272 protocols[i].protocol_type =
273 protocol_values[i];
274 protocols[i].transport_protocol_properties =
275 RTCORBA::ProtocolProperties::_nil ();
276 protocols[i].orb_protocol_properties =
277 RTCORBA::ProtocolProperties::_nil ();
280 CORBA::Policy_var protocol_policy =
281 rt_orb->create_client_protocol_policy (protocols);
283 policies.length (policies.length () + 1);
284 policies[policies.length () - 1] =
285 protocol_policy;
287 return 0;
290 void
291 get_auto_priority_lanes_and_bands (CORBA::ULong number_of_lanes,
292 RTCORBA::RTORB_ptr rt_orb,
293 CORBA::ULong stacksize,
294 CORBA::ULong static_threads,
295 CORBA::ULong dynamic_threads,
296 CORBA::Boolean allow_request_buffering,
297 CORBA::ULong max_buffered_requests,
298 CORBA::ULong max_request_buffer_size,
299 CORBA::Boolean allow_borrowing,
300 CORBA::PolicyList &policies,
301 int debug)
303 RTCORBA::ThreadpoolLanes lanes;
304 lanes.length (number_of_lanes);
306 RTCORBA::PriorityBands bands;
307 bands.length (number_of_lanes);
309 CORBA::Short priority_range =
310 RTCORBA::maxPriority - RTCORBA::minPriority;
312 if (debug)
313 ACE_DEBUG ((LM_DEBUG,
314 "\nUsing %d lanes\n",
315 number_of_lanes));
317 for (CORBA::ULong i = 0;
318 i < number_of_lanes;
319 ++i)
321 CORBA::Short high_priority =
322 CORBA::Short (
323 ACE_OS::floor ((priority_range /
324 double (number_of_lanes)) *
325 (i + 1)));
327 CORBA::Short low_priority =
328 CORBA::Short (
329 ACE_OS::ceil ((priority_range /
330 double (number_of_lanes)) *
331 i));
333 lanes[i].lane_priority = high_priority;
334 lanes[i].static_threads = static_threads;
335 lanes[i].dynamic_threads = dynamic_threads;
337 bands[i].high = high_priority;
338 bands[i].low = low_priority;
340 if (debug)
341 ACE_DEBUG ((LM_DEBUG,
342 "%d: [%d %d] ",
343 i + 1,
344 low_priority,
345 high_priority));
348 if (debug)
349 ACE_DEBUG ((LM_DEBUG,
350 "\n\n"));
352 RTCORBA::ThreadpoolId threadpool_id =
353 rt_orb->create_threadpool_with_lanes (stacksize,
354 lanes,
355 allow_borrowing,
356 allow_request_buffering,
357 max_buffered_requests,
358 max_request_buffer_size);
360 policies.length (policies.length () + 1);
361 policies[policies.length () - 1] =
362 rt_orb->create_priority_banded_connection_policy (bands);
364 policies.length (policies.length () + 1);
365 policies[policies.length () - 1] =
366 rt_orb->create_threadpool_policy (threadpool_id);