Salome HOME
0023227: [CEA 1718] Correction of the script hxx2salome.py in YACSGEN
[tools/yacsgen.git] / module_generator / paco_tmpl.py
1 #!/usr/bin/env python
2 # -*- coding: utf-8 *-
3 # Copyright (C) 2009-2015  EDF R&D
4 #
5 # This library is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU Lesser General Public
7 # License as published by the Free Software Foundation; either
8 # version 2.1 of the License, or (at your option) any later version.
9 #
10 # This library is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 # Lesser General Public License for more details.
14 #
15 # You should have received a copy of the GNU Lesser General Public
16 # License along with this library; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
18 #
19 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #
21
22 #  Author : Andre RIBES (EDF R&D)
23
24 try:
25   from string import Template
26 except:
27   from compat import Template,set
28   
29 compoMakefile="""
30 include $$(top_srcdir)/adm_local/make_common_starter.am
31
32 BUILT_SOURCES = SALOME_Exception.hxx SALOME_GenericObj.hxx SALOMEDS.hxx SALOME_PyNode.hxx SALOME_Comm.hxx SALOME_Parametric.hxx
33
34 %.hxx : @KERNEL_ROOT_DIR@/idl/salome/%.idl
35 \t$$(OMNIORB_IDL) -bcxx $$(IDLCXXFLAGS) $$(OMNIORB_IDLCXXFLAGS) $$(IDL_INCLUDES) -I@KERNEL_ROOT_DIR@/idl/salome -Wbh=.hxx -Wbs=.cxx $$<
36
37 AM_CFLAGS=$$(KERNEL_INCLUDES) $$(PACO_INCLUDES) -fexceptions
38
39 lib_LTLIBRARIES = lib${component}Engine.la
40 lib${component}Engine_la_SOURCES      = ${component}.cxx ${sources}
41 nodist_lib${component}Engine_la_SOURCES =
42 lib${component}Engine_la_CXXFLAGS = -I$$(top_builddir)/idl $$(SALOME_INCLUDES) $$(PACO_INCLUDES) $$(MPI_INCLUDES) ${includes}
43 lib${component}Engine_la_LIBADD   = -L$$(top_builddir)/idl -lSalomeIDL${module} @KERNEL_ROOT_DIR@/lib/salome/libSalomeParallelDSCContainer.la @PACOPATH@/lib/libPaCO_direct_comScheduling.la $$(FLIBS) ${libs} $$(PACO_LIBS) $$(SALOME_LIBS)
44 lib${component}Engine_la_LDFLAGS = ${rlibs}
45 salomeinclude_HEADERS = ${component}.hxx
46 """
47 compoMakefile=Template(compoMakefile)
48
49 paco_sources = """\
50 $$(top_builddir)/idl/${module}PaCO_${module}_ORB_${component}_client.cxx $$(top_builddir)/idl/${module}PaCO_${module}_ORB_${component}_server.cxx $$(top_builddir)/idl/${module}PaCO.cxx $$(top_builddir)/idl/${module}.cxx
51 """
52 paco_sources = Template(paco_sources)
53
54 hxxCompo="""
55 #ifndef _${component}_HXX_
56 #define _${component}_HXX_
57
58 #include "${module}PaCO_${module}_ORB_${component}_server.hxx"
59 #include "ParallelDSC_i.hxx"
60 #include "Param_Double_Port_uses_i.hxx"
61 #include "Param_Double_Port_provides_i.hxx"
62
63 #include "PortProperties_i.hxx"
64 #include <Utils_SALOME_Exception.hxx>
65 #include <paco_omni.h>
66 #include <paco_${parallel_lib}.h>
67 #include <paco_dummy.h>
68
69 class ${component}_i:
70   public virtual ${module}_ORB::${component}_serv,
71   public virtual Engines_ParallelDSC_i
72 {
73   public:
74     ${component}_i(CORBA::ORB_ptr orb,
75                    char * ior,
76                    int rank,
77                    PortableServer::POA_ptr poa,
78                    PortableServer::ObjectId * contId, 
79                    const char *instanceName, 
80                    const char *interfaceName);
81
82     virtual ~${component}_i();
83
84     void provides_port_changed(const char* provides_port_name,
85                                int connection_nbr,
86                                const Engines::DSC::Message message) {}
87
88     void uses_port_changed(const char* uses_port_name,
89                            Engines::DSC::uses_port * new_uses_port,
90                            const Engines::DSC::Message message) {delete new_uses_port;}
91
92     CORBA::Boolean init_service(const char * service_name);
93
94 ${servicesdef}
95  private:
96    PortProperties_i *  _fake_properties;
97 ${services_init_ok} 
98 ${parallelstreamports}
99 };
100
101 extern "C"
102 {
103   PortableServer::ObjectId * ${component}Engine_factory(CORBA::ORB_ptr orb,
104                                                         char * ior,
105                                                         int rank,
106                                                         PortableServer::POA_ptr poa,
107                                                         PortableServer::ObjectId * contId,
108                                                         const char *instanceName,
109                                                         const char *interfaceName);
110
111   PortableServer::ObjectId * ${component}EngineProxy_factory(CORBA::ORB_ptr orb,
112                                                              paco_fabrique_thread * fab_thread,
113                                                              PortableServer::POA_ptr poa,
114                                                              PortableServer::ObjectId * contId,
115                                                              RegistryConnexion **,
116                                                              const char *instanceName,
117                                                              int node_number);
118   void ${component}_isAPACO_Component() {};                                                             
119 }
120 #endif
121
122 """
123 hxxCompo=Template(hxxCompo)
124
125 hxxinit_ok = """\
126    bool _${service_name}_init_ok;"""
127 hxxinit_ok = Template(hxxinit_ok)
128
129 hxxparallel_outstream = """\
130    ${type}_uses_i * _${name}_port;
131    bool _${name}_port_start_ok;"""
132 hxxparallel_outstream = Template(hxxparallel_outstream)
133
134 hxxparallel_instream = """\
135    ${type}_provides_i * _${name}_port;"""
136 hxxparallel_instream = Template(hxxparallel_instream)
137
138 cxxCompo="""
139 #include "${component}.hxx"
140 #include <string>
141
142 //DEFS
143 ${servicesdef}
144 //ENDDEF
145
146 //! Constructor for component "${component}" instance
147 /*!
148  *
149  */
150 ${component}_i::${component}_i(CORBA::ORB_ptr orb,
151                                char * ior,
152                                int rank,
153                                PortableServer::POA_ptr poa,
154                                PortableServer::ObjectId * contId, 
155                                const char *instanceName, 
156                                const char *interfaceName) :
157   ${module}_ORB::${component}_serv(orb, ior, rank),
158   ${module}_ORB::${component}_base_serv(orb, ior, rank),
159   Engines_ParallelDSC_i(orb, ior, rank, poa, contId, instanceName, interfaceName),
160   Engines_Parallel_Component_i(orb, ior, rank, poa, contId, instanceName, interfaceName),
161   Engines::Parallel_DSC_serv(orb, ior, rank),
162   InterfaceParallel_impl(orb,ior, rank),
163   Engines::Superv_Component_serv(orb, ior, rank),
164   Engines::DSC_serv(orb, ior, rank), 
165   Engines::EngineComponent_serv(orb,ior, rank), 
166   Engines::Parallel_Component_serv(orb,ior, rank), 
167   Engines::Parallel_DSC_base_serv(orb, ior, rank),
168   Engines::Superv_Component_base_serv(orb, ior, rank),
169   Engines::DSC_base_serv(orb, ior, rank), 
170   Engines::EngineComponent_base_serv(orb,ior, rank), 
171   Engines::Parallel_Component_base_serv(orb,ior, rank) 
172
173 {
174   std::cerr << "creating paralle component" << std::endl;
175   _thisObj = this ;
176   _id = _poa->activate_object(_thisObj);
177 ${cons_services}  
178 }
179
180 //! Destructor for component "${component}" instance
181 ${component}_i::~${component}_i()
182 {
183 ${des_services}  
184 }
185
186 //! Register datastream ports for a component service given its name
187 /*!
188  *  \param service_name : service name
189  *  \\return true if port registering succeeded, false if not
190  */
191 CORBA::Boolean
192 ${component}_i::init_service(const char * service_name) {
193   CORBA::Boolean rtn = false;
194   string s_name(service_name);
195 ${initservice}
196   return rtn;
197 }
198
199 ${servicesimpl}
200 """
201 cxxCompo=Template(cxxCompo)
202
203 cxx_cons_service = """\
204   _${service_name}_init_ok = false;
205 """
206 cxx_cons_service = Template(cxx_cons_service)
207
208 cxx_cons_parallel_outstream = """\
209   _${name}_port = NULL;
210   _${name}_port_start_ok = false;
211 """
212 cxx_cons_parallel_outstream = Template(cxx_cons_parallel_outstream)
213
214 cxx_cons_parallel_instream = """\
215   _${name}_port = NULL;
216 """
217 cxx_cons_parallel_instream = Template(cxx_cons_parallel_instream)
218
219 cxx_des_parallel_stream = """\
220   if (_${name}_port)
221     delete _${name}_port;
222 """
223 cxx_des_parallel_stream = Template(cxx_des_parallel_stream)
224
225 initService="""\
226   if (s_name == "${service_name}")
227   {
228     if (!_${service_name}_init_ok)
229     {
230 ${init_parallel_datastream_ports}
231       _${service_name}_init_ok = true;
232     }
233     rtn = true;
234   }
235 """
236 initService=Template(initService)
237
238 hxxparallel_outstream_init = """\
239       _${name}_port = new ${type}_uses_i(this, \"${name}\", _orb);
240       _${name}_port->add_port_to_component();
241 """
242 hxxparallel_outstream_init = Template(hxxparallel_outstream_init)
243
244 hxxparallel_instream_init = """\
245       _${name}_port = ${type}_provides_i::init_port(this, \"${name}\", _orb);
246       ${type}_provides_i::wait_init_port(this, \"${name}\", _orb);
247 """
248 hxxparallel_instream_init = Template(hxxparallel_instream_init)
249
250 cxxService="""
251 void ${component}_i::${service}(${parameters})
252 {
253   std::cerr << "Begin of ${component}_i::${service} of node " << _myRank << std::endl;
254   if (_myRank == 0)
255     beginService("${component}_i::${service}");
256 ${connect_parallel_streamport}
257   try
258     {
259 //BODY
260 ${body}
261 //ENDBODY
262     }
263   catch ( const SALOME_Exception & ex)
264     {
265       SALOME::ExceptionStruct es;
266       es.text=CORBA::string_dup(ex.what());
267       es.type=SALOME::INTERNAL_ERROR;
268       throw SALOME::SALOME_Exception(es);
269     }
270   catch ( const SALOME::SALOME_Exception & ex)
271     {
272       throw;
273     }
274   catch ( const std::exception& ex)
275     {
276       SALOME::ExceptionStruct es;
277       es.text=CORBA::string_dup(ex.what());
278       es.type=SALOME::INTERNAL_ERROR;
279       throw SALOME::SALOME_Exception(es);
280     }
281   catch (...)
282     {
283       std::cerr << "unknown exception" << std::endl;
284       SALOME::ExceptionStruct es;
285       es.text=CORBA::string_dup(" unknown exception");
286       es.type=SALOME::INTERNAL_ERROR;
287       throw SALOME::SALOME_Exception(es);
288     }
289   if (_myRank == 0)
290     endService("${component}_i::${service}");
291   std::cerr << "End of ${component}_i::${service} of node " << _myRank << std::endl;
292 }
293
294 """
295 cxxService=Template(cxxService)
296
297 cxxService_connect = """\
298   if (!_${name}_port_start_ok)
299   {
300     _${name}_port->start_port();
301     _${name}_port_start_ok = true;
302   }
303 """
304 cxxService_connect = Template(cxxService_connect)
305
306 cxxFactoryDummy = """
307 extern "C"
308 {
309   PortableServer::ObjectId * ${component}Engine_factory(CORBA::ORB_ptr orb, char * ior, int rank,
310                                                         PortableServer::POA_ptr poa, 
311                                                         PortableServer::ObjectId * contId,
312                                                         const char *instanceName, 
313                                                         const char *interfaceName)
314   {
315     std::cerr << "Begin of ${component}Engine_factory()" << std::endl;
316
317     paco_fabrique_manager * pfm = paco_getFabriqueManager();
318     pfm->register_com("${component}_node_dummy", new paco_dummy_fabrique());
319     pfm->register_thread("${component}_node_omni", new paco_omni_fabrique());
320     ${component}_i * ${component}_node = new ${component}_i(CORBA::ORB::_duplicate(orb), ior, rank, poa, contId, instanceName, interfaceName);
321     ${component}_node->setLibCom("${component}_node_dummy", ${component}_node);
322     ${component}_node->setLibThread("${component}_node_omni");
323
324     std::cerr << "End of ${component}Engine_factory()" << std::endl;
325     return ${component}_node->getId();
326   }
327
328   PortableServer::ObjectId * ${component}EngineProxy_factory(CORBA::ORB_ptr orb, 
329                                                              paco_fabrique_thread * fab_thread,
330                                                              PortableServer::POA_ptr poa,
331                                                              PortableServer::ObjectId * contId,
332                                                              RegistryConnexion ** connexion,
333                                                              const char *instanceName,
334                                                              int node_number)
335   {
336     cerr << "Begin of ${component}EngineProxy_factory()" << endl;
337     
338     paco_fabrique_manager* pfm = paco_getFabriqueManager();
339     pfm->register_com("proxy_dummy", new paco_dummy_fabrique());
340     pfm->register_thread("proxy_thread", new paco_omni_fabrique());
341     ${module}_ORB::${component}_proxy_impl * proxy = new ${module}_ORB::${component}_proxy_impl(CORBA::ORB::_duplicate(orb),
342                                                                                         fab_thread);
343     PortableServer::ObjectId * id = poa->activate_object(proxy);
344     proxy->_remove_ref();
345     // Initialisation du proxy
346     proxy->setLibCom("proxy_dummy", proxy);
347     proxy->setLibThread("proxy_thread");
348     PaCO::PacoTopology_t serveur_topo;
349     serveur_topo.total = node_number;
350     proxy->setTopology(serveur_topo);
351
352     // Ajout dans le registry ...
353     CORBA::Object_var o = poa->id_to_reference(*contId); // container ior...  
354     const CORBA::String_var the_ior = orb->object_to_string(o);
355     *connexion = new RegistryConnexion(0, 0, the_ior, "theSession", instanceName);
356
357     cerr << "End of ${component}EngineProxy_factory()" << endl;
358     return id;
359   }
360 }
361 """
362 cxxFactoryDummy = Template(cxxFactoryDummy)
363
364 cxxFactoryMpi = """
365 extern "C"
366 {
367   PortableServer::ObjectId * ${component}Engine_factory(CORBA::ORB_ptr orb, char * ior, int rank,
368                                                         PortableServer::POA_ptr poa, 
369                                                         PortableServer::ObjectId * contId,
370                                                         const char *instanceName, 
371                                                         const char *interfaceName)
372   {
373     std::cerr << "Begin of ${component}Engine_factory()" << std::endl;
374
375     paco_fabrique_manager * pfm = paco_getFabriqueManager();
376     pfm->register_com("${component}_node_mpi", new paco_mpi_fabrique());
377     pfm->register_thread("${component}_node_omni", new paco_omni_fabrique());
378     ${component}_i * ${component}_node = new ${component}_i(CORBA::ORB::_duplicate(orb), ior, rank, poa, contId, instanceName, interfaceName);
379     MPI_Comm parallel_object_group = MPI_COMM_WORLD;
380     ${component}_node->setLibCom("${component}_node_mpi", &parallel_object_group);
381     ${component}_node->setLibThread("${component}_node_omni");
382
383     std::cerr << "End of ${component}Engine_factory()" << std::endl;
384     return ${component}_node->getId();
385   }
386
387   PortableServer::ObjectId * ${component}EngineProxy_factory(CORBA::ORB_ptr orb, 
388                                                              paco_fabrique_thread * fab_thread,
389                                                              PortableServer::POA_ptr poa,
390                                                              PortableServer::ObjectId * contId,
391                                                              RegistryConnexion ** connexion,
392                                                              const char *instanceName,
393                                                              int node_number)
394   {
395     cerr << "Begin of ${component}EngineProxy_factory()" << endl;
396     
397     paco_fabrique_manager* pfm = paco_getFabriqueManager();
398     pfm->register_com("proxy_dummy", new paco_dummy_fabrique());
399     pfm->register_thread("proxy_thread", new paco_omni_fabrique());
400     ${module}_ORB::${component}_proxy_impl * proxy = new ${module}_ORB::${component}_proxy_impl(CORBA::ORB::_duplicate(orb),
401                                                                                         fab_thread);
402     PortableServer::ObjectId * id = poa->activate_object(proxy);
403     proxy->_remove_ref();
404     // Initialisation du proxy
405     proxy->setLibCom("proxy_dummy", proxy);
406     proxy->setLibThread("proxy_thread");
407     PaCO::PacoTopology_t serveur_topo;
408     serveur_topo.total = node_number;
409     proxy->setTopology(serveur_topo);
410
411     // Ajout dans le registry ...
412     CORBA::Object_var o = poa->id_to_reference(*contId); // container ior...  
413     const CORBA::String_var the_ior = orb->object_to_string(o);
414     *connexion = new RegistryConnexion(0, 0, the_ior, "theSession", instanceName);
415
416     cerr << "End of ${component}EngineProxy_factory()" << endl;
417     return id;
418   }
419 }
420 """
421 cxxFactoryMpi = Template(cxxFactoryMpi)