changeset 1136:208dc67b9bab

sample custom storage plugin
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 09 Sep 2014 16:47:04 +0200
parents 67c3c1e4a6e0
children d9c27f9f1a51
files Plugins/Engine/OrthancPlugins.cpp Plugins/OrthancCPlugin/OrthancCPlugin.h Plugins/Samples/StorageArea/CMakeLists.txt Plugins/Samples/StorageArea/Plugin.cpp
diffstat 4 files changed, 187 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/Plugins/Engine/OrthancPlugins.cpp	Tue Sep 09 15:55:43 2014 +0200
+++ b/Plugins/Engine/OrthancPlugins.cpp	Tue Sep 09 16:47:04 2014 +0200
@@ -817,6 +817,7 @@
           *reinterpret_cast<const _OrthancPluginRegisterStorageArea*>(parameters);
         
         pimpl_->storageArea_ = p;
+        pimpl_->hasStorageArea_ = true;
         return true;
       }
 
--- a/Plugins/OrthancCPlugin/OrthancCPlugin.h	Tue Sep 09 15:55:43 2014 +0200
+++ b/Plugins/OrthancCPlugin/OrthancCPlugin.h	Tue Sep 09 16:47:04 2014 +0200
@@ -1508,7 +1508,13 @@
     params.create_ = create;
     params.read_ = read;
     params.remove_ = remove;
+
+#ifdef  __cplusplus
     params.free_ = free;
+#else
+    params.free_ = ::free;
+#endif
+
     context->InvokeService(context, _OrthancPluginService_RegisterStorageArea, &params);
   }
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Plugins/Samples/StorageArea/CMakeLists.txt	Tue Sep 09 16:47:04 2014 +0200
@@ -0,0 +1,17 @@
+cmake_minimum_required(VERSION 2.8)
+
+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()
+
+include_directories(${CMAKE_SOURCE_DIR}/../../OrthancCPlugin/)
+add_library(PluginTest SHARED Plugin.cpp)
+
+if (${CMAKE_SYSTEM_NAME} STREQUAL "Linux")
+  # Linking with "pthread" is necessary, otherwise the software crashes
+  # http://sourceware.org/bugzilla/show_bug.cgi?id=10652#c17
+  target_link_libraries(PluginTest pthread dl)
+endif()
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Plugins/Samples/StorageArea/Plugin.cpp	Tue Sep 09 16:47:04 2014 +0200
@@ -0,0 +1,163 @@
+/**
+ * Orthanc - A Lightweight, RESTful DICOM Store
+ * Copyright (C) 2012-2014 Medical Physics Department, CHU of Liege,
+ * Belgium
+ *
+ * Permission is hereby granted, free of charge, to any person
+ * obtaining a copy of this software and associated documentation
+ * files (the "Software"), to deal in the Software without
+ * restriction, including without limitation the rights to use, copy,
+ * modify, merge, publish, distribute, sublicense, and/or sell copies
+ * of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ **/
+
+
+#include <OrthancCPlugin.h>
+
+#include <string.h>
+#include <stdio.h>
+#include <string>
+
+static OrthancPluginContext* context = NULL;
+
+
+static std::string GetPath(const char* uuid)
+{
+  return "plugin_" + std::string(uuid);
+}
+
+
+static int32_t StorageCreate(const char* uuid,
+                             const void* content,
+                             int64_t size,
+                             OrthancPluginContentType type)
+{
+  std::string path = GetPath(uuid);
+
+  FILE* fp = fopen(path.c_str(), "wb");
+  if (!fp)
+  {
+    return -1;
+  }
+
+  bool ok = fwrite(content, size, 1, fp) == 1;
+  fclose(fp);
+
+  return ok ? 0 : -1;
+}
+
+
+static int32_t StorageRead(void** content,
+                           int64_t* size,
+                           const char* uuid,
+                           OrthancPluginContentType type)
+{
+  std::string path = GetPath(uuid);
+
+  FILE* fp = fopen(path.c_str(), "rb");
+  if (!fp)
+  {
+    return -1;
+  }
+
+  if (fseek(fp, 0, SEEK_END) < 0)
+  {
+    fclose(fp);
+    return -1;
+  }
+
+  *size = ftell(fp);
+
+  if (fseek(fp, 0, SEEK_SET) < 0)
+  {
+    fclose(fp);
+    return -1;
+  }
+
+  bool ok = true;
+
+  if (*size == 0)
+  {
+    *content = NULL;
+  }
+  else
+  {
+    *content = malloc(*size);
+    if (content == NULL ||
+        fread(*content, *size, 1, fp) != 1)
+    {
+      ok = false;
+    }
+  }
+
+  fclose(fp);
+
+  return ok ? 0 : -1;  
+}
+
+
+static int32_t StorageRemove(const char* uuid,
+                             OrthancPluginContentType type)
+{
+  std::string path = GetPath(uuid);
+  return remove(path.c_str());
+}
+
+
+extern "C"
+{
+  ORTHANC_PLUGINS_API int32_t OrthancPluginInitialize(OrthancPluginContext* c)
+  {
+    char info[1024];
+
+    context = c;
+    OrthancPluginLogWarning(context, "Storage plugin is initializing");
+
+    /* Check the version of the Orthanc core */
+    if (OrthancPluginCheckVersion(c) == 0)
+    {
+      sprintf(info, "Your version of Orthanc (%s) must be above %d.%d.%d to run this plugin",
+              c->orthancVersion,
+              ORTHANC_PLUGINS_MINIMAL_MAJOR_NUMBER,
+              ORTHANC_PLUGINS_MINIMAL_MINOR_NUMBER,
+              ORTHANC_PLUGINS_MINIMAL_REVISION_NUMBER);
+      OrthancPluginLogError(context, info);
+      return -1;
+    }
+
+    OrthancPluginRegisterStorageArea(context, StorageCreate, StorageRead, StorageRemove);
+
+    return 0;
+  }
+
+
+  ORTHANC_PLUGINS_API void OrthancPluginFinalize()
+  {
+    OrthancPluginLogWarning(context, "Storage plugin is finalizing");
+  }
+
+
+  ORTHANC_PLUGINS_API const char* OrthancPluginGetName()
+  {
+    return "storage";
+  }
+
+
+  ORTHANC_PLUGINS_API const char* OrthancPluginGetVersion()
+  {
+    return "1.0";
+  }
+}