00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include "config.h"
00019
00020 #include <string.h>
00021 #include <stdarg.h>
00022 #include <assert_pp.h>
00023
00024 #include <iostream>
00025
00026 #include "time_util.h"
00027 #include "HeyParser.hh"
00028 #include "factory_library.h"
00029
00030 #include "ManagerImpl.hh"
00031 #include "RealTimeTaskImpl.hh"
00032 #include "StubRealTimeTask.hh"
00033 #include "StubPolicy.hh"
00034
00035 using namespace std;
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047 static int bfHey(CORBA::ORB_ptr orb,
00048 PortableServer::POA_ptr poa,
00049 HeyParser &hp,
00050 ostream &out,
00051 ostream &err)
00052 {
00053 static HeyPropertyInfo suites[] = {
00054 HeyPropertyInfo("manager",
00055 (1L << HeyParser::CREATE_PROPERTY),
00056 "",
00057 "name:string - The name of the manager.\n"
00058 "\n"
00059 "Create a Broker::Manager object. This object is "
00060 "used to coordinate\n"
00061 "actions with the other Broker objects. You need to "
00062 "have atleast one of\n"
00063 "of these active at all times.\n"),
00064 HeyPropertyInfo("stub-real-time-task",
00065 (1L << HeyParser::CREATE_PROPERTY),
00066 "",
00067 "name:string - The name of the task.\n"
00068 "\n"
00069 "Create a StubRealTimeTask object. Mostly used for "
00070 "testing.\n"),
00071 HeyPropertyInfo("stub-policy",
00072 (1L << HeyParser::CREATE_PROPERTY),
00073 "",
00074 "name:string - The name of the policy.\n"
00075 "\n"
00076 "Create a StubPolicy object. Mostly used for "
00077 "testing.\n"),
00078 HeyPropertyInfo("real-time-task",
00079 (1L << HeyParser::CREATE_PROPERTY),
00080 "",
00081 "name:string - The name for the task object.\n"
00082 "\n"
00083 "Create a RealTimeTaskImpl object. This object is "
00084 "used to pass CPU reports\n"
00085 "on to the manager object.\n"),
00086 HeyPropertyInfo::HPI_NULL
00087 };
00088
00089 const char *prop_name, *prop_value;
00090 int retval = EXIT_FAILURE;
00091
00092 require(!CORBA::is_nil(orb));
00093 require(!CORBA::is_nil(poa));
00094
00095 switch( hp.what() )
00096 {
00097 case HeyParser::LIST_PROPERTIES:
00098 try
00099 {
00100 hp.popProperty(prop_name, prop_value);
00101 }
00102 catch(const HeyParserException &e)
00103 {
00104 out << "manager" << endl;
00105 out << "stub-real-time-task" << endl;
00106 out << "stub-policy" << endl;
00107 out << "real-time-task" << endl;
00108 }
00109 break;
00110 case HeyParser::CREATE_PROPERTY:
00111 hp.popProperty(prop_name, prop_value);
00112 if( strcasecmp(prop_name, "manager") == 0 )
00113 {
00114 ManagerImpl *mi = new ManagerImpl(hp.withValue("name"));
00115 PortableServer::ObjectId_var oid;
00116 Broker::Manager_var manager;
00117
00118 oid = poa->activate_object(mi);
00119 manager = Broker::Manager::_narrow(poa->id_to_reference(oid.in()));
00120 out << orb->object_to_string(manager._retn()) << endl;
00121 retval = EXIT_SUCCESS;
00122 }
00123 else if( strcasecmp(prop_name, "stub-real-time-task") == 0 )
00124 {
00125 PortableServer::ObjectId_var oid;
00126 Broker::RealTimeTask_var rtt;
00127 StubRealTimeTask *srtt;
00128
00129 srtt = new StubRealTimeTask(hp.withValue("name"));
00130 oid = poa->activate_object(srtt);
00131 rtt = Broker::RealTimeTask::
00132 _narrow(poa->id_to_reference(oid.in()));
00133 out << orb->object_to_string(rtt._retn()) << endl;
00134 retval = EXIT_SUCCESS;
00135 }
00136 else if( strcasecmp(prop_name, "stub-policy") == 0 )
00137 {
00138 PortableServer::ObjectId_var oid;
00139 Broker::Policy_var policy;
00140 StubPolicy *sp;
00141
00142 sp = new StubPolicy(hp.withValue("name"));
00143 oid = poa->activate_object(sp);
00144 policy = Broker::Policy::_narrow(poa->id_to_reference(oid.in()));
00145 out << orb->object_to_string(policy._retn()) << endl;
00146 retval = EXIT_SUCCESS;
00147 }
00148 else if( strcasecmp(prop_name, "real-time-task") == 0 )
00149 {
00150 PortableServer::ObjectId_var oid;
00151 Broker::TaskParameters tp(2);
00152 const char *str;
00153
00154 tp.length(1);
00155
00156 str = hp.withValue("name");
00157 tp[0].name = "name";
00158 tp[0].value <<= str;
00159
00160 RealTimeTaskImpl *rtti = new RealTimeTaskImpl(tp);
00161 Broker::RealTimeTask_var rtt;
00162
00163 oid = poa->activate_object(rtti);
00164 rtt = Broker::RealTimeTask::
00165 _narrow(poa->id_to_reference(oid.in()));
00166 out << orb->object_to_string(rtt._retn()) << endl;
00167 retval = EXIT_SUCCESS;
00168 }
00169 else
00170 {
00171 err << "Unknown property: " << prop_name << endl;
00172 }
00173 break;
00174 case HeyParser::GET_SUITES:
00175 try
00176 {
00177 hp.popProperty(prop_name, prop_value);
00178 err << "Unknown property: " << prop_name << endl;
00179 }
00180 catch(const HeyParserException &e)
00181 {
00182 out << suites;
00183 retval = EXIT_SUCCESS;
00184 }
00185 break;
00186 default:
00187 err << "Unknown verb" << endl;
00188 break;
00189 }
00190 return( retval );
00191 }
00192
00193
00194
00195
00196
00197
00198
00199
00200 static int bfManagerHey(CORBA::ORB_ptr orb, HeyParser &hp)
00201 {
00202 static HeyPropertyInfo suites[] = {
00203 HeyPropertyInfo("policy",
00204 (1L << HeyParser::SET_PROPERTY) |
00205 (1L << HeyParser::GET_PROPERTY),
00206 "",
00207 "Get or set the object that implements the contention "
00208 "policy. Policy objects\n"
00209 "can be created using the libpolicies library.\n"),
00210 HeyPropertyInfo("add-task",
00211 (1L << HeyParser::EXECUTE_PROPERTY),
00212 "",
00213 "task:ior - The task to add.\n"
00214 "pid:pid_t - The process ID to begin scheduling for.\n"
00215 "period:time - The period for the reservation.\n"
00216 "deadline:time - The deadline for the reservation.\n"
00217 "... - Any additional key/value parameters.\n"
00218 "\n"
00219 "Add a task to the manager. Tasks must be added to "
00220 "the manager before\n"
00221 "they can send a CPU usage report.\n"),
00222 HeyPropertyInfo("tasks",
00223 (1L << HeyParser::LIST_PROPERTIES),
00224 "",
00225 "List the tasks currently being managed.\n"),
00226 HeyPropertyInfo("task",
00227 (1L << HeyParser::GET_PROPERTY),
00228 "name:string - The task name.",
00229 "Get the IOR for the task matching the given name.\n"),
00230 HeyPropertyInfo::HPI_NULL
00231 };
00232
00233 static HeyPropertyInfo task_suites[] = {
00234 HeyPropertyInfo("remove",
00235 (1L << HeyParser::EXECUTE_PROPERTY),
00236 "",
00237 "Remove the task from the manager.\n"),
00238 HeyPropertyInfo::HPI_NULL
00239 };
00240
00241 const char *prop_name, *prop_value;
00242 Broker::Manager_var manager;
00243 int retval = EXIT_FAILURE;
00244 Broker::TaskList_var tl;
00245 CORBA::Object_var obj;
00246 Broker::Task_var task;
00247
00248 obj = orb->string_to_object(hp.who());
00249 manager = Broker::Manager::_narrow(obj.in());
00250 if( CORBA::is_nil(manager.in()) )
00251 {
00252 throw CORBA::BAD_PARAM();
00253 }
00254
00255 tl = manager->GetTaskList();
00256 switch( hp.what() )
00257 {
00258 case HeyParser::LIST_PROPERTIES:
00259 try
00260 {
00261 hp.popProperty(prop_name, prop_value);
00262 if( strcasecmp(prop_name, "tasks") == 0 )
00263 {
00264 unsigned int lpc;
00265
00266 for( lpc = 0; lpc < tl->length(); lpc++ )
00267 {
00268 try
00269 {
00270 cout << tl[lpc]->Name() << endl;
00271 }
00272 catch(const CORBA::SystemException &e)
00273 {
00274 cout << orb->object_to_string(tl[lpc]) << endl;
00275 cerr << e << endl;
00276 }
00277 }
00278 retval = EXIT_SUCCESS;
00279 }
00280 else
00281 {
00282 cerr << "Unknown property: " << prop_name << endl;
00283 }
00284 }
00285 catch(const HeyParserException &e)
00286 {
00287 cout << "policy" << endl;
00288 cout << "add-task" << endl;
00289 cout << "tasks" << endl;
00290 cout << "task" << endl;
00291 retval = EXIT_SUCCESS;
00292 }
00293 break;
00294 case HeyParser::EXECUTE_PROPERTY:
00295 hp.popProperty(prop_name, prop_value);
00296 if( strcasecmp(prop_name, "add-task") == 0 )
00297 {
00298 Broker::ScheduleParameters sp;
00299 Broker::Task_var task;
00300 const char **with_sp;
00301 unsigned int lpc;
00302 size_t sp_count;
00303
00304 obj = orb->string_to_object(hp.withValue("task"));
00305 task = Broker::Task::_narrow(obj.in());
00306 if( CORBA::is_nil(task.in()) )
00307 {
00308 throw CORBA::BAD_PARAM();
00309 }
00310
00311 hp.with(with_sp, sp_count);
00312 for( lpc = 0; lpc < sp_count; lpc += 2 )
00313 {
00314 if( strcasecmp(with_sp[lpc], "task") != 0 )
00315 {
00316 sp.length(sp.length() + 1);
00317 sp[sp.length() - 1].name = with_sp[lpc];
00318 sp[sp.length() - 1].value <<= with_sp[lpc + 1];
00319 }
00320 }
00321
00322 try
00323 {
00324 manager->AddTask(task.in(), sp);
00325
00326 cout << "ok" << endl;
00327 }
00328 catch(const Broker::DuplicateScheduleParameter &e)
00329 {
00330 cerr << e << ": " << e.name << endl;
00331 }
00332 catch(const Broker::InvalidScheduleParameter &e)
00333 {
00334 cerr << e << ": " << e.message << endl;
00335 }
00336 catch(const Broker::MissingScheduleParameter &e)
00337 {
00338 cerr << e << ": " << e.name << endl;
00339 }
00340 }
00341 else if( strcasecmp(prop_name, "task") == 0 )
00342 {
00343 Broker::Task_var task;
00344
00345 task = ManagerImpl::ResolveTask(orb, tl, prop_value);
00346 hp.popProperty(prop_name, prop_value);
00347 if( strcasecmp(prop_name, "remove") == 0 )
00348 {
00349 manager->RemoveTask(task.in());
00350
00351 cout << "ok" << endl;
00352 retval = EXIT_SUCCESS;
00353 }
00354 else
00355 {
00356 cerr << "Unknown property: " << prop_name << endl;
00357 }
00358 }
00359 else
00360 {
00361 cerr << "Unknown property: " << prop_name << endl;
00362 }
00363 break;
00364 case HeyParser::GET_PROPERTY:
00365 hp.popProperty(prop_name, prop_value);
00366 if( strcasecmp(prop_name, "policy") == 0 )
00367 {
00368 Broker::Policy_var policy;
00369
00370 policy = manager->CurrentPolicy();
00371 if( CORBA::is_nil(policy.in()) )
00372 {
00373 cout << "(none)" << endl;
00374 }
00375 else
00376 {
00377 cout << orb->object_to_string(manager->CurrentPolicy())
00378 << endl;
00379 }
00380 retval = EXIT_SUCCESS;
00381 }
00382 else if( strcasecmp(prop_name, "task") == 0 )
00383 {
00384 Broker::Task_var task;
00385
00386 task = ManagerImpl::ResolveTask(orb, tl, prop_value);
00387 cout << orb->object_to_string(task.in()) << endl;
00388 }
00389 else
00390 {
00391 cerr << "Unknown property: " << prop_name << endl;
00392 }
00393 break;
00394 case HeyParser::SET_PROPERTY:
00395 hp.popProperty(prop_name, prop_value);
00396 if( strcasecmp(prop_name, "policy") == 0 )
00397 {
00398 Broker::Policy_var policy;
00399 const char *policy_ior;
00400
00401 policy_ior = hp.to();
00402 obj = orb->string_to_object(policy_ior);
00403 if( !CORBA::is_nil(obj.in()) )
00404 {
00405 policy = Broker::Policy::_narrow(obj.in());
00406 if( CORBA::is_nil(policy.in()) )
00407 {
00408 throw CORBA::BAD_PARAM();
00409 }
00410 }
00411 manager->CurrentPolicy(policy.in());
00412 cout << "ok" << endl;
00413 retval = EXIT_SUCCESS;
00414 }
00415 else
00416 {
00417 cerr << "Unknown property: " << prop_name << endl;
00418 }
00419 break;
00420 case HeyParser::GET_SUITES:
00421 try
00422 {
00423 hp.popProperty(prop_name, prop_value);
00424 if( strcasecmp(prop_name, "task") == 0 )
00425 {
00426 cout << task_suites;
00427 retval = EXIT_SUCCESS;
00428 }
00429 else
00430 {
00431 cerr << "Unknown property: " << prop_name << endl;
00432 }
00433 }
00434 catch(const HeyParserException &e)
00435 {
00436 cout << suites;
00437 retval = EXIT_SUCCESS;
00438 }
00439 break;
00440 default:
00441 cerr << "Unknown verb..." << endl;
00442 break;
00443 }
00444 return( retval );
00445 }
00446
00447
00448
00449
00450
00451
00452
00453
00454 static int bfRealTimeTaskHey(CORBA::ORB_ptr orb, HeyParser &hp)
00455 {
00456 static HeyPropertyInfo suites[] = {
00457 HeyPropertyInfo("name",
00458 (1L << HeyParser::GET_PROPERTY),
00459 "",
00460 "Get the name of this task.\n"),
00461 HeyPropertyInfo("manager",
00462 (1L << HeyParser::SET_PROPERTY),
00463 "",
00464 "Set the manager for this task. This should not "
00465 "be called directly, use\n"
00466 "the manager's add-task method instead.\n"),
00467 HeyPropertyInfo("begin-cpu-scheduling",
00468 (1L << HeyParser::EXECUTE_PROPERTY),
00469 "",
00470 "pid:pid_t - The process ID to begin scheduling for.\n"
00471 "period:time - The period for the reservation.\n"
00472 "deadline:time - The deadline for the reservation.\n"
00473 "... - Any additional key/value parameters.\n"
00474 "\n"
00475 "Begin CPU scheduling for the task. This should not "
00476 "be called directly, use\n"
00477 "the manager's add-task method instead.\n"),
00478 HeyPropertyInfo("end-cpu-scheduling",
00479 (1L << HeyParser::EXECUTE_PROPERTY),
00480 "",
00481 "End CPU scheduling for this task. This should not "
00482 "be called directly, use\n"
00483 "the manager to remove the task instead.\n"),
00484 HeyPropertyInfo("report-cpu",
00485 (1L << HeyParser::EXECUTE_PROPERTY),
00486 "",
00487 "status.period:int - The application's requested "
00488 "length of the next period.\n"
00489 "status.compute:int - The application's requested CPU "
00490 "time for the next period.\n"
00491 "advice.period:int - The requested length of the next "
00492 "period.\n"
00493 "advice.compute:int - The requested CPU time for the "
00494 "next period.\n"
00495 "\n"
00496 "Make a CPU usage report.\n"),
00497 HeyPropertyInfo::HPI_NULL
00498 };
00499
00500 const char *prop_name, *prop_value;
00501 Broker::RealTimeTask_var rtt;
00502 int retval = EXIT_FAILURE;
00503 CORBA::Object_var obj;
00504
00505 obj = orb->string_to_object(hp.who());
00506 rtt = Broker::RealTimeTask::_narrow(obj.in());
00507 if( CORBA::is_nil(rtt.in()) )
00508 {
00509 throw CORBA::BAD_PARAM();
00510 }
00511
00512 switch( hp.what() )
00513 {
00514 case HeyParser::LIST_PROPERTIES:
00515 try
00516 {
00517 hp.popProperty(prop_name, prop_value);
00518 cerr << "Unknown property: " << prop_name << endl;
00519 }
00520 catch(const HeyParserException &e)
00521 {
00522 cout << "name" << endl;
00523 cout << "report-cpu" << endl;
00524 retval = EXIT_SUCCESS;
00525 }
00526 break;
00527 case HeyParser::GET_PROPERTY:
00528 hp.popProperty(prop_name, prop_value);
00529 if( strcasecmp(prop_name, "name") == 0 )
00530 {
00531 cout << rtt->Name() << endl;
00532 }
00533 else
00534 {
00535 cerr << "Unknown property: " << prop_name << endl;
00536 }
00537 break;
00538 case HeyParser::SET_PROPERTY:
00539 hp.popProperty(prop_name, prop_value);
00540 if( strcasecmp(prop_name, "manager") == 0 )
00541 {
00542 Broker::Manager_var manager;
00543
00544 obj = orb->string_to_object(hp.to());
00545 if( !CORBA::is_nil(obj.in()) )
00546 {
00547 manager = Broker::Manager::_narrow(obj.in());
00548 if( CORBA::is_nil(manager.in()) )
00549 {
00550 cerr << "Invalid manager IOR." << endl;
00551 throw CORBA::BAD_PARAM();
00552 }
00553 }
00554 rtt->SetManager(manager.in());
00555
00556 cout << "ok\n";
00557 }
00558 else
00559 {
00560 cerr << "Unknown property: " << prop_name << endl;
00561 }
00562 break;
00563 case HeyParser::EXECUTE_PROPERTY:
00564 hp.popProperty(prop_name, prop_value);
00565 if( strcasecmp(prop_name, "report-cpu") == 0 )
00566 {
00567 unsigned long long status_period_ull, status_compute_ull;
00568 unsigned long long advice_period_ull, advice_compute_ull;
00569 Broker::KeyedReportParameters krp;
00570
00571 if( string_to_microsec(&status_period_ull,
00572 hp.withValue("status.period")) == 0 )
00573 {
00574 cerr << "Invalid status.period value: "
00575 << status_period_ull
00576 << endl;
00577 }
00578 else if( string_to_microsec(&status_compute_ull,
00579 hp.withValue("status.compute")) == 0 )
00580 {
00581 cerr << "Invalid status.compute value: "
00582 << status_compute_ull
00583 << endl;
00584 }
00585 else if( string_to_microsec(&advice_period_ull,
00586 hp.withValue("advice.period")) == 0 )
00587 {
00588 cerr << "Invalid advice.period value: "
00589 << advice_period_ull << endl;
00590 }
00591 else if( string_to_microsec(&advice_compute_ull,
00592 hp.withValue("advice.compute")) == 0 )
00593 {
00594 cerr << "Invalid advice.compute value: "
00595 << advice_compute_ull << endl;
00596 }
00597 else
00598 {
00599 Broker::CPUReserve status, advice;
00600
00601 status.Period = status_period_ull;
00602 status.Compute = status_compute_ull;
00603 advice.Period = advice_period_ull;
00604 advice.Compute = advice_compute_ull;
00605 rtt->ReportCPU(status, advice, krp);
00606 cout << "ok" << endl;
00607 retval = EXIT_SUCCESS;
00608 }
00609 }
00610 else
00611 {
00612 cerr << "Unknown property: " << prop_name << endl;
00613 }
00614 break;
00615 case HeyParser::GET_SUITES:
00616 try
00617 {
00618 hp.popProperty(prop_name, prop_value);
00619 cerr << "Unknown property: " << prop_name << endl;
00620 }
00621 catch(const HeyParserException &e)
00622 {
00623 cout << suites;
00624 retval = EXIT_SUCCESS;
00625 }
00626 break;
00627 default:
00628 cerr << "Unknown verb..." << endl;
00629 break;
00630 }
00631 return( retval );
00632 }
00633
00634 int FACTORY_METHOD_SYMBOL(factory_library_op_t op, int tag, va_list args)
00635 {
00636 int retval = ENOSYS;
00637
00638 struct {
00639 const char *hey_server_hint;
00640 HeyParser *hey_parser;
00641 CORBA::ORB_ptr orb;
00642 PortableServer::POA_ptr poa;
00643 ostream *out;
00644 ostream *err;
00645 } attr = {
00646 NULL,
00647 NULL,
00648 NULL,
00649 NULL,
00650 NULL,
00651 NULL,
00652 };
00653
00654 while( tag != FMA_TAG_DONE )
00655 {
00656 switch( tag )
00657 {
00658 case FMA_ORB:
00659 attr.orb = va_arg(args, CORBA::ORB_ptr);
00660 break;
00661 case FMA_POA:
00662 attr.poa = va_arg(args, PortableServer::POA_ptr);
00663 break;
00664 case FMA_HeyParser:
00665 attr.hey_parser = va_arg(args, HeyParser *);
00666 break;
00667 case FMA_HeyServerHint:
00668 attr.hey_server_hint = va_arg(args, const char *);
00669 break;
00670 case FMA_StandardOut:
00671 attr.out = va_arg(args, ostream *);
00672 break;
00673 case FMA_StandardError:
00674 attr.err = va_arg(args, ostream *);
00675 break;
00676 default:
00677 break;
00678 }
00679 tag = va_arg(args, int);
00680 }
00681
00682 switch( op )
00683 {
00684 case FLO_QUERY:
00685 if( attr.hey_server_hint != NULL )
00686 {
00687 if( strcmp(attr.hey_server_hint, "IDL:Broker/Manager:1.0") == 0 )
00688 {
00689 retval = EXIT_SUCCESS;
00690 }
00691 else if( strcmp(attr.hey_server_hint,
00692 "IDL:Broker/RealTimeTask:1.0") == 0 )
00693 {
00694 retval = EXIT_SUCCESS;
00695 }
00696 else
00697 {
00698 }
00699 }
00700 else
00701 {
00702 cout << "\tIDL:Broker/Manager:1.0" << endl
00703 << "\tIDL:Broker/RealTimeTask:1.0" << endl;
00704 }
00705 break;
00706 case FLO_HEY:
00707 if( attr.hey_server_hint != NULL )
00708 {
00709 try
00710 {
00711 if( strcmp(attr.hey_server_hint,
00712 "IDL:Broker/Manager:1.0") == 0 )
00713 {
00714 retval = bfManagerHey(attr.orb, *attr.hey_parser);
00715 }
00716 else if( strcmp(attr.hey_server_hint,
00717 "IDL:Broker/RealTimeTask:1.0") == 0 )
00718 {
00719 retval = bfRealTimeTaskHey(attr.orb, *attr.hey_parser);
00720 }
00721 else
00722 {
00723 }
00724 }
00725 catch(const HeyParserException &he)
00726 {
00727 cerr << "Parse error: " << he << endl;
00728 }
00729 catch(const CORBA::SystemException &e)
00730 {
00731 cerr << "Caught Exception: " << e << endl;
00732 }
00733 catch(...)
00734 {
00735 cerr << "Caught an unhandled exception" << endl;
00736 }
00737 }
00738 else
00739 {
00740 retval = bfHey(attr.orb,
00741 attr.poa,
00742 *attr.hey_parser,
00743 *attr.out,
00744 *attr.err);
00745 }
00746 break;
00747 default:
00748 break;
00749 }
00750
00751 return( retval );
00752 }