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