comparison OrthancServer/main.cpp @ 759:8cfc6119a5bd dicom-rt

integration mainline -> dicom-rt
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 16 Apr 2014 16:04:55 +0200
parents e318e9d49815 4afad8cb94fd
children e57e08ed510f
comparison
equal deleted inserted replaced
605:b82292ba2083 759:8cfc6119a5bd
1 /** 1 /**
2 * Orthanc - A Lightweight, RESTful DICOM Store 2 * Orthanc - A Lightweight, RESTful DICOM Store
3 * Copyright (C) 2012-2013 Medical Physics Department, CHU of Liege, 3 * Copyright (C) 2012-2014 Medical Physics Department, CHU of Liege,
4 * Belgium 4 * Belgium
5 * 5 *
6 * This program is free software: you can redistribute it and/or 6 * This program is free software: you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as 7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation, either version 3 of the 8 * published by the Free Software Foundation, either version 3 of the
28 * You should have received a copy of the GNU General Public License 28 * You should have received a copy of the GNU General Public License
29 * along with this program. If not, see <http://www.gnu.org/licenses/>. 29 * along with this program. If not, see <http://www.gnu.org/licenses/>.
30 **/ 30 **/
31 31
32 32
33 //#include "OrthancRestApi.h" 33 #include "OrthancRestApi/OrthancRestApi.h"
34 #include "RadiotherapyRestApi.h"
35 34
36 #include <fstream> 35 #include <fstream>
37 #include <glog/logging.h> 36 #include <glog/logging.h>
38 #include <boost/algorithm/string/predicate.hpp> 37 #include <boost/algorithm/string/predicate.hpp>
39 38
40 #include "../Core/HttpServer/EmbeddedResourceHttpHandler.h" 39 #include "../Core/HttpServer/EmbeddedResourceHttpHandler.h"
41 #include "../Core/HttpServer/FilesystemHttpHandler.h" 40 #include "../Core/HttpServer/FilesystemHttpHandler.h"
42 #include "../Core/Lua/LuaFunctionCall.h" 41 #include "../Core/Lua/LuaFunctionCall.h"
43 #include "../Core/DicomFormat/DicomArray.h" 42 #include "../Core/DicomFormat/DicomArray.h"
44 #include "DicomProtocol/DicomServer.h" 43 #include "DicomProtocol/DicomServer.h"
44 #include "DicomProtocol/DicomUserConnection.h"
45 #include "OrthancInitialization.h" 45 #include "OrthancInitialization.h"
46 #include "ServerContext.h" 46 #include "ServerContext.h"
47 #include "OrthancFindRequestHandler.h"
48 #include "OrthancMoveRequestHandler.h"
49 #include "ServerToolbox.h"
47 50
48 using namespace Orthanc; 51 using namespace Orthanc;
49 52
50 53
51 54
52 class MyStoreRequestHandler : public IStoreRequestHandler 55 class OrthancStoreRequestHandler : public IStoreRequestHandler
53 { 56 {
54 private: 57 private:
55 ServerContext& server_; 58 ServerContext& server_;
56 59
57 public: 60 public:
58 MyStoreRequestHandler(ServerContext& context) : 61 OrthancStoreRequestHandler(ServerContext& context) :
59 server_(context) 62 server_(context)
60 { 63 {
61 } 64 }
62 65
63 virtual void Handle(const std::string& dicomFile, 66 virtual void Handle(const std::string& dicomFile,
71 } 74 }
72 } 75 }
73 }; 76 };
74 77
75 78
76 class MyFindRequestHandler : public IFindRequestHandler
77 {
78 private:
79 ServerContext& context_;
80
81 public:
82 MyFindRequestHandler(ServerContext& context) :
83 context_(context)
84 {
85 }
86
87 virtual void Handle(const DicomMap& input,
88 DicomFindAnswers& answers)
89 {
90 LOG(WARNING) << "Find-SCU request received";
91 DicomArray a(input);
92 a.Print(stdout);
93 }
94 };
95
96
97 class MyMoveRequestHandler : public IMoveRequestHandler
98 {
99 private:
100 ServerContext& context_;
101
102 public:
103 MyMoveRequestHandler(ServerContext& context) :
104 context_(context)
105 {
106 }
107
108 public:
109 virtual IMoveRequestIterator* Handle(const std::string& target,
110 const DicomMap& input)
111 {
112 LOG(WARNING) << "Move-SCU request received";
113 return NULL;
114 }
115 };
116
117 79
118 class MyDicomServerFactory : 80 class MyDicomServerFactory :
119 public IStoreRequestHandlerFactory, 81 public IStoreRequestHandlerFactory,
120 public IFindRequestHandlerFactory, 82 public IFindRequestHandlerFactory,
121 public IMoveRequestHandlerFactory 83 public IMoveRequestHandlerFactory
128 { 90 {
129 } 91 }
130 92
131 virtual IStoreRequestHandler* ConstructStoreRequestHandler() 93 virtual IStoreRequestHandler* ConstructStoreRequestHandler()
132 { 94 {
133 return new MyStoreRequestHandler(context_); 95 return new OrthancStoreRequestHandler(context_);
134 } 96 }
135 97
136 virtual IFindRequestHandler* ConstructFindRequestHandler() 98 virtual IFindRequestHandler* ConstructFindRequestHandler()
137 { 99 {
138 return new MyFindRequestHandler(context_); 100 return new OrthancFindRequestHandler(context_);
139 } 101 }
140 102
141 virtual IMoveRequestHandler* ConstructMoveRequestHandler() 103 virtual IMoveRequestHandler* ConstructMoveRequestHandler()
142 { 104 {
143 return new MyMoveRequestHandler(context_); 105 return new OrthancMoveRequestHandler(context_);
144 } 106 }
145 107
146 void Done() 108 void Done()
147 { 109 {
110 }
111 };
112
113
114 class OrthancApplicationEntityFilter : public IApplicationEntityFilter
115 {
116 public:
117 virtual bool IsAllowedConnection(const std::string& /*callingIp*/,
118 const std::string& /*callingAet*/)
119 {
120 return true;
121 }
122
123 virtual bool IsAllowedRequest(const std::string& /*callingIp*/,
124 const std::string& callingAet,
125 DicomRequestType type)
126 {
127 if (type == DicomRequestType_Store)
128 {
129 // Incoming store requests are always accepted, even from unknown AET
130 return true;
131 }
132
133 if (!IsKnownAETitle(callingAet))
134 {
135 LOG(ERROR) << "Unknown remote DICOM modality AET: \"" << callingAet << "\"";
136 return false;
137 }
138 else
139 {
140 return true;
141 }
148 } 142 }
149 }; 143 };
150 144
151 145
152 class MyIncomingHttpRequestFilter : public IIncomingHttpRequestFilter 146 class MyIncomingHttpRequestFilter : public IIncomingHttpRequestFilter
238 232
239 void PrintVersion(char* path) 233 void PrintVersion(char* path)
240 { 234 {
241 std::cout 235 std::cout
242 << path << " " << ORTHANC_VERSION << std::endl 236 << path << " " << ORTHANC_VERSION << std::endl
243 << "Copyright (C) 2012-2013 Medical Physics Department, CHU of Liege (Belgium) " << std::endl 237 << "Copyright (C) 2012-2014 Medical Physics Department, CHU of Liege (Belgium) " << std::endl
244 << "Licensing GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>, with OpenSSL exception." << std::endl 238 << "Licensing GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>, with OpenSSL exception." << std::endl
245 << "This is free software: you are free to change and redistribute it." << std::endl 239 << "This is free software: you are free to change and redistribute it." << std::endl
246 << "There is NO WARRANTY, to the extent permitted by law." << std::endl 240 << "There is NO WARRANTY, to the extent permitted by law." << std::endl
247 << std::endl 241 << std::endl
248 << "Written by Sebastien Jodogne <s.jodogne@gmail.com>" << std::endl; 242 << "Written by Sebastien Jodogne <s.jodogne@gmail.com>" << std::endl;
289 283
290 if (boost::starts_with(argv[i], "--config=")) 284 if (boost::starts_with(argv[i], "--config="))
291 { 285 {
292 std::string configurationSample; 286 std::string configurationSample;
293 GetFileResource(configurationSample, EmbeddedResources::CONFIGURATION_SAMPLE); 287 GetFileResource(configurationSample, EmbeddedResources::CONFIGURATION_SAMPLE);
288
289 #if defined(_WIN32)
290 // Replace UNIX newlines with DOS newlines
291 boost::replace_all(configurationSample, "\n", "\r\n");
292 #endif
294 293
295 std::string target = std::string(argv[i]).substr(9); 294 std::string target = std::string(argv[i]).substr(9);
296 std::ofstream f(target.c_str()); 295 std::ofstream f(target.c_str());
297 f << configurationSample; 296 f << configurationSample;
298 f.close(); 297 f.close();
333 332
334 LOG(WARNING) << "Storage directory: " << storageDirectory; 333 LOG(WARNING) << "Storage directory: " << storageDirectory;
335 LOG(WARNING) << "Index directory: " << indexDirectory; 334 LOG(WARNING) << "Index directory: " << indexDirectory;
336 335
337 context.SetCompressionEnabled(GetGlobalBoolParameter("StorageCompression", false)); 336 context.SetCompressionEnabled(GetGlobalBoolParameter("StorageCompression", false));
337 context.SetStoreMD5ForAttachments(GetGlobalBoolParameter("StoreMD5ForAttachments", true));
338 338
339 std::list<std::string> luaScripts; 339 std::list<std::string> luaScripts;
340 GetGlobalListOfStringsParameter(luaScripts, "LuaScripts"); 340 GetGlobalListOfStringsParameter(luaScripts, "LuaScripts");
341 for (std::list<std::string>::const_iterator 341 for (std::list<std::string>::const_iterator
342 it = luaScripts.begin(); it != luaScripts.end(); it++) 342 it = luaScripts.begin(); it != luaScripts.end(); ++it)
343 { 343 {
344 std::string path = InterpretStringParameterAsPath(*it); 344 std::string path = InterpretStringParameterAsPath(*it);
345 LOG(WARNING) << "Installing the Lua scripts from: " << path; 345 LOG(WARNING) << "Installing the Lua scripts from: " << path;
346 std::string script; 346 std::string script;
347 Toolbox::ReadFile(script, path); 347 Toolbox::ReadFile(script, path);
368 context.GetIndex().SetMaximumStorageSize(0); 368 context.GetIndex().SetMaximumStorageSize(0);
369 } 369 }
370 370
371 MyDicomServerFactory serverFactory(context); 371 MyDicomServerFactory serverFactory(context);
372 372
373
374 { 373 {
375 // DICOM server 374 // DICOM server
376 DicomServer dicomServer; 375 DicomServer dicomServer;
376 OrthancApplicationEntityFilter dicomFilter;
377 dicomServer.SetCalledApplicationEntityTitleCheck(GetGlobalBoolParameter("DicomCheckCalledAet", false)); 377 dicomServer.SetCalledApplicationEntityTitleCheck(GetGlobalBoolParameter("DicomCheckCalledAet", false));
378 dicomServer.SetStoreRequestHandlerFactory(serverFactory); 378 dicomServer.SetStoreRequestHandlerFactory(serverFactory);
379 //dicomServer.SetMoveRequestHandlerFactory(serverFactory); 379 dicomServer.SetMoveRequestHandlerFactory(serverFactory);
380 //dicomServer.SetFindRequestHandlerFactory(serverFactory); 380 dicomServer.SetFindRequestHandlerFactory(serverFactory);
381 dicomServer.SetPortNumber(GetGlobalIntegerParameter("DicomPort", 4242)); 381 dicomServer.SetPortNumber(GetGlobalIntegerParameter("DicomPort", 4242));
382 dicomServer.SetApplicationEntityTitle(GetGlobalStringParameter("DicomAet", "ORTHANC")); 382 dicomServer.SetApplicationEntityTitle(GetGlobalStringParameter("DicomAet", "ORTHANC"));
383 dicomServer.SetApplicationEntityFilter(dicomFilter);
383 384
384 // HTTP server 385 // HTTP server
385 MyIncomingHttpRequestFilter httpFilter(context); 386 MyIncomingHttpRequestFilter httpFilter(context);
386 MongooseServer httpServer; 387 MongooseServer httpServer;
387 httpServer.SetPortNumber(GetGlobalIntegerParameter("HttpPort", 8042)); 388 httpServer.SetPortNumber(GetGlobalIntegerParameter("HttpPort", 8042));
401 else 402 else
402 { 403 {
403 httpServer.SetSslEnabled(false); 404 httpServer.SetSslEnabled(false);
404 } 405 }
405 406
406 LOG(WARNING) << "DICOM server listening on port: " << dicomServer.GetPortNumber();
407 LOG(WARNING) << "HTTP server listening on port: " << httpServer.GetPortNumber();
408
409 #if ORTHANC_STANDALONE == 1 407 #if ORTHANC_STANDALONE == 1
410 httpServer.RegisterHandler(new EmbeddedResourceHttpHandler("/app", EmbeddedResources::ORTHANC_EXPLORER)); 408 httpServer.RegisterHandler(new EmbeddedResourceHttpHandler("/app", EmbeddedResources::ORTHANC_EXPLORER));
411 #else 409 #else
412 httpServer.RegisterHandler(new FilesystemHttpHandler("/app", ORTHANC_PATH "/OrthancExplorer")); 410 httpServer.RegisterHandler(new FilesystemHttpHandler("/app", ORTHANC_PATH "/OrthancExplorer"));
413 #endif 411 #endif
414 412
415 //httpServer.RegisterHandler(new OrthancRestApi(context)); 413 httpServer.RegisterHandler(new OrthancRestApi(context));
416 httpServer.RegisterHandler(new RadiotherapyRestApi(context)); 414
417 415 // GO !!! Start the requested servers
418 // GO !!! 416 if (GetGlobalBoolParameter("HttpServerEnabled", true))
419 httpServer.Start(); 417 {
420 dicomServer.Start(); 418 httpServer.Start();
419 LOG(WARNING) << "HTTP server listening on port: " << httpServer.GetPortNumber();
420 }
421 else
422 {
423 LOG(WARNING) << "The HTTP server is disabled";
424 }
425
426 if (GetGlobalBoolParameter("DicomServerEnabled", true))
427 {
428 dicomServer.Start();
429 LOG(WARNING) << "DICOM server listening on port: " << dicomServer.GetPortNumber();
430 }
431 else
432 {
433 LOG(WARNING) << "The DICOM server is disabled";
434 }
421 435
422 LOG(WARNING) << "Orthanc has started"; 436 LOG(WARNING) << "Orthanc has started";
423 Toolbox::ServerBarrier(); 437 Toolbox::ServerBarrier();
424 438
425 // Stop 439 // We're done
426 LOG(WARNING) << "Orthanc is stopping"; 440 LOG(WARNING) << "Orthanc is stopping";
427 } 441 }
428 442
429 serverFactory.Done(); 443 serverFactory.Done();
430 } 444 }
439 status = -1; 453 status = -1;
440 } 454 }
441 455
442 OrthancFinalize(); 456 OrthancFinalize();
443 457
458 LOG(WARNING) << "Orthanc has stopped";
459
444 return status; 460 return status;
445 } 461 }