Revert "Use a variable on the stack to not have a temporary in the call"
[ACE_TAO.git] / TAO / examples / Simulator / Event_Supplier / Event_Sup.cpp
blob984e5904e7b1ceeaa7abcc01f3483b7a17340be4
2 //=============================================================================
3 /**
4 * @file Event_Sup.cpp
6 * Event Supplier for the flight simulator
8 * @author originally David Levine (levine@cs.wustl.edu) and Tim Harrison (harrison@cs.wustl.edu) modified Michael Kircher (mk1@cs.wustl.edu)
9 */
10 //=============================================================================
13 #include "Event_Sup.h"
14 #include "NavWeapC.h"
16 #include "orbsvcs/Event_Utilities.h"
17 #include "orbsvcs/Event_Service_Constants.h"
18 #include "orbsvcs/Scheduler_Factory.h"
19 #include "orbsvcs/RtecEventChannelAdminC.h"
21 #include "tao/Utils/ORB_Manager.h"
23 #include "ace/Get_Opt.h"
24 #include "ace/Sched_Params.h"
25 #include "ace/OS_NS_unistd.h"
26 #include "ace/OS_NS_stdio.h"
27 #include "ace/OS_NS_string.h"
28 #include "ace/OS_NS_ctype.h"
30 static const char usage [] =
31 "[[-?]\n"
32 " [-O[RBport] ORB port number]\n"
33 " [-m <count> of messages to send [100]\n"
34 " [-f name of schedler input data file]]\n";
37 Event_Supplier::Event_Supplier (int argc, ACE_TCHAR** argv)
38 : argc_(argc),
39 argv_(argv),
40 total_messages_(10),
41 input_file_name_(0)
43 navigation_.roll = navigation_.pitch = 0;
46 Event_Supplier::~Event_Supplier ()
48 this->dOVE_Supplier_.disconnect ();
51 int
52 Event_Supplier::init ()
54 this->get_options (argc_, argv_);
55 return this->dOVE_Supplier_.connect ();
58 void
59 Event_Supplier::start_generating_events ()
61 unsigned long total_sent = 0;
63 // Load the scheduling data for the simulation.
64 ACE_Unbounded_Queue<Schedule_Viewer_Data *> schedule_data;
65 this->load_schedule_data (schedule_data);
66 ACE_Unbounded_Queue_Iterator<Schedule_Viewer_Data *>
67 schedule_iter (schedule_data);
69 if (schedule_iter.done ())
71 ACE_ERROR ((LM_ERROR,
72 "Event_Supplier::start_generating_events: "
73 "there is no scheduling data\n"));
74 return;
77 CORBA::Any any;
81 // Insert the event data
82 this->insert_event_data (any,
83 schedule_iter);
85 // deliver it over the wire
86 dOVE_Supplier_.notify (any);
88 if (total_sent < 5)
89 ACE_DEBUG ((LM_DEBUG,
90 "Pushing event data.\n"));
91 else if (total_sent == 5)
92 ACE_DEBUG ((LM_DEBUG,
93 "Everything is running. Going to be mute.\n"));
95 while (++total_sent < this->total_messages_);
97 // clean up the scheduling data
98 Schedule_Viewer_Data **data_temp;
99 for (schedule_iter.first ();
100 schedule_iter.done () == 0;
101 schedule_iter.advance ())
102 if (schedule_iter.next (data_temp) && data_temp)
103 delete (*data_temp);
106 void
107 Event_Supplier::load_schedule_data
108 (ACE_Unbounded_Queue<Schedule_Viewer_Data *> &schedule_data)
110 Schedule_Viewer_Data *data = 0;
112 if (this->input_file_name_)
114 // Open the scheduler data input file and read its contents into
115 // a queue.
116 FILE *input_file;
118 int scan_count = 0;
119 input_file = ACE_OS::fopen(this->input_file_name_, "r");
121 if (input_file)
123 // Get a line at a time from the data file and parse it.
124 char input_buf[BUFSIZ];
125 while (ACE_OS::fgets (input_buf, BUFSIZ, input_file))
127 // Run through leading whitespace.
128 char *temp = input_buf;
129 while (*temp && ACE_OS::ace_isspace (*temp))
130 ++temp;
132 // If there is anything besides whitespace in the line
133 // read, scan its fields into the scheduling data
134 // structure.
135 if (ACE_OS::strlen (temp) > 0)
137 ACE_NEW (data, Schedule_Viewer_Data);
138 scan_count = sscanf (temp, "%s %lf %lf %lu %lu %lu %lu",
139 data->operation_name,
140 &data->utilitzation,
141 &data->overhead,
142 &data->arrival_time,
143 &data->deadline_time,
144 &data->completion_time,
145 &data->computation_time);
146 if (scan_count != 7)
148 ACE_ERROR ((LM_ERROR,
149 "Event_Supplier::start_generating_events: "
150 "scanned incorrect number of data elements: %d\n", scan_count));
152 delete data;
153 return;
156 // Insert the data into the queue.
157 schedule_data.enqueue_tail (data);
161 else
163 ACE_ERROR ((LM_ERROR,
164 "Event_Supplier::start_generating_events: "
165 "could not open input file [%s].\n",
166 this->input_file_name_));
167 return;
170 else
172 u_long last_completion = 0;
174 // Just create 10 dummy scheduling records and use them.
175 for (int i = 0; i < 10; ++i)
177 ACE_NEW (data, Schedule_Viewer_Data);
179 const char *oper_name = 0;
180 switch (i % 4)
182 case 0:
183 oper_name = "high_20";
184 break;
186 case 1:
187 oper_name = "low_20";
188 break;
190 case 2:
191 oper_name = "high_10";
192 break;
194 case 3:
195 default:
196 oper_name = "low_10";
197 break;
200 ACE_OS::strncpy (data->operation_name,
201 oper_name,
202 BUFSIZ-1);
205 data->utilitzation = (double)(20.0+ACE_OS::rand() %10);
206 data->overhead = (double)(ACE_OS::rand() %20);
208 data->arrival_time = ACE_OS::rand() % 200;
209 data->computation_time = (ACE_OS::rand() % 100) + 10;
211 data->completion_time = last_completion + (ACE_OS::rand() % 100) + 100;
212 data->completion_time =
213 data->completion_time < data->arrival_time + data->computation_time
214 ? data->arrival_time + data->computation_time
215 : data->completion_time;
217 last_completion = data->completion_time;
219 data->deadline_time = data->completion_time + (ACE_OS::rand() % 200) - 50;
221 // insert the data into the queue.
222 schedule_data.enqueue_tail (data);
227 // This function fills in the random data into the anys transported by
228 // the event channel.
230 void
231 Event_Supplier::insert_event_data (CORBA::Any &data,
232 ACE_Unbounded_Queue_Iterator<Schedule_Viewer_Data *> &schedule_iter)
234 static u_long last_completion = 0;
238 Schedule_Viewer_Data **sched_data;
240 if ((schedule_iter.next (sched_data)) && (sched_data) && (*sched_data))
242 if ((ACE_OS::strcmp((*sched_data)->operation_name, "high_20") == 0) ||
243 (ACE_OS::strcmp((*sched_data)->operation_name, "low_20") == 0) ||
244 (ACE_OS::strcmp((*sched_data)->operation_name, "high_1") == 0) ||
245 (ACE_OS::strcmp((*sched_data)->operation_name, "low_1") == 0))
247 if ((ACE_OS::strcmp((*sched_data)->operation_name, "high_20") == 0) ||
248 (ACE_OS::strcmp((*sched_data)->operation_name, "high_1") == 0))
250 navigation_.criticality = 1;
252 else
254 navigation_.criticality = 0;
257 navigation_.position_latitude = ACE_OS::rand() % 90;
258 navigation_.position_longitude = ACE_OS::rand() % 180;
259 navigation_.altitude = ACE_OS::rand() % 100;
260 navigation_.heading = ACE_OS::rand() % 180;
261 navigation_.roll = (navigation_.roll >= 180) ? -180 : navigation_.roll + 1;
262 navigation_.pitch = (navigation_.pitch >= 90) ? -90 : navigation_.pitch + 1;
264 navigation_.utilization = (*sched_data)->utilitzation;
265 navigation_.overhead = (*sched_data)->overhead;
266 navigation_.arrival_time = (*sched_data)->arrival_time;
267 navigation_.deadline_time = (*sched_data)->deadline_time;
268 navigation_.completion_time = (*sched_data)->completion_time;
269 navigation_.computation_time = (*sched_data)->computation_time;
270 navigation_.update_data = 0;
273 // because the scheduler data does not supply these values
274 navigation_.utilization = (double) (20.0 + ACE_OS::rand() % 10);
275 navigation_.overhead = (double) (ACE_OS::rand() % 10);
277 data <<= navigation_;
279 else if ((ACE_OS::strcmp((*sched_data)->operation_name, "high_10") == 0) ||
280 (ACE_OS::strcmp((*sched_data)->operation_name, "low_10") == 0) ||
281 (ACE_OS::strcmp((*sched_data)->operation_name, "high_5") == 0) ||
282 (ACE_OS::strcmp((*sched_data)->operation_name, "low_5") == 0))
284 if ((ACE_OS::strcmp((*sched_data)->operation_name, "high_10") == 0) ||
285 (ACE_OS::strcmp((*sched_data)->operation_name, "high_5") == 0))
287 weapons_.criticality = 1;
289 else
291 weapons_.criticality = 0;
294 weapons_.number_of_weapons = 2;
295 weapons_.weapon1_identifier = CORBA::string_alloc (30);
296 ACE_OS::strcpy (weapons_.weapon1_identifier.inout (),"Photon Torpedoes");
297 weapons_.weapon1_status =(ACE_OS::rand() % 4) == 0 ? 0 : 1 ;
298 weapons_.weapon2_identifier = CORBA::string_alloc (30);
299 ACE_OS::strcpy (weapons_.weapon2_identifier.inout (),"Quantum Torpedoes");
300 weapons_.weapon2_status = (ACE_OS::rand() % 4) == 0 ? 0 : 1;
301 weapons_.weapon3_identifier = CORBA::string_alloc (1);
302 ACE_OS::strcpy (weapons_.weapon3_identifier.inout (), "");
303 weapons_.weapon3_status = 0;
304 weapons_.weapon4_identifier = CORBA::string_alloc (1);
305 ACE_OS::strcpy (weapons_.weapon4_identifier.inout (), "");
306 weapons_.weapon4_status = 0;
307 weapons_.weapon5_identifier = CORBA::string_alloc (1);
308 ACE_OS::strcpy (weapons_.weapon5_identifier.inout (), "");
309 weapons_.weapon5_status = 0;
310 weapons_.utilization = (*sched_data)->utilitzation;
311 weapons_.overhead = (*sched_data)->overhead;
312 weapons_.arrival_time = (*sched_data)->arrival_time;
313 weapons_.deadline_time = (*sched_data)->deadline_time;
314 weapons_.completion_time = (*sched_data)->completion_time;
315 weapons_.computation_time = (*sched_data)->computation_time;
316 weapons_.update_data = 0;
318 // because the scheduler data does not supply these values
319 weapons_.utilization = (double) (20.0 + ACE_OS::rand() % 10);
320 weapons_.overhead = (double) (ACE_OS::rand() % 10);
322 data <<= weapons_;
324 else {
325 ACE_ERROR ((LM_ERROR,
326 "Event_Supplier::insert_event_data:"
327 "unrecognized operation name [%s]",
328 (*sched_data)->operation_name));
332 if (last_completion > (*sched_data)->completion_time)
333 last_completion = 0;
335 if ((*sched_data)->completion_time >= last_completion)
337 ACE_Time_Value pause (0,
338 (*sched_data)->completion_time -
339 last_completion);
340 ACE_OS::sleep (pause);
341 last_completion = (*sched_data)->completion_time;
344 else
345 ACE_ERROR ((LM_ERROR,
346 "Event_Supplier::insert_event_data:"
347 "Could Not access scheduling data"));
349 schedule_iter.advance ();
351 if (schedule_iter.done ())
352 schedule_iter.first ();
354 catch (const CORBA::Exception&)
356 ACE_ERROR ((LM_ERROR,
357 "(%t)Error in Event_Supplier::insert_event_data.\n"));
362 // Function get_options.
364 unsigned int
365 Event_Supplier::get_options (int argc, ACE_TCHAR *argv [])
367 ACE_Get_Opt get_opt (argc, argv, ACE_TEXT("f:m:"));
368 int opt;
369 int temp;
371 while ((opt = get_opt ()) != -1)
373 switch (opt)
375 case 'm':
376 temp = ACE_OS::atoi (get_opt.opt_arg ());
377 if (temp > 0)
379 this->total_messages_ = (u_int) temp;
380 ACE_DEBUG ((LM_DEBUG,
381 "Messages to send: %d\n",
382 this->total_messages_));
384 else
385 ACE_ERROR_RETURN ((LM_ERROR,
386 "%s: count must be > 0",
387 argv[0]),
389 break;
390 case 'f':
391 this->input_file_name_ = get_opt.opt_arg ();
393 if (!this->input_file_name_ || ACE_OS::strlen (this->input_file_name_) > 0)
394 ACE_DEBUG ((LM_DEBUG,"Reading file!\n"));
395 else
397 this->input_file_name_ = 0;
398 ACE_ERROR_RETURN ((LM_ERROR,
399 "%s: file name must be specified with -f option",
400 argv[0]),
403 break;
404 default:
405 case '?':
406 ACE_DEBUG ((LM_DEBUG,
407 "Usage: %s %s\n",
408 argv[0], usage));
409 ACE_OS::exit (0);
410 break;
414 if (argc != get_opt.opt_ind ())
415 ACE_ERROR_RETURN ((LM_ERROR,
416 "%s: too many arguments\n"
417 "Usage: %s %s\n",
418 argv[0],
419 argv[0],
420 usage),
423 return 0;
426 // function main
429 ACE_TMAIN(int argc, ACE_TCHAR *argv[])
433 // Initialize ORB.
434 TAO_ORB_Manager orb_Manager;
436 orb_Manager.init (argc,
437 argv);
440 // Create the demo supplier.
441 Event_Supplier *event_Supplier_ptr;
443 ACE_NEW_RETURN (event_Supplier_ptr,
444 Event_Supplier(argc, argv),
445 -1);
447 // Initialize everthing
448 if (event_Supplier_ptr->init () == -1)
449 ACE_OS::exit (1);
451 // now we can go ahead
452 event_Supplier_ptr->start_generating_events ();
454 // when done, we clean up
455 delete event_Supplier_ptr;
457 catch (const CORBA::Exception& ex)
459 ex._tao_print_exception ("SYS_EX");
462 return 0;