Main Page | Namespace List | Class Hierarchy | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

DelegateFactory.cc

Go to the documentation of this file.
00001 /*
00002  * DelegateFactory.cc
00003  *
00004  * Copyright (c) 2003, 2004 The University of Utah and the Flux Group.
00005  * All rights reserved.
00006  *
00007  * This file is licensed under the terms of the GNU Public License.  
00008  * See the file "license.terms" for restrictions on redistribution 
00009  * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
00010  */
00011 
00012 /**
00013  * @file DelegateFactory.cc
00014  *
00015  * The shared library interface for libdelegates.
00016  */
00017 
00018 #include "config.h"
00019 
00020 #include <errno.h>
00021 #include <string.h>
00022 #include <stdarg.h>
00023 #include <stdlib.h>
00024 
00025 #include <iostream>
00026 
00027 #include <assert_pp.h>
00028 #include <HeyParser.hh>
00029 #include <factory_library.h>
00030 
00031 #include "RealTimeTaskDelegateImpl.hh"
00032 #include "StrictPolicyDelegateImpl.hh"
00033 
00034 #include <AnyPrinter.hh>
00035 
00036 using namespace std;
00037 
00038 /**
00039  * Handle 'hey's directed at the library.
00040  *
00041  * @param orb Pointer to the ORB.
00042  * @param poa The POA to use when activating objects.
00043  * @param hp The HeyParser that describes the action to take.
00044  * @param out Standard out.
00045  * @param err Standard error.
00046  * @return Zero on success, an errno value otherwise.
00047  */
00048 static int dfHey(CORBA::ORB_ptr orb,
00049                  PortableServer::POA_ptr poa,
00050                  HeyParser &hp,
00051                  ostream &out,
00052                  ostream &err)
00053 {
00054     static HeyPropertyInfo suites[] = {
00055         HeyPropertyInfo("real-time-task-delegate",
00056                         (1L << HeyParser::CREATE_PROPERTY),
00057                         "",
00058                         "Create a RealTimeTaskDelegate object.  This is a "
00059                         "blank delegate that does\n"
00060                         "no adaptation.\n"),
00061         HeyPropertyInfo("strict-policy-delegate",
00062                         (1L << HeyParser::CREATE_PROPERTY),
00063                         "",
00064                         "Create a StrictPolicyDelegate object.  This is a "
00065                         "blank delegate that does\n"
00066                         "no adaptation.\n"),
00067         HeyPropertyInfo::HPI_NULL
00068     };
00069 
00070     const char *prop_name, *prop_value;
00071     int retval = EXIT_FAILURE;
00072 
00073     require(!CORBA::is_nil(orb));
00074 
00075     switch( hp.what() )
00076     {
00077     case HeyParser::LIST_PROPERTIES:
00078         try
00079         {
00080             hp.popProperty(prop_name, prop_value);
00081         }
00082         catch(const HeyParserException &e)
00083         {
00084             out << "real-time-task-delegate" << endl;
00085         }
00086         break;
00087     case HeyParser::CREATE_PROPERTY:
00088         hp.popProperty(prop_name, prop_value);
00089         if( strcasecmp(prop_name, "real-time-task-delegate") == 0 )
00090         {
00091             BrokerDelegates::RealTimeTaskDelegate_var rttd;
00092             PortableServer::ObjectId_var oid;
00093             RealTimeTaskDelegateImpl *rttdi;
00094 
00095             rttdi = new RealTimeTaskDelegateImpl();
00096             oid = poa->activate_object(rttdi);
00097             rttd = BrokerDelegates::RealTimeTaskDelegate::
00098                 _narrow(poa->id_to_reference(oid.in()));
00099             out << orb->object_to_string(rttd._retn()) << endl;
00100             retval = EXIT_SUCCESS;
00101         }
00102         else if( strcasecmp(prop_name, "strict-policy-delegate") == 0 )
00103         {
00104             BrokerDelegates::StrictPolicyDelegate_var spd;
00105             PortableServer::ObjectId_var oid;
00106             StrictPolicyDelegateImpl *spdi;
00107 
00108             spdi = new StrictPolicyDelegateImpl();
00109             oid = poa->activate_object(spdi);
00110             spd = BrokerDelegates::StrictPolicyDelegate::
00111                 _narrow(poa->id_to_reference(oid.in()));
00112             out << orb->object_to_string(spd._retn()) << endl;
00113             retval = EXIT_SUCCESS;
00114         }
00115         else
00116         {
00117             err << "Unknown property: " << prop_name << endl;
00118         }
00119         break;
00120     case HeyParser::GET_SUITES:
00121         try
00122         {
00123             hp.popProperty(prop_name, prop_value);
00124             err << "Unknown property: " << prop_name << endl;
00125         }
00126         catch(const HeyParserException &e)
00127         {
00128             out << suites;
00129             retval = EXIT_SUCCESS;
00130         }
00131         break;
00132     default:
00133         err << "Unknown verb" << endl;
00134         break;
00135     }
00136     return( retval );
00137 }
00138 
00139 /**
00140  * Handle 'hey's directed at a Delegate object.
00141  *
00142  * @param orb Pointer to the ORB.
00143  * @param hp The HeyParser that describes the action to take.
00144  * @return Zero on success, an errno value otherwise.
00145  */
00146 static int dfDelegateHey(CORBA::ORB_ptr orb, HeyParser &hp)
00147 {
00148     static HeyPropertyInfo suites[] = {
00149         HeyPropertyInfo("remote-object",
00150                         (1L << HeyParser::SET_PROPERTY) |
00151                         (1L << HeyParser::GET_PROPERTY),
00152                         "",
00153                         "Get or set the object the delegate proxies.\n"),
00154         HeyPropertyInfo("insert",
00155                         (1L << HeyParser::EXECUTE_PROPERTY),
00156                         "",
00157                         "delegate:object - The delegate to insert.\n"
00158                         "\n"
00159                         "Insert another delegate between this object and the\n"
00160                         "remote object.\n"),
00161         HeyPropertyInfo("<delegate-attribute>",
00162                         (1L << HeyParser::SET_PROPERTY) |
00163                         (1L << HeyParser::GET_PROPERTY),
00164                         "",
00165                         "Get or set a delegate attribute.\n"),
00166         HeyPropertyInfo::HPI_NULL
00167     };
00168     
00169     const char *prop_name, *prop_value, *to_value;
00170     BrokerDelegates::Delegate_var delegate;
00171     int retval = EXIT_FAILURE;
00172     CORBA::Object_var obj;
00173     
00174     obj = orb->string_to_object(hp.who());
00175     delegate = BrokerDelegates::Delegate::_narrow(obj.in());
00176     if( CORBA::is_nil(delegate.in()) )
00177     {
00178         throw CORBA::BAD_PARAM();
00179     }
00180     
00181     switch( hp.what() )
00182     {
00183     case HeyParser::LIST_PROPERTIES:
00184         try
00185         {
00186             hp.popProperty(prop_name, prop_value);
00187         }
00188         catch(const HeyParserException &e)
00189         {
00190             cout << "remote-object" << endl;
00191             retval = EXIT_SUCCESS;
00192         }
00193         break;
00194     case HeyParser::GET_PROPERTY:
00195         hp.popProperty(prop_name, prop_value);
00196         if( strcasecmp(prop_name, "remote-object") == 0 )
00197         {
00198             obj = delegate->GetDelegateAttributeObject(prop_name);
00199             cout << orb->object_to_string(obj.in()) << endl;
00200             retval = EXIT_SUCCESS;
00201         }
00202         else
00203         {
00204             try
00205             {
00206                 CORBA::Any *any;
00207 
00208                 any = delegate->GetDelegateAttribute(prop_name);
00209                 AnyPrinter::print_to_stream(cout, *any);
00210             }
00211             catch(const CORBA::BAD_PARAM &e)
00212             {
00213                 cerr << "Unknown property: " << prop_name << endl;
00214             }
00215         }
00216         break;
00217     case HeyParser::SET_PROPERTY:
00218         hp.popProperty(prop_name, prop_value);
00219         if( strcasecmp(prop_name, "remote-object") == 0 )
00220         {
00221             to_value = hp.to();
00222             obj = orb->string_to_object(to_value);
00223             delegate->SetDelegateAttributeObject(prop_name, obj.in());
00224             cout << "ok" << endl;
00225             retval = EXIT_SUCCESS;
00226         }
00227         else
00228         {
00229             try
00230             {
00231                 CORBA::Any any;
00232 
00233                 any <<= hp.to();
00234                 delegate->SetDelegateAttribute(prop_name, any);
00235                 cout << "ok" << endl;
00236                 retval = EXIT_SUCCESS;
00237             }
00238             catch(const CORBA::BAD_PARAM &e)
00239             {
00240                 cerr << "Unknown property: " << prop_name << endl;
00241             }
00242         }
00243         break;
00244     case HeyParser::EXECUTE_PROPERTY:
00245         hp.popProperty(prop_name, prop_value);
00246         if( strcasecmp(prop_name, "insert") == 0 )
00247         {
00248             BrokerDelegates::Delegate_var interposer;
00249             CORBA::Object_var remote_object;
00250             
00251             obj = orb->string_to_object(hp.withValue("delegate"));
00252             interposer = BrokerDelegates::Delegate::_narrow(obj.in());
00253             if( CORBA::is_nil(interposer.in()) )
00254             {
00255                 cerr << "Error: delegate value not a Delegate\n" << endl;
00256                 throw CORBA::BAD_PARAM();
00257             }
00258             
00259             remote_object = delegate->
00260                 GetDelegateAttributeObject("remote-object");
00261             if( CORBA::is_nil(remote_object.in()) )
00262             {
00263                 cerr << "Error: no remote object to interpose on.\n" << endl;
00264                 throw CORBA::BAD_PARAM();
00265             }
00266             
00267             interposer->SetDelegateAttributeObject("remote-object",
00268                                                    remote_object.in());
00269             delegate->SetDelegateAttributeObject("remote-object",
00270                                                  interposer.in());
00271             cout << "ok" << endl;
00272             retval = EXIT_SUCCESS;
00273         }
00274         else
00275         {
00276             cerr << "Unknown property: " << prop_name << endl;
00277         }
00278         break;
00279     case HeyParser::GET_SUITES:
00280         try
00281         {
00282             hp.popProperty(prop_name, prop_value);
00283             cerr << "Unknown property: " << prop_name << endl;
00284         }
00285         catch(const HeyParserException &e)
00286         {
00287             cout << suites;
00288             retval = EXIT_SUCCESS;
00289         }
00290         break;
00291     default:
00292         cerr << "Unknown verb..." << endl;
00293         break;
00294     }
00295     return( retval );
00296 }
00297 
00298 int FACTORY_METHOD_SYMBOL(factory_library_op_t op, int tag, va_list args)
00299 {
00300     int retval = EXIT_FAILURE;
00301 
00302     struct {
00303         const char *hey_server_hint;
00304         HeyParser *hey_parser;
00305         CORBA::ORB_ptr orb;
00306         PortableServer::POA_ptr poa;
00307         ostream *out;
00308         ostream *err;
00309     } attr = {
00310         NULL,
00311         NULL,
00312         NULL,
00313         NULL,
00314         NULL,
00315         NULL,
00316     };
00317     
00318     while( tag != FMA_TAG_DONE )
00319     {
00320         switch( tag )
00321         {
00322         case FMA_ORB:
00323             attr.orb = va_arg(args, CORBA::ORB_ptr);
00324             break;
00325         case FMA_POA:
00326             attr.poa = va_arg(args, PortableServer::POA_ptr);
00327             break;
00328         case FMA_HeyParser:
00329             attr.hey_parser = va_arg(args, HeyParser *);
00330             break;
00331         case FMA_HeyServerHint:
00332             attr.hey_server_hint = va_arg(args, const char *);
00333             break;
00334         case FMA_StandardOut:
00335             attr.out = va_arg(args, ostream *);
00336             break;
00337         case FMA_StandardError:
00338             attr.err = va_arg(args, ostream *);
00339             break;
00340         default:
00341             break;
00342         }
00343         tag = va_arg(args, int);
00344     }
00345 
00346     switch( op )
00347     {
00348     case FLO_QUERY:
00349         if( attr.hey_server_hint != NULL )
00350         {
00351             if( strcmp(attr.hey_server_hint,
00352                        "IDL:BrokerDelegates/Delegate:1.0") == 0 )
00353             {
00354                 retval = EXIT_SUCCESS;
00355             }
00356             else
00357             {
00358             }
00359         }
00360         else
00361         {
00362             cout << "\tIDL:BrokerDelegates/Delegate:1.0" << endl;
00363         }
00364         break;
00365     case FLO_HEY:
00366         if( attr.hey_server_hint != NULL )
00367         {
00368             try
00369             {
00370                 if( strcmp(attr.hey_server_hint,
00371                            "IDL:BrokerDelegates/Delegate:1.0") == 0 )
00372                 {
00373                     retval = dfDelegateHey(attr.orb, *attr.hey_parser);
00374                 }
00375                 else
00376                 {
00377                 }
00378             }
00379             catch(const HeyParserException &he)
00380             {
00381                 cerr << "Parse error: " << he << endl;
00382             }
00383             catch(const CORBA::SystemException &e)
00384             {
00385                 cerr << "Caught Exception: " << e << endl;
00386             }
00387             catch(...)
00388             {
00389                 cerr << "Caught an unhandled exception" << endl;
00390             }
00391         }
00392         else
00393         {
00394             retval = dfHey(attr.orb,
00395                            attr.poa,
00396                            *attr.hey_parser,
00397                            *attr.out,
00398                            *attr.err);
00399         }
00400         break;
00401     default:
00402         break;
00403     }
00404     
00405     return( retval );
00406 }

Generated on Fri Oct 22 07:50:24 2004 for CPU Broker by  doxygen 1.3.9.1