changeset 893:f57802f8b4dc plugins

plugins for windows
author Sebastien Jodogne <s.jodogne@gmail.com>
date Mon, 16 Jun 2014 16:14:56 +0200
parents 517e28b420af
children 690aeb4cb899
files OrthancServer/main.cpp Plugins/Engine/PluginsManager.cpp Plugins/Engine/SharedLibrary.cpp Plugins/Engine/SharedLibrary.h Resources/Configuration.json UnitTestsSources/PluginsTests.cpp
diffstat 6 files changed, 85 insertions(+), 28 deletions(-) [+]
line wrap: on
line diff
--- a/OrthancServer/main.cpp	Mon Jun 16 15:41:13 2014 +0200
+++ b/OrthancServer/main.cpp	Mon Jun 16 16:14:56 2014 +0200
@@ -48,6 +48,7 @@
 #include "OrthancFindRequestHandler.h"
 #include "OrthancMoveRequestHandler.h"
 #include "ServerToolbox.h"
+#include "../Plugins/Engine/PluginsManager.h"
 
 using namespace Orthanc;
 
@@ -204,7 +205,7 @@
 };
 
 
-void PrintHelp(char* path)
+static void PrintHelp(char* path)
 {
   std::cout 
     << "Usage: " << path << " [OPTION]... [CONFIGURATION]" << std::endl
@@ -231,7 +232,7 @@
 }
 
 
-void PrintVersion(char* path)
+static void PrintVersion(char* path)
 {
   std::cout
     << path << " " << ORTHANC_VERSION << std::endl
@@ -244,6 +245,39 @@
 }
 
 
+
+static void LoadLuaScripts(ServerContext& context)
+{
+  std::list<std::string> luaScripts;
+  Configuration::GetGlobalListOfStringsParameter(luaScripts, "LuaScripts");
+  for (std::list<std::string>::const_iterator
+         it = luaScripts.begin(); it != luaScripts.end(); ++it)
+  {
+    std::string path = Configuration::InterpretStringParameterAsPath(*it);
+    LOG(WARNING) << "Installing the Lua scripts from: " << path;
+    std::string script;
+    Toolbox::ReadFile(script, path);
+    context.GetLuaContext().Execute(script);
+  }
+}
+
+
+static void LoadPlugins(PluginsManager& pluginsManager)
+{
+  std::list<std::string> plugins;
+  Configuration::GetGlobalListOfStringsParameter(plugins, "Plugins");
+  for (std::list<std::string>::const_iterator
+         it = plugins.begin(); it != plugins.end(); ++it)
+  {
+    std::string path = Configuration::InterpretStringParameterAsPath(*it);
+    LOG(WARNING) << "Registering a plugin from: " << path;
+    pluginsManager.RegisterPlugin(path);
+  }  
+}
+
+
+
+
 int main(int argc, char* argv[]) 
 {
   // Initialize Google's logging library.
@@ -337,18 +371,10 @@
     context.SetCompressionEnabled(Configuration::GetGlobalBoolParameter("StorageCompression", false));
     context.SetStoreMD5ForAttachments(Configuration::GetGlobalBoolParameter("StoreMD5ForAttachments", true));
 
-    std::list<std::string> luaScripts;
-    Configuration::GetGlobalListOfStringsParameter(luaScripts, "LuaScripts");
-    for (std::list<std::string>::const_iterator
-           it = luaScripts.begin(); it != luaScripts.end(); ++it)
-    {
-      std::string path = Configuration::InterpretStringParameterAsPath(*it);
-      LOG(WARNING) << "Installing the Lua scripts from: " << path;
-      std::string script;
-      Toolbox::ReadFile(script, path);
-      context.GetLuaContext().Execute(script);
-    }
+    LoadLuaScripts(context);
 
+    PluginsManager pluginsManager;
+    LoadPlugins(pluginsManager);
 
     try
     {
--- a/Plugins/Engine/PluginsManager.cpp	Mon Jun 16 15:41:13 2014 +0200
+++ b/Plugins/Engine/PluginsManager.cpp	Mon Jun 16 16:14:56 2014 +0200
@@ -53,6 +53,9 @@
   {
     typedef int32_t (*Initialize) (const OrthancPluginContext*);
 
+#if defined(_WIN32)
+    Initialize initialize = (Initialize) plugin.GetFunction("OrthancPluginInitialize");
+#else
     /**
      * gcc would complain about "ISO C++ forbids casting between
      * pointer-to-function and pointer-to-object" without the trick
@@ -64,8 +67,9 @@
 
     Initialize initialize;
     *(void **) (&initialize) = plugin.GetFunction("OrthancPluginInitialize");
+#endif
+
     assert(initialize != NULL);
-
     int32_t error = initialize(&context);
 
     if (error != 0)
@@ -81,10 +85,14 @@
   {
     typedef void (*Finalize) ();
 
+#if defined(_WIN32)
+    Finalize finalize = (Finalize) plugin.GetFunction("OrthancPluginFinalize");
+#else
     Finalize finalize;
     *(void **) (&finalize) = plugin.GetFunction("OrthancPluginFinalize");
+#endif
+
     assert(finalize != NULL);
-
     finalize();
   }
 
@@ -93,10 +101,14 @@
   {
     typedef const char* (*GetName) ();
 
+#if defined(_WIN32)
+    GetName getName = (GetName) plugin.GetFunction("OrthancPluginGetName");
+#else
     GetName getName;
     *(void **) (&getName) = plugin.GetFunction("OrthancPluginGetName");
+#endif
+
     assert(getName != NULL);
-
     return getName();
   }
 
@@ -105,10 +117,14 @@
   {
     typedef const char* (*GetVersion) ();
 
+#if defined(_WIN32)
+    GetVersion getVersion = (GetVersion) plugin.GetFunction("OrthancPluginGetVersion");
+#else
     GetVersion getVersion;
     *(void **) (&getVersion) = plugin.GetFunction("OrthancPluginGetVersion");
+#endif
+
     assert(getVersion != NULL);
-
     return getVersion();
   }
 
@@ -150,6 +166,9 @@
     {
       if (it->second != NULL)
       {
+        LOG(WARNING) << "Unregistering plugin '" << CallGetName(*it->second)
+                     << "' (version " << CallGetVersion(*it->second) << ")";
+
         CallFinalize(*(it->second));
         delete it->second;
       }
--- a/Plugins/Engine/SharedLibrary.cpp	Mon Jun 16 15:41:13 2014 +0200
+++ b/Plugins/Engine/SharedLibrary.cpp	Mon Jun 16 16:14:56 2014 +0200
@@ -52,7 +52,7 @@
   {
 #if defined(_WIN32)
     handle_ = ::LoadLibraryA(path.c_str());
-    if (handle == NULL)
+    if (handle_ == NULL)
     {
       LOG(ERROR) << "LoadLibrary(" << path << ") failed: Error " << ::GetLastError();
       throw OrthancException(ErrorCode_SharedLibrary);
@@ -93,7 +93,7 @@
   }
 
 
-  void* SharedLibrary::GetFunctionInternal(const std::string& name)
+  SharedLibrary::FunctionPointer SharedLibrary::GetFunctionInternal(const std::string& name)
   {
     if (!handle_)
     {
@@ -110,9 +110,9 @@
   }
 
 
-  void* SharedLibrary::GetFunction(const std::string& name)
+  SharedLibrary::FunctionPointer SharedLibrary::GetFunction(const std::string& name)
   {
-    void* result = GetFunctionInternal(name);
+    SharedLibrary::FunctionPointer result = GetFunctionInternal(name);
   
     if (result == NULL)
     {
--- a/Plugins/Engine/SharedLibrary.h	Mon Jun 16 15:41:13 2014 +0200
+++ b/Plugins/Engine/SharedLibrary.h	Mon Jun 16 16:14:56 2014 +0200
@@ -40,11 +40,18 @@
 {
   class SharedLibrary : boost::noncopyable
   {
+  public:
+#if defined(_WIN32)
+    typedef FARPROC FunctionPointer;
+#else
+    typedef void* FunctionPointer;
+#endif
+
   private:
     std::string path_;
     void *handle_;
 
-    void* GetFunctionInternal(const std::string& name);
+    FunctionPointer GetFunctionInternal(const std::string& name);
 
   public:
     SharedLibrary(const std::string& path);
@@ -58,6 +65,6 @@
 
     bool HasFunction(const std::string& name);
 
-    void* GetFunction(const std::string& name);
+    FunctionPointer GetFunction(const std::string& name);
   };
 }
--- a/Resources/Configuration.json	Mon Jun 16 15:41:13 2014 +0200
+++ b/Resources/Configuration.json	Mon Jun 16 16:14:56 2014 +0200
@@ -28,13 +28,14 @@
   // of patients)
   "MaximumPatientCount" : 0,
   
-  // List of paths to the custom Lua scripts to load into this
-  // instance of Orthanc
+  // List of paths to the custom Lua scripts that are to be loaded
+  // into this instance of Orthanc
   "LuaScripts" : [
   ],
 
+  // List of paths to the plugins that are to be loaded into this
+  // instance of Orthanc
   "Plugins" : [
-    
   ],
 
 
--- a/UnitTestsSources/PluginsTests.cpp	Mon Jun 16 15:41:13 2014 +0200
+++ b/UnitTestsSources/PluginsTests.cpp	Mon Jun 16 16:14:56 2014 +0200
@@ -42,7 +42,11 @@
 TEST(SharedLibrary, Basic)
 {
 #if defined(_WIN32)
-#error Support your platform here
+  SharedLibrary l("kernel32.dll");
+  ASSERT_THROW(l.GetFunction("world"), OrthancException);
+  ASSERT_TRUE(l.GetFunction("GetVersionExW") != NULL);
+  ASSERT_TRUE(l.HasFunction("GetVersionExW"));
+  ASSERT_FALSE(l.HasFunction("world"));
 
 #elif defined(__linux)
   SharedLibrary l("libdl.so");
@@ -63,7 +67,7 @@
   PluginsManager manager;
 
 #if defined(_WIN32)
-#error Support your platform here
+//#error Support your platform here
 
 #elif defined(__linux)
   //manager.RegisterPlugin("./libPluginTest.so");