From 0b88876b0a0ee5eae29cab3882cb1eda6da4dad0 Mon Sep 17 00:00:00 2001 From: Anthony Geay Date: Wed, 5 Sep 2018 16:53:06 +0200 Subject: [PATCH] Observer using plugins through python --- src/engine/CMakeLists.txt | 2 + src/engine/ObserverAsPlugin.cxx | 79 +++++++++++++++++++++++++++++++++ src/engine/ObserverAsPlugin.hxx | 32 +++++++++++++ src/engine_swig/pilot.i | 15 +++++++ src/yacsloader/driver.cxx | 61 ++----------------------- 5 files changed, 131 insertions(+), 58 deletions(-) create mode 100644 src/engine/ObserverAsPlugin.cxx create mode 100644 src/engine/ObserverAsPlugin.hxx diff --git a/src/engine/CMakeLists.txt b/src/engine/CMakeLists.txt index 3e8442219..388bfea5f 100644 --- a/src/engine/CMakeLists.txt +++ b/src/engine/CMakeLists.txt @@ -108,6 +108,7 @@ SET(YACSlibEngine_HEADERS ElementaryPoint.hxx SetOfPoints.hxx PlayGround.hxx + ObserverAsPlugin.hxx ) # --- sources --- @@ -183,6 +184,7 @@ SET(YACSlibEngine_SOURCES SetOfPoints.cxx PlayGround.cxx ComplexWeight.cxx + ObserverAsPlugin.cxx ) SET(YACSlibEngine_HEADERS ${YACSlibEngine_HEADERS} PARENT_SCOPE) # Make it visible to src/engine_swig to handle dependencies diff --git a/src/engine/ObserverAsPlugin.cxx b/src/engine/ObserverAsPlugin.cxx new file mode 100644 index 000000000..eb9dccfcb --- /dev/null +++ b/src/engine/ObserverAsPlugin.cxx @@ -0,0 +1,79 @@ +// Copyright (C) 2018 CEA/DEN, EDF R&D +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +// +// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com +// + +#include "ObserverAsPlugin.hxx" +#include "Dispatcher.hxx" +#include "Exception.hxx" + +#include + +#ifndef WIN32 +#include +#include +#endif + +void *HandleOnLoadedPlugin=nullptr; +void (*DefineCustomObservers)(YACS::ENGINE::Dispatcher *, YACS::ENGINE::ComposedNode *, YACS::ENGINE::Executor *)=nullptr; +void (*CleanUpObservers) ()=nullptr; + +void YACS::ENGINE::LoadObserversPluginIfAny(ComposedNode *rootNode, Executor *executor) +{ + static const char SYMBOLE_NAME_1[]="DefineCustomObservers"; + static const char SYMBOLE_NAME_2[]="CleanUpObservers"; +#ifndef WIN32 + Dispatcher *disp(Dispatcher::getDispatcher()); + if(!disp) + throw YACS::Exception("Internal error ! No dispatcher !"); + char *yacsDriverPluginPath(getenv("YACS_DRIVER_PLUGIN_PATH")); + if(!yacsDriverPluginPath) + return ; + void *handle(dlopen(yacsDriverPluginPath, RTLD_LAZY | RTLD_GLOBAL)); + if(!handle) + { + std::string message(dlerror()); + std::ostringstream oss; oss << "Error during load of \"" << yacsDriverPluginPath << "\" defined by the YACS_DRIVER_PLUGIN_PATH env var : " << message; + throw YACS::Exception(oss.str()); + } + DefineCustomObservers=(void (*)(YACS::ENGINE::Dispatcher *, YACS::ENGINE::ComposedNode *, YACS::ENGINE::Executor *))(dlsym(handle,SYMBOLE_NAME_1)); + if(!DefineCustomObservers) + { + std::ostringstream oss; oss << "Error during load of \"" << yacsDriverPluginPath << "\" ! Library has been correctly loaded but symbol " << SYMBOLE_NAME_1 << " does not exists !"; + throw YACS::Exception(oss.str()); + } + CleanUpObservers=(void (*)())(dlsym(handle,SYMBOLE_NAME_2)); + if(!CleanUpObservers) + { + std::ostringstream oss; oss << "Error during load of \"" << yacsDriverPluginPath << "\" ! Library has been correctly loaded but symbol " << SYMBOLE_NAME_2 << " does not exists !"; + throw YACS::Exception(oss.str()); + } + HandleOnLoadedPlugin=handle; + DefineCustomObservers(disp,rootNode,executor); +#endif +} + +void YACS::ENGINE::UnLoadObserversPluginIfAny() +{ +#ifndef WIN32 + if(HandleOnLoadedPlugin) + { + CleanUpObservers(); + dlclose(HandleOnLoadedPlugin); + } +#endif +} diff --git a/src/engine/ObserverAsPlugin.hxx b/src/engine/ObserverAsPlugin.hxx new file mode 100644 index 000000000..1e65d1b34 --- /dev/null +++ b/src/engine/ObserverAsPlugin.hxx @@ -0,0 +1,32 @@ +// Copyright (C) 2018 CEA/DEN, EDF R&D +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +// +// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com +// + +#pragma once + +namespace YACS +{ + namespace ENGINE + { + class ComposedNode; + class Executor; + + void LoadObserversPluginIfAny(ComposedNode *rootNode, Executor *executor); + void UnLoadObserversPluginIfAny(); + } +} diff --git a/src/engine_swig/pilot.i b/src/engine_swig/pilot.i index 642fb3518..c2c324fe7 100644 --- a/src/engine_swig/pilot.i +++ b/src/engine_swig/pilot.i @@ -64,6 +64,7 @@ #include "ForkBlocPoint.hxx" #include "LinkedBlocPoint.hxx" #include "ElementaryPoint.hxx" +#include "ObserverAsPlugin.hxx" using namespace YACS::ENGINE; @@ -645,3 +646,17 @@ namespace YACS } } +%rename(LoadObserversPluginIfAny) LoadObserversPluginIfAnySwig; +%rename(UnLoadObserversPluginIfAny) UnLoadObserversPluginIfAnySwig; + +%inline{ + void LoadObserversPluginIfAnySwig(YACS::ENGINE::ComposedNode *rootNode, YACS::ENGINE::ExecutorSwig *executor) + { + YACS::ENGINE::LoadObserversPluginIfAny(rootNode,executor); + } + + void UnLoadObserversPluginIfAnySwig() + { + YACS::ENGINE::UnLoadObserversPluginIfAny(); + } +} diff --git a/src/yacsloader/driver.cxx b/src/yacsloader/driver.cxx index a26298375..2671ddd3b 100644 --- a/src/yacsloader/driver.cxx +++ b/src/yacsloader/driver.cxx @@ -29,6 +29,7 @@ #include "LoadState.hxx" #include "Dispatcher.hxx" #include "LinkInfo.hxx" +#include "ObserverAsPlugin.hxx" #ifdef SALOME_KERNEL #include "SALOME_NamingService.hxx" @@ -101,62 +102,6 @@ typedef struct { string lockFile; } thread_st; -#ifndef WIN32 -#include -#include -#endif - -std::string LoadedDriverPluginLibrary; -void *HandleOnLoadedPlugin=0; -void (*DefineCustomObservers)(YACS::ENGINE::Dispatcher *, YACS::ENGINE::ComposedNode *, YACS::ENGINE::Executor *)=0; -void (*CleanUpObservers) ()=0; - -void LoadObserversPluginIfAny(YACS::ENGINE::ComposedNode *rootNode, YACS::ENGINE::Executor *executor) -{ - static const char SYMBOLE_NAME_1[]="DefineCustomObservers"; - static const char SYMBOLE_NAME_2[]="CleanUpObservers"; -#ifndef WIN32 - Dispatcher *disp(Dispatcher::getDispatcher()); - if(!disp) - throw YACS::Exception("Internal error ! No dispatcher !"); - char *yacsDriverPluginPath(getenv("YACS_DRIVER_PLUGIN_PATH")); - if(!yacsDriverPluginPath) - return ; - void *handle(dlopen(yacsDriverPluginPath, RTLD_LAZY | RTLD_GLOBAL)); - if(!handle) - { - std::string message(dlerror()); - std::ostringstream oss; oss << "Error during load of \"" << yacsDriverPluginPath << "\" defined by the YACS_DRIVER_PLUGIN_PATH env var : " << message; - throw YACS::Exception(oss.str()); - } - DefineCustomObservers=(void (*)(YACS::ENGINE::Dispatcher *, YACS::ENGINE::ComposedNode *, YACS::ENGINE::Executor *))(dlsym(handle,SYMBOLE_NAME_1)); - if(!DefineCustomObservers) - { - std::ostringstream oss; oss << "Error during load of \"" << yacsDriverPluginPath << "\" ! Library has been correctly loaded but symbol " << SYMBOLE_NAME_1 << " does not exists !"; - throw YACS::Exception(oss.str()); - } - CleanUpObservers=(void (*)())(dlsym(handle,SYMBOLE_NAME_2)); - if(!CleanUpObservers) - { - std::ostringstream oss; oss << "Error during load of \"" << yacsDriverPluginPath << "\" ! Library has been correctly loaded but symbol " << SYMBOLE_NAME_2 << " does not exists !"; - throw YACS::Exception(oss.str()); - } - HandleOnLoadedPlugin=handle; - DefineCustomObservers(disp,rootNode,executor); -#endif -} - -void UnLoadObserversPluginIfAny() -{ -#ifndef WIN32 - if(HandleOnLoadedPlugin) - { - CleanUpObservers(); - dlclose(HandleOnLoadedPlugin); - } -#endif -} - #if defined WIN32 || defined __APPLE__ static int #else @@ -565,7 +510,7 @@ int main (int argc, char* argv[]) st->lockFile = rootFile + ".lock"; pthread_create(&th,NULL,&dumpState,(void*)st); } - LoadObserversPluginIfAny(p,&executor); + YACS::ENGINE::LoadObserversPluginIfAny(p,&executor); cerr << "+++++++++++++++++++ start calculation +++++++++++++++++++" << endl; executor.RunW(p,myArgs.display, fromScratch); cerr << "+++++++++++++++++++ end calculation +++++++++++++++++++" << endl; @@ -615,7 +560,7 @@ int main (int argc, char* argv[]) r->fini(); delete r; delete disp; - UnLoadObserversPluginIfAny(); + YACS::ENGINE::UnLoadObserversPluginIfAny(); return return_value; } catch (YACS::Exception& e) -- 2.39.2