ALMA Computing Group

acsexmplClientDynamicComponent.cpp

Go to the documentation of this file.
00001 /*******************************************************************************
00002 *    ALMA - Atacama Large Millimiter Array
00003 *    (c) European Southern Observatory, 2002
00004 *    Copyright by ESO (in the framework of the ALMA collaboration)
00005 *    and Cosylab 2002, All rights reserved
00006 *
00007 *    This library is free software; you can redistribute it and/or
00008 *    modify it under the terms of the GNU Lesser General Public
00009 *    License as published by the Free Software Foundation; either
00010 *    version 2.1 of the License, or (at your option) any later version.
00011 *
00012 *    This library is distributed in the hope that it will be useful,
00013 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015 *    Lesser General Public License for more details.
00016 *
00017 *    You should have received a copy of the GNU Lesser General Public
00018 *    License along with this library; if not, write to the Free Software
00019 *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
00020 *
00021 *
00022 * "@(#) $Id: acsexmplClientDynamicComponent.cpp,v 1.9 2007/02/01 05:14:26 cparedes Exp $"
00023 *
00024 * who       when      what
00025 * --------  --------  ----------------------------------------------
00026 * dfugate  2004/01/25  created 
00027 */
00028 
00052 
00057 
00088 /* @}*/
00089 /* @}*/
00090 
00091 #include <maciSimpleClient.h>
00092 #include "acsexmplHelloWorldC.h"
00093 
00094 ACE_RCSID(acsexmpl, acsexmpClient, "$Id: acsexmplClientDynamicComponent.cpp,v 1.9 2007/02/01 05:14:26 cparedes Exp $")
00095 using namespace maci;
00096        
00097 /*******************************************************************************/
00101 int main(int argc, char *argv[])
00102 {
00103     ComponentInfo_var cInfo;
00104 
00105     // Checks command-line arguments.
00106     if (argc < 2)
00107         {
00108         ACS_SHORT_LOG((LM_INFO, "Usage: %s <container name> <options>", argv[0]));
00109         return -1;
00110         }
00111     else
00112         {
00113         ACS_SHORT_LOG((LM_INFO, "Welcome to %s!", argv[0]));
00114         }
00115     
00116     //Creates and initializes the SimpleClient object
00117     SimpleClient client;
00118     if (client.init(argc,argv) == 0)
00119         {
00120         ACE_DEBUG((LM_DEBUG,"Cannot init client"));
00121         return -1;
00122         }
00123     else
00124         {
00125         //Must log into manager before we can really do anything
00126         client.login();
00127         }
00128 
00129     try
00130         {
00131         //In order for us to obtain a reference to a so-called dynamic component,
00132         //we must first create an instance of an IDL ComponentSpec structure.  This 
00133         //structure defines the various specififications for the component and is equivalent
00134         //to the entries found in $ACS_CDB/MACI/Components/Components.xml.  Specific options
00135         //for the values can be found in the MACI Specifications document (or maci.idl if 
00136         //you prefer).
00137         ComponentSpec_var cSpec = new ComponentSpec();    //use _var type for automatic memory management
00138         cSpec->component_name = CORBA::string_dup(COMPONENT_SPEC_ANY);    //name of the component
00139         cSpec->component_type = CORBA::string_dup("IDL:alma/acsexmplHelloWorld/HelloWorld:1.0");    //IDL interface implemented by the component
00140         cSpec->component_code = CORBA::string_dup("acsexmplHelloWorldImpl");     //executable code for the component (e.g. DLL)
00141         cSpec->container_name = CORBA::string_dup(argv[1]);     //container where the component is deployed
00142 
00143         //The IDL ComponentInfo structure returned by the get_dynamic_component method
00144         //contains tons of information about the newly created component and the most important
00145         //field is "reference" (i.e., the unnarrowed dynamic component).
00146         cInfo  = client.manager()->get_dynamic_component(client.handle(),    //Must pass the client's handle
00147                                                                            cSpec.in(),    //Pass the component specifications
00148                                                                            false);    //Inform manager this component is NOT the default for it's type!
00149  
00150         //As always, the reference must be CORBA casted to it's correct type.
00151         acsexmplHelloWorld::HelloWorld_var hwRef = acsexmplHelloWorld::HelloWorld::_narrow(cInfo->reference.in());
00152 
00153         //Ensure it's a valid reference
00154         if (CORBA::is_nil(hwRef.in()) == false)
00155             {
00156             ACS_SHORT_LOG((LM_INFO, "Retrieved valid reference from manager."));
00157             //Do something useful with the component...
00158             hwRef->displayMessage();
00159             ACS_SHORT_LOG((LM_INFO, "Method of dynamic component successfully invoked. Have a nice day!"));
00160             }
00161         else
00162             {
00163             ACS_SHORT_LOG((LM_ERROR, "Bad reference retrieved from manager"));
00164             }
00165         }
00166 // first we catch CORBA user exception ...
00167     catch( maciErrType::IncompleteComponentSpecEx &_ex)  // can be thrown by get_dynamic_component
00168         {
00169         // we convert CORBA exception to C++ local exception
00170         maciErrType::IncompleteComponentSpecExImpl ex(_ex);
00171         ex.log();
00172         }
00173     catch( maciErrType::InvalidComponentSpecEx &_ex) // can be thrown by get_dynamic_component
00174         {
00175         // we convert CORBA exception to C++ local exception
00176         maciErrType::InvalidComponentSpecExImpl ex(_ex);
00177         ex.log();
00178         }
00179     catch( maciErrType::ComponentSpecIncompatibleWithActiveComponentEx &_ex) // can be thrown by get_dynamic_component
00180         {
00181         // we convert CORBA exception to C++ local exception
00182         maciErrType::ComponentSpecIncompatibleWithActiveComponentExImpl ex(_ex);
00183         ex.log();
00184         }
00185     catch( maciErrType::CannotGetComponentEx &_ex) // can be thrown by get_dynamic_component
00186         {
00187         // we convert CORBA exception to C++ local exception
00188         maciErrType::CannotGetComponentExImpl ex(_ex);
00189         ex.log();
00190         }
00191     catch( CORBA::SystemException &_ex )  // ... than CORBA system exception
00192         {
00193         ACSErrTypeCommon::CORBAProblemExImpl corbaProblemEx(__FILE__, __LINE__,
00194                                                             "main");
00195         corbaProblemEx.setMinor(_ex.minor());
00196         corbaProblemEx.setCompletionStatus(_ex.completed());
00197         corbaProblemEx.setInfo(_ex._info().c_str());
00198         corbaProblemEx.log();
00199         return -1;
00200         }
00201     catch(...)
00202         {
00203         ACSErrTypeCommon::UnexpectedExceptionExImpl uex(__FILE__, __LINE__, 
00204                                                         "main");
00205         uex.log();
00206         return -1;
00207         }//try-catch
00208 
00209         try
00210             {
00211             //Must remember to release the dynamic component!
00212             client.releaseComponent( cInfo->name);      
00213         
00214             //Cleanly log out of manager.
00215             client.logout();
00216         }
00217         catch(maciErrType::CannotReleaseComponentExImpl &_ex) // this can be thrown by releaseComponent
00218             {
00219             _ex.log();
00220             return -1;
00221             }
00222         catch(...)
00223             {
00224             ACSErrTypeCommon::UnexpectedExceptionExImpl uex(__FILE__, __LINE__, 
00225                                                             "main");
00226             uex.log();
00227             return -1;
00228             }//try-catch
00229   
00230     //sleep for 3 sec to allow everytihng to cleanup and stabilize
00231     //so that the tests can be determinitstic.
00232     ACE_OS::sleep(3);   
00233     return 0;
00234 }
00238 /*___oOo___*/
00239 
00240 
00241