Salome HOME
merge from BR_V51_AR 7 may 09
[modules/kernel.git] / src / ParallelContainer / SALOME_ParallelContainerNodeDummy.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 //  SALOME ParallelContainerNodeDummy : launcher of a PaCO++ object
23 //  File   : SALOME_ParallelContainerNodeDummy.cxx
24 //  Author : AndrĂ© Ribes, EDF
25 //  Module : SALOME PARALLEL
26 //
27 #include <iostream>
28 #include <string>
29 #include <stdio.h>
30
31 #ifndef WIN32
32 #include <unistd.h>
33 #else
34 #include <process.h>
35 #endif
36
37 #include "SALOME_ParallelContainer_i.hxx"
38
39 #include <paco_omni.h>
40 #include <paco_dummy.h>
41
42 #include "SALOME_NamingService.hxx"
43
44 #include "utilities.h"
45 #include "Basics_Utils.hxx"
46 #include "Utils_ORB_INIT.hxx"
47 #include "Utils_SINGLETON.hxx"
48 #include "SALOMETraceCollector.hxx"
49 #include "OpUtil.hxx"
50
51 #include "Container_init_python.hxx"
52
53 using namespace std;
54
55 #ifdef _DEBUG_
56 #include <signal.h>
57
58 void handler(int t) {
59   cerr << "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << endl;
60   cerr << "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << endl;
61   cerr << "SIGSEGV in :" << getpid() << endl;
62   cerr << "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << endl;
63   cerr << "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << endl;
64   while (1) {}
65 }
66 #endif
67
68 typedef void (*sighandler_t)(int);
69 sighandler_t setsig(int sig, sighandler_t handler)
70 {
71   struct sigaction context, ocontext;
72   context.sa_handler = handler;
73   sigemptyset(&context.sa_mask);
74   context.sa_flags = 0;
75   if (sigaction(sig, &context, &ocontext) == -1)
76     return SIG_ERR;
77   return ocontext.sa_handler;
78 }
79
80 void AttachDebugger()
81 {
82   if(getenv ("DEBUGGER"))
83   {
84     std::stringstream exec;
85     exec << "$DEBUGGER SALOME_ParallelContainerNodeDummy " << getpid() << "&";
86     std::cerr << exec.str() << std::endl;
87     system(exec.str().c_str());
88     while(1);
89   }
90 }
91
92 void Handler(int theSigId)
93 {
94   std::cerr << "SIGSEGV: "  << std::endl;
95   AttachDebugger();
96   //to exit or not to exit
97   exit(1);
98 }
99
100 void terminateHandler(void)
101 {
102   std::cerr << "Terminate: not managed exception !"  << std::endl;
103   AttachDebugger();
104 }
105
106 void unexpectedHandler(void)
107 {
108   std::cerr << "Unexpected: unexpected exception !"  << std::endl;
109   AttachDebugger();
110 }
111
112 int main(int argc, char* argv[])
113 {
114   INFOS("Launching a parallel container node");
115
116   if(getenv ("DEBUGGER"))
117   {
118     setsig(SIGSEGV,&Handler);
119     set_terminate(&terminateHandler);
120     set_unexpected(&unexpectedHandler);
121   }
122
123   // Initialise the ORB.
124   CORBA::ORB_var orb = CORBA::ORB_init(argc, argv);
125   KERNEL_PYTHON::init_python(argc,argv);
126
127   std::string containerName("");
128   if(argc > 1) {
129     containerName = argv[1];
130   }
131   std::string proxy_hostname("");
132   if(argc > 3) {
133     proxy_hostname = argv[3];
134   }
135   int myid = 0;
136   if(argc > 4) 
137     sscanf(argv[4],"%d",&myid);
138
139   try {  
140     CORBA::Object_var obj = orb->resolve_initial_references("RootPOA");
141     PortableServer::POA_var root_poa = PortableServer::POA::_narrow(obj);
142     PortableServer::POAManager_var pman = root_poa->the_POAManager();
143
144 #ifndef WIN32
145     // add this container to the kill list
146     char aCommand[100];
147     sprintf(aCommand, "addToKillList.py %d SALOME_ParallelContainerNode", getpid());
148     system(aCommand);
149 #endif
150
151     SALOME_NamingService * ns = new SALOME_NamingService(orb);
152     // Get the proxy
153     string proxyNameInNS = ns->BuildContainerNameForNS(containerName.c_str(), 
154                                                        proxy_hostname.c_str());
155     obj = ns->Resolve(proxyNameInNS.c_str());
156     char * proxy_ior = orb->object_to_string(obj);
157
158     // Creating a node
159     string node_name = containerName + "Node";
160     Engines_Parallel_Container_i * servant = new Engines_Parallel_Container_i(CORBA::ORB::_duplicate(orb), 
161                                                                               proxy_ior,
162                                                                               myid,
163                                                                               root_poa,
164                                                                               node_name);
165     // PaCO++ init
166     paco_fabrique_manager * pfm = paco_getFabriqueManager();
167     pfm->register_com("dummy", new paco_dummy_fabrique());
168     pfm->register_thread("omni", new paco_omni_fabrique());
169     servant->setLibCom("dummy", servant);
170     servant->setLibThread("omni");
171
172     // Activation
173     obj = servant->_this();
174
175     // In the NamingService
176     string hostname = Kernel_Utils::GetHostname();
177     char buffer [5];
178     snprintf(buffer, 5, "%d", myid);
179     node_name = node_name + buffer;
180     string _containerName = ns->BuildContainerNameForNS((char*) node_name.c_str(),
181                                                         hostname.c_str());
182     cerr << "---------" << _containerName << "----------" << endl;
183     ns->Register(obj, _containerName.c_str());
184     pman->activate();
185     orb->run();
186     PyGILState_Ensure();
187     //Delete python container that destroy orb from python (pyCont._orb.destroy())
188     Py_Finalize();
189     CORBA::string_free(proxy_ior);
190     delete ns;
191   }
192   catch (PaCO::PACO_Exception& e)
193   {
194     INFOS("Caught PaCO::PACO_Exception");
195     std::cerr << e << std::endl;
196   }
197   catch(CORBA::SystemException&)
198   {
199     INFOS("Caught CORBA::SystemException.");
200   }
201   catch(PortableServer::POA::ServantAlreadyActive&)
202   {
203     INFOS("Caught CORBA::ServantAlreadyActiveException");
204   }
205   catch(CORBA::Exception&)
206   {
207     INFOS("Caught CORBA::Exception.");
208   }
209   catch(omniORB::fatalException& fe) 
210   {
211     INFOS("Caught omniORB::fatalException:");
212     INFOS(" file: " << fe.file());
213     INFOS(" line: " << fe.line());
214     INFOS(" mesg: " << fe.errmsg());
215   }
216   catch(std::exception& exc)
217   {
218     INFOS("Caught std::exception - "<<exc.what()); 
219   }
220   catch(...)
221   {
222     INFOS("Caught unknown exception.");
223   }
224   return 0 ;
225 }
226