Salome HOME
merge from branch BR_V5_DEV
[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 "Utils_ORB_INIT.hxx"
46 #include "Utils_SINGLETON.hxx"
47 #include "SALOMETraceCollector.hxx"
48 #include "OpUtil.hxx"
49
50 using namespace std;
51
52 #ifdef _DEBUG_
53 #include <signal.h>
54
55 void handler(int t) {
56   cerr << "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << endl;
57   cerr << "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << endl;
58   cerr << "SIGSEGV in :" << getpid() << endl;
59   cerr << "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << endl;
60   cerr << "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << endl;
61   while (1) {}
62 }
63 #endif
64
65 typedef void (*sighandler_t)(int);
66 sighandler_t setsig(int sig, sighandler_t handler)
67 {
68   struct sigaction context, ocontext;
69   context.sa_handler = handler;
70   sigemptyset(&context.sa_mask);
71   context.sa_flags = 0;
72   if (sigaction(sig, &context, &ocontext) == -1)
73     return SIG_ERR;
74   return ocontext.sa_handler;
75 }
76
77 void AttachDebugger()
78 {
79   if(getenv ("DEBUGGER"))
80   {
81     std::stringstream exec;
82     exec << "$DEBUGGER SALOME_ParallelContainerNodeDummy " << getpid() << "&";
83     std::cerr << exec.str() << std::endl;
84     system(exec.str().c_str());
85     while(1);
86   }
87 }
88
89 void Handler(int theSigId)
90 {
91   std::cerr << "SIGSEGV: "  << std::endl;
92   AttachDebugger();
93   //to exit or not to exit
94   exit(1);
95 }
96
97 void terminateHandler(void)
98 {
99   std::cerr << "Terminate: not managed exception !"  << std::endl;
100   AttachDebugger();
101 }
102
103 void unexpectedHandler(void)
104 {
105   std::cerr << "Unexpected: unexpected exception !"  << std::endl;
106   AttachDebugger();
107 }
108
109 int main(int argc, char* argv[])
110 {
111   INFOS("Launching a parallel container node");
112
113   if(getenv ("DEBUGGER"))
114   {
115     setsig(SIGSEGV,&Handler);
116     set_terminate(&terminateHandler);
117     set_unexpected(&unexpectedHandler);
118   }
119
120   // Initialise the ORB.
121   CORBA::ORB_var orb = CORBA::ORB_init(argc, argv);
122
123   std::string containerName("");
124   if(argc > 1) {
125     containerName = argv[1];
126   }
127   std::string hostname("");
128   if(argc > 3) {
129     hostname = argv[3];
130   }
131
132   try {  
133     CORBA::Object_var obj = orb->resolve_initial_references("RootPOA");
134     PortableServer::POA_var root_poa = PortableServer::POA::_narrow(obj);
135     PortableServer::POAManager_var pman = root_poa->the_POAManager();
136
137 #ifndef WIN32
138     // add this container to the kill list
139     char aCommand[100];
140     sprintf(aCommand, "addToKillList.py %d SALOME_ParallelContainerNode", getpid());
141     system(aCommand);
142 #endif
143
144     SALOME_NamingService * ns = new SALOME_NamingService(CORBA::ORB::_duplicate(orb));
145     // Get the proxy
146     string proxyNameInNS = ns->BuildContainerNameForNS(containerName.c_str(), 
147                                                        hostname.c_str());
148     obj = ns->Resolve(proxyNameInNS.c_str());
149     char * proxy_ior = orb->object_to_string(obj);
150
151     // Creating a node
152     string node_name = containerName + "Node";
153     Engines_Parallel_Container_i * servant = new Engines_Parallel_Container_i(CORBA::ORB::_duplicate(orb), 
154                                                                               proxy_ior,
155                                                                               0,
156                                                                               root_poa,
157                                                                               (char*) node_name.c_str(),
158                                                                               argc, argv);
159     // PaCO++ init
160     paco_fabrique_manager * pfm = paco_getFabriqueManager();
161     pfm->register_com("dummy", new paco_dummy_fabrique());
162     pfm->register_thread("omni", new paco_omni_fabrique());
163     servant->setLibCom("dummy", servant);
164     servant->setLibThread("omni");
165
166     // Activation
167     PortableServer::ObjectId * _id = root_poa->activate_object(servant);
168     servant->set_id(_id);
169     obj = root_poa->id_to_reference(*_id);
170
171     // In the NamingService
172     string hostname = Kernel_Utils::GetHostname();
173     int myid = 0;
174     char buffer [5];
175     snprintf(buffer, 5, "%d", myid);
176     node_name = node_name + buffer;
177     string _containerName = ns->BuildContainerNameForNS((char*) node_name.c_str(),
178                                                         hostname.c_str());
179     cerr << "---------" << _containerName << "----------" << endl;
180     ns->Register(obj, _containerName.c_str());
181     pman->activate();
182     orb->run();
183   }
184   catch (PaCO::PACO_Exception& e)
185   {
186     INFOS("Caught PaCO::PACO_Exception");
187     std::cerr << e << std::endl;
188   }
189   catch(CORBA::SystemException&)
190   {
191     INFOS("Caught CORBA::SystemException.");
192   }
193   catch(PortableServer::POA::ServantAlreadyActive&)
194   {
195     INFOS("Caught CORBA::ServantAlreadyActiveException");
196   }
197   catch(CORBA::Exception&)
198   {
199     INFOS("Caught CORBA::Exception.");
200   }
201   catch(std::exception& exc)
202   {
203     INFOS("Caught std::exception - "<<exc.what()); 
204   }
205   catch(...)
206   {
207     INFOS("Caught unknown exception.");
208   }
209   return 0 ;
210 }
211