Back to index

salome-kernel  6.5.0
TestContainer.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00004 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00005 //
00006 // This library is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 2.1 of the License.
00010 //
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 // Lesser General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU Lesser General Public
00017 // License along with this library; if not, write to the Free Software
00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00019 //
00020 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00021 //
00022 
00023 //  SALOME TestContainer : test of container creation and its life cycle
00024 //  File   : TestContainer.cxx
00025 //  Author : Paul RASCLE, EDF - MARC TAJCHMAN, CEA
00026 //  Module : SALOME
00027 //  $Header: /home/server/cvs/KERNEL/KERNEL_SRC/src/TestContainer/TestContainer.cxx,v 1.15.2.1.10.2.12.1 2012-04-12 14:05:31 vsr Exp $
00028 //
00029 #include "utilities.h"
00030 #include <iostream>
00031 #ifndef WIN32
00032 #include <unistd.h>
00033 #endif
00034 #include <SALOMEconfig.h>
00035 #include CORBA_CLIENT_HEADER(SALOME_Component)
00036 #include CORBA_CLIENT_HEADER(SALOME_TestComponent)
00037 
00038 #include "SALOME_NamingService.hxx"
00039 #include "NamingService_WaitForServerReadiness.hxx"
00040 #include "Basics_Utils.hxx"
00041 #include "Utils_ORB_INIT.hxx"
00042 #include "Utils_SINGLETON.hxx"
00043 #include "Utils_SALOME_Exception.hxx"
00044 #include "Utils_CommException.hxx"
00045 
00046 static std::ostream& operator<<(std::ostream& os, const CORBA::Exception& e)
00047 {
00048   CORBA::Any tmp;
00049   tmp<<= e;
00050   CORBA::TypeCode_var tc = tmp.type();
00051   const char *p = tc->name();
00052   os<<"Test blocking exception was catch of the kind : ";
00053   if ( *p != '\0' ) {
00054     os<<p;
00055   } 
00056   else  { 
00057     os << tc->id();
00058   }
00059   
00060   return os;
00061 }
00062 
00063 Engines::TestComponent_ptr create_instance(Engines::Container_ptr iGenFact,
00064                                            std::string componenttName)
00065 {
00066   char* reason;
00067 #if defined(_DEBUG_) || defined(_DEBUG)
00068   bool isLib =
00069     iGenFact->load_component_Library(componenttName.c_str(),reason);
00070   ASSERT(isLib);
00071 #else
00072   iGenFact->load_component_Library(componenttName.c_str(),reason);
00073 #endif
00074   CORBA::string_free(reason);
00075   CORBA::Object_var obj = iGenFact->create_component_instance(componenttName.c_str(), 0);
00076   Engines::TestComponent_var anInstance = Engines::TestComponent::_narrow(obj);
00077   MESSAGE("create anInstance");
00078   SCRUTE(anInstance->instanceName());
00079   return anInstance._retn();
00080 }
00081 
00082 int main (int argc, char * argv[])
00083 {
00084   // Initializing omniORB
00085   ORB_INIT &init = *SINGLETON_<ORB_INIT>::Instance() ;
00086   CORBA::ORB_ptr orb = init( argc , argv ) ;
00087   //  LocalTraceCollector *myThreadTrace = SALOMETraceCollector::instance(orb);
00088 
00089   try
00090     {
00091       SALOME_NamingService _NS(orb) ;
00092       std::string containerName = "/Containers/" ;
00093       std::string hostName = Kernel_Utils::GetHostname();
00094       containerName += hostName + "/FactoryServer";
00095       NamingService_WaitForServerReadiness(&_NS,containerName);
00096 
00097       CORBA::Object_var obj = _NS.Resolve(containerName.c_str()) ;
00098       Engines::Container_var iGenFact = Engines::Container::_narrow(obj);
00099       iGenFact->ping() ;
00100 
00101       int nbInstances = 5;
00102 
00103       std::vector<Engines::TestComponent_var> instances(nbInstances);
00104     
00105       MESSAGE("------------------------------- create instances ");
00106       for (int iter = 0; iter < nbInstances ; iter++)
00107         {
00108           instances[iter] = create_instance(iGenFact,"SalomeTestComponent");
00109         }
00110 
00111       MESSAGE("------------------------------ set env instances ");
00112       for (int iter = 0; iter < nbInstances ; iter++)
00113         {
00114           Engines::TestComponent_var anInstance = instances[iter];
00115           SCRUTE(anInstance->instanceName());
00116           Engines::FieldsDict_var dico = new Engines::FieldsDict;
00117           dico->length(3);
00118           dico[0].key=CORBA::string_dup("key_0");
00119           dico[0].value <<="value_0";
00120           dico[1].key=CORBA::string_dup("key_1");
00121           dico[1].value <<=(CORBA::UShort)72;
00122           dico[2].key=CORBA::string_dup("key_2");
00123           dico[2].value <<=(CORBA::ULong)iter;
00124           anInstance->setProperties(dico);
00125           MESSAGE("Coucou " << anInstance->Coucou(iter));
00126           anInstance->Setenv();
00127         }
00128 
00129       MESSAGE("---------------------------------- get instances ");
00130       for (int iter = 0; iter < nbInstances ; iter++)
00131         {
00132           Engines::TestComponent_var anInstance = instances[iter];
00133           SCRUTE(anInstance->instanceName());
00134           Engines::FieldsDict_var dico2 =  anInstance->getProperties();
00135           for (CORBA::ULong i=0; i<dico2->length(); i++)
00136             {
00137               MESSAGE("dico2["<<i<<"].key="<<dico2[i].key);
00138               MESSAGE("dico2["<<i<<"].value type ="<<dico2[i].value.type()->kind());
00139               if (dico2[i].value.type()->kind() == CORBA::tk_string)
00140                 {
00141                   const char* value;
00142                   dico2[i].value >>= value;
00143                   MESSAGE("dico2["<<i<<"].value="<<value);
00144                 }
00145             }
00146         }
00147 
00148       MESSAGE("------------------------------- remove instances ");
00149       for (int iter = 0; iter < nbInstances ; iter++)
00150         {
00151           Engines::TestComponent_var anInstance = instances[iter];
00152           SCRUTE(anInstance->instanceName());
00153           iGenFact->remove_impl(anInstance) ;
00154           //iGenFact->finalize_removal() ; // unpredictable results ...
00155         } 
00156       MESSAGE("------------------------------- PYTHON ");
00157       {
00158 //      bool isLib =
00159 //        iGenFact->load_component_Library("SALOME_TestComponentPy");
00160 //      ASSERT(isLib);
00161 //      CORBA::Object_var obj =
00162 //        iGenFact->create_component_instance("SALOME_TestComponentPy",
00163 //                                            0);
00164 //      Engines::TestComponent_var anInstance =
00165 //        Engines::TestComponent::_narrow(obj);
00166 //      MESSAGE("create anInstance");
00167 //      SCRUTE(anInstance->instanceName());
00168       MESSAGE("------------------------------- create instances ");
00169       for (int iter = 0; iter < nbInstances ; iter++)
00170         {
00171           instances[iter] = create_instance(iGenFact,"SALOME_TestComponentPy");
00172         }
00173 
00174       MESSAGE("---------------------------------- get instances ");
00175       for (int iter = 0; iter < nbInstances ; iter++)
00176         {
00177           Engines::TestComponent_var anInstance = instances[iter];
00178           SCRUTE(anInstance->instanceName());
00179           MESSAGE("Coucou " << anInstance->Coucou(iter));
00180         }
00181       }
00182    
00183       // Clean-up.
00184       iGenFact->finalize_removal() ;
00185       orb->shutdown(0);
00186     }
00187   catch(CORBA::COMM_FAILURE& ex) {
00188     INFOS("Caught system exception COMM_FAILURE -- unable to contact the object.")
00189       }
00190   catch(CORBA::SystemException& e) {
00191     INFOS("Caught a CORBA::SystemException."<<e)
00192       }
00193   catch(CORBA::Exception& e) {
00194     INFOS("Caught CORBA::Exception."<<e)
00195       }
00196   catch(ServiceUnreachable& e) {
00197     INFOS("Caught Exception. "<<e)
00198       }
00199   catch(...) {
00200     INFOS("Caught unknown exception.")
00201       }
00202 
00203   //  delete myThreadTrace;
00204   return 0;
00205 }