comparison OrthancServer/Plugins/Samples/MultitenantDicom/Plugin.cpp @ 5273:7cb1b851f5c8

Added a sample plugin bringing multitenant DICOM support through labels
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 14 Apr 2023 11:49:24 +0200
parents
children acaea72a3e91
comparison
equal deleted inserted replaced
5272:a45e8b6115f6 5273:7cb1b851f5c8
1 /**
2 * Orthanc - A Lightweight, RESTful DICOM Store
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
4 * Department, University Hospital of Liege, Belgium
5 * Copyright (C) 2017-2023 Osimis S.A., Belgium
6 * Copyright (C) 2021-2023 Sebastien Jodogne, ICTEAM UCLouvain, Belgium
7 *
8 * This program is free software: you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public License
10 * as published by the Free Software Foundation, either version 3 of
11 * the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this program. If not, see
20 * <http://www.gnu.org/licenses/>.
21 **/
22
23
24 #include "MultitenantDicomServer.h"
25
26 #include "../../../../OrthancFramework/Sources/DicomParsing/FromDcmtkBridge.h"
27 #include "../../../../OrthancFramework/Sources/Logging.h"
28 #include "../../../../OrthancFramework/Sources/OrthancException.h"
29
30 #include "../Common/OrthancPluginCppWrapper.h"
31
32
33 typedef std::list<MultitenantDicomServer*> DicomServers;
34
35 static DicomServers dicomServers_;
36
37
38 static OrthancPluginErrorCode OnChangeCallback(OrthancPluginChangeType changeType,
39 OrthancPluginResourceType resourceType,
40 const char* resourceId)
41 {
42 switch (changeType)
43 {
44 case OrthancPluginChangeType_OrthancStarted:
45 {
46 for (DicomServers::iterator it = dicomServers_.begin(); it != dicomServers_.end(); ++it)
47 {
48 if (*it != NULL)
49 {
50 try
51 {
52 (*it)->Start();
53 }
54 catch (Orthanc::OrthancException& e)
55 {
56 LOG(ERROR) << "Exception while stopping the multitenant DICOM server: " << e.What();
57 }
58 }
59 }
60
61 break;
62 }
63
64 case OrthancPluginChangeType_OrthancStopped:
65 {
66 for (DicomServers::iterator it = dicomServers_.begin(); it != dicomServers_.end(); ++it)
67 {
68 if (*it != NULL)
69 {
70 try
71 {
72 (*it)->Stop();
73 }
74 catch (Orthanc::OrthancException& e)
75 {
76 LOG(ERROR) << "Exception while stopping the multitenant DICOM server: " << e.What();
77 }
78 }
79 }
80
81 break;
82 }
83
84 default:
85 break;
86 }
87
88 return OrthancPluginErrorCode_Success;
89 }
90
91
92 extern "C"
93 {
94 ORTHANC_PLUGINS_API int32_t OrthancPluginInitialize(OrthancPluginContext* context)
95 {
96 OrthancPlugins::SetGlobalContext(context);
97
98 /* Check the version of the Orthanc core */
99 if (OrthancPluginCheckVersion(OrthancPlugins::GetGlobalContext()) == 0)
100 {
101 char info[1024];
102 sprintf(info, "Your version of Orthanc (%s) must be above %d.%d.%d to run this plugin",
103 OrthancPlugins::GetGlobalContext()->orthancVersion,
104 ORTHANC_PLUGINS_MINIMAL_MAJOR_NUMBER,
105 ORTHANC_PLUGINS_MINIMAL_MINOR_NUMBER,
106 ORTHANC_PLUGINS_MINIMAL_REVISION_NUMBER);
107 OrthancPluginLogError(OrthancPlugins::GetGlobalContext(), info);
108 return -1;
109 }
110
111 #if ORTHANC_FRAMEWORK_VERSION_IS_ABOVE(1, 7, 2)
112 Orthanc::Logging::InitializePluginContext(context);
113 #else
114 Orthanc::Logging::Initialize(context);
115 #endif
116
117 if (!OrthancPlugins::CheckMinimalOrthancVersion(1, 12, 0))
118 {
119 OrthancPlugins::ReportMinimalOrthancVersion(1, 12, 0);
120 return -1;
121 }
122
123 Orthanc::FromDcmtkBridge::InitializeDictionary(false /* loadPrivateDictionary */);
124 /* Disable "gethostbyaddr" (which results in memory leaks) and use raw IP addresses */
125 dcmDisableGethostbyaddr.set(OFTrue);
126
127 OrthancPluginSetDescription(context, "Multitenant plugin for Orthanc.");
128
129 OrthancPluginRegisterOnChangeCallback(context, OnChangeCallback);
130
131 try
132 {
133 OrthancPlugins::OrthancConfiguration globalConfig;
134
135 OrthancPlugins::OrthancConfiguration pluginConfig;
136 globalConfig.GetSection(pluginConfig, KEY_MULTITENANT_DICOM);
137
138 if (pluginConfig.GetJson().isMember(KEY_SERVERS))
139 {
140 const Json::Value& servers = pluginConfig.GetJson() [KEY_SERVERS];
141
142 if (servers.type() != Json::arrayValue)
143 {
144 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadParameterType, "Configuration option \"" +
145 std::string(KEY_MULTITENANT_DICOM) + "." + std::string(KEY_SERVERS) + "\" must be an array");
146 }
147 else
148 {
149 for (Json::Value::ArrayIndex i = 0; i < servers.size(); i++)
150 {
151 dicomServers_.push_back(new MultitenantDicomServer(servers[i]));
152 }
153 }
154 }
155
156 return 0;
157 }
158 catch (Orthanc::OrthancException& e)
159 {
160 LOG(ERROR) << "Exception while starting the multitenant DICOM server: " << e.What();
161 return -1;
162 }
163 }
164
165
166 ORTHANC_PLUGINS_API void OrthancPluginFinalize()
167 {
168 for (DicomServers::iterator it = dicomServers_.begin(); it != dicomServers_.end(); ++it)
169 {
170 if (*it != NULL)
171 {
172 try
173 {
174 delete *it;
175 }
176 catch (Orthanc::OrthancException& e)
177 {
178 LOG(ERROR) << "Exception while destroying the multitenant DICOM server: " << e.What();
179 }
180 }
181 }
182 }
183
184
185 ORTHANC_PLUGINS_API const char* OrthancPluginGetName()
186 {
187 return "multitenant-dicom";
188 }
189
190
191 ORTHANC_PLUGINS_API const char* OrthancPluginGetVersion()
192 {
193 return ORTHANC_PLUGIN_VERSION;
194 }
195 }