2 //=============================================================================
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)
10 //=============================================================================
13 #include "Event_Sup.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
[] =
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
)
43 navigation_
.roll
= navigation_
.pitch
= 0;
46 Event_Supplier::~Event_Supplier ()
48 this->dOVE_Supplier_
.disconnect ();
52 Event_Supplier::init ()
54 this->get_options (argc_
, argv_
);
55 return this->dOVE_Supplier_
.connect ();
59 Event_Supplier::start_generating_events (void)
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 ())
72 "Event_Supplier::start_generating_events: "
73 "there is no scheduling data\n"));
82 // Insert the event data
83 this->insert_event_data (any
,
86 // deliver it over the wire
87 dOVE_Supplier_
.notify (any
);
91 "Pushing event data.\n"));
92 else if (total_sent
== 5)
94 "Everything is running. Going to be mute.\n"));
96 while (++total_sent
< this->total_messages_
);
98 // clean up the scheduling data
99 Schedule_Viewer_Data
**data_temp
;
100 for (schedule_iter
.first ();
101 schedule_iter
.done () == 0;
102 schedule_iter
.advance ())
103 if (schedule_iter
.next (data_temp
) && data_temp
)
108 Event_Supplier::load_schedule_data
109 (ACE_Unbounded_Queue
<Schedule_Viewer_Data
*> &schedule_data
)
111 Schedule_Viewer_Data
*data
= 0;
113 if (this->input_file_name_
)
115 // Open the scheduler data input file and read its contents into
120 input_file
= ACE_OS::fopen(this->input_file_name_
, "r");
124 // Get a line at a time from the data file and parse it.
125 char input_buf
[BUFSIZ
];
126 while (ACE_OS::fgets (input_buf
, BUFSIZ
, input_file
))
128 // Run through leading whitespace.
129 char *temp
= input_buf
;
130 while (*temp
&& ACE_OS::ace_isspace (*temp
))
133 // If there is anything besides whitespace in the line
134 // read, scan its fields into the scheduling data
136 if (ACE_OS::strlen (temp
) > 0)
138 ACE_NEW (data
, Schedule_Viewer_Data
);
139 scan_count
= sscanf (temp
, "%s %lf %lf %lu %lu %lu %lu",
140 data
->operation_name
,
144 &data
->deadline_time
,
145 &data
->completion_time
,
146 &data
->computation_time
);
149 ACE_ERROR ((LM_ERROR
,
150 "Event_Supplier::start_generating_events: "
151 "scanned incorrect number of data elements: %d\n", scan_count
));
157 // Insert the data into the queue.
158 schedule_data
.enqueue_tail (data
);
164 ACE_ERROR ((LM_ERROR
,
165 "Event_Supplier::start_generating_events: "
166 "could not open input file [%s].\n",
167 this->input_file_name_
));
173 u_long last_completion
= 0;
175 // Just create 10 dummy scheduling records and use them.
176 for (int i
= 0; i
< 10; ++i
)
178 ACE_NEW (data
, Schedule_Viewer_Data
);
180 const char *oper_name
= 0;
184 oper_name
= "high_20";
188 oper_name
= "low_20";
192 oper_name
= "high_10";
197 oper_name
= "low_10";
201 ACE_OS::strncpy (data
->operation_name
,
206 data
->utilitzation
= (double)(20.0+ACE_OS::rand() %10);
207 data
->overhead
= (double)(ACE_OS::rand() %20);
209 data
->arrival_time
= ACE_OS::rand() % 200;
210 data
->computation_time
= (ACE_OS::rand() % 100) + 10;
212 data
->completion_time
= last_completion
+ (ACE_OS::rand() % 100) + 100;
213 data
->completion_time
=
214 data
->completion_time
< data
->arrival_time
+ data
->computation_time
215 ? data
->arrival_time
+ data
->computation_time
216 : data
->completion_time
;
218 last_completion
= data
->completion_time
;
220 data
->deadline_time
= data
->completion_time
+ (ACE_OS::rand() % 200) - 50;
222 // insert the data into the queue.
223 schedule_data
.enqueue_tail (data
);
228 // This function fills in the random data into the anys transported by
229 // the event channel.
232 Event_Supplier::insert_event_data (CORBA::Any
&data
,
233 ACE_Unbounded_Queue_Iterator
<Schedule_Viewer_Data
*> &schedule_iter
)
235 static u_long last_completion
= 0;
239 Schedule_Viewer_Data
**sched_data
;
241 if ((schedule_iter
.next (sched_data
)) && (sched_data
) && (*sched_data
))
243 if ((ACE_OS::strcmp((*sched_data
)->operation_name
, "high_20") == 0) ||
244 (ACE_OS::strcmp((*sched_data
)->operation_name
, "low_20") == 0) ||
245 (ACE_OS::strcmp((*sched_data
)->operation_name
, "high_1") == 0) ||
246 (ACE_OS::strcmp((*sched_data
)->operation_name
, "low_1") == 0))
248 if ((ACE_OS::strcmp((*sched_data
)->operation_name
, "high_20") == 0) ||
249 (ACE_OS::strcmp((*sched_data
)->operation_name
, "high_1") == 0))
251 navigation_
.criticality
= 1;
255 navigation_
.criticality
= 0;
258 navigation_
.position_latitude
= ACE_OS::rand() % 90;
259 navigation_
.position_longitude
= ACE_OS::rand() % 180;
260 navigation_
.altitude
= ACE_OS::rand() % 100;
261 navigation_
.heading
= ACE_OS::rand() % 180;
262 navigation_
.roll
= (navigation_
.roll
>= 180) ? -180 : navigation_
.roll
+ 1;
263 navigation_
.pitch
= (navigation_
.pitch
>= 90) ? -90 : navigation_
.pitch
+ 1;
265 navigation_
.utilization
= (*sched_data
)->utilitzation
;
266 navigation_
.overhead
= (*sched_data
)->overhead
;
267 navigation_
.arrival_time
= (*sched_data
)->arrival_time
;
268 navigation_
.deadline_time
= (*sched_data
)->deadline_time
;
269 navigation_
.completion_time
= (*sched_data
)->completion_time
;
270 navigation_
.computation_time
= (*sched_data
)->computation_time
;
271 navigation_
.update_data
= 0;
274 // because the scheduler data does not supply these values
275 navigation_
.utilization
= (double) (20.0 + ACE_OS::rand() % 10);
276 navigation_
.overhead
= (double) (ACE_OS::rand() % 10);
278 data
<<= navigation_
;
280 else if ((ACE_OS::strcmp((*sched_data
)->operation_name
, "high_10") == 0) ||
281 (ACE_OS::strcmp((*sched_data
)->operation_name
, "low_10") == 0) ||
282 (ACE_OS::strcmp((*sched_data
)->operation_name
, "high_5") == 0) ||
283 (ACE_OS::strcmp((*sched_data
)->operation_name
, "low_5") == 0))
285 if ((ACE_OS::strcmp((*sched_data
)->operation_name
, "high_10") == 0) ||
286 (ACE_OS::strcmp((*sched_data
)->operation_name
, "high_5") == 0))
288 weapons_
.criticality
= 1;
292 weapons_
.criticality
= 0;
295 weapons_
.number_of_weapons
= 2;
296 weapons_
.weapon1_identifier
= CORBA::string_alloc (30);
297 ACE_OS::strcpy (weapons_
.weapon1_identifier
.inout (),"Photon Torpedoes");
298 weapons_
.weapon1_status
=(ACE_OS::rand() % 4) == 0 ? 0 : 1 ;
299 weapons_
.weapon2_identifier
= CORBA::string_alloc (30);
300 ACE_OS::strcpy (weapons_
.weapon2_identifier
.inout (),"Quantum Torpedoes");
301 weapons_
.weapon2_status
= (ACE_OS::rand() % 4) == 0 ? 0 : 1;
302 weapons_
.weapon3_identifier
= CORBA::string_alloc (1);
303 ACE_OS::strcpy (weapons_
.weapon3_identifier
.inout (), "");
304 weapons_
.weapon3_status
= 0;
305 weapons_
.weapon4_identifier
= CORBA::string_alloc (1);
306 ACE_OS::strcpy (weapons_
.weapon4_identifier
.inout (), "");
307 weapons_
.weapon4_status
= 0;
308 weapons_
.weapon5_identifier
= CORBA::string_alloc (1);
309 ACE_OS::strcpy (weapons_
.weapon5_identifier
.inout (), "");
310 weapons_
.weapon5_status
= 0;
311 weapons_
.utilization
= (*sched_data
)->utilitzation
;
312 weapons_
.overhead
= (*sched_data
)->overhead
;
313 weapons_
.arrival_time
= (*sched_data
)->arrival_time
;
314 weapons_
.deadline_time
= (*sched_data
)->deadline_time
;
315 weapons_
.completion_time
= (*sched_data
)->completion_time
;
316 weapons_
.computation_time
= (*sched_data
)->computation_time
;
317 weapons_
.update_data
= 0;
319 // because the scheduler data does not supply these values
320 weapons_
.utilization
= (double) (20.0 + ACE_OS::rand() % 10);
321 weapons_
.overhead
= (double) (ACE_OS::rand() % 10);
326 ACE_ERROR ((LM_ERROR
,
327 "Event_Supplier::insert_event_data:"
328 "unrecognized operation name [%s]",
329 (*sched_data
)->operation_name
));
334 if (last_completion
> (*sched_data
)->completion_time
)
337 if ((*sched_data
)->completion_time
>= last_completion
)
339 ACE_Time_Value
pause (0,
340 (*sched_data
)->completion_time
-
342 ACE_OS::sleep (pause
);
343 last_completion
= (*sched_data
)->completion_time
;
347 ACE_ERROR ((LM_ERROR
,
348 "Event_Supplier::insert_event_data:"
349 "Could Not access scheduling data"));
351 schedule_iter
.advance ();
353 if (schedule_iter
.done ())
354 schedule_iter
.first ();
356 catch (const CORBA::Exception
&)
358 ACE_ERROR ((LM_ERROR
,
359 "(%t)Error in Event_Supplier::insert_event_data.\n"));
365 // Function get_options.
368 Event_Supplier::get_options (int argc
, ACE_TCHAR
*argv
[])
370 ACE_Get_Opt
get_opt (argc
, argv
, ACE_TEXT("f:m:"));
374 while ((opt
= get_opt ()) != -1)
379 temp
= ACE_OS::atoi (get_opt
.opt_arg ());
382 this->total_messages_
= (u_int
) temp
;
383 ACE_DEBUG ((LM_DEBUG
,
384 "Messages to send: %d\n",
385 this->total_messages_
));
388 ACE_ERROR_RETURN ((LM_ERROR
,
389 "%s: count must be > 0",
394 this->input_file_name_
= get_opt
.opt_arg ();
396 if (!this->input_file_name_
|| ACE_OS::strlen (this->input_file_name_
) > 0)
397 ACE_DEBUG ((LM_DEBUG
,"Reading file!\n"));
400 this->input_file_name_
= 0;
401 ACE_ERROR_RETURN ((LM_ERROR
,
402 "%s: file name must be specified with -f option",
409 ACE_DEBUG ((LM_DEBUG
,
417 if (argc
!= get_opt
.opt_ind ())
418 ACE_ERROR_RETURN ((LM_ERROR
,
419 "%s: too many arguments\n"
432 ACE_TMAIN(int argc
, ACE_TCHAR
*argv
[])
437 TAO_ORB_Manager orb_Manager
;
439 orb_Manager
.init (argc
,
443 // Create the demo supplier.
444 Event_Supplier
*event_Supplier_ptr
;
446 ACE_NEW_RETURN (event_Supplier_ptr
,
447 Event_Supplier(argc
, argv
),
450 // Initialize everthing
451 if (event_Supplier_ptr
->init () == -1)
454 // now we can go ahead
455 event_Supplier_ptr
->start_generating_events ();
457 // when done, we clean up
458 delete event_Supplier_ptr
;
461 catch (const CORBA::Exception
& ex
)
463 ex
._tao_print_exception ("SYS_EX");