Salome HOME
PR: merge some files (MPI) from debug_V2_1_0a2
[modules/kernel.git] / src / TestMPIContainer / TestMPIContainer.cxx
1 // using namespace std;
2 //=============================================================================
3 // File      : TestMPIContainer.cxx
4 // Created   : mer jui 4 13:11:27 CEST 2003
5 // Author    : Bernard SECHER, 2003
6 // Project   : SALOME
7 // Copyright : CEA 2003
8 // $Header$
9 //=============================================================================
10
11 #include "utilities.h"
12 #include <iostream>
13 #include <unistd.h>
14 #include <string>
15 #include <SALOMEconfig.h>
16 #include CORBA_CLIENT_HEADER(SALOME_MPIContainer)
17 #include CORBA_CLIENT_HEADER(SALOME_TestMPIComponent)
18
19 # include "Utils_ORB_INIT.hxx"
20 # include "Utils_SINGLETON.hxx"
21 #include "SALOME_NamingService.hxx"
22 #include "OpUtil.hxx"
23 #include "LocalTraceCollector.hxx"
24 using namespace std;
25
26 int main (int argc, char * argv[])
27 {
28
29   // Initializing omniORB
30   ORB_INIT &init = *SINGLETON_<ORB_INIT>::Instance() ;
31   CORBA::ORB_var &orb = init( argc , argv ) ;
32   LocalTraceCollector *myThreadTrace = LocalTraceCollector::instance(orb);
33     
34   BEGIN_OF(argv[0])
35   try{
36
37     int status;
38
39     if( argc != 3 || strcmp(argv[1],"-np") ){
40       cout << "Usage: TestMPIContainer -np nbproc" << endl;
41       exit(0);
42     }
43
44     // Obtain a reference to the root POA
45     CORBA::Object_var obj = orb->resolve_initial_references("RootPOA") ;
46     PortableServer::POA_var poa = PortableServer::POA::_narrow(obj) ;
47
48     // Use Name Service to find container
49     SALOME_NamingService NS(orb) ;
50     string containerName = "/Containers/" ;
51     string hostName = GetHostname();
52     containerName += hostName + "/MPIFactoryServer_" + argv[2];
53
54     string dirn(getenv("KERNEL_ROOT_DIR"));
55     dirn += "/lib/salome/libSalomeTestMPIComponentEngine.so";
56     
57     // Try to resolve MPI Container
58     obj = NS.Resolve(containerName.c_str()) ;
59     Engines::MPIContainer_var iGenFact = Engines::MPIContainer::_narrow(obj);
60
61     if(CORBA::is_nil(iGenFact)){
62
63       // Launch MPI Container
64       string cmd("mpirun -np ");
65       cmd += argv[2];
66       cmd += " ";
67       cmd += getenv("KERNEL_ROOT_DIR");
68       cmd += "/bin/salome/SALOME_MPIContainer MPIFactoryServer_";
69       cmd += argv[2];
70       cmd += " &";
71       MESSAGE(cmd);
72       status = system(cmd.c_str());
73       if (status == -1) {
74         INFOS("TestMPIContainer launch MPI Container failed (system command status -1)") ;
75       }
76       else if (status == 217) {
77         INFOS("TestMPIContainer launch MPI Container failed (system command status 217)") ;
78       }
79     
80       // Try to resolve MPI Container
81       int it = 0;
82       do{
83         sleep(1);
84         obj = NS.Resolve(containerName.c_str()) ;
85         iGenFact = Engines::MPIContainer::_narrow(obj);
86         MESSAGE("Waiting for MPI Container " << containerName << " : it = " << it );
87       }while( CORBA::is_nil(iGenFact) && (it++<15) );
88
89     }
90
91     if(CORBA::is_nil(iGenFact)){
92       MESSAGE("launching container failed");
93       exit(1);
94     }
95
96
97     Engines::TestMPIComponent_var m1;
98
99 //     for (int iter = 0; iter < 1 ; iter++){
100 //       MESSAGE("----------------------------------------------------" << iter);   
101     obj = iGenFact->load_impl("TestMPIComponent",dirn.c_str());
102
103     INFOS("Get Handle on MPI Component");
104     m1 = Engines::TestMPIComponent::_narrow(obj);
105     if(CORBA::is_nil(m1)){
106       INFOS("getting handle on MPI component failed");
107     }
108     else{ 
109       m1->Coucou(1L);
110 // //   sleep(5);
111       INFOS("Unload MPI Component");
112       iGenFact->remove_impl(m1) ;
113     }
114 //     }
115 //     // Clean-up.
116 // //     iGenFact->finalize_removal() ;
117 // //     sleep(5);
118     iGenFact->MPIShutdown();
119 //     INFOS("shut down corba server for Test MPI Container");
120 //     orb->destroy();
121   }
122   catch(CORBA::COMM_FAILURE& ex) {
123     INFOS("Caught system exception COMM_FAILURE -- unable to contact the object.");
124   }
125   catch(CORBA::SystemException&) {
126     INFOS("Caught a CORBA::SystemException.");
127   }
128   catch(CORBA::Exception&) {
129     INFOS("Caught CORBA::Exception.");
130   }
131   catch(...) {
132     INFOS("Caught unknown exception.");
133   }
134
135   END_OF(argv[0]);
136   delete myThreadTrace;
137   return 0 ;
138 }