Merge pull request #1551 from DOCGroup/plm_jira_333
[ACE_TAO.git] / TAO / examples / Simulator / Event_Supplier / Event_Sup.cpp
blob5afcf85c89c87e48fb76c1f16ebaa9556d077ad1
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 (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 ())
71 ACE_ERROR ((LM_ERROR,
72 "Event_Supplier::start_generating_events: "
73 "there is no scheduling data\n"));
74 return;
77 CORBA::Any any;
82 // Insert the event data
83 this->insert_event_data (any,
84 schedule_iter);
86 // deliver it over the wire
87 dOVE_Supplier_.notify (any);
89 if (total_sent < 5)
90 ACE_DEBUG ((LM_DEBUG,
91 "Pushing event data.\n"));
92 else if (total_sent == 5)
93 ACE_DEBUG ((LM_DEBUG,
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)
104 delete (*data_temp);
107 void
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
116 // a queue.
117 FILE *input_file;
119 int scan_count = 0;
120 input_file = ACE_OS::fopen(this->input_file_name_, "r");
122 if (input_file)
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))
131 ++temp;
133 // If there is anything besides whitespace in the line
134 // read, scan its fields into the scheduling data
135 // structure.
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,
141 &data->utilitzation,
142 &data->overhead,
143 &data->arrival_time,
144 &data->deadline_time,
145 &data->completion_time,
146 &data->computation_time);
147 if (scan_count != 7)
149 ACE_ERROR ((LM_ERROR,
150 "Event_Supplier::start_generating_events: "
151 "scanned incorrect number of data elements: %d\n", scan_count));
153 delete data;
154 return;
157 // Insert the data into the queue.
158 schedule_data.enqueue_tail (data);
162 else
164 ACE_ERROR ((LM_ERROR,
165 "Event_Supplier::start_generating_events: "
166 "could not open input file [%s].\n",
167 this->input_file_name_));
168 return;
171 else
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;
181 switch (i % 4)
183 case 0:
184 oper_name = "high_20";
185 break;
187 case 1:
188 oper_name = "low_20";
189 break;
191 case 2:
192 oper_name = "high_10";
193 break;
195 case 3:
196 default:
197 oper_name = "low_10";
198 break;
201 ACE_OS::strncpy (data->operation_name,
202 oper_name,
203 BUFSIZ-1);
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.
231 void
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;
253 else
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;
290 else
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);
323 data <<= weapons_;
325 else {
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)
335 last_completion = 0;
337 if ((*sched_data)->completion_time >= last_completion)
339 ACE_Time_Value pause (0,
340 (*sched_data)->completion_time -
341 last_completion);
342 ACE_OS::sleep (pause);
343 last_completion = (*sched_data)->completion_time;
346 else
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.
367 unsigned int
368 Event_Supplier::get_options (int argc, ACE_TCHAR *argv [])
370 ACE_Get_Opt get_opt (argc, argv, ACE_TEXT("f:m:"));
371 int opt;
372 int temp;
374 while ((opt = get_opt ()) != -1)
376 switch (opt)
378 case 'm':
379 temp = ACE_OS::atoi (get_opt.opt_arg ());
380 if (temp > 0)
382 this->total_messages_ = (u_int) temp;
383 ACE_DEBUG ((LM_DEBUG,
384 "Messages to send: %d\n",
385 this->total_messages_));
387 else
388 ACE_ERROR_RETURN ((LM_ERROR,
389 "%s: count must be > 0",
390 argv[0]),
392 break;
393 case 'f':
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"));
398 else
400 this->input_file_name_ = 0;
401 ACE_ERROR_RETURN ((LM_ERROR,
402 "%s: file name must be specified with -f option",
403 argv[0]),
406 break;
407 default:
408 case '?':
409 ACE_DEBUG ((LM_DEBUG,
410 "Usage: %s %s\n",
411 argv[0], usage));
412 ACE_OS::exit (0);
413 break;
417 if (argc != get_opt.opt_ind ())
418 ACE_ERROR_RETURN ((LM_ERROR,
419 "%s: too many arguments\n"
420 "Usage: %s %s\n",
421 argv[0],
422 argv[0],
423 usage),
426 return 0;
429 // function main
432 ACE_TMAIN(int argc, ACE_TCHAR *argv[])
436 // Initialize ORB.
437 TAO_ORB_Manager orb_Manager;
439 orb_Manager.init (argc,
440 argv);
443 // Create the demo supplier.
444 Event_Supplier *event_Supplier_ptr;
446 ACE_NEW_RETURN (event_Supplier_ptr,
447 Event_Supplier(argc, argv),
448 -1);
450 // Initialize everthing
451 if (event_Supplier_ptr->init () == -1)
452 ACE_OS::exit (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");
466 return 0;