Salome HOME
PR: mergefrom_BSEC_br1_14Mar04
[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
24 int main (int argc, char * argv[])
25 {
26
27   try{
28
29     int status;
30
31     MESSAGE("COUCOU");
32     if( argc != 3 || strcmp(argv[1],"-np") ){
33       cout << "Usage: TestMPIContainer -np nbproc" << endl;
34       exit(0);
35     }
36
37     // Initializing omniORB
38     CORBA::ORB_var orb = CORBA::ORB_init(argc, argv);
39     
40     // Obtain a reference to the root POA
41     CORBA::Object_var obj = orb->resolve_initial_references("RootPOA") ;
42     PortableServer::POA_var poa = PortableServer::POA::_narrow(obj) ;
43
44     // Use Name Service to find container
45     SALOME_NamingService NS(orb) ;
46     string containerName = "/Containers/" ;
47     string hostName = GetHostname();
48     containerName += hostName + "/MPIFactoryServer_" + argv[2];
49
50     MESSAGE(containerName);
51
52     string dirn(getenv("KERNEL_ROOT_DIR"));
53     dirn += "/lib/salome/libSalomeTestMPIComponentEngine.so";
54     MESSAGE(dirn.c_str());
55     
56     // Try to resolve MPI Container
57     obj = NS.Resolve(containerName.c_str()) ;
58     Engines::Container_var iGenFact = Engines::Container::_narrow(obj);
59
60     if(CORBA::is_nil(iGenFact)){
61
62       // Launch MPI Container
63       string cmd("mpirun -np ");
64       cmd += argv[2];
65       cmd += " ";
66       cmd += getenv("KERNEL_ROOT_DIR");
67       cmd += "/bin/salome/SALOME_MPIContainer MPIFactoryServer_";
68       cmd += argv[2];
69       cmd += " &";
70       MESSAGE(cmd);
71       status = system(cmd.c_str());
72       if (status == -1) {
73         INFOS("TestMPIContainer launch MPI Container failed (system command status -1)") ;
74       }
75       else if (status == 217) {
76         INFOS("TestMPIContainer launch MPI Container failed (system command status 217)") ;
77       }
78     
79       // Try to resolve MPI Container
80       int it = 0;
81       do{
82         sleep(1);
83         obj = NS.Resolve(containerName.c_str()) ;
84         iGenFact = Engines::Container::_narrow(obj);
85         MESSAGE("Waiting for MPI Container " << containerName << " : it = " << it );
86       }while( CORBA::is_nil(iGenFact) && (it++<15) );
87
88     }
89
90     if(CORBA::is_nil(iGenFact)){
91       MESSAGE("echec recuperation poignee container");
92       exit(1);
93     }
94
95     Engines::TestMPIComponent_var m1;
96
97     for (int iter = 0; iter < 1 ; iter++){
98       MESSAGE("----------------------------------------------------" << iter);   
99       obj = iGenFact->load_impl("TestMPIComponent",dirn.c_str());
100
101       m1 = Engines::TestMPIComponent::_narrow(obj);
102       if(CORBA::is_nil(m1)){
103         INFOS("echec recuperation poignee composant");
104       }
105       else{
106       
107         INFOS("recup m1");
108         SCRUTE(m1->instanceName());
109         INFOS("Lancement de coucou");
110         m1->Coucou(1L);
111         INFOS("On remove le composant");
112         iGenFact->remove_impl(m1) ;
113         sleep(5);
114       }
115     }
116     // Clean-up.
117     iGenFact->finalize_removal() ;
118     orb->destroy();
119   }
120   catch(CORBA::COMM_FAILURE& ex) {
121     INFOS("Caught system exception COMM_FAILURE -- unable to contact the object.");
122   }
123   catch(CORBA::SystemException&) {
124     INFOS("Caught a CORBA::SystemException.");
125   }
126   catch(CORBA::Exception&) {
127     INFOS("Caught CORBA::Exception.");
128   }
129   catch(...) {
130     INFOS("Caught unknown exception.");
131   }
132
133   return 0;
134 }