Mercurial > hg > orthanc-webviewer
comparison Plugin/Plugin.cpp @ 271:d3b58d11e3ec
removed global variable "context_"
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Tue, 30 Jun 2020 10:28:05 +0200 |
parents | 4e9d30c19b4b |
children | 3a9749b1dfce |
comparison
equal
deleted
inserted
replaced
270:3e9307f6da67 | 271:d3b58d11e3ec |
---|---|
53 # define ORTHANC_PLUGINS_API | 53 # define ORTHANC_PLUGINS_API |
54 #endif | 54 #endif |
55 | 55 |
56 | 56 |
57 | 57 |
58 static OrthancPluginContext* context_ = NULL; | |
59 | |
60 | |
61 class CacheContext | 58 class CacheContext |
62 { | 59 { |
63 private: | 60 private: |
64 class DynamicString : public Orthanc::IDynamicObject | 61 class DynamicString : public Orthanc::IDynamicObject |
65 { | 62 { |
97 const std::string& instanceId = dynamic_cast<DynamicString&>(*obj).GetValue(); | 94 const std::string& instanceId = dynamic_cast<DynamicString&>(*obj).GetValue(); |
98 | 95 |
99 // On the reception of a new instance, indalidate the parent series of the instance | 96 // On the reception of a new instance, indalidate the parent series of the instance |
100 std::string uri = "/instances/" + std::string(instanceId); | 97 std::string uri = "/instances/" + std::string(instanceId); |
101 Json::Value instance; | 98 Json::Value instance; |
102 if (OrthancPlugins::GetJsonFromOrthanc(instance, context_, uri)) | 99 if (OrthancPlugins::GetJsonFromOrthanc(instance, OrthancPlugins::GetGlobalContext(), uri)) |
103 { | 100 { |
104 std::string seriesId = instance["ParentSeries"].asString(); | 101 std::string seriesId = instance["ParentSeries"].asString(); |
105 cache->GetScheduler().Invalidate(OrthancPlugins::CacheBundle_SeriesInformation, seriesId); | 102 cache->GetScheduler().Invalidate(OrthancPlugins::CacheBundle_SeriesInformation, seriesId); |
106 } | 103 } |
107 } | 104 } |
113 explicit CacheContext(const std::string& path) : storage_(path), stop_(false) | 110 explicit CacheContext(const std::string& path) : storage_(path), stop_(false) |
114 { | 111 { |
115 boost::filesystem::path p(path); | 112 boost::filesystem::path p(path); |
116 db_.Open((p / "cache.db").string()); | 113 db_.Open((p / "cache.db").string()); |
117 | 114 |
118 cache_.reset(new OrthancPlugins::CacheManager(context_, db_, storage_)); | 115 cache_.reset(new OrthancPlugins::CacheManager(OrthancPlugins::GetGlobalContext(), db_, storage_)); |
119 //cache_->SetSanityCheckEnabled(true); // For debug | 116 //cache_->SetSanityCheckEnabled(true); // For debug |
120 | 117 |
121 scheduler_.reset(new OrthancPlugins::CacheScheduler(*cache_, 100)); | 118 scheduler_.reset(new OrthancPlugins::CacheScheduler(*cache_, 100)); |
122 | 119 |
123 newInstancesThread_ = boost::thread(NewInstancesThread, this); | 120 newInstancesThread_ = boost::thread(NewInstancesThread, this); |
182 { | 179 { |
183 try | 180 try |
184 { | 181 { |
185 if (request->method != OrthancPluginHttpMethod_Get) | 182 if (request->method != OrthancPluginHttpMethod_Get) |
186 { | 183 { |
187 OrthancPluginSendMethodNotAllowed(context_, output, "GET"); | 184 OrthancPluginSendMethodNotAllowed(OrthancPlugins::GetGlobalContext(), output, "GET"); |
188 return OrthancPluginErrorCode_Success; | 185 return OrthancPluginErrorCode_Success; |
189 } | 186 } |
190 | 187 |
191 const std::string id = request->groups[0]; | 188 const std::string id = request->groups[0]; |
192 std::string content; | 189 std::string content; |
193 | 190 |
194 if (cache_->GetScheduler().Access(content, bundle, id)) | 191 if (cache_->GetScheduler().Access(content, bundle, id)) |
195 { | 192 { |
196 OrthancPluginAnswerBuffer(context_, output, content.c_str(), content.size(), "application/json"); | 193 OrthancPluginAnswerBuffer(OrthancPlugins::GetGlobalContext(), output, content.c_str(), content.size(), "application/json"); |
197 } | 194 } |
198 else | 195 else |
199 { | 196 { |
200 OrthancPluginSendHttpStatusCode(context_, output, 404); | 197 OrthancPluginSendHttpStatusCode(OrthancPlugins::GetGlobalContext(), output, 404); |
201 } | 198 } |
202 | 199 |
203 return OrthancPluginErrorCode_Success; | 200 return OrthancPluginErrorCode_Success; |
204 } | 201 } |
205 catch (Orthanc::OrthancException& e) | 202 catch (Orthanc::OrthancException& e) |
227 const char* url, | 224 const char* url, |
228 const OrthancPluginHttpRequest* request) | 225 const OrthancPluginHttpRequest* request) |
229 { | 226 { |
230 if (request->method != OrthancPluginHttpMethod_Get) | 227 if (request->method != OrthancPluginHttpMethod_Get) |
231 { | 228 { |
232 OrthancPluginSendMethodNotAllowed(context_, output, "GET"); | 229 OrthancPluginSendMethodNotAllowed(OrthancPlugins::GetGlobalContext(), output, "GET"); |
233 return OrthancPluginErrorCode_Success; | 230 return OrthancPluginErrorCode_Success; |
234 } | 231 } |
235 | 232 |
236 const std::string path = std::string(WEB_VIEWER_PATH) + std::string(request->groups[0]); | 233 const std::string path = std::string(WEB_VIEWER_PATH) + std::string(request->groups[0]); |
237 const char* mime = OrthancPlugins::GetMimeType(path); | 234 const char* mime = OrthancPlugins::GetMimeType(path); |
239 std::string s; | 236 std::string s; |
240 try | 237 try |
241 { | 238 { |
242 Orthanc::SystemToolbox::ReadFile(s, path); | 239 Orthanc::SystemToolbox::ReadFile(s, path); |
243 const char* resource = s.size() ? s.c_str() : NULL; | 240 const char* resource = s.size() ? s.c_str() : NULL; |
244 OrthancPluginAnswerBuffer(context_, output, resource, s.size(), mime); | 241 OrthancPluginAnswerBuffer(OrthancPlugins::GetGlobalContext(), output, resource, s.size(), mime); |
245 } | 242 } |
246 catch (Orthanc::OrthancException&) | 243 catch (Orthanc::OrthancException&) |
247 { | 244 { |
248 LOG(ERROR) << "Inexistent file in served folder: " << path; | 245 LOG(ERROR) << "Inexistent file in served folder: " << path; |
249 OrthancPluginSendHttpStatusCode(context_, output, 404); | 246 OrthancPluginSendHttpStatusCode(OrthancPlugins::GetGlobalContext(), output, 404); |
250 } | 247 } |
251 | 248 |
252 return OrthancPluginErrorCode_Success; | 249 return OrthancPluginErrorCode_Success; |
253 } | 250 } |
254 #endif | 251 #endif |
260 const char* url, | 257 const char* url, |
261 const OrthancPluginHttpRequest* request) | 258 const OrthancPluginHttpRequest* request) |
262 { | 259 { |
263 if (request->method != OrthancPluginHttpMethod_Get) | 260 if (request->method != OrthancPluginHttpMethod_Get) |
264 { | 261 { |
265 OrthancPluginSendMethodNotAllowed(context_, output, "GET"); | 262 OrthancPluginSendMethodNotAllowed(OrthancPlugins::GetGlobalContext(), output, "GET"); |
266 return OrthancPluginErrorCode_Success; | 263 return OrthancPluginErrorCode_Success; |
267 } | 264 } |
268 | 265 |
269 std::string path = "/" + std::string(request->groups[0]); | 266 std::string path = "/" + std::string(request->groups[0]); |
270 const char* mime = OrthancPlugins::GetMimeType(path); | 267 const char* mime = OrthancPlugins::GetMimeType(path); |
273 { | 270 { |
274 std::string s; | 271 std::string s; |
275 Orthanc::EmbeddedResources::GetDirectoryResource(s, folder, path.c_str()); | 272 Orthanc::EmbeddedResources::GetDirectoryResource(s, folder, path.c_str()); |
276 | 273 |
277 const char* resource = s.size() ? s.c_str() : NULL; | 274 const char* resource = s.size() ? s.c_str() : NULL; |
278 OrthancPluginAnswerBuffer(context_, output, resource, s.size(), mime); | 275 OrthancPluginAnswerBuffer(OrthancPlugins::GetGlobalContext(), output, resource, s.size(), mime); |
279 | 276 |
280 return OrthancPluginErrorCode_Success; | 277 return OrthancPluginErrorCode_Success; |
281 } | 278 } |
282 catch (std::runtime_error&) | 279 catch (std::runtime_error&) |
283 { | 280 { |
284 LOG(ERROR) << "Unknown static resource in plugin: " << request->groups[0]; | 281 LOG(ERROR) << "Unknown static resource in plugin: " << request->groups[0]; |
285 OrthancPluginSendHttpStatusCode(context_, output, 404); | 282 OrthancPluginSendHttpStatusCode(OrthancPlugins::GetGlobalContext(), output, 404); |
286 return OrthancPluginErrorCode_Success; | 283 return OrthancPluginErrorCode_Success; |
287 } | 284 } |
288 } | 285 } |
289 | 286 |
290 | 287 |
295 { | 292 { |
296 try | 293 try |
297 { | 294 { |
298 if (request->method != OrthancPluginHttpMethod_Get) | 295 if (request->method != OrthancPluginHttpMethod_Get) |
299 { | 296 { |
300 OrthancPluginSendMethodNotAllowed(context_, output, "GET"); | 297 OrthancPluginSendMethodNotAllowed(OrthancPlugins::GetGlobalContext(), output, "GET"); |
301 return OrthancPluginErrorCode_Success; | 298 return OrthancPluginErrorCode_Success; |
302 } | 299 } |
303 | 300 |
304 const std::string id = request->groups[0]; | 301 const std::string id = request->groups[0]; |
305 Json::Value series; | 302 Json::Value series; |
306 | 303 |
307 if (OrthancPlugins::GetJsonFromOrthanc(series, context_, "/series/" + id) && | 304 if (OrthancPlugins::GetJsonFromOrthanc(series, OrthancPlugins::GetGlobalContext(), "/series/" + id) && |
308 series.type() == Json::objectValue) | 305 series.type() == Json::objectValue) |
309 { | 306 { |
310 bool value = (series["IsStable"].asBool() || | 307 bool value = (series["IsStable"].asBool() || |
311 series["Status"].asString() == "Complete"); | 308 series["Status"].asString() == "Complete"); |
312 std::string answer = value ? "true" : "false"; | 309 std::string answer = value ? "true" : "false"; |
313 OrthancPluginAnswerBuffer(context_, output, answer.c_str(), answer.size(), "application/json"); | 310 OrthancPluginAnswerBuffer(OrthancPlugins::GetGlobalContext(), output, answer.c_str(), answer.size(), "application/json"); |
314 } | 311 } |
315 else | 312 else |
316 { | 313 { |
317 OrthancPluginSendHttpStatusCode(context_, output, 404); | 314 OrthancPluginSendHttpStatusCode(OrthancPlugins::GetGlobalContext(), output, 404); |
318 } | 315 } |
319 | 316 |
320 return OrthancPluginErrorCode_Success; | 317 return OrthancPluginErrorCode_Success; |
321 } | 318 } |
322 catch (Orthanc::OrthancException& e) | 319 catch (Orthanc::OrthancException& e) |
342 boost::filesystem::path& cachePath, | 339 boost::filesystem::path& cachePath, |
343 int& cacheSize) | 340 int& cacheSize) |
344 { | 341 { |
345 /* Read the configuration of the Web viewer */ | 342 /* Read the configuration of the Web viewer */ |
346 Json::Value configuration; | 343 Json::Value configuration; |
347 if (!OrthancPlugins::ReadConfiguration(configuration, context_)) | 344 if (!OrthancPlugins::ReadConfiguration(configuration, OrthancPlugins::GetGlobalContext())) |
348 { | 345 { |
349 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat); | 346 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat); |
350 } | 347 } |
351 | 348 |
352 // By default, the cache of the Web viewer is located inside the | 349 // By default, the cache of the Web viewer is located inside the |
393 ORTHANC_PLUGINS_API int32_t OrthancPluginInitialize(OrthancPluginContext* context) | 390 ORTHANC_PLUGINS_API int32_t OrthancPluginInitialize(OrthancPluginContext* context) |
394 { | 391 { |
395 using namespace OrthancPlugins; | 392 using namespace OrthancPlugins; |
396 | 393 |
397 OrthancPlugins::SetGlobalContext(context); | 394 OrthancPlugins::SetGlobalContext(context); |
395 | |
396 #if defined(ORTHANC_FRAMEWORK_VERSION_IS_ABOVE) // This indicates Orthanc framework >= 1.7.2 | |
398 Orthanc::Logging::InitializePluginContext(context); | 397 Orthanc::Logging::InitializePluginContext(context); |
399 context_ = context; | 398 #else |
399 Orthanc::Logging::Initialize(context); | |
400 #endif | |
401 | |
400 assert(DisplayPerformanceWarning()); | 402 assert(DisplayPerformanceWarning()); |
401 LOG(WARNING) << "Initializing the Web viewer"; | 403 LOG(WARNING) << "Initializing the Web viewer"; |
402 | 404 |
403 | 405 |
404 /* Check the version of the Orthanc core */ | 406 /* Check the version of the Orthanc core */ |
405 if (OrthancPluginCheckVersion(context_) == 0) | 407 if (OrthancPluginCheckVersion(context) == 0) |
406 { | 408 { |
407 char info[1024]; | 409 char info[1024]; |
408 sprintf(info, "Your version of Orthanc (%s) must be above %d.%d.%d to run this plugin", | 410 sprintf(info, "Your version of Orthanc (%s) must be above %d.%d.%d to run this plugin", |
409 context_->orthancVersion, | 411 context->orthancVersion, |
410 ORTHANC_PLUGINS_MINIMAL_MAJOR_NUMBER, | 412 ORTHANC_PLUGINS_MINIMAL_MAJOR_NUMBER, |
411 ORTHANC_PLUGINS_MINIMAL_MINOR_NUMBER, | 413 ORTHANC_PLUGINS_MINIMAL_MINOR_NUMBER, |
412 ORTHANC_PLUGINS_MINIMAL_REVISION_NUMBER); | 414 ORTHANC_PLUGINS_MINIMAL_REVISION_NUMBER); |
413 OrthancPluginLogError(context, info); | 415 OrthancPluginLogError(context, info); |
414 return -1; | 416 return -1; |
415 } | 417 } |
416 | 418 |
417 OrthancPluginSetDescription(context_, "Provides a Web viewer of DICOM series within Orthanc."); | 419 OrthancPluginSetDescription(context, "Provides a Web viewer of DICOM series within Orthanc."); |
418 | 420 |
419 | 421 |
420 /* By default, use half of the available processing cores for the decoding of DICOM images */ | 422 /* By default, use half of the available processing cores for the decoding of DICOM images */ |
421 int decodingThreads = boost::thread::hardware_concurrency() / 2; | 423 int decodingThreads = boost::thread::hardware_concurrency() / 2; |
422 if (decodingThreads == 0) | 424 if (decodingThreads == 0) |
445 | 447 |
446 /* Look for a change in the versions */ | 448 /* Look for a change in the versions */ |
447 std::string orthancVersion("unknown"), webViewerVersion("unknown"); | 449 std::string orthancVersion("unknown"), webViewerVersion("unknown"); |
448 bool clear = false; | 450 bool clear = false; |
449 if (!scheduler.LookupProperty(orthancVersion, CacheProperty_OrthancVersion) || | 451 if (!scheduler.LookupProperty(orthancVersion, CacheProperty_OrthancVersion) || |
450 orthancVersion != std::string(context_->orthancVersion)) | 452 orthancVersion != std::string(context->orthancVersion)) |
451 { | 453 { |
452 LOG(WARNING) << "The version of Orthanc has changed from \"" << orthancVersion | 454 LOG(WARNING) << "The version of Orthanc has changed from \"" << orthancVersion |
453 << "\" to \"" << context_->orthancVersion | 455 << "\" to \"" << context->orthancVersion |
454 << "\": The cache of the Web viewer will be cleared"; | 456 << "\": The cache of the Web viewer will be cleared"; |
455 clear = true; | 457 clear = true; |
456 } | 458 } |
457 | 459 |
458 if (!scheduler.LookupProperty(webViewerVersion, CacheProperty_WebViewerVersion) || | 460 if (!scheduler.LookupProperty(webViewerVersion, CacheProperty_WebViewerVersion) || |
468 /* Clear the cache if needed */ | 470 /* Clear the cache if needed */ |
469 if (clear) | 471 if (clear) |
470 { | 472 { |
471 LOG(WARNING) << "Clearing the cache of the Web viewer"; | 473 LOG(WARNING) << "Clearing the cache of the Web viewer"; |
472 scheduler.Clear(); | 474 scheduler.Clear(); |
473 scheduler.SetProperty(CacheProperty_OrthancVersion, context_->orthancVersion); | 475 scheduler.SetProperty(CacheProperty_OrthancVersion, context->orthancVersion); |
474 scheduler.SetProperty(CacheProperty_WebViewerVersion, ORTHANC_PLUGIN_VERSION); | 476 scheduler.SetProperty(CacheProperty_WebViewerVersion, ORTHANC_PLUGIN_VERSION); |
475 } | 477 } |
476 else | 478 else |
477 { | 479 { |
478 LOG(INFO) << "No change in the versions, no need to clear the cache of the Web viewer"; | 480 LOG(INFO) << "No change in the versions, no need to clear the cache of the Web viewer"; |
479 } | 481 } |
480 | 482 |
481 | 483 |
482 /* Configure the cache */ | 484 /* Configure the cache */ |
483 scheduler.RegisterPolicy(new ViewerPrefetchPolicy(context_)); | 485 scheduler.RegisterPolicy(new ViewerPrefetchPolicy(context)); |
484 scheduler.Register(CacheBundle_SeriesInformation, | 486 scheduler.Register(CacheBundle_SeriesInformation, |
485 new SeriesInformationAdapter(context_, scheduler), 1); | 487 new SeriesInformationAdapter(context, scheduler), 1); |
486 scheduler.Register(CacheBundle_DecodedImage, | 488 scheduler.Register(CacheBundle_DecodedImage, |
487 new DecodedImageAdapter(context_), decodingThreads); | 489 new DecodedImageAdapter(context), decodingThreads); |
488 | 490 |
489 | 491 |
490 /* Set the quotas */ | 492 /* Set the quotas */ |
491 scheduler.SetQuota(CacheBundle_SeriesInformation, 1000, 0); // Keep info about 1000 series | 493 scheduler.SetQuota(CacheBundle_SeriesInformation, 1000, 0); // Keep info about 1000 series |
492 | 494 |
512 return -1; | 514 return -1; |
513 } | 515 } |
514 | 516 |
515 | 517 |
516 /* Install the callbacks */ | 518 /* Install the callbacks */ |
517 OrthancPluginRegisterRestCallbackNoLock(context_, "/web-viewer/series/(.*)", ServeCache<CacheBundle_SeriesInformation>); | 519 OrthancPluginRegisterRestCallbackNoLock(context, "/web-viewer/series/(.*)", ServeCache<CacheBundle_SeriesInformation>); |
518 OrthancPluginRegisterRestCallbackNoLock(context_, "/web-viewer/is-stable-series/(.*)", IsStableSeries); | 520 OrthancPluginRegisterRestCallbackNoLock(context, "/web-viewer/is-stable-series/(.*)", IsStableSeries); |
519 OrthancPluginRegisterRestCallbackNoLock(context_, "/web-viewer/instances/(.*)", ServeCache<CacheBundle_DecodedImage>); | 521 OrthancPluginRegisterRestCallbackNoLock(context, "/web-viewer/instances/(.*)", ServeCache<CacheBundle_DecodedImage>); |
520 OrthancPluginRegisterRestCallbackNoLock(context, "/web-viewer/libs/(.*)", ServeEmbeddedFolder<Orthanc::EmbeddedResources::JAVASCRIPT_LIBS>); | 522 OrthancPluginRegisterRestCallbackNoLock(context, "/web-viewer/libs/(.*)", ServeEmbeddedFolder<Orthanc::EmbeddedResources::JAVASCRIPT_LIBS>); |
521 | 523 |
522 #if ORTHANC_STANDALONE == 1 | 524 #if ORTHANC_STANDALONE == 1 |
523 OrthancPluginRegisterRestCallbackNoLock(context, "/web-viewer/app/(.*)", ServeEmbeddedFolder<Orthanc::EmbeddedResources::WEB_VIEWER>); | 525 OrthancPluginRegisterRestCallbackNoLock(context, "/web-viewer/app/(.*)", ServeEmbeddedFolder<Orthanc::EmbeddedResources::WEB_VIEWER>); |
524 #else | 526 #else |
529 | 531 |
530 | 532 |
531 /* Extend the default Orthanc Explorer with custom JavaScript */ | 533 /* Extend the default Orthanc Explorer with custom JavaScript */ |
532 std::string explorer; | 534 std::string explorer; |
533 Orthanc::EmbeddedResources::GetFileResource(explorer, Orthanc::EmbeddedResources::ORTHANC_EXPLORER); | 535 Orthanc::EmbeddedResources::GetFileResource(explorer, Orthanc::EmbeddedResources::ORTHANC_EXPLORER); |
534 OrthancPluginExtendOrthancExplorer(context_, explorer.c_str()); | 536 OrthancPluginExtendOrthancExplorer(context, explorer.c_str()); |
535 | 537 |
536 return 0; | 538 return 0; |
537 } | 539 } |
538 | 540 |
539 | 541 |