Salome HOME
89ad42ab03e73f5987493ad5c89678505053d02e
[modules/kernel.git] / src / TestMPIContainer / TestMPIContainer.cxx
1 // Copyright (C) 2007-2023  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 //=============================================================================
24 // File      : TestMPIContainer.cxx
25 // Created   : mer jui 4 13:11:27 CEST 2003
26 // Author    : Bernard SECHER, 2003
27 // Project   : SALOME
28 // $Header$
29 //=============================================================================
30 //
31 #include "utilities.h"
32 #include <iostream>
33 #include <unistd.h>
34 #include <string>
35 #include <SALOMEconfig.h>
36 #include CORBA_CLIENT_HEADER(SALOME_MPIContainer)
37 #include CORBA_CLIENT_HEADER(SALOME_TestMPIComponent)
38
39 #include "ArgvKeeper.hxx"
40 #include "Basics_Utils.hxx"
41 #include "SALOME_NamingService.hxx"
42 #include "OpUtil.hxx"
43
44 int main (int argc, char * argv[])
45 {
46
47   // Initializing omniORB
48   SetArgcArgv(argc, argv);
49    CORBA::ORB_ptr orb = KERNEL::GetRefToORB();
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       std::cout << "Usage: TestMPIContainer -np nbproc" << std::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     std::string containerName = "/Containers/" ;
69     std::string hostName = Kernel_Utils::GetHostname();
70     containerName += hostName + "/MPIFactoryServer_" + argv[2];
71
72     std::string dirn(getenv("KERNEL_ROOT_DIR"));
73 #ifdef __APPLE__
74     dirn += "/lib/salome/libSalomeTestMPIComponentEngine.dylib";
75 #else
76     dirn += "/lib/salome/libSalomeTestMPIComponentEngine.so";
77 #endif
78     
79     // Try to resolve MPI Container
80     obj = NS.Resolve(containerName.c_str()) ;
81     Engines::MPIContainer_var iGenFact = Engines::MPIContainer::_narrow(obj);
82
83     if(CORBA::is_nil(iGenFact)){
84
85       // Launch MPI Container
86       std::string cmd("mpirun -np ");
87       cmd += argv[2];
88       cmd += " ";
89       cmd += getenv("KERNEL_ROOT_DIR");
90       cmd += "/bin/salome/SALOME_MPIContainer MPIFactoryServer_";
91       cmd += argv[2];
92       cmd += " &";
93       MESSAGE(cmd);
94       status = system(cmd.c_str());
95       if (status == -1) {
96         INFOS("TestMPIContainer launch MPI Container failed (system command status -1)") ;
97       }
98       else if (status == 217) {
99         INFOS("TestMPIContainer launch MPI Container failed (system command status 217)") ;
100       }
101     
102       // Try to resolve MPI Container
103       int it = 0;
104       do{
105         sleep(1);
106         obj = NS.Resolve(containerName.c_str()) ;
107         iGenFact = Engines::MPIContainer::_narrow(obj);
108         MESSAGE("Waiting for MPI Container " << containerName << " : it = " << it );
109       }while( CORBA::is_nil(iGenFact) && (it++<15) );
110
111     }
112
113     if(CORBA::is_nil(iGenFact)){
114       MESSAGE("launching container failed");
115       exit(1);
116     }
117
118
119     Engines::TestMPIComponent_var m1;
120
121 //     for (int iter = 0; iter < 1 ; iter++){
122 //       MESSAGE("----------------------------------------------------" << iter);   
123     obj = iGenFact->load_impl("TestMPIComponent",dirn.c_str());
124
125     INFOS("Get Handle on MPI Component");
126     m1 = Engines::TestMPIComponent::_narrow(obj);
127     if(CORBA::is_nil(m1)){
128       INFOS("getting handle on MPI component failed");
129     }
130     else{ 
131       m1->Coucou(1L);
132 // Display MPI component in naming service
133       std::string cmdn = "nameclt list Containers.dir/";
134       cmdn += hostName;
135       cmdn += ".dir";
136       system(cmdn.c_str());
137       cmdn = "nameclt list Containers.dir/";
138       cmdn += hostName;
139       cmdn += ".dir/MPIFactoryServer_";
140       cmdn += argv[2];
141       cmdn += ".dir";
142       system(cmdn.c_str());
143 // //   sleep(5);
144       INFOS("Unload MPI Component");
145       iGenFact->remove_impl(m1) ;
146     }
147 //     }
148 //     // Clean-up.
149 // //     iGenFact->finalize_removal() ;
150 // //     sleep(5);
151     iGenFact->Shutdown();
152 //     INFOS("shut down corba server for Test MPI Container");
153 //     orb->destroy();
154   }
155   catch(CORBA::COMM_FAILURE& ex) {
156     INFOS("Caught system exception COMM_FAILURE -- unable to contact the object.");
157   }
158   catch(CORBA::SystemException&) {
159     INFOS("Caught a CORBA::SystemException.");
160   }
161   catch(CORBA::Exception&) {
162     INFOS("Caught CORBA::Exception.");
163   }
164   catch(...) {
165     INFOS("Caught unknown exception.");
166   }
167
168   END_OF(argv[0]);
169   //  delete myThreadTrace;
170   return 0 ;
171 }