changeset 894:690aeb4cb899 plugins

REST callbacks
author Sebastien Jodogne <s.jodogne@gmail.com>
date Mon, 16 Jun 2014 17:31:09 +0200
parents f57802f8b4dc
children 7e8cde5905fd
files Plugins/Engine/PluginsManager.cpp Plugins/Engine/PluginsManager.h Plugins/OrthancCPlugin/OrthancCPlugin.h Resources/Samples/Plugins/Basic/CMakeLists.txt Resources/Samples/Plugins/Basic/Plugin.c UnitTestsSources/PluginsTests.cpp
diffstat 6 files changed, 130 insertions(+), 20 deletions(-) [+]
line wrap: on
line diff
--- a/Plugins/Engine/PluginsManager.cpp	Mon Jun 16 16:14:56 2014 +0200
+++ b/Plugins/Engine/PluginsManager.cpp	Mon Jun 16 17:31:09 2014 +0200
@@ -32,6 +32,8 @@
 
 #include "PluginsManager.h"
 
+#include "../../Core/Toolbox.h"
+
 #include <glog/logging.h>
 #include <cassert>
 #include <memory>
@@ -144,20 +146,70 @@
     LOG(INFO) << str;
   }
 
-  static int32_t InvokeService(const char* serviceName,
-                               const void* serviceParameters)
+  void PluginsManager::RegisterRestCallback(const OrthancPluginContext* context,
+                                            const char* path, 
+                                            OrthancRestCallback callback)
   {
     // TODO
-    return 0;
+    LOG(INFO) << "Plugin has registered a REST callback on: " << path;
+
+    PluginsManager* manager = reinterpret_cast<PluginsManager*>(context->pimpl);
+    manager->restCallbacks_.push_back(callback);
+
+
+    const char* pp = "/hello/world";
+
+    UriComponents components;
+    Toolbox::SplitUriComponents(components, pp);
+
+    OrthancRestUrl url;
+    url.path = pp;
+
+    std::vector<const char*> c(components.size());
+    for (unsigned int i = 0; i < components.size(); i++)
+    {
+      c[i] = components[i].c_str();
+    }
+
+    if (components.size() == 0)
+    {
+      url.components = NULL;
+      url.componentsSize = 0;
+    }
+    else
+    {
+      url.components = &c[0];
+      url.componentsSize = components.size();
+    }
+
+    // TODO
+    url.parameters = NULL;
+    url.parametersSize = 0;
+
+    callback(NULL, OrthancHttpMethod_Get, &url, NULL, 0);
   }
 
+
+  static void AnswerBuffer(OrthancRestOutput* output,
+                           const char* answer,
+                           uint32_t answerSize,
+                           const char* mimeType)
+  {
+    std::cout << "MIME " << mimeType << ": " << answer << std::endl;
+  }
+
+
   PluginsManager::PluginsManager()
   {
+    memset(&context_, 0, sizeof(context_));
+    context_.pimpl = this;
     context_.orthancVersion = ORTHANC_VERSION;
-    context_.InvokeService = InvokeService;
+    context_.FreeBuffer = ::free;
     context_.LogError = LogError;
     context_.LogWarning = LogWarning;
     context_.LogInfo = LogInfo;
+    context_.RegisterRestCallback = RegisterRestCallback;
+    context_.AnswerBuffer = AnswerBuffer;
   }
 
   PluginsManager::~PluginsManager()
--- a/Plugins/Engine/PluginsManager.h	Mon Jun 16 16:14:56 2014 +0200
+++ b/Plugins/Engine/PluginsManager.h	Mon Jun 16 17:31:09 2014 +0200
@@ -36,6 +36,7 @@
 #include "../OrthancCPlugin/OrthancCPlugin.h"
 
 #include <map>
+#include <list>
 
 namespace Orthanc
 {
@@ -43,9 +44,15 @@
   {
   private:
     typedef std::map<std::string, SharedLibrary*>  Plugins;
+    typedef std::list<OrthancRestCallback>  RestCallbacks;
 
     OrthancPluginContext  context_;
     Plugins  plugins_;
+    RestCallbacks  restCallbacks_;
+
+    static void RegisterRestCallback(const OrthancPluginContext* context,
+                                     const char* path, 
+                                     OrthancRestCallback callback);
 
   public:
     PluginsManager();
--- a/Plugins/OrthancCPlugin/OrthancCPlugin.h	Mon Jun 16 16:14:56 2014 +0200
+++ b/Plugins/OrthancCPlugin/OrthancCPlugin.h	Mon Jun 16 17:31:09 2014 +0200
@@ -55,30 +55,61 @@
 {
 #endif
 
-  typedef void (*OrthancPluginLogError) (const char* str);
+  typedef struct OrthancRestOutput_t OrthancRestOutput;
+
+  typedef enum
+  {
+    OrthancHttpMethod_Get = 1,
+    OrthancHttpMethod_Post = 2,
+    OrthancHttpMethod_Put = 3,
+    OrthancHttpMethod_Delete = 4
+  } OrthancHttpMethod;
 
-  typedef void (*OrthancPluginLogWarning) (const char* str);
+  typedef struct OrthancRestUrl_t
+  {
+    const char* path;
+    const char* const* components;
+    uint32_t componentsSize;
+    const char* const* parameters;
+    uint32_t parametersSize;                                          
+  } OrthancRestUrl;
 
-  typedef void (*OrthancPluginLogInfo) (const char* str);
 
   typedef int32_t (*OrthancPluginService) (const char* serviceName,
                                            const void* serviceParameters);
 
-  typedef struct OrthancPluginContext
+  typedef int32_t (*OrthancRestCallback) (OrthancRestOutput* output,
+                                          OrthancHttpMethod method,
+                                          const OrthancRestUrl* url,
+                                          const char* body,
+                                          uint32_t bodySize);
+
+  typedef struct OrthancPluginContext_t
   {
+    void* pimpl;
+
     const char* orthancVersion;
-    OrthancPluginService InvokeService;
-    OrthancPluginLogError LogError;
-    OrthancPluginLogWarning LogWarning;
-    OrthancPluginLogInfo LogInfo;
+    void (*FreeBuffer) (void* buffer);
+
+    /* Logging functions */
+    void (*LogError) (const char* str);
+    void (*LogWarning) (const char* str);
+    void (*LogInfo) (const char* str);
 
-    /* TODO REGISTER */
+    /* REST API */
+    void (*RegisterRestCallback) (const struct OrthancPluginContext_t* context,
+                                  const char* path, 
+                                  OrthancRestCallback callback);
 
+    void (*AnswerBuffer) (OrthancRestOutput* output,
+                          const char* answer,
+                          uint32_t answerSize,
+                          const char* mimeType);
   } OrthancPluginContext;
 
 
   /**
-     Each plugin must define 4 functions, whose signature is:
+     Each plugin must define 4 functions, whose signature are:
      - int32_t OrthancPluginInitialize(const OrthancPluginContext*);
      - void OrthancPluginFinalize();
      - const char* OrthancPluginGetName();
--- a/Resources/Samples/Plugins/Basic/CMakeLists.txt	Mon Jun 16 16:14:56 2014 +0200
+++ b/Resources/Samples/Plugins/Basic/CMakeLists.txt	Mon Jun 16 17:31:09 2014 +0200
@@ -2,6 +2,11 @@
 
 project(Basic)
 
+if (${CMAKE_COMPILER_IS_GNUCXX})
+  set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -pedantic -Werror")
+  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -pedantic -Werror")
+endif()
+
 add_library(PluginTest SHARED Plugin.c)
 
 if (${CMAKE_SYSTEM_NAME} STREQUAL "Linux")
--- a/Resources/Samples/Plugins/Basic/Plugin.c	Mon Jun 16 16:14:56 2014 +0200
+++ b/Resources/Samples/Plugins/Basic/Plugin.c	Mon Jun 16 17:31:09 2014 +0200
@@ -33,15 +33,31 @@
 static const OrthancPluginContext* context = NULL;
 
 
+ORTHANC_PLUGINS_API int32_t Callback(OrthancRestOutput* output,
+                                     OrthancHttpMethod method,
+                                     const OrthancRestUrl* url,
+                                     const char* body,
+                                     uint32_t bodySize)
+{
+  char buffer[1024];
+  sprintf(buffer, "Callback on URL [%s]\n", url->path);
+  context->LogInfo(buffer);
+  context->AnswerBuffer(output, buffer, strlen(buffer), "text/plain");
+  return 1;
+}
+
+
 ORTHANC_PLUGINS_API int32_t OrthancPluginInitialize(const OrthancPluginContext* c)
 {
+  char info[1024];
+
   context = c;
   context->LogWarning("Plugin is initializing");
 
-  char info[1024];
   sprintf(info, "The version of Orthanc is '%s'", context->orthancVersion);
+  context->LogInfo(info);
 
-  context->LogInfo(info);
+  context->RegisterRestCallback(c, "/plugin/hello", Callback);
 
   return 0;
 }
--- a/UnitTestsSources/PluginsTests.cpp	Mon Jun 16 16:14:56 2014 +0200
+++ b/UnitTestsSources/PluginsTests.cpp	Mon Jun 16 17:31:09 2014 +0200
@@ -58,7 +58,6 @@
 #else
 #error Support your platform here
 #endif
-
 }
 
 
@@ -70,10 +69,10 @@
 //#error Support your platform here
 
 #elif defined(__linux)
-  //manager.RegisterPlugin("./libPluginTest.so");
-  //ASSERT_THROW(manager.RegisterPlugin("./libPluginTest.so"), OrthancException);
+  manager.RegisterPlugin("./libPluginTest.so");
+  ASSERT_THROW(manager.RegisterPlugin("./libPluginTest.so"), OrthancException);
 
-  manager.ScanFolderForPlugins(".", true);
+  //manager.ScanFolderForPlugins(".", true);
 
 #else
 #error Support your platform here