Salome HOME
merge from branch BR_For40_DSC tag mergeto_BR_Dev_For_4_0_16apr07
[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_PARALLEL
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 int main(int argc, char* argv[])
67 {
68         INFOS("Launching a parallel container node");
69
70 #ifdef DEBUG_PARALLEL
71         signal(SIGSEGV, handler);
72 #endif
73
74         // Initialise the ORB.
75         ORB_INIT &init = *SINGLETON_<ORB_INIT>::Instance();
76         ASSERT(SINGLETON_<ORB_INIT>::IsAlreadyExisting());
77         CORBA::ORB_var orb = init(0, 0);
78         //CORBA::ORB_var orb = CORBA::ORB_init(argc, argv);
79
80         char * containerName = "";
81         if(argc > 1) {
82                 containerName = argv[1];
83         }
84
85         char * hostname = "";
86         if(argc > 3) {
87                 hostname = argv[3];
88         }
89
90         try {  
91                 CORBA::Object_var obj = orb->resolve_initial_references("RootPOA");
92                 PortableServer::POA_var root_poa = PortableServer::POA::_narrow(obj);
93                 PortableServer::POAManager_var pman = root_poa->the_POAManager();
94
95 #ifndef WNT
96                 // add this container to the kill list
97                 char aCommand[100];
98                 sprintf(aCommand, "addToKillList.py %d SALOME_ParallelContainerNode", getpid());
99                 system(aCommand);
100 #endif
101
102                 SALOME_NamingService * ns = new SALOME_NamingService(CORBA::ORB::_duplicate(orb));
103                 // Get the proxy
104                 string proxyNameInNS = ns->BuildContainerNameForNS(containerName, hostname);
105                 obj = ns->Resolve(proxyNameInNS.c_str());
106                 char * proxy_ior = orb->object_to_string(obj);
107
108                 // Creating a node
109                 string name(containerName);
110                 string node_name = name + "Node";
111                 Engines_Parallel_Container_i * servant = new Engines_Parallel_Container_i(CORBA::ORB::_duplicate(orb), proxy_ior,
112                                                                                                                                                                                                                                                                                                                         root_poa,
113                                                                                                                                                                                                                                                                                                                         (char*) node_name.c_str(),
114                                                                                                                                                                                                                                                                                                                         argc, argv);
115                 // PaCO++ init
116                 paco_fabrique_manager * pfm = paco_getFabriqueManager();
117                 pfm->register_com("dummy", new paco_dummy_fabrique());
118                 pfm->register_thread("omni", new paco_omni_fabrique());
119
120                 // Global context
121                 PaCO_operation * global_ptr = servant->getContext("global_paco_context");
122                 global_ptr->setLibCom("dummy",NULL);
123                 global_ptr->setLibThread("omni");
124
125                 // Activation
126                 PortableServer::ObjectId * _id = root_poa->activate_object(servant);
127                 servant->set_id(_id);
128                 obj = root_poa->id_to_reference(*_id);
129
130                 // In the NamingService
131                 string hostname = GetHostname();
132                 int myid = 0;
133                 char buffer [5];
134                 snprintf(buffer, 5, "%d", myid);
135                 node_name = node_name + buffer;
136                 string _containerName = ns->BuildContainerNameForNS((char*) node_name.c_str(),
137                                                                                                                                                                                                                                 hostname.c_str());
138                 cerr << "---------" << _containerName << "----------" << endl;
139                 ns->Register(obj, _containerName.c_str());
140                 pman->activate();
141                 orb->run();
142         }
143         catch(CORBA::SystemException&)
144         {
145                 INFOS("Caught CORBA::SystemException.");
146         }
147         catch(PortableServer::POA::ServantAlreadyActive&)
148         {
149                 INFOS("Caught CORBA::ServantAlreadyActiveException");
150         }
151         catch(CORBA::Exception&)
152         {
153                 INFOS("Caught CORBA::Exception.");
154         }
155         catch(std::exception& exc)
156         {
157                 INFOS("Caught std::exception - "<<exc.what()); 
158         }
159         catch(...)
160         {
161                 INFOS("Caught unknown exception.");
162         }
163         return 0 ;
164 }
165