Salome HOME
BugID: IPAL19426 Fixed methods Save/Load
[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/ or email : webmaster.salome@opencascade.com
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 // $Header$
27 //=============================================================================
28
29 #include "utilities.h"
30 #include <iostream>
31 #include <unistd.h>
32 #include <string>
33 #include <SALOMEconfig.h>
34 #include CORBA_CLIENT_HEADER(SALOME_MPIContainer)
35 #include CORBA_CLIENT_HEADER(SALOME_TestMPIComponent)
36
37 # include "Utils_ORB_INIT.hxx"
38 # include "Utils_SINGLETON.hxx"
39 #include "SALOME_NamingService.hxx"
40 #include "OpUtil.hxx"
41 using namespace std;
42
43 int main (int argc, char * argv[])
44 {
45
46   // Initializing omniORB
47   ORB_INIT &init = *SINGLETON_<ORB_INIT>::Instance() ;
48   CORBA::ORB_var &orb = init( argc , argv ) ;
49   //  SALOMETraceCollector *myThreadTrace = SALOMETraceCollector::instance(orb);
50     
51   BEGIN_OF(argv[0])
52   try{
53
54     int status;
55
56     if( argc != 3 || strcmp(argv[1],"-np") ){
57       cout << "Usage: TestMPIContainer -np nbproc" << endl;
58       exit(0);
59     }
60
61     // Obtain a reference to the root POA
62     CORBA::Object_var obj = orb->resolve_initial_references("RootPOA") ;
63     PortableServer::POA_var poa = PortableServer::POA::_narrow(obj) ;
64
65     // Use Name Service to find container
66     SALOME_NamingService NS(orb) ;
67     string containerName = "/Containers/" ;
68     string hostName = GetHostname();
69     containerName += hostName + "/MPIFactoryServer_" + argv[2];
70
71     string dirn(getenv("KERNEL_ROOT_DIR"));
72     dirn += "/lib/salome/libSalomeTestMPIComponentEngine.so";
73     
74     // Try to resolve MPI Container
75     obj = NS.Resolve(containerName.c_str()) ;
76     Engines::MPIContainer_var iGenFact = Engines::MPIContainer::_narrow(obj);
77
78     if(CORBA::is_nil(iGenFact)){
79
80       // Launch MPI Container
81       string cmd("mpirun -np ");
82       cmd += argv[2];
83       cmd += " ";
84       cmd += getenv("KERNEL_ROOT_DIR");
85       cmd += "/bin/salome/SALOME_MPIContainer MPIFactoryServer_";
86       cmd += argv[2];
87       cmd += " &";
88       MESSAGE(cmd);
89       status = system(cmd.c_str());
90       if (status == -1) {
91         INFOS("TestMPIContainer launch MPI Container failed (system command status -1)") ;
92       }
93       else if (status == 217) {
94         INFOS("TestMPIContainer launch MPI Container failed (system command status 217)") ;
95       }
96     
97       // Try to resolve MPI Container
98       int it = 0;
99       do{
100         sleep(1);
101         obj = NS.Resolve(containerName.c_str()) ;
102         iGenFact = Engines::MPIContainer::_narrow(obj);
103         MESSAGE("Waiting for MPI Container " << containerName << " : it = " << it );
104       }while( CORBA::is_nil(iGenFact) && (it++<15) );
105
106     }
107
108     if(CORBA::is_nil(iGenFact)){
109       MESSAGE("launching container failed");
110       exit(1);
111     }
112
113
114     Engines::TestMPIComponent_var m1;
115
116 //     for (int iter = 0; iter < 1 ; iter++){
117 //       MESSAGE("----------------------------------------------------" << iter);   
118     obj = iGenFact->load_impl("TestMPIComponent",dirn.c_str());
119
120     INFOS("Get Handle on MPI Component");
121     m1 = Engines::TestMPIComponent::_narrow(obj);
122     if(CORBA::is_nil(m1)){
123       INFOS("getting handle on MPI component failed");
124     }
125     else{ 
126       m1->Coucou(1L);
127 // //   sleep(5);
128       INFOS("Unload MPI Component");
129       iGenFact->remove_impl(m1) ;
130     }
131 //     }
132 //     // Clean-up.
133 // //     iGenFact->finalize_removal() ;
134 // //     sleep(5);
135     iGenFact->Shutdown();
136 //     INFOS("shut down corba server for Test MPI Container");
137 //     orb->destroy();
138   }
139   catch(CORBA::COMM_FAILURE& ex) {
140     INFOS("Caught system exception COMM_FAILURE -- unable to contact the object.");
141   }
142   catch(CORBA::SystemException&) {
143     INFOS("Caught a CORBA::SystemException.");
144   }
145   catch(CORBA::Exception&) {
146     INFOS("Caught CORBA::Exception.");
147   }
148   catch(...) {
149     INFOS("Caught unknown exception.");
150   }
151
152   END_OF(argv[0]);
153   //  delete myThreadTrace;
154   return 0 ;
155 }