1 // Copyright (C) 2006-2014 CEA/DEN, EDF R&D
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
24 #define dlopen LoadLibrary
25 #define dlclose FreeLibrary
26 #define dlsym GetProcAddress
31 #include "CppContainer.hxx"
32 #include "CppComponent.hxx"
33 #include "Exception.hxx"
38 using namespace YACS::ENGINE;
41 #include "YacsTrace.hxx"
43 //=============================================================================
45 * Local C++ container class constructor
47 //=============================================================================
49 CppContainer::CppContainer() : _trueCont(0L)
51 DEBTRACE("CppContainer::CppContainer()");
55 //=============================================================================
57 * Local C++ container class destructor
59 //=============================================================================
61 CppContainer::~CppContainer()
63 DEBTRACE("CppContainer::~CppContainer()");
66 void CppContainer::lock()
71 void CppContainer::unLock()
76 bool CppContainer::isAlreadyStarted(const ComponentInstance *inst) const
78 return NULL != _trueCont;
81 void CppContainer::start(const ComponentInstance *inst) throw (YACS::Exception)
83 _trueCont = LocalContainer::get();
86 Container *CppContainer::clone() const
88 if(_isAttachedOnCloning)
91 return (Container*) (this);
94 return new CppContainer(*this);
97 Container *CppContainer::cloneAlways() const
99 return new CppContainer(*this);
102 bool CppContainer::loadComponentLibrary(const std::string & componentName) throw (YACS::Exception)
106 LocalLibrary L = _trueCont->loadComponentLibrary(componentName);
111 std::string mesg = "CppContainer not started";
112 throw YACS::Exception(mesg);
117 CppComponent * CppContainer::createComponentInstance(const std::string & componentName, int /* studyID */)
119 DEBTRACE("CppContainer::createComponentInstance");
121 return _trueCont->createComponentInstance(componentName.c_str());
124 std::string mesg = "CppContainer not started";
125 throw YACS::Exception(mesg);
129 void CppContainer::createInternalInstance(const std::string & name, void *&obj,
130 RunFunction &r, TerminateFunction &t)
132 DEBTRACE("CppContainer::createInternalInstance");
134 _trueCont->createInternalInstance(name.c_str(), obj, r, t);
137 std::string mesg = "CppContainer not started";
138 throw YACS::Exception(mesg);
142 void CppContainer::unregisterComponentInstance(CppComponent * C)
146 _trueCont->unregisterComponentInstance(C);
151 std::string CppContainer::getPlacementId(const ComponentInstance *inst) const
156 std::string CppContainer::getFullPlacementId(const ComponentInstance *inst) const
161 void CppContainer::checkCapabilityToDealWith(const ComponentInstance *inst) const throw(YACS::Exception)
163 if(inst->getKind()!=CppComponent::KIND)
164 throw Exception("CppContainer::checkCapabilityToDealWith : CppContainer is not able to deal with this type of ComponentInstance.");
167 std::map<std::string, LocalLibrary> LocalContainer::_library_map; // libraries, loaded
168 std::multimap<std::string, CppComponent *> LocalContainer::_instance_map;
170 LocalContainer * LocalContainer::_singleton = NULL;
172 LocalContainer::LocalContainer()
176 LocalContainer::~LocalContainer()
181 LocalContainer * LocalContainer::get()
183 if (NULL == _singleton)
185 _singleton = new LocalContainer;
190 void LocalContainer::destroy()
192 if (NULL == _singleton)
195 // destroy all component instances
196 _instance_mapMutex.lock(); // lock
197 std::multimap<std::string, CppComponent *>::iterator iI, iJ;
198 for (iI=_instance_map.begin(); iI != _instance_map.end(); iI = iJ)
201 iI->second->setContainer(NULL);
204 _instance_map.clear();
205 _instance_mapMutex.unlock(); // unlock
207 // unload all dynamic libraries
208 _library_mapMutex.lock();
209 std::map<std::string, LocalLibrary>::iterator iL;
210 for (iL=_library_map.begin(); iL != _library_map.end(); iL++)
211 dlclose(iL->second.handle);
212 _library_map.clear();
213 _library_mapMutex.unlock();
220 //=============================================================================
221 //! Find or create a new C++ component instance
223 * Create a new component class (C++ implementation)
224 * \param name : component name
226 * Try to return a handle to an new instance of a C++ component
227 * If the associated library is not loaded, try to load it
229 * \return a handle to the component instance or throw an exception
230 * if it's not possible
232 //=============================================================================
233 CppComponent * LocalContainer::createComponentInstance(const char * name)
239 createInternalInstance(name, o, r, t);
242 C = new CppComponent(o, r, t, name);
243 _instance_mapMutex.lock(); // lock to be alone
244 _instance_map.insert(std::pair<std::string, CppComponent *>(name, C));
245 _instance_mapMutex.unlock(); // unlock
249 void LocalContainer::createInternalInstance(const char *name, void *&obj,
250 RunFunction &r, TerminateFunction &t)
254 std::map<std::string, LocalLibrary>::iterator foundL = _library_map.find(name);
255 if (foundL != _library_map.end())
258 L = loadComponentLibrary(name, NULL, false);
261 InitFunction i = L.initHandle;
262 t = L.terminateHandle;
264 PingFunction p = L.pingHandle;
271 void LocalContainer::unregisterComponentInstance(CppComponent * C)
273 _instance_mapMutex.lock(); // lock to be alone
274 _instance_map.erase(C->getCompoName());
275 _instance_mapMutex.unlock(); // unlock
278 inline void toupper (std::string & s)
280 transform (s.begin (), s.end (), s.begin (), (int(*)(int)) toupper);
283 LocalLibrary LocalContainer::loadComponentLibrary(const std::string & aCompName, const char * prefix, bool forcedLoad)
286 // if forcedLoad is true, unload library if it exists
287 // if forcedLoad is false, return the existing library or load it
290 unLoadComponentLibrary(aCompName);
293 std::map<std::string, LocalLibrary >::iterator itLib
294 = _library_map.find(aCompName);
295 if (itLib != _library_map.end()) return itLib->second;
298 // --- try dlopen C++ component
305 std::string s = aCompName + "_ROOT_DIR";
307 const char * t = getenv(s.c_str());
312 sprefix += "/lib/salome";
317 std::string impl_name = std::string ("lib") + aCompName + std::string("Local.so");
319 impl_name = sprefix + std::string("/") + impl_name;
321 std::string impl_name = aCompName + std::string("Local.dll");
322 impl_name = sprefix + std::string("\\") + impl_name;
324 DEBTRACE("impl_name = " << impl_name);
328 handle = dlopen( impl_name.c_str() ) ;
331 handle = dlopen( impl_name.c_str() , RTLD_LAZY ) ;
336 sError = "Not available here !";
343 if ((sError = dlerror()) || !handle)
346 std::stringstream msg;
347 msg << "Can't load shared library : " << impl_name
348 << " (dlopen error : " << sError << ") at "
349 << __FILE__ << ":" << __LINE__;
350 throw YACS::Exception(msg.str());
353 void *ihandle, *rhandle, *phandle = NULL, *thandle = NULL;
355 ihandle = dlsym(handle, "__init");
359 if (sError = dlerror())
363 std::stringstream msg;
364 msg << "Library " << impl_name
365 << " doesn't contains initialization function (" << sError << ") at "
366 << __FILE__ << ":" << __LINE__;
367 throw YACS::Exception(msg.str());
370 rhandle = dlsym(handle, "__run");
374 if (sError = dlerror())
378 std::stringstream msg;
379 msg << "Library " << impl_name
380 << " doesn't contains main switch function (" << sError << ") at "
381 << __FILE__ << ":" << __LINE__;
382 throw YACS::Exception(msg.str());
385 thandle = dlsym(handle, "__terminate");
389 if (sError = dlerror())
393 std::stringstream msg;
394 msg << "Library " << impl_name
395 << " doesn't contains terminate function (" << sError << ") at "
396 << __FILE__ << ":" << __LINE__;
397 throw YACS::Exception(msg.str());
399 phandle = dlsym(handle, "__ping");
401 _library_map[aCompName] = LocalLibrary(handle, (InitFunction) ihandle,
402 (RunFunction) rhandle,
403 (PingFunction) phandle,
404 (TerminateFunction) thandle);
405 return _library_map[aCompName];
408 void LocalContainer::unLoadComponentLibrary(const std::string & aCompName)
410 std::map<std::string, LocalLibrary >::iterator itLib
411 = _library_map.find(aCompName);
413 if (itLib == _library_map.end()) return;
415 dlclose(itLib->second.handle);
416 _library_map.erase(itLib);