comparison Sources/Plugin.cpp @ 72:e94f177c3653

reorganization
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 04 Oct 2024 17:25:46 +0200
parents a5c1be2ec26a
children
comparison
equal deleted inserted replaced
71:f3bbafc067d0 72:e94f177c3653
28 28
29 #if !defined(ORTHANC_ENABLE_3DHOP) 29 #if !defined(ORTHANC_ENABLE_3DHOP)
30 # error Macro ORTHANC_ENABLE_3DHOP must be defined 30 # error Macro ORTHANC_ENABLE_3DHOP must be defined
31 #endif 31 #endif
32 32
33 #include "ResourcesCache.h"
34 #include "STLToolbox.h"
33 #include "StructureSetGeometry.h" 35 #include "StructureSetGeometry.h"
34 #include "STLToolbox.h"
35 #include "VTKToolbox.h" 36 #include "VTKToolbox.h"
36 37
37 #include <EmbeddedResources.h> 38 #include <EmbeddedResources.h>
38 39
39 #include "../Resources/Orthanc/Plugins/OrthancPluginCppWrapper.h" 40 #include "../Resources/Orthanc/Plugins/OrthancPluginCppWrapper.h"
103 dictionary["ORTHANC_STL_CREATOR_VERSION_UID_1_1"] = ORTHANC_STL_CREATOR_VERSION_UID_1_1; 104 dictionary["ORTHANC_STL_CREATOR_VERSION_UID_1_1"] = ORTHANC_STL_CREATOR_VERSION_UID_1_1;
104 dictionary["ORTHANC_STL_CREATOR_VERSION_UID_1_2"] = ORTHANC_STL_CREATOR_VERSION_UID_1_2; 105 dictionary["ORTHANC_STL_CREATOR_VERSION_UID_1_2"] = ORTHANC_STL_CREATOR_VERSION_UID_1_2;
105 } 106 }
106 107
107 #endif 108 #endif
108
109
110 // Forward declaration
111 void ReadStaticAsset(std::string& target,
112 const std::string& path);
113
114
115 /**
116 * As the static assets are gzipped by the "EmbedStaticAssets.py"
117 * script, we use a cache to maintain the uncompressed assets in order
118 * to avoid multiple gzip decodings.
119 **/
120 class ResourcesCache : public boost::noncopyable
121 {
122 public:
123 class IHandler : public boost::noncopyable
124 {
125 public:
126 virtual ~IHandler()
127 {
128 }
129
130 virtual void Apply(const std::string& resource) = 0;
131 };
132
133 private:
134 typedef std::map<std::string, std::string*> Content;
135
136 boost::shared_mutex mutex_;
137 Content content_;
138
139 class RestOutputHandler : public IHandler
140 {
141 private:
142 OrthancPluginRestOutput* output_;
143 std::string mime_;
144
145 public:
146 RestOutputHandler(OrthancPluginRestOutput* output,
147 const std::string& mime) :
148 output_(output),
149 mime_(mime)
150 {
151 }
152
153 virtual void Apply(const std::string& resource) ORTHANC_OVERRIDE
154 {
155 OrthancPluginAnswerBuffer(OrthancPlugins::GetGlobalContext(), output_,
156 resource.c_str(), resource.size(), mime_.c_str());
157 }
158 };
159
160 class StoreResourceIntoString : public IHandler
161 {
162 private:
163 std::string& target_;
164
165 public:
166 StoreResourceIntoString(std::string& target) :
167 target_(target)
168 {
169 }
170
171 virtual void Apply(const std::string& resource) ORTHANC_OVERRIDE
172 {
173 target_ = resource;
174 }
175 };
176
177 public:
178 ~ResourcesCache()
179 {
180 for (Content::iterator it = content_.begin(); it != content_.end(); ++it)
181 {
182 assert(it->second != NULL);
183 delete it->second;
184 }
185 }
186
187 void Apply(IHandler& handler,
188 const std::string& path)
189 {
190 {
191 // Check whether the cache already contains the resource
192 boost::shared_lock<boost::shared_mutex> lock(mutex_);
193
194 Content::const_iterator found = content_.find(path);
195
196 if (found != content_.end())
197 {
198 assert(found->second != NULL);
199 handler.Apply(*found->second);
200 return;
201 }
202 }
203
204 // This resource has not been cached yet
205
206 std::unique_ptr<std::string> item(new std::string);
207 ReadStaticAsset(*item, path);
208 handler.Apply(*item);
209
210 {
211 // Store the resource into the cache
212 boost::unique_lock<boost::shared_mutex> lock(mutex_);
213
214 if (content_.find(path) == content_.end())
215 {
216 content_[path] = item.release();
217 }
218 }
219 }
220
221 void Answer(OrthancPluginRestOutput* output,
222 const std::string& path)
223 {
224 const std::string mime = Orthanc::EnumerationToString(Orthanc::SystemToolbox::AutodetectMimeType(path));
225
226 RestOutputHandler handler(output, mime);
227 Apply(handler, path);
228 }
229
230 void ReadResource(std::string& target,
231 const std::string& path)
232 {
233 StoreResourceIntoString handler(target);
234 Apply(handler, path);
235 }
236 };
237 109
238 110
239 static ResourcesCache cache_; 111 static ResourcesCache cache_;
240 112
241 void ServeFile(OrthancPluginRestOutput* output, 113 void ServeFile(OrthancPluginRestOutput* output,