Salome HOME
Fix compilation pb.
[modules/kernel.git] / src / TestContainer / TestContainer.cxx
index 14de77051264d06597819352161b42ffd29c4b5b..0ef063b74f296f90c56a2119401678ab178a1e65 100644 (file)
@@ -17,7 +17,7 @@
 //  License along with this library; if not, write to the Free Software 
 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA 
 // 
-//  See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
+// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
 //
 //
 //
@@ -26,7 +26,6 @@
 //  Module : SALOME
 //  $Header$
 
-using namespace std;
 #include "utilities.h"
 #include <iostream>
 #include <unistd.h>
@@ -35,67 +34,169 @@ using namespace std;
 #include CORBA_CLIENT_HEADER(SALOME_TestComponent)
 
 #include "SALOME_NamingService.hxx"
+#include "NamingService_WaitForServerReadiness.hxx"
 #include "OpUtil.hxx"
+#include "Utils_ORB_INIT.hxx"
+#include "Utils_SINGLETON.hxx"
+#include "Utils_SALOME_Exception.hxx"
+#include "Utils_CommException.hxx"
+using namespace std;
+
+static ostream& operator<<(ostream& os, const CORBA::Exception& e)
+{
+  CORBA::Any tmp;
+  tmp<<= e;
+  CORBA::TypeCode_var tc = tmp.type();
+  const char *p = tc->name();
+  os<<"Test blocking exception was catch of the kind : ";
+  if ( *p != '\0' ) {
+    os<<p;
+  } 
+  else  { 
+    os << tc->id();
+  }
+  
+  return os;
+}
+
+Engines::TestComponent_ptr create_instance(Engines::Container_ptr iGenFact,
+                                          string componenttName)
+{
+  bool isLib =
+    iGenFact->load_component_Library(componenttName.c_str());
+  //    iGenFact->load_component_Library("SalomeTestComponent");
+  ASSERT(isLib);
+  CORBA::Object_var obj =
+    //    iGenFact->create_component_instance("SalomeTestComponent",
+    iGenFact->create_component_instance(componenttName.c_str(),
+                                       0);
+  Engines::TestComponent_var anInstance = Engines::TestComponent::_narrow(obj);
+  MESSAGE("create anInstance");
+  SCRUTE(anInstance->instanceName());
+  return anInstance._retn();
+}
 
 int main (int argc, char * argv[])
 {
+  // Initializing omniORB
+  ORB_INIT &init = *SINGLETON_<ORB_INIT>::Instance() ;
+  CORBA::ORB_ptr orb = init( argc , argv ) ;
+  //  LocalTraceCollector *myThreadTrace = SALOMETraceCollector::instance(orb);
 
   try
     {
-      // Initializing omniORB
-      CORBA::ORB_var orb = CORBA::ORB_init(argc, argv);
-    
-      // use IOR to find container
-      //if (argc != 2) { return 1; }
-      //CORBA::Object_var obj = orb->string_to_object(argv[1]);
-      //Engines::Container_var iGenFact = Engines::Container::_narrow(obj);
-
-      // Obtain a reference to the root POA
-      CORBA::Object_var obj = orb->resolve_initial_references("RootPOA") ;
-      PortableServer::POA_var poa = PortableServer::POA::_narrow(obj) ;
-    
-      // Use Name Service to find container
       SALOME_NamingService _NS(orb) ;
       string containerName = "/Containers/" ;
       string hostName = GetHostname();
       containerName += hostName + "/FactoryServer";
+      NamingService_WaitForServerReadiness(&_NS,containerName);
 
-      obj = _NS.Resolve(containerName.c_str()) ;
+      CORBA::Object_var obj = _NS.Resolve(containerName.c_str()) ;
       Engines::Container_var iGenFact = Engines::Container::_narrow(obj);
+      iGenFact->ping() ;
+
+      int nbInstances = 5;
 
-      Engines::TestComponent_var m1;
+      vector<Engines::TestComponent_var> instances(nbInstances);
     
-      for (int iter = 0; iter < 3 ; iter++)
+      MESSAGE("------------------------------- create instances ");
+      for (int iter = 0; iter < nbInstances ; iter++)
        {
-         INFOS("----------------------------------------------------" << iter);   
-          string dirn = getenv("SALOME_ROOT_DIR");
-          dirn += "/lib/salome/libSalomeTestComponentEngine.so";
-          obj = iGenFact->load_impl("SalomeTestComponent",dirn.c_str());
-         m1 = Engines::TestComponent::_narrow(obj);
-         INFOS("recup m1");
-         SCRUTE(m1->instanceName());
-         INFOS("Coucou " << m1->Coucou(1L));
-         iGenFact->remove_impl(m1) ;
+         instances[iter] = create_instance(iGenFact,"SalomeTestComponent");
+       }
+
+      MESSAGE("------------------------------ set env instances ");
+      for (int iter = 0; iter < nbInstances ; iter++)
+       {
+         Engines::TestComponent_var anInstance = instances[iter];
+         SCRUTE(anInstance->instanceName());
+         Engines::FieldsDict_var dico = new Engines::FieldsDict;
+         dico->length(3);
+         dico[0].key=CORBA::string_dup("key_0");
+         dico[0].value <<="value_0";
+         dico[1].key=CORBA::string_dup("key_1");
+         dico[1].value <<=(CORBA::UShort)72;
+         dico[2].key=CORBA::string_dup("key_2");
+         dico[2].value <<=(CORBA::ULong)iter;
+         anInstance->setProperties(dico);
+         MESSAGE("Coucou " << anInstance->Coucou(iter));
+         anInstance->Setenv();
+       }
+
+      MESSAGE("---------------------------------- get instances ");
+      for (int iter = 0; iter < nbInstances ; iter++)
+       {
+         Engines::TestComponent_var anInstance = instances[iter];
+         SCRUTE(anInstance->instanceName());
+         Engines::FieldsDict_var dico2 =  anInstance->getProperties();
+         for (CORBA::ULong i=0; i<dico2->length(); i++)
+           {
+             MESSAGE("dico2["<<i<<"].key="<<dico2[i].key);
+             MESSAGE("dico2["<<i<<"].value type ="<<dico2[i].value.type()->kind());
+             if (dico2[i].value.type()->kind() == CORBA::tk_string)
+               {
+                 const char* value;
+                 dico2[i].value >>= value;
+                 MESSAGE("dico2["<<i<<"].value="<<value);
+               }
+           }
+       }
+
+      MESSAGE("------------------------------- remove instances ");
+      for (int iter = 0; iter < nbInstances ; iter++)
+       {
+         Engines::TestComponent_var anInstance = instances[iter];
+         SCRUTE(anInstance->instanceName());
+         iGenFact->remove_impl(anInstance) ;
          //iGenFact->finalize_removal() ; // unpredictable results ...
-          sleep(5);
-       }    
+       } 
+      MESSAGE("------------------------------- PYTHON ");
+      {
+//     bool isLib =
+//       iGenFact->load_component_Library("SALOME_TestComponentPy");
+//     ASSERT(isLib);
+//     CORBA::Object_var obj =
+//       iGenFact->create_component_instance("SALOME_TestComponentPy",
+//                                           0);
+//     Engines::TestComponent_var anInstance =
+//       Engines::TestComponent::_narrow(obj);
+//     MESSAGE("create anInstance");
+//     SCRUTE(anInstance->instanceName());
+      MESSAGE("------------------------------- create instances ");
+      for (int iter = 0; iter < nbInstances ; iter++)
+       {
+         instances[iter] = create_instance(iGenFact,"SALOME_TestComponentPy");
+       }
+
+      MESSAGE("---------------------------------- get instances ");
+      for (int iter = 0; iter < nbInstances ; iter++)
+       {
+         Engines::TestComponent_var anInstance = instances[iter];
+         SCRUTE(anInstance->instanceName());
+         MESSAGE("Coucou " << anInstance->Coucou(iter));
+       }
+      }
+   
       // Clean-up.
       iGenFact->finalize_removal() ;
-      orb->destroy();
+      orb->shutdown(0);
     }
   catch(CORBA::COMM_FAILURE& ex) {
     INFOS("Caught system exception COMM_FAILURE -- unable to contact the object.")
       }
-  catch(CORBA::SystemException&) {
-    INFOS("Caught a CORBA::SystemException.")
+  catch(CORBA::SystemException& e) {
+    INFOS("Caught a CORBA::SystemException."<<e)
+      }
+  catch(CORBA::Exception& e) {
+    INFOS("Caught CORBA::Exception."<<e)
       }
-  catch(CORBA::Exception&) {
-    INFOS("Caught CORBA::Exception.")
+  catch(ServiceUnreachable& e) {
+    INFOS("Caught Exception. "<<e)
       }
   catch(...) {
     INFOS("Caught unknown exception.")
       }
 
+  //  delete myThreadTrace;
   return 0;
 }
-