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