comparison OrthancServer/main.cpp @ 1103:bec1eccf976c

Hot restart of Orthanc
author Sebastien Jodogne <s.jodogne@gmail.com>
date Thu, 07 Aug 2014 11:33:46 +0200
parents ce6386b37afd
children da56a7916e8a
comparison
equal deleted inserted replaced
1102:ce6386b37afd 1103:bec1eccf976c
312 } 312 }
313 } 313 }
314 314
315 315
316 316
317 static bool StartOrthanc()
318 {
319 std::string storageDirectoryStr = Configuration::GetGlobalStringParameter("StorageDirectory", "OrthancStorage");
320 boost::filesystem::path storageDirectory = Configuration::InterpretStringParameterAsPath(storageDirectoryStr);
321 boost::filesystem::path indexDirectory = Configuration::InterpretStringParameterAsPath(
322 Configuration::GetGlobalStringParameter("IndexDirectory", storageDirectoryStr));
323 ServerContext context(storageDirectory, indexDirectory);
324
325 LOG(WARNING) << "Storage directory: " << storageDirectory;
326 LOG(WARNING) << "Index directory: " << indexDirectory;
327
328 context.SetCompressionEnabled(Configuration::GetGlobalBoolParameter("StorageCompression", false));
329 context.SetStoreMD5ForAttachments(Configuration::GetGlobalBoolParameter("StoreMD5ForAttachments", true));
330
331 LoadLuaScripts(context);
332
333 try
334 {
335 context.GetIndex().SetMaximumPatientCount(Configuration::GetGlobalIntegerParameter("MaximumPatientCount", 0));
336 }
337 catch (...)
338 {
339 context.GetIndex().SetMaximumPatientCount(0);
340 }
341
342 try
343 {
344 uint64_t size = Configuration::GetGlobalIntegerParameter("MaximumStorageSize", 0);
345 context.GetIndex().SetMaximumStorageSize(size * 1024 * 1024);
346 }
347 catch (...)
348 {
349 context.GetIndex().SetMaximumStorageSize(0);
350 }
351
352 MyDicomServerFactory serverFactory(context);
353 bool isReset = false;
354
355 {
356 // DICOM server
357 DicomServer dicomServer;
358 OrthancApplicationEntityFilter dicomFilter;
359 dicomServer.SetCalledApplicationEntityTitleCheck(Configuration::GetGlobalBoolParameter("DicomCheckCalledAet", false));
360 dicomServer.SetStoreRequestHandlerFactory(serverFactory);
361 dicomServer.SetMoveRequestHandlerFactory(serverFactory);
362 dicomServer.SetFindRequestHandlerFactory(serverFactory);
363 dicomServer.SetPortNumber(Configuration::GetGlobalIntegerParameter("DicomPort", 4242));
364 dicomServer.SetApplicationEntityTitle(Configuration::GetGlobalStringParameter("DicomAet", "ORTHANC"));
365 dicomServer.SetApplicationEntityFilter(dicomFilter);
366
367 // HTTP server
368 MyIncomingHttpRequestFilter httpFilter(context);
369 MongooseServer httpServer;
370 httpServer.SetPortNumber(Configuration::GetGlobalIntegerParameter("HttpPort", 8042));
371 httpServer.SetRemoteAccessAllowed(Configuration::GetGlobalBoolParameter("RemoteAccessAllowed", false));
372 httpServer.SetIncomingHttpRequestFilter(httpFilter);
373
374 httpServer.SetAuthenticationEnabled(Configuration::GetGlobalBoolParameter("AuthenticationEnabled", false));
375 Configuration::SetupRegisteredUsers(httpServer);
376
377 if (Configuration::GetGlobalBoolParameter("SslEnabled", false))
378 {
379 std::string certificate = Configuration::InterpretStringParameterAsPath(
380 Configuration::GetGlobalStringParameter("SslCertificate", "certificate.pem"));
381 httpServer.SetSslEnabled(true);
382 httpServer.SetSslCertificate(certificate.c_str());
383 }
384 else
385 {
386 httpServer.SetSslEnabled(false);
387 }
388
389 OrthancRestApi restApi(context);
390
391 #if ORTHANC_STANDALONE == 1
392 EmbeddedResourceHttpHandler staticResources("/app", EmbeddedResources::ORTHANC_EXPLORER);
393 #else
394 FilesystemHttpHandler staticResources("/app", ORTHANC_PATH "/OrthancExplorer");
395 #endif
396
397 PluginsHttpHandler httpPlugins(context);
398 httpPlugins.SetOrthancRestApi(restApi);
399
400 PluginsManager pluginsManager;
401 pluginsManager.RegisterServiceProvider(httpPlugins);
402 LoadPlugins(pluginsManager);
403
404 httpServer.RegisterHandler(httpPlugins);
405 httpServer.RegisterHandler(staticResources);
406 httpServer.RegisterHandler(restApi);
407 httpPlugins.SetOrthancRestApi(restApi);
408 context.SetPluginsHttpHandler(httpPlugins);
409
410 // GO !!! Start the requested servers
411 if (Configuration::GetGlobalBoolParameter("HttpServerEnabled", true))
412 {
413 httpServer.Start();
414 LOG(WARNING) << "HTTP server listening on port: " << httpServer.GetPortNumber();
415 }
416 else
417 {
418 LOG(WARNING) << "The HTTP server is disabled";
419 }
420
421 if (Configuration::GetGlobalBoolParameter("DicomServerEnabled", true))
422 {
423 dicomServer.Start();
424 LOG(WARNING) << "DICOM server listening on port: " << dicomServer.GetPortNumber();
425 }
426 else
427 {
428 LOG(WARNING) << "The DICOM server is disabled";
429 }
430
431 LOG(WARNING) << "Orthanc has started";
432 Toolbox::ServerBarrier(restApi.ResetRequestReceivedFlag());
433 isReset = restApi.ResetRequestReceivedFlag();
434
435 if (isReset)
436 {
437 LOG(WARNING) << "Reset request received, restarting Orthanc";
438 }
439
440 // We're done
441 LOG(WARNING) << "Orthanc is stopping";
442 }
443
444 serverFactory.Done();
445
446 return isReset;
447 }
448
449
450
317 451
318 int main(int argc, char* argv[]) 452 int main(int argc, char* argv[])
319 { 453 {
320 // Initialize Google's logging library. 454 // Initialize Google's logging library.
321 FLAGS_logtostderr = true; 455 FLAGS_logtostderr = true;
386 520
387 521
388 int status = 0; 522 int status = 0;
389 try 523 try
390 { 524 {
391 OrthancInitialize(configurationFile); 525 for (;;)
392 526 {
393 std::string storageDirectoryStr = Configuration::GetGlobalStringParameter("StorageDirectory", "OrthancStorage"); 527 OrthancInitialize(configurationFile);
394 boost::filesystem::path storageDirectory = Configuration::InterpretStringParameterAsPath(storageDirectoryStr); 528
395 boost::filesystem::path indexDirectory = Configuration::InterpretStringParameterAsPath( 529 bool reset = StartOrthanc();
396 Configuration::GetGlobalStringParameter("IndexDirectory", storageDirectoryStr)); 530 if (reset)
397 ServerContext context(storageDirectory, indexDirectory);
398
399 LOG(WARNING) << "Storage directory: " << storageDirectory;
400 LOG(WARNING) << "Index directory: " << indexDirectory;
401
402 context.SetCompressionEnabled(Configuration::GetGlobalBoolParameter("StorageCompression", false));
403 context.SetStoreMD5ForAttachments(Configuration::GetGlobalBoolParameter("StoreMD5ForAttachments", true));
404
405 LoadLuaScripts(context);
406
407 try
408 {
409 context.GetIndex().SetMaximumPatientCount(Configuration::GetGlobalIntegerParameter("MaximumPatientCount", 0));
410 }
411 catch (...)
412 {
413 context.GetIndex().SetMaximumPatientCount(0);
414 }
415
416 try
417 {
418 uint64_t size = Configuration::GetGlobalIntegerParameter("MaximumStorageSize", 0);
419 context.GetIndex().SetMaximumStorageSize(size * 1024 * 1024);
420 }
421 catch (...)
422 {
423 context.GetIndex().SetMaximumStorageSize(0);
424 }
425
426 MyDicomServerFactory serverFactory(context);
427
428 {
429 // DICOM server
430 DicomServer dicomServer;
431 OrthancApplicationEntityFilter dicomFilter;
432 dicomServer.SetCalledApplicationEntityTitleCheck(Configuration::GetGlobalBoolParameter("DicomCheckCalledAet", false));
433 dicomServer.SetStoreRequestHandlerFactory(serverFactory);
434 dicomServer.SetMoveRequestHandlerFactory(serverFactory);
435 dicomServer.SetFindRequestHandlerFactory(serverFactory);
436 dicomServer.SetPortNumber(Configuration::GetGlobalIntegerParameter("DicomPort", 4242));
437 dicomServer.SetApplicationEntityTitle(Configuration::GetGlobalStringParameter("DicomAet", "ORTHANC"));
438 dicomServer.SetApplicationEntityFilter(dicomFilter);
439
440 // HTTP server
441 MyIncomingHttpRequestFilter httpFilter(context);
442 MongooseServer httpServer;
443 httpServer.SetPortNumber(Configuration::GetGlobalIntegerParameter("HttpPort", 8042));
444 httpServer.SetRemoteAccessAllowed(Configuration::GetGlobalBoolParameter("RemoteAccessAllowed", false));
445 httpServer.SetIncomingHttpRequestFilter(httpFilter);
446
447 httpServer.SetAuthenticationEnabled(Configuration::GetGlobalBoolParameter("AuthenticationEnabled", false));
448 Configuration::SetupRegisteredUsers(httpServer);
449
450 if (Configuration::GetGlobalBoolParameter("SslEnabled", false))
451 { 531 {
452 std::string certificate = Configuration::InterpretStringParameterAsPath( 532 OrthancFinalize();
453 Configuration::GetGlobalStringParameter("SslCertificate", "certificate.pem"));
454 httpServer.SetSslEnabled(true);
455 httpServer.SetSslCertificate(certificate.c_str());
456 } 533 }
457 else 534 else
458 { 535 {
459 httpServer.SetSslEnabled(false); 536 break;
460 } 537 }
461 538 }
462 OrthancRestApi restApi(context);
463
464 #if ORTHANC_STANDALONE == 1
465 EmbeddedResourceHttpHandler staticResources("/app", EmbeddedResources::ORTHANC_EXPLORER);
466 #else
467 FilesystemHttpHandler staticResources("/app", ORTHANC_PATH "/OrthancExplorer");
468 #endif
469
470 PluginsHttpHandler httpPlugins(context);
471 httpPlugins.SetOrthancRestApi(restApi);
472
473 PluginsManager pluginsManager;
474 pluginsManager.RegisterServiceProvider(httpPlugins);
475 LoadPlugins(pluginsManager);
476
477 httpServer.RegisterHandler(httpPlugins);
478 httpServer.RegisterHandler(staticResources);
479 httpServer.RegisterHandler(restApi);
480 httpPlugins.SetOrthancRestApi(restApi);
481 context.SetPluginsHttpHandler(httpPlugins);
482
483 // GO !!! Start the requested servers
484 if (Configuration::GetGlobalBoolParameter("HttpServerEnabled", true))
485 {
486 httpServer.Start();
487 LOG(WARNING) << "HTTP server listening on port: " << httpServer.GetPortNumber();
488 }
489 else
490 {
491 LOG(WARNING) << "The HTTP server is disabled";
492 }
493
494 if (Configuration::GetGlobalBoolParameter("DicomServerEnabled", true))
495 {
496 dicomServer.Start();
497 LOG(WARNING) << "DICOM server listening on port: " << dicomServer.GetPortNumber();
498 }
499 else
500 {
501 LOG(WARNING) << "The DICOM server is disabled";
502 }
503
504 LOG(WARNING) << "Orthanc has started";
505 Toolbox::ServerBarrier();
506
507 // We're done
508 LOG(WARNING) << "Orthanc is stopping";
509 }
510
511 serverFactory.Done();
512 } 539 }
513 catch (OrthancException& e) 540 catch (OrthancException& e)
514 { 541 {
515 LOG(ERROR) << "EXCEPTION [" << e.What() << "]"; 542 LOG(ERROR) << "Uncaught exception, stopping now: [" << e.What() << "]";
516 status = -1; 543 status = -1;
517 } 544 }
518 catch (...) 545 catch (...)
519 { 546 {
520 LOG(ERROR) << "NATIVE EXCEPTION"; 547 LOG(ERROR) << "Native exception, stopping now";
521 status = -1; 548 status = -1;
522 } 549 }
523 550
524 OrthancFinalize(); 551 OrthancFinalize();
525 552