1 # Copyright (C) 2009-2024 EDF
3 # This library is free software; you can redistribute it and/or
4 # modify it under the terms of the GNU Lesser General Public
5 # License as published by the Free Software Foundation; either
6 # version 2.1 of the License, or (at your option) any later version.
8 # This library is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 # Lesser General Public License for more details.
13 # You should have received a copy of the GNU Lesser General Public
14 # License along with this library; if not, write to the Free Software
15 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 from string import Template
23 from module_generator.compat import Template,set
26 #include "${component}.hxx"
30 #include <Calcium.hxx>
31 #include <CalciumException.hxx>
34 #include <SALOME_Embedded_NamingService_Client.hxx>
35 #include <SALOME_NamingService.hxx>
36 #include <Utils_SALOME_Exception.hxx>
40 #define BUILD_EXE ${exe}
42 typedef void (*sighandler_t)(int);
43 sighandler_t setsig(int sig, sighandler_t handler)
45 struct sigaction context, ocontext;
46 context.sa_handler = handler;
47 sigemptyset(&context.sa_mask);
49 if (sigaction(sig, &context, &ocontext) == -1)
51 return ocontext.sa_handler;
54 static void AttachDebugger()
61 size = backtrace (array, size);
62 strings = backtrace_symbols (array, size);
63 for (i = 0; i < size; i++)
64 _what=_what+strings[i]+ '\\n';
67 std::cerr << pthread_self() << std::endl;
68 std::cerr << _what << std::endl;
70 if(getenv ("DEBUGGER"))
72 std::stringstream exec;
74 exec << "$$DEBUGGER " << "${exe_path} " << getpid() << "&";
76 exec << "$$DEBUGGER SALOME_Container " << getpid() << "&";
78 std::cerr << exec.str() << std::endl;
79 system(exec.str().c_str());
84 static void THandler(int theSigId)
86 std::cerr << "SIGSEGV: " << std::endl;
88 //to exit or not to exit
92 static void terminateHandler(void)
94 std::cerr << "Terminate: not managed exception !" << std::endl;
96 throw SALOME_Exception("Terminate: not managed exception !");
99 static void unexpectedHandler(void)
101 std::cerr << "Unexpected: unexpected exception !" << std::endl;
103 throw SALOME_Exception("Unexpected: unexpected exception !");
109 #define F_FUNC(lname,uname) __stdcall uname
110 #define F_CALL(lname,uname) uname
111 #define STR_PSTR(str) char *str, int _(Len,str)
112 #define STR_PLEN(str)
113 #define STR_PTR(str) str
114 #define STR_LEN(str) _(Len,str)
115 #define STR_CPTR(str) str,strlen(str)
116 #define STR_CLEN(str)
118 #define F_FUNC(lname,uname) _(lname,_) /* Fortran function name */
119 #define F_CALL(lname,uname) _(lname,_) /* Fortran function call */
120 #define STR_PSTR(str) char *str /* fortran string arg pointer */
121 #define STR_PLEN(str) , int _(Len,str) /* fortran string arg length */
122 #define STR_PTR(str) str /* fortran string pointer */
123 #define STR_LEN(str) _(Len,str) /* fortran string length */
124 #define STR_CPTR(str) str /* fortran string calling arg pointer */
125 #define STR_CLEN(str) , strlen(str) /* fortran string calling arg length */
134 extern "C" void cp_exit(int err);
136 extern "C" void F_FUNC(cpexit,CPEXIT)(int *err)
146 //! Constructor for component "${component}" instance
150 ${component}_i::${component}_i(CORBA::ORB_ptr orb,
151 PortableServer::POA_ptr poa,
152 PortableServer::ObjectId * contId,
153 const char *instanceName,
154 const char *interfaceName)
155 : Superv_Component_i(orb, poa, contId, instanceName, interfaceName)
158 setsig(SIGSEGV,&THandler);
159 set_terminate(&terminateHandler);
160 set_unexpected(&unexpectedHandler);
163 _id = _poa->activate_object(_thisObj);
166 ${component}_i::${component}_i(CORBA::ORB_ptr orb,
167 PortableServer::POA_ptr poa,
168 Engines::Container_ptr container,
169 const char *instanceName,
170 const char *interfaceName)
171 : Superv_Component_i(orb, poa, container, instanceName, interfaceName)
174 setsig(SIGSEGV,&THandler);
175 set_terminate(&terminateHandler);
176 set_unexpected(&unexpectedHandler);
179 _id = _poa->activate_object(_thisObj);
182 //! Destructor for component "${component}" instance
183 ${component}_i::~${component}_i()
187 void ${component}_i::destroy()
191 if(!CORBA::is_nil(_orb))
194 Engines_Component_i::destroy();
198 //! Register datastream ports for a component service given its name
200 * \param service_name : service name
201 * \\return true if port registering succeeded, false if not
204 ${component}_i::init_service(const char * service_name) {
205 CORBA::Boolean rtn = false;
206 string s_name(service_name);
215 PortableServer::ObjectId * ${component}Engine_factory( CORBA::ORB_ptr orb,
216 PortableServer::POA_ptr poa,
217 PortableServer::ObjectId * contId,
218 const char *instanceName,
219 const char *interfaceName)
221 MESSAGE("PortableServer::ObjectId * ${component}Engine_factory()");
222 ${component}_i * myEngine = new ${component}_i(orb, poa, contId, instanceName, interfaceName);
223 return myEngine->getId() ;
229 CORBA::ORB_var orb = CORBA::ORB_init( argc , &argv ) ;
230 PortableServer::POAManager_var pman;
231 CORBA::Object_var obj;
234 obj = orb->resolve_initial_references("RootPOA");
235 PortableServer::POA_var poa = PortableServer::POA::_narrow(obj);
236 PortableServer::POAManager_var pman = poa->the_POAManager();
237 std::string containerName(getenv("SALOME_CONTAINERNAME"));
238 std::string instanceName(getenv("SALOME_INSTANCE"));
239 obj=orb->string_to_object(getenv("SALOME_CONTAINER"));
240 Engines::Container_var container = Engines::Container::_narrow(obj);
241 ${component}_i * myEngine = new ${component}_i(orb, poa, container, instanceName.c_str(), "${component}");
243 obj=myEngine->POA_${module}_ORB::${component}::_this();
244 Engines::EngineComponent_var component = Engines::EngineComponent::_narrow(obj);
245 string component_registerName = containerName + "/" + instanceName;
246 Engines::EmbeddedNamingService_var embedded_NS = container->get_embedded_NS_if_ssl();
247 if( CORBA::is_nil(embedded_NS))
249 // Use the salome session to get the naming service
250 SALOME_NamingService salome_NS(orb);
251 salome_NS.Register(component,component_registerName.c_str());
255 SALOME_Embedded_NamingService_Client ns_client(embedded_NS);
256 ns_client.Register(component,component_registerName.c_str());
261 catch(CORBA::Exception&)
263 std::cerr << "Caught CORBA::Exception."<< std::endl;
265 catch(std::exception& exc)
267 std::cerr << "Caught std::exception - "<<exc.what() << std::endl;
271 std::cerr << "Caught unknown exception." << std::endl;
275 void F_FUNC(yacsinit,YACSINIT)()
281 cxxCompo=Template(cxxCompo)
284 #ifndef _${component}_HXX_
285 #define _${component}_HXX_
287 #include <SALOME_Component.hh>
288 #include "Superv_Component_i.hxx"
289 #include "${module}.hh"
295 class ${component}_i: public virtual POA_${module}_ORB::${component},
296 ${inheritedclass} public virtual Superv_Component_i
299 ${component}_i(CORBA::ORB_ptr orb, PortableServer::POA_ptr poa,
300 PortableServer::ObjectId * contId,
301 const char *instanceName, const char *interfaceName);
302 ${component}_i(CORBA::ORB_ptr orb, PortableServer::POA_ptr poa,
303 Engines::Container_ptr container,
304 const char *instanceName, const char *interfaceName);
305 virtual ~${component}_i();
307 CORBA::Boolean init_service(const char * service_name);
313 PortableServer::ObjectId * ${component}Engine_factory( CORBA::ORB_ptr orb,
314 PortableServer::POA_ptr poa,
315 PortableServer::ObjectId * contId,
316 const char *instanceName,
317 const char *interfaceName);
323 hxxCompo=Template(hxxCompo)
326 void ${component}_i::${service}(${parameters})
328 beginService("${component}_i::${service}");
329 Superv_Component_i * component = dynamic_cast<Superv_Component_i*>(this);
330 //char nom_instance[INSTANCE_LEN];
331 //int info = cp_cd(component,nom_instance);
337 //cp_fin(component,CP_ARRET);
339 catch ( const CalciumException & ex)
341 std::cerr << ex.what() << std::endl;
342 //cp_fin(component,CP_ARRET);
343 SALOME::ExceptionStruct es;
344 es.text=CORBA::string_dup(ex.what());
345 es.type=SALOME::INTERNAL_ERROR;
346 throw SALOME::SALOME_Exception(es);
348 catch ( const SALOME_Exception & ex)
350 //cp_fin(component,CP_ARRET);
351 SALOME::ExceptionStruct es;
352 es.text=CORBA::string_dup(ex.what());
353 es.type=SALOME::INTERNAL_ERROR;
354 throw SALOME::SALOME_Exception(es);
356 catch ( const SALOME::SALOME_Exception & ex)
358 //cp_fin(component,CP_ARRET);
361 catch ( const std::exception& ex)
363 //std::cerr << typeid(ex).name() << std::endl;
364 SALOME::ExceptionStruct es;
365 es.text=CORBA::string_dup(ex.what());
366 es.type=SALOME::INTERNAL_ERROR;
367 throw SALOME::SALOME_Exception(es);
371 std::cerr << "unknown exception" << std::endl;
375 //cp_fin(component,CP_ARRET);
376 SALOME::ExceptionStruct es;
377 es.text=CORBA::string_dup(" unknown exception");
378 es.type=SALOME::INTERNAL_ERROR;
379 throw SALOME::SALOME_Exception(es);
381 endService("${component}_i::${service}");
385 cxxService=Template(cxxService)
388 if (s_name == "${service}")
392 //initialization CALCIUM ports IN
394 //initialization CALCIUM ports OUT
397 catch(const PortAlreadyDefined& ex)
399 std::cerr << "${component}: " << ex.what() << std::endl;
400 //Ports already created : we use them
404 std::cerr << "${component}: unknown exception" << std::endl;
409 initService=Template(initService)
413 export SALOME_CONTAINER=$$1
414 export SALOME_CONTAINERNAME=$$2
415 export SALOME_INSTANCE=$$3
419 exeCPP=Template(exeCPP)
423 # CMakeLists.txt in src/<component>
424 # template parameters:
425 # module : module name
426 # component : component name
427 # componentlib : name of the target library
428 # includes : additional headers, separated by spaces or \n. can be empty
429 # sources : additional sources, separated by spaces or \n. can be empty
430 # libs : additional libraries
431 # find_libs : find_library commands
432 # target_properties : set_target_properties commands
433 cmake_src_compo_cpp = """
435 # additional include directories
437 $${KERNEL_INCLUDE_DIRS}
438 $${OMNIORB_INCLUDE_DIR}
439 $${PROJECT_BINARY_DIR}
440 $${PROJECT_BINARY_DIR}/idl
444 # --- definitions ---
446 $${OMNIORB_DEFINITIONS}
449 # find additional libraries
452 # libraries to link to
454 $${OMNIORB_LIBRARIES}
455 $${KERNEL_SalomeIDLKernel}
457 $${KERNEL_SalomeContainer}
464 # header files / no moc processing
466 SET(${module}_HEADERS
473 SET(${module}_SOURCES
480 ADD_LIBRARY(${componentlib} $${${module}_SOURCES})
481 TARGET_LINK_LIBRARIES(${componentlib} $${_link_LIBRARIES} )
483 INSTALL(TARGETS ${componentlib} EXPORT $${PROJECT_NAME}TargetGroup DESTINATION $${SALOME_INSTALL_LIBS})
485 INSTALL(FILES $${${module}_HEADERS} DESTINATION $${SALOME_INSTALL_HEADERS})
487 cmake_src_compo_cpp = Template(cmake_src_compo_cpp)