Salome HOME
2af83eedbcf9e1e8bbe1d3f8c539fd3f9cc22e11
[modules/yacs.git] / src / engine / ObserverAsPlugin.cxx
1 // Copyright (C) 2018-2024  CEA, EDF
2 //
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.
7 //
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include "ObserverAsPlugin.hxx"
21 #include "Dispatcher.hxx"
22 #include "Exception.hxx"
23
24 #include <sstream>
25
26 #ifndef WIN32
27 #include <dlfcn.h>
28 #include <stdlib.h>
29 #endif
30
31 void *HandleOnLoadedPlugin=nullptr;
32 void (*DefineCustomObservers)(YACS::ENGINE::Dispatcher *, YACS::ENGINE::ComposedNode *, YACS::ENGINE::Executor *)=nullptr;
33 void (*CleanUpObservers) ()=nullptr;
34
35 void YACS::ENGINE::LoadObserversPluginIfAny(ComposedNode *rootNode, Executor *executor)
36 {
37   static const char SYMBOLE_NAME_1[]="DefineCustomObservers";
38   static const char SYMBOLE_NAME_2[]="CleanUpObservers";
39 #ifndef WIN32
40   Dispatcher *disp(Dispatcher::getDispatcher());
41   if(!disp)
42     throw YACS::Exception("Internal error ! No dispatcher !");
43   char *yacsDriverPluginPath(getenv("YACS_DRIVER_PLUGIN_PATH"));
44   if(!yacsDriverPluginPath)
45     return ;
46   void *handle(dlopen(yacsDriverPluginPath, RTLD_LAZY | RTLD_GLOBAL));
47   if(!handle)
48     {
49       std::string message(dlerror());
50       std::ostringstream oss; oss << "Error during load of \"" << yacsDriverPluginPath << "\" defined by the YACS_DRIVER_PLUGIN_PATH env var : " << message;
51       throw YACS::Exception(oss.str());
52     }
53   DefineCustomObservers=(void (*)(YACS::ENGINE::Dispatcher *, YACS::ENGINE::ComposedNode *, YACS::ENGINE::Executor *))(dlsym(handle,SYMBOLE_NAME_1));
54   if(!DefineCustomObservers)
55     {
56       std::ostringstream oss; oss << "Error during load of \"" << yacsDriverPluginPath << "\" ! Library has been correctly loaded but symbol " << SYMBOLE_NAME_1 << " does not exists !";
57       throw YACS::Exception(oss.str());
58     }
59   CleanUpObservers=(void (*)())(dlsym(handle,SYMBOLE_NAME_2));
60   if(!CleanUpObservers)
61     {
62       std::ostringstream oss; oss << "Error during load of \"" << yacsDriverPluginPath << "\" ! Library has been correctly loaded but symbol " << SYMBOLE_NAME_2 << " does not exists !";
63       throw YACS::Exception(oss.str());
64     }
65   HandleOnLoadedPlugin=handle;
66   DefineCustomObservers(disp,rootNode,executor);
67 #endif
68 }
69
70 void YACS::ENGINE::UnLoadObserversPluginIfAny()
71 {
72 #ifndef WIN32
73   if(HandleOnLoadedPlugin)
74     {
75       CleanUpObservers();
76       dlclose(HandleOnLoadedPlugin);
77     }
78 #endif
79 }