Salome HOME
Fix compilation pb.
[modules/kernel.git] / src / ParallelContainer / SALOME_ParallelContainerNodeDummy.cxx
1 //  SALOME ParallelContainerNodeDummy : launcher of a PaCO++ object
2 //
3 //  Copyright (C) 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.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
21 //
22 //
23 //
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 WNT
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 "Utils_ORB_INIT.hxx"
47 #include "Utils_SINGLETON.hxx"
48 #include "SALOMETraceCollector.hxx"
49 #include "OpUtil.hxx"
50
51 using namespace std;
52
53 #ifdef _DEBUG_
54 #include <signal.h>
55
56 void handler(int t) {
57         cerr << "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << endl;
58         cerr << "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << endl;
59         cerr << "SIGSEGV in :" << getpid() << endl;
60         cerr << "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << endl;
61         cerr << "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << 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 #ifdef _DEBUG_
115 //      signal(SIGSEGV, handler);
116 #endif
117   if(getenv ("DEBUGGER"))
118     {
119       setsig(SIGSEGV,&Handler);
120       set_terminate(&terminateHandler);
121       set_unexpected(&unexpectedHandler);
122     }
123
124         // Initialise the ORB.
125         ORB_INIT &init = *SINGLETON_<ORB_INIT>::Instance();
126         ASSERT(SINGLETON_<ORB_INIT>::IsAlreadyExisting());
127         CORBA::ORB_var orb = init(0, 0);
128         //CORBA::ORB_var orb = CORBA::ORB_init(argc, argv);
129
130         char * containerName = "";
131         if(argc > 1) {
132                 containerName = argv[1];
133         }
134
135         char * hostname = "";
136         if(argc > 3) {
137                 hostname = argv[3];
138         }
139
140         try {  
141                 CORBA::Object_var obj = orb->resolve_initial_references("RootPOA");
142                 PortableServer::POA_var root_poa = PortableServer::POA::_narrow(obj);
143                 PortableServer::POAManager_var pman = root_poa->the_POAManager();
144
145 #ifndef WNT
146                 // add this container to the kill list
147                 char aCommand[100];
148                 sprintf(aCommand, "addToKillList.py %d SALOME_ParallelContainerNode", getpid());
149                 system(aCommand);
150 #endif
151
152                 SALOME_NamingService * ns = new SALOME_NamingService(CORBA::ORB::_duplicate(orb));
153                 // Get the proxy
154                 string proxyNameInNS = ns->BuildContainerNameForNS(containerName, hostname);
155                 obj = ns->Resolve(proxyNameInNS.c_str());
156                 char * proxy_ior = orb->object_to_string(obj);
157
158                 // Creating a node
159                 string name(containerName);
160                 string node_name = name + "Node";
161                 Engines_Parallel_Container_i * servant = new Engines_Parallel_Container_i(CORBA::ORB::_duplicate(orb), proxy_ior,
162                                                                                                                                                                                                                                                                                                                         root_poa,
163                                                                                                                                                                                                                                                                                                                         (char*) node_name.c_str(),
164                                                                                                                                                                                                                                                                                                                         argc, argv);
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
170                 // Global context
171                 PaCO_operation * global_ptr = servant->getContext("global_paco_context");
172                 global_ptr->setLibCom("dummy",NULL);
173                 global_ptr->setLibThread("omni");
174
175                 // Activation
176                 PortableServer::ObjectId * _id = root_poa->activate_object(servant);
177                 servant->set_id(_id);
178                 obj = root_poa->id_to_reference(*_id);
179
180                 // In the NamingService
181                 string hostname = GetHostname();
182                 int myid = 0;
183                 char buffer [5];
184                 snprintf(buffer, 5, "%d", myid);
185                 node_name = node_name + buffer;
186                 string _containerName = ns->BuildContainerNameForNS((char*) node_name.c_str(),
187                                                                                                                                                                                                                                 hostname.c_str());
188                 cerr << "---------" << _containerName << "----------" << endl;
189                 ns->Register(obj, _containerName.c_str());
190                 pman->activate();
191                 orb->run();
192         }
193         catch(CORBA::SystemException&)
194         {
195                 INFOS("Caught CORBA::SystemException.");
196         }
197         catch(PortableServer::POA::ServantAlreadyActive&)
198         {
199                 INFOS("Caught CORBA::ServantAlreadyActiveException");
200         }
201         catch(CORBA::Exception&)
202         {
203                 INFOS("Caught CORBA::Exception.");
204         }
205         catch(std::exception& exc)
206         {
207                 INFOS("Caught std::exception - "<<exc.what()); 
208         }
209         catch(...)
210         {
211                 INFOS("Caught unknown exception.");
212         }
213         return 0 ;
214 }
215