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