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