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