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 <stdlib.h>
00023
00024 #include <iostream>
00025
00026 #include <assert_pp.h>
00027 #include <HeyParser.hh>
00028 #include <factory_library.h>
00029
00030 #include "ManagerImpl.hh"
00031 #include "StrictPolicyImpl.hh"
00032
00033 using namespace std;
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044 static int pfHey(CORBA::ORB_ptr orb, HeyParser &hp, ostream &out, ostream &err)
00045 {
00046 static HeyPropertyInfo suites[] = {
00047 HeyPropertyInfo("strict-policy",
00048 (1L << HeyParser::CREATE_PROPERTY),
00049 "",
00050 "name:string - The name of the StrictPolicy.\n"
00051 "\n"
00052 "Create a StrictPolicy object.\n"),
00053 HeyPropertyInfo::HPI_NULL
00054 };
00055
00056 const char *prop_name, *prop_value;
00057 int retval = EXIT_FAILURE;
00058
00059 require(!CORBA::is_nil(orb));
00060
00061 switch( hp.what() )
00062 {
00063 case HeyParser::LIST_PROPERTIES:
00064 try
00065 {
00066 hp.popProperty(prop_name, prop_value);
00067 }
00068 catch(const HeyParserException &e)
00069 {
00070 out << "strict-policy" << endl;
00071 }
00072 break;
00073 case HeyParser::CREATE_PROPERTY:
00074 hp.popProperty(prop_name, prop_value);
00075 if( strcasecmp(prop_name, "strict-policy") == 0 )
00076 {
00077 StrictPolicyImpl *spi = new StrictPolicyImpl(hp.withValue("name"));
00078 BrokerPolicies::StrictPolicy_var sp;
00079
00080 sp = spi->_this();
00081 out << orb->object_to_string(sp._retn()) << endl;
00082 retval = EXIT_SUCCESS;
00083 }
00084 else
00085 {
00086 err << "Unknown property: " << prop_name << endl;
00087 }
00088 break;
00089 case HeyParser::GET_SUITES:
00090 out << suites;
00091 retval = EXIT_SUCCESS;
00092 break;
00093 }
00094 return( retval );
00095 }
00096
00097
00098
00099
00100
00101
00102
00103
00104 static int pfStrictPolicyHey(CORBA::ORB_ptr orb, HeyParser &hp)
00105 {
00106 static HeyPropertyInfo suites[] = {
00107 HeyPropertyInfo("tasks",
00108 (1L << HeyParser::LIST_PROPERTIES),
00109 "",
00110 "List tasks currently being managed.\n"),
00111 HeyPropertyInfo("tasks",
00112 (1L << HeyParser::GET_SUITES),
00113 "",
00114 "Get a description of the sub-properties of task.\n"),
00115 HeyPropertyInfo("task",
00116 (1L << HeyParser::GET_PROPERTY),
00117 "name:string - The task name.",
00118 "Get the IOR for the task matching the given name.\n"),
00119 HeyPropertyInfo("max-cpu",
00120 (1L << HeyParser::SET_PROPERTY) |
00121 (1L << HeyParser::GET_PROPERTY),
00122 "",
00123 "Get or set the maximum CPU allocation. The value is "
00124 "a floating point number between 0.0 and 1.0 that "
00125 "represents the percentage of CPU that the policy "
00126 "will allocate to the tasks it manages.\n"),
00127 HeyPropertyInfo::HPI_NULL
00128 };
00129
00130 static HeyPropertyInfo task_suites[] = {
00131 HeyPropertyInfo("priority",
00132 (1L << HeyParser::SET_PROPERTY) |
00133 (1L << HeyParser::GET_PROPERTY),
00134 "",
00135 "Set or get the priority of the given task in this "
00136 "policy.\n"),
00137 HeyPropertyInfo::HPI_NULL
00138 };
00139
00140 const char *prop_name, *prop_value, *to_value;
00141 BrokerPolicies::StrictPolicy_var sp;
00142 int retval = EXIT_FAILURE;
00143 Broker::TaskList_var tl;
00144 CORBA::Object_var obj;
00145 Broker::Task_var task;
00146
00147 obj = orb->string_to_object(hp.who());
00148 sp = BrokerPolicies::StrictPolicy::_narrow(obj.in());
00149 if( CORBA::is_nil(sp.in()) )
00150 {
00151 throw CORBA::BAD_PARAM();
00152 }
00153
00154 tl = sp->GetTaskList();
00155 switch( hp.what() )
00156 {
00157 case HeyParser::LIST_PROPERTIES:
00158 try
00159 {
00160 hp.popProperty(prop_name, prop_value);
00161 if( strcasecmp(prop_name, "tasks") == 0 )
00162 {
00163 unsigned int lpc;
00164
00165 for( lpc = 0; lpc < tl->length(); lpc++ )
00166 {
00167 cout << tl[lpc]->Name() << endl;
00168 }
00169 retval = EXIT_SUCCESS;
00170 }
00171 else if( strcasecmp(prop_name, "task") == 0 )
00172 {
00173 cout << "priority" << endl;
00174 }
00175 else
00176 {
00177 cerr << "Unknown property: " << prop_name << endl;
00178 }
00179 }
00180 catch(const HeyParserException &e)
00181 {
00182 cout << "max-cpu" << endl;
00183 cout << "tasks" << endl;
00184 cout << "task" << endl;
00185 retval = EXIT_SUCCESS;
00186 }
00187 break;
00188 case HeyParser::GET_PROPERTY:
00189 hp.popProperty(prop_name, prop_value);
00190 if( strcasecmp(prop_name, "task") == 0 )
00191 {
00192 task = ManagerImpl::ResolveTask(orb, tl, prop_value);
00193 hp.popProperty(prop_name, prop_value);
00194 if( strcasecmp(prop_name, "priority") == 0 )
00195 {
00196 cout << sp->GetTaskPriority(task.in()) << endl;
00197 retval = EXIT_SUCCESS;
00198 }
00199 else
00200 {
00201 cout << orb->object_to_string(task.in()) << endl;
00202 retval = EXIT_SUCCESS;
00203 }
00204 }
00205 else if( strcasecmp(prop_name, "max-cpu") == 0 )
00206 {
00207 cout << sp->GetMaxCPUAllocation() << endl;
00208 retval = EXIT_SUCCESS;
00209 }
00210 else
00211 {
00212 cerr << "Unknown property: " << prop_name << endl;
00213 }
00214 break;
00215 case HeyParser::SET_PROPERTY:
00216 hp.popProperty(prop_name, prop_value);
00217 if( strcasecmp(prop_name, "task") == 0 )
00218 {
00219 task = ManagerImpl::ResolveTask(orb, tl, prop_value);
00220 hp.popProperty(prop_name, prop_value);
00221 if( strcasecmp(prop_name, "priority") == 0 )
00222 {
00223 CORBA::Short pri;
00224
00225 to_value = hp.to();
00226 if( sscanf(to_value, "%hd", &pri) == 1 )
00227 {
00228 sp->SetTaskPriority(task.in(), pri);
00229 cout << "ok" << endl;
00230 retval = EXIT_SUCCESS;
00231 }
00232 else
00233 {
00234 cerr << "Priority not a number: "
00235 << to_value
00236 << endl;
00237 }
00238 }
00239 else
00240 {
00241 cerr << "Unknown property: " << prop_name << endl;
00242 }
00243 }
00244 else if( strcasecmp(prop_name, "max-cpu") == 0 )
00245 {
00246 CORBA::Float pct;
00247
00248 to_value = hp.to();
00249 if( sscanf(to_value, "%f", &pct) == 1 )
00250 {
00251 if( (pct < BrokerPolicies::StrictPolicy::CPU_ALLOCATION_MIN) ||
00252 (pct > BrokerPolicies::StrictPolicy::CPU_ALLOCATION_MAX) )
00253 {
00254 cerr << "Invalid percentage, expecting a "
00255 << "value greater than "
00256 << BrokerPolicies::StrictPolicy::CPU_ALLOCATION_MIN
00257 << " and less than "
00258 << BrokerPolicies::StrictPolicy::CPU_ALLOCATION_MAX
00259 << endl;
00260 }
00261 else
00262 {
00263 sp->SetMaxCPUAllocation(pct);
00264 cout << "ok" << endl;
00265 retval = EXIT_SUCCESS;
00266 }
00267 }
00268 else
00269 {
00270 cerr << "max-cpu value not a percentage: "
00271 << to_value
00272 << endl;
00273 }
00274 }
00275 else
00276 {
00277 cerr << "Unknown property: " << prop_name << endl;
00278 }
00279 break;
00280 case HeyParser::GET_SUITES:
00281 try
00282 {
00283 hp.popProperty(prop_name, prop_value);
00284 if( strcasecmp(prop_name, "task") == 0 )
00285 {
00286 cout << task_suites;
00287 retval = EXIT_SUCCESS;
00288 }
00289 else
00290 {
00291 cerr << "Unknown property: " << prop_name << endl;
00292 }
00293 }
00294 catch(const HeyParserException &e)
00295 {
00296 cout << suites;
00297 retval = EXIT_SUCCESS;
00298 }
00299 break;
00300 default:
00301 cerr << "Unknown verb..." << endl;
00302 break;
00303 }
00304 return( retval );
00305 }
00306
00307 int FACTORY_METHOD_SYMBOL(factory_library_op_t op, int tag, va_list args)
00308 {
00309 int retval = EXIT_FAILURE;
00310
00311 struct {
00312 const char *hey_server_hint;
00313 HeyParser *hey_parser;
00314 CORBA::ORB_ptr orb;
00315 ostream *out;
00316 ostream *err;
00317 } attr = {
00318 NULL,
00319 NULL,
00320 NULL,
00321 NULL,
00322 NULL,
00323 };
00324
00325 while( tag != FMA_TAG_DONE )
00326 {
00327 switch( tag )
00328 {
00329 case FMA_ORB:
00330 attr.orb = va_arg(args, CORBA::ORB_ptr);
00331 break;
00332 case FMA_HeyParser:
00333 attr.hey_parser = va_arg(args, HeyParser *);
00334 break;
00335 case FMA_HeyServerHint:
00336 attr.hey_server_hint = va_arg(args, const char *);
00337 break;
00338 case FMA_StandardOut:
00339 attr.out = va_arg(args, ostream *);
00340 break;
00341 case FMA_StandardError:
00342 attr.err = va_arg(args, ostream *);
00343 break;
00344 default:
00345 break;
00346 }
00347 tag = va_arg(args, int);
00348 }
00349
00350 switch( op )
00351 {
00352 case FLO_QUERY:
00353 if( attr.hey_server_hint != NULL )
00354 {
00355 if( strcmp(attr.hey_server_hint,
00356 "IDL:BrokerPolicies/StrictPolicy:1.0") == 0 )
00357 {
00358 retval = EXIT_SUCCESS;
00359 }
00360 else
00361 {
00362 }
00363 }
00364 else
00365 {
00366 cout << "\tIDL:BrokerPolicies/StrictPolicy:1.0" << endl;
00367 }
00368 break;
00369 case FLO_HEY:
00370 if( attr.hey_server_hint != NULL )
00371 {
00372 try
00373 {
00374 if( strcmp(attr.hey_server_hint,
00375 "IDL:BrokerPolicies/StrictPolicy:1.0") == 0 )
00376 {
00377 retval = pfStrictPolicyHey(attr.orb, *attr.hey_parser);
00378 }
00379 else
00380 {
00381 }
00382 }
00383 catch(const HeyParserException &he)
00384 {
00385 cerr << "Parse error: " << he << endl;
00386 }
00387 catch(const CORBA::SystemException &e)
00388 {
00389 cerr << "Caught Exception: " << e << endl;
00390 }
00391 catch(...)
00392 {
00393 cerr << "Caught an unhandled exception" << endl;
00394 }
00395 }
00396 else
00397 {
00398 retval = pfHey(attr.orb, *attr.hey_parser, *attr.out, *attr.err);
00399 }
00400 break;
00401 default:
00402 break;
00403 }
00404
00405 return( retval );
00406 }