# HG changeset patch # User Sebastien Jodogne # Date 1728055546 -7200 # Node ID e94f177c36530d5e363f6eefac192949420e1fb8 # Parent f3bbafc067d02989ad3f206c9e22795cbc3d0ac8 reorganization diff -r f3bbafc067d0 -r e94f177c3653 CMakeLists.txt --- a/CMakeLists.txt Fri Oct 04 17:18:42 2024 +0200 +++ b/CMakeLists.txt Fri Oct 04 17:25:46 2024 +0200 @@ -253,6 +253,7 @@ add_library(OrthancSTL SHARED Sources/Extent2D.cpp Sources/Plugin.cpp + Sources/ResourcesCache.cpp Sources/STLToolbox.cpp Sources/StructurePolygon.cpp Sources/StructureSet.cpp diff -r f3bbafc067d0 -r e94f177c3653 Sources/Plugin.cpp --- a/Sources/Plugin.cpp Fri Oct 04 17:18:42 2024 +0200 +++ b/Sources/Plugin.cpp Fri Oct 04 17:25:46 2024 +0200 @@ -30,8 +30,9 @@ # error Macro ORTHANC_ENABLE_3DHOP must be defined #endif +#include "ResourcesCache.h" +#include "STLToolbox.h" #include "StructureSetGeometry.h" -#include "STLToolbox.h" #include "VTKToolbox.h" #include @@ -107,135 +108,6 @@ #endif -// Forward declaration -void ReadStaticAsset(std::string& target, - const std::string& path); - - -/** - * As the static assets are gzipped by the "EmbedStaticAssets.py" - * script, we use a cache to maintain the uncompressed assets in order - * to avoid multiple gzip decodings. - **/ -class ResourcesCache : public boost::noncopyable -{ -public: - class IHandler : public boost::noncopyable - { - public: - virtual ~IHandler() - { - } - - virtual void Apply(const std::string& resource) = 0; - }; - -private: - typedef std::map Content; - - boost::shared_mutex mutex_; - Content content_; - - class RestOutputHandler : public IHandler - { - private: - OrthancPluginRestOutput* output_; - std::string mime_; - - public: - RestOutputHandler(OrthancPluginRestOutput* output, - const std::string& mime) : - output_(output), - mime_(mime) - { - } - - virtual void Apply(const std::string& resource) ORTHANC_OVERRIDE - { - OrthancPluginAnswerBuffer(OrthancPlugins::GetGlobalContext(), output_, - resource.c_str(), resource.size(), mime_.c_str()); - } - }; - - class StoreResourceIntoString : public IHandler - { - private: - std::string& target_; - - public: - StoreResourceIntoString(std::string& target) : - target_(target) - { - } - - virtual void Apply(const std::string& resource) ORTHANC_OVERRIDE - { - target_ = resource; - } - }; - -public: - ~ResourcesCache() - { - for (Content::iterator it = content_.begin(); it != content_.end(); ++it) - { - assert(it->second != NULL); - delete it->second; - } - } - - void Apply(IHandler& handler, - const std::string& path) - { - { - // Check whether the cache already contains the resource - boost::shared_lock lock(mutex_); - - Content::const_iterator found = content_.find(path); - - if (found != content_.end()) - { - assert(found->second != NULL); - handler.Apply(*found->second); - return; - } - } - - // This resource has not been cached yet - - std::unique_ptr item(new std::string); - ReadStaticAsset(*item, path); - handler.Apply(*item); - - { - // Store the resource into the cache - boost::unique_lock lock(mutex_); - - if (content_.find(path) == content_.end()) - { - content_[path] = item.release(); - } - } - } - - void Answer(OrthancPluginRestOutput* output, - const std::string& path) - { - const std::string mime = Orthanc::EnumerationToString(Orthanc::SystemToolbox::AutodetectMimeType(path)); - - RestOutputHandler handler(output, mime); - Apply(handler, path); - } - - void ReadResource(std::string& target, - const std::string& path) - { - StoreResourceIntoString handler(target); - Apply(handler, path); - } -}; - - static ResourcesCache cache_; void ServeFile(OrthancPluginRestOutput* output, diff -r f3bbafc067d0 -r e94f177c3653 Sources/ResourcesCache.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Sources/ResourcesCache.cpp Fri Oct 04 17:25:46 2024 +0200 @@ -0,0 +1,138 @@ +/** + * SPDX-FileCopyrightText: 2023-2024 Sebastien Jodogne, UCLouvain, Belgium + * SPDX-License-Identifier: GPL-3.0-or-later + */ + +/** + * STL plugin for Orthanc + * Copyright (C) 2023-2024 Sebastien Jodogne, UCLouvain, Belgium + * + * This program is free software: you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program 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 + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + **/ + + +#include "ResourcesCache.h" + +#include "../Resources/Orthanc/Plugins/OrthancPluginCppWrapper.h" + +#include +#include + + +// Forward declaration +void ReadStaticAsset(std::string& target, + const std::string& path); + + +class ResourcesCache::RestOutputHandler : public IHandler +{ +private: + OrthancPluginRestOutput* output_; + std::string mime_; + +public: + RestOutputHandler(OrthancPluginRestOutput* output, + const std::string& mime) : + output_(output), + mime_(mime) + { + } + + virtual void Apply(const std::string& resource) ORTHANC_OVERRIDE + { + OrthancPluginAnswerBuffer(OrthancPlugins::GetGlobalContext(), output_, + resource.c_str(), resource.size(), mime_.c_str()); + } +}; + + +class ResourcesCache::StoreResourceIntoString : public IHandler +{ +private: + std::string& target_; + +public: + StoreResourceIntoString(std::string& target) : + target_(target) + { + } + + virtual void Apply(const std::string& resource) ORTHANC_OVERRIDE + { + target_ = resource; + } +}; + + +ResourcesCache::~ResourcesCache() +{ + for (Content::iterator it = content_.begin(); it != content_.end(); ++it) + { + assert(it->second != NULL); + delete it->second; + } +} + + +void ResourcesCache::Apply(IHandler& handler, + const std::string& path) +{ + { + // Check whether the cache already contains the resource + boost::shared_lock lock(mutex_); + + Content::const_iterator found = content_.find(path); + + if (found != content_.end()) + { + assert(found->second != NULL); + handler.Apply(*found->second); + return; + } + } + + // This resource has not been cached yet + + std::unique_ptr item(new std::string); + ReadStaticAsset(*item, path); + handler.Apply(*item); + + { + // Store the resource into the cache + boost::unique_lock lock(mutex_); + + if (content_.find(path) == content_.end()) + { + content_[path] = item.release(); + } + } +} + + +void ResourcesCache::Answer(OrthancPluginRestOutput* output, + const std::string& path) +{ + const std::string mime = Orthanc::EnumerationToString(Orthanc::SystemToolbox::AutodetectMimeType(path)); + + RestOutputHandler handler(output, mime); + Apply(handler, path); +} + + +void ResourcesCache::ReadResource(std::string& target, + const std::string& path) +{ + StoreResourceIntoString handler(target); + Apply(handler, path); +} diff -r f3bbafc067d0 -r e94f177c3653 Sources/ResourcesCache.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Sources/ResourcesCache.h Fri Oct 04 17:25:46 2024 +0200 @@ -0,0 +1,73 @@ +/** + * SPDX-FileCopyrightText: 2023-2024 Sebastien Jodogne, UCLouvain, Belgium + * SPDX-License-Identifier: GPL-3.0-or-later + */ + +/** + * STL plugin for Orthanc + * Copyright (C) 2023-2024 Sebastien Jodogne, UCLouvain, Belgium + * + * This program is free software: you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program 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 + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + **/ + + +#pragma once + +#include + +#include +#include +#include +#include + + +/** + * As the static assets are gzipped by the "EmbedStaticAssets.py" + * script, we use a cache to maintain the uncompressed assets in order + * to avoid multiple gzip decodings. + **/ +class ResourcesCache : public boost::noncopyable +{ +public: + class IHandler : public boost::noncopyable + { + public: + virtual ~IHandler() + { + } + + virtual void Apply(const std::string& resource) = 0; + }; + +private: + typedef std::map Content; + + boost::shared_mutex mutex_; + Content content_; + + class RestOutputHandler; + class StoreResourceIntoString; + +public: + ~ResourcesCache(); + + void Apply(IHandler& handler, + const std::string& path); + + void Answer(OrthancPluginRestOutput* output, + const std::string& path); + + void ReadResource(std::string& target, + const std::string& path); +};