Salome HOME
PR: merge from branch BR_auto_V310 tag mergefrom_OCC_development_for_3_2_0a2_10mar06
[modules/kernel.git] / src / TestMPIContainer / TestMPIContainer.cxx
1 // Copyright (C) 2005  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
3 // 
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either 
7 // version 2.1 of the License.
8 // 
9 // This library is distributed in the hope that it will be useful 
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
12 // Lesser General Public License for more details.
13 //
14 // You should have received a copy of the GNU Lesser General Public  
15 // License along with this library; if not, write to the Free Software 
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17 //
18 // See http://www.salome-platform.org/
19 //
20 // using namespace std;
21 //=============================================================================
22 // File      : TestMPIContainer.cxx
23 // Created   : mer jui 4 13:11:27 CEST 2003
24 // Author    : Bernard SECHER, 2003
25 // Project   : SALOME
26 // Copyright : CEA 2003
27 // $Header$
28 //=============================================================================
29
30 #include "utilities.h"
31 #include <iostream>
32 #include <unistd.h>
33 #include <string>
34 #include <SALOMEconfig.h>
35 #include CORBA_CLIENT_HEADER(SALOME_MPIContainer)
36 #include CORBA_CLIENT_HEADER(SALOME_TestMPIComponent)
37
38 # include "Utils_ORB_INIT.hxx"
39 # include "Utils_SINGLETON.hxx"
40 #include "SALOME_NamingService.hxx"
41 #include "OpUtil.hxx"
42 using namespace std;
43
44 int main (int argc, char * argv[])
45 {
46
47   // Initializing omniORB
48   ORB_INIT &init = *SINGLETON_<ORB_INIT>::Instance() ;
49   CORBA::ORB_var &orb = init( argc , argv ) ;
50   //  SALOMETraceCollector *myThreadTrace = SALOMETraceCollector::instance(orb);
51     
52   BEGIN_OF(argv[0])
53   try{
54
55     int status;
56
57     if( argc != 3 || strcmp(argv[1],"-np") ){
58       cout << "Usage: TestMPIContainer -np nbproc" << endl;
59       exit(0);
60     }
61
62     // Obtain a reference to the root POA
63     CORBA::Object_var obj = orb->resolve_initial_references("RootPOA") ;
64     PortableServer::POA_var poa = PortableServer::POA::_narrow(obj) ;
65
66     // Use Name Service to find container
67     SALOME_NamingService NS(orb) ;
68     string containerName = "/Containers/" ;
69     string hostName = GetHostname();
70     containerName += hostName + "/MPIFactoryServer_" + argv[2];
71
72     string dirn(getenv("KERNEL_ROOT_DIR"));
73     dirn += "/lib/salome/libSalomeTestMPIComponentEngine.so";
74     
75     // Try to resolve MPI Container
76     obj = NS.Resolve(containerName.c_str()) ;
77     Engines::MPIContainer_var iGenFact = Engines::MPIContainer::_narrow(obj);
78
79     if(CORBA::is_nil(iGenFact)){
80
81       // Launch MPI Container
82       string cmd("mpirun -np ");
83       cmd += argv[2];
84       cmd += " ";
85       cmd += getenv("KERNEL_ROOT_DIR");
86       cmd += "/bin/salome/SALOME_MPIContainer MPIFactoryServer_";
87       cmd += argv[2];
88       cmd += " &";
89       MESSAGE(cmd);
90       status = system(cmd.c_str());
91       if (status == -1) {
92         INFOS("TestMPIContainer launch MPI Container failed (system command status -1)") ;
93       }
94       else if (status == 217) {
95         INFOS("TestMPIContainer launch MPI Container failed (system command status 217)") ;
96       }
97     
98       // Try to resolve MPI Container
99       int it = 0;
100       do{
101         sleep(1);
102         obj = NS.Resolve(containerName.c_str()) ;
103         iGenFact = Engines::MPIContainer::_narrow(obj);
104         MESSAGE("Waiting for MPI Container " << containerName << " : it = " << it );
105       }while( CORBA::is_nil(iGenFact) && (it++<15) );
106
107     }
108
109     if(CORBA::is_nil(iGenFact)){
110       MESSAGE("launching container failed");
111       exit(1);
112     }
113
114
115     Engines::TestMPIComponent_var m1;
116
117 //     for (int iter = 0; iter < 1 ; iter++){
118 //       MESSAGE("----------------------------------------------------" << iter);   
119     obj = iGenFact->load_impl("TestMPIComponent",dirn.c_str());
120
121     INFOS("Get Handle on MPI Component");
122     m1 = Engines::TestMPIComponent::_narrow(obj);
123     if(CORBA::is_nil(m1)){
124       INFOS("getting handle on MPI component failed");
125     }
126     else{ 
127       m1->Coucou(1L);
128 // //   sleep(5);
129       INFOS("Unload MPI Component");
130       iGenFact->remove_impl(m1) ;
131     }
132 //     }
133 //     // Clean-up.
134 // //     iGenFact->finalize_removal() ;
135 // //     sleep(5);
136     iGenFact->Shutdown();
137 //     INFOS("shut down corba server for Test MPI Container");
138 //     orb->destroy();
139   }
140   catch(CORBA::COMM_FAILURE& ex) {
141     INFOS("Caught system exception COMM_FAILURE -- unable to contact the object.");
142   }
143   catch(CORBA::SystemException&) {
144     INFOS("Caught a CORBA::SystemException.");
145   }
146   catch(CORBA::Exception&) {
147     INFOS("Caught CORBA::Exception.");
148   }
149   catch(...) {
150     INFOS("Caught unknown exception.");
151   }
152
153   END_OF(argv[0]);
154   //  delete myThreadTrace;
155   return 0 ;
156 }