Mercurial > hg > orthanc
comparison OrthancServer/main.cpp @ 1452:b737acb13da5
refactoring of the main function
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Thu, 02 Jul 2015 11:35:41 +0200 |
parents | 5ba7471780ae |
children | a68545767975 |
comparison
equal
deleted
inserted
replaced
1451:538fc8359a9a | 1452:b737acb13da5 |
---|---|
381 } | 381 } |
382 } | 382 } |
383 | 383 |
384 | 384 |
385 | 385 |
386 | 386 // Returns "true" if restart is required |
387 | 387 static bool WaitForExit(ServerContext& context, |
388 static bool StartOrthanc(int argc, char *argv[]) | 388 OrthancRestApi& restApi) |
389 { | 389 { |
390 #if ENABLE_PLUGINS == 1 | 390 LOG(WARNING) << "Orthanc has started"; |
391 OrthancPlugins plugins; | 391 |
392 plugins.SetCommandLineArguments(argc, argv); | 392 Toolbox::ServerBarrier(restApi.ResetRequestReceivedFlag()); |
393 LoadPlugins(plugins); | 393 bool restart = restApi.ResetRequestReceivedFlag(); |
394 #endif | 394 |
395 | 395 if (restart) |
396 // "storage" and "database" must be declared BEFORE "ServerContext | 396 { |
397 // context", to avoid mess in the invokation order of the destructors. | 397 LOG(WARNING) << "Reset request received, restarting Orthanc"; |
398 std::auto_ptr<IDatabaseWrapper> database; | 398 } |
399 std::auto_ptr<IStorageArea> storage; | 399 |
400 std::auto_ptr<ServerContext> context; | 400 // We're done |
401 | 401 LOG(WARNING) << "Orthanc is stopping"; |
402 if (plugins.HasDatabase()) | 402 |
403 { | 403 return restart; |
404 context.reset(new ServerContext(plugins.GetDatabase())); | 404 } |
405 | |
406 | |
407 | |
408 static bool StartHttpServer(ServerContext& context, | |
409 OrthancRestApi& restApi) | |
410 { | |
411 if (!Configuration::GetGlobalBoolParameter("HttpServerEnabled", true)) | |
412 { | |
413 LOG(WARNING) << "The HTTP server is disabled"; | |
414 return WaitForExit(context, restApi); | |
415 } | |
416 | |
417 // HTTP server | |
418 MyIncomingHttpRequestFilter httpFilter(context); | |
419 MongooseServer httpServer; | |
420 httpServer.SetPortNumber(Configuration::GetGlobalIntegerParameter("HttpPort", 8042)); | |
421 httpServer.SetRemoteAccessAllowed(Configuration::GetGlobalBoolParameter("RemoteAccessAllowed", false)); | |
422 httpServer.SetKeepAliveEnabled(Configuration::GetGlobalBoolParameter("KeepAlive", false)); | |
423 httpServer.SetIncomingHttpRequestFilter(httpFilter); | |
424 | |
425 httpServer.SetAuthenticationEnabled(Configuration::GetGlobalBoolParameter("AuthenticationEnabled", false)); | |
426 Configuration::SetupRegisteredUsers(httpServer); | |
427 | |
428 if (Configuration::GetGlobalBoolParameter("SslEnabled", false)) | |
429 { | |
430 std::string certificate = Configuration::InterpretStringParameterAsPath( | |
431 Configuration::GetGlobalStringParameter("SslCertificate", "certificate.pem")); | |
432 httpServer.SetSslEnabled(true); | |
433 httpServer.SetSslCertificate(certificate.c_str()); | |
405 } | 434 } |
406 else | 435 else |
407 { | 436 { |
408 database.reset(Configuration::CreateDatabaseWrapper()); | 437 httpServer.SetSslEnabled(false); |
409 context.reset(new ServerContext(*database)); | 438 } |
410 } | 439 |
411 | 440 httpServer.Register(context.GetHttpHandler()); |
412 context->SetCompressionEnabled(Configuration::GetGlobalBoolParameter("StorageCompression", false)); | 441 |
413 context->SetStoreMD5ForAttachments(Configuration::GetGlobalBoolParameter("StoreMD5ForAttachments", true)); | 442 httpServer.Start(); |
414 | 443 LOG(WARNING) << "HTTP server listening on port: " << httpServer.GetPortNumber(); |
415 LoadLuaScripts(*context); | 444 |
416 | 445 bool restart = WaitForExit(context, restApi); |
417 try | 446 |
418 { | 447 httpServer.Stop(); |
419 context->GetIndex().SetMaximumPatientCount(Configuration::GetGlobalIntegerParameter("MaximumPatientCount", 0)); | 448 LOG(WARNING) << " HTTP server has stopped"; |
420 } | 449 |
421 catch (...) | 450 return restart; |
422 { | 451 } |
423 context->GetIndex().SetMaximumPatientCount(0); | 452 |
424 } | 453 |
425 | 454 static bool StartDicomServer(ServerContext& context, |
426 try | 455 OrthancRestApi& restApi) |
427 { | 456 { |
428 uint64_t size = Configuration::GetGlobalIntegerParameter("MaximumStorageSize", 0); | 457 if (!Configuration::GetGlobalBoolParameter("DicomServerEnabled", true)) |
429 context->GetIndex().SetMaximumStorageSize(size * 1024 * 1024); | 458 { |
430 } | 459 LOG(WARNING) << "The DICOM server is disabled"; |
431 catch (...) | 460 return StartHttpServer(context, restApi); |
432 { | 461 } |
433 context->GetIndex().SetMaximumStorageSize(0); | 462 |
434 } | 463 MyDicomServerFactory serverFactory(context); |
435 | 464 |
436 | 465 // DICOM server |
437 #if ENABLE_PLUGINS == 1 | 466 DicomServer dicomServer; |
438 OrthancRestApi restApi(*context); | 467 OrthancApplicationEntityFilter dicomFilter(context); |
439 plugins.SetServerContext(*context); | 468 dicomServer.SetCalledApplicationEntityTitleCheck(Configuration::GetGlobalBoolParameter("DicomCheckCalledAet", false)); |
440 context->GetHttpHandler().Register(plugins, false); | 469 dicomServer.SetStoreRequestHandlerFactory(serverFactory); |
441 context->SetPlugins(plugins); | 470 dicomServer.SetMoveRequestHandlerFactory(serverFactory); |
442 #endif | 471 dicomServer.SetFindRequestHandlerFactory(serverFactory); |
443 | 472 dicomServer.SetPortNumber(Configuration::GetGlobalIntegerParameter("DicomPort", 4242)); |
473 dicomServer.SetApplicationEntityTitle(Configuration::GetGlobalStringParameter("DicomAet", "ORTHANC")); | |
474 dicomServer.SetApplicationEntityFilter(dicomFilter); | |
475 | |
476 dicomServer.Start(); | |
477 LOG(WARNING) << "DICOM server listening on port: " << dicomServer.GetPortNumber(); | |
478 | |
479 bool restart = StartHttpServer(context, restApi); | |
480 | |
481 dicomServer.Stop(); | |
482 LOG(WARNING) << " DICOM server has stopped"; | |
483 | |
484 serverFactory.Done(); | |
485 | |
486 return restart; | |
487 } | |
488 | |
489 | |
490 static bool ConfigureHttpHandler(ServerContext& context, | |
491 OrthancPlugins *plugins) | |
492 { | |
493 // By order of priority, first apply the "plugins" layer, so that | |
494 // plugins can overwrite the built-in REST API of Orthanc | |
495 if (plugins) | |
496 { | |
497 assert(context.HasPlugins()); | |
498 context.GetHttpHandler().Register(*plugins, false); | |
499 } | |
500 | |
501 // Secondly, apply the "static resources" layer | |
444 #if ORTHANC_STANDALONE == 1 | 502 #if ORTHANC_STANDALONE == 1 |
445 EmbeddedResourceHttpHandler staticResources("/app", EmbeddedResources::ORTHANC_EXPLORER); | 503 EmbeddedResourceHttpHandler staticResources("/app", EmbeddedResources::ORTHANC_EXPLORER); |
446 #else | 504 #else |
447 FilesystemHttpHandler staticResources("/app", ORTHANC_PATH "/OrthancExplorer"); | 505 FilesystemHttpHandler staticResources("/app", ORTHANC_PATH "/OrthancExplorer"); |
448 #endif | 506 #endif |
449 | 507 |
450 context->GetHttpHandler().Register(staticResources, false); | 508 context.GetHttpHandler().Register(staticResources, false); |
451 context->GetHttpHandler().Register(restApi, true); | 509 |
452 | 510 // Thirdly, consider the built-in REST API of Orthanc |
453 | 511 OrthancRestApi restApi(context); |
454 MyDicomServerFactory serverFactory(*context); | 512 context.GetHttpHandler().Register(restApi, true); |
455 bool isReset = false; | 513 |
456 | 514 return StartDicomServer(context, restApi); |
457 { | 515 } |
458 // DICOM server | 516 |
459 DicomServer dicomServer; | 517 |
460 OrthancApplicationEntityFilter dicomFilter(*context); | 518 static bool ConfigureServerContext(IDatabaseWrapper& database, |
461 dicomServer.SetCalledApplicationEntityTitleCheck(Configuration::GetGlobalBoolParameter("DicomCheckCalledAet", false)); | 519 IStorageArea& storageArea, |
462 dicomServer.SetStoreRequestHandlerFactory(serverFactory); | 520 OrthancPlugins *plugins) |
463 dicomServer.SetMoveRequestHandlerFactory(serverFactory); | 521 { |
464 dicomServer.SetFindRequestHandlerFactory(serverFactory); | 522 ServerContext context(database); |
465 dicomServer.SetPortNumber(Configuration::GetGlobalIntegerParameter("DicomPort", 4242)); | 523 context.SetStorageArea(storageArea); |
466 dicomServer.SetApplicationEntityTitle(Configuration::GetGlobalStringParameter("DicomAet", "ORTHANC")); | 524 |
467 dicomServer.SetApplicationEntityFilter(dicomFilter); | 525 context.SetCompressionEnabled(Configuration::GetGlobalBoolParameter("StorageCompression", false)); |
468 | 526 context.SetStoreMD5ForAttachments(Configuration::GetGlobalBoolParameter("StoreMD5ForAttachments", true)); |
469 // HTTP server | 527 |
470 MyIncomingHttpRequestFilter httpFilter(*context); | 528 try |
471 MongooseServer httpServer; | 529 { |
472 httpServer.SetPortNumber(Configuration::GetGlobalIntegerParameter("HttpPort", 8042)); | 530 context.GetIndex().SetMaximumPatientCount(Configuration::GetGlobalIntegerParameter("MaximumPatientCount", 0)); |
473 httpServer.SetRemoteAccessAllowed(Configuration::GetGlobalBoolParameter("RemoteAccessAllowed", false)); | 531 } |
474 httpServer.SetKeepAliveEnabled(Configuration::GetGlobalBoolParameter("KeepAlive", false)); | 532 catch (...) |
475 httpServer.SetIncomingHttpRequestFilter(httpFilter); | 533 { |
476 | 534 context.GetIndex().SetMaximumPatientCount(0); |
477 httpServer.SetAuthenticationEnabled(Configuration::GetGlobalBoolParameter("AuthenticationEnabled", false)); | 535 } |
478 Configuration::SetupRegisteredUsers(httpServer); | 536 |
479 | 537 try |
480 if (Configuration::GetGlobalBoolParameter("SslEnabled", false)) | 538 { |
481 { | 539 uint64_t size = Configuration::GetGlobalIntegerParameter("MaximumStorageSize", 0); |
482 std::string certificate = Configuration::InterpretStringParameterAsPath( | 540 context.GetIndex().SetMaximumStorageSize(size * 1024 * 1024); |
483 Configuration::GetGlobalStringParameter("SslCertificate", "certificate.pem")); | 541 } |
484 httpServer.SetSslEnabled(true); | 542 catch (...) |
485 httpServer.SetSslCertificate(certificate.c_str()); | 543 { |
486 } | 544 context.GetIndex().SetMaximumStorageSize(0); |
487 else | 545 } |
488 { | 546 |
489 httpServer.SetSslEnabled(false); | 547 LoadLuaScripts(context); |
490 } | 548 |
491 | 549 if (plugins) |
492 httpServer.Register(context->GetHttpHandler()); | 550 { |
493 | 551 plugins->SetServerContext(context); |
552 context.SetPlugins(*plugins); | |
553 } | |
554 | |
555 bool restart = ConfigureHttpHandler(context, plugins); | |
556 context.Stop(); | |
557 | |
558 if (plugins) | |
559 { | |
560 context.ResetPlugins(); | |
561 } | |
562 | |
563 return restart; | |
564 } | |
565 | |
566 | |
567 static bool ConfigurePlugins(int argc, | |
568 char* argv[]) | |
569 { | |
570 std::auto_ptr<IDatabaseWrapper> databasePtr; | |
571 std::auto_ptr<IStorageArea> storage; | |
494 | 572 |
495 #if ENABLE_PLUGINS == 1 | 573 #if ENABLE_PLUGINS == 1 |
496 // Prepare the storage area | 574 OrthancPlugins plugins; |
497 if (plugins.HasStorageArea()) | 575 plugins.SetCommandLineArguments(argc, argv); |
498 { | 576 LoadPlugins(plugins); |
499 LOG(WARNING) << "Using a custom storage area from plugins"; | 577 |
500 storage.reset(plugins.GetStorageArea()); | 578 IDatabaseWrapper* database = NULL; |
501 } | 579 if (plugins.HasDatabase()) |
502 else | 580 { |
581 LOG(WARNING) << "Using a custom database from plugins"; | |
582 database = &plugins.GetDatabase(); | |
583 } | |
584 else | |
585 { | |
586 databasePtr.reset(Configuration::CreateDatabaseWrapper()); | |
587 database = databasePtr.get(); | |
588 } | |
589 | |
590 if (plugins.HasStorageArea()) | |
591 { | |
592 LOG(WARNING) << "Using a custom storage area from plugins"; | |
593 storage.reset(plugins.CreateStorageArea()); | |
594 } | |
595 else | |
596 { | |
597 storage.reset(Configuration::CreateStorageArea()); | |
598 } | |
599 | |
600 assert(database != NULL); | |
601 assert(storage.get() != NULL); | |
602 | |
603 return ConfigureServerContext(*database, *storage, &plugins); | |
604 | |
605 #else | |
606 // The plugins are disabled | |
607 databasePtr.reset(Configuration::CreateDatabaseWrapper()); | |
608 storage.reset(Configuration::CreateStorageArea()); | |
609 | |
610 return ConfigureServerContext(*databasePtr, *storage, NULL); | |
503 #endif | 611 #endif |
504 { | 612 } |
505 storage.reset(Configuration::CreateStorageArea()); | 613 |
506 } | 614 |
507 | 615 static bool StartOrthanc(int argc, char* argv[]) |
508 context->SetStorageArea(*storage); | 616 { |
509 | 617 return ConfigurePlugins(argc, argv); |
510 // GO !!! Start the requested servers | 618 } |
511 if (Configuration::GetGlobalBoolParameter("HttpServerEnabled", true)) | |
512 { | |
513 #if ENABLE_PLUGINS == 1 | |
514 plugins.SetOrthancRestApi(restApi); | |
515 #endif | |
516 | |
517 httpServer.Start(); | |
518 LOG(WARNING) << "HTTP server listening on port: " << httpServer.GetPortNumber(); | |
519 } | |
520 else | |
521 { | |
522 LOG(WARNING) << "The HTTP server is disabled"; | |
523 } | |
524 | |
525 if (Configuration::GetGlobalBoolParameter("DicomServerEnabled", true)) | |
526 { | |
527 dicomServer.Start(); | |
528 LOG(WARNING) << "DICOM server listening on port: " << dicomServer.GetPortNumber(); | |
529 } | |
530 else | |
531 { | |
532 LOG(WARNING) << "The DICOM server is disabled"; | |
533 } | |
534 | |
535 LOG(WARNING) << "Orthanc has started"; | |
536 | |
537 Toolbox::ServerBarrier(restApi.ResetRequestReceivedFlag()); | |
538 isReset = restApi.ResetRequestReceivedFlag(); | |
539 | |
540 if (isReset) | |
541 { | |
542 LOG(WARNING) << "Reset request received, restarting Orthanc"; | |
543 } | |
544 | |
545 // We're done | |
546 LOG(WARNING) << "Orthanc is stopping"; | |
547 | |
548 context->Stop(); | |
549 | |
550 #if ENABLE_PLUGINS == 1 | |
551 context->ResetPlugins(); | |
552 plugins.ResetOrthancRestApi(); | |
553 LOG(WARNING) << " Plugins have stopped"; | |
554 #endif | |
555 | |
556 dicomServer.Stop(); | |
557 LOG(WARNING) << " DICOM server has stopped"; | |
558 | |
559 httpServer.Stop(); | |
560 LOG(WARNING) << " HTTP server has stopped"; | |
561 } | |
562 | |
563 serverFactory.Done(); | |
564 | |
565 return isReset; | |
566 } | |
567 | |
568 | |
569 | 619 |
570 | 620 |
571 int main(int argc, char* argv[]) | 621 int main(int argc, char* argv[]) |
572 { | 622 { |
573 // Initialize Google's logging library. | 623 // Initialize Google's logging library. |
644 { | 694 { |
645 for (;;) | 695 for (;;) |
646 { | 696 { |
647 OrthancInitialize(configurationFile); | 697 OrthancInitialize(configurationFile); |
648 | 698 |
649 bool reset = StartOrthanc(argc, argv); | 699 bool restart = StartOrthanc(argc, argv); |
650 if (reset) | 700 if (restart) |
651 { | 701 { |
652 OrthancFinalize(); | 702 OrthancFinalize(); |
653 } | 703 } |
654 else | 704 else |
655 { | 705 { |