Mercurial > hg > orthanc-gcp
comparison Plugin/Plugin.cpp @ 0:520cba9a0d42
initial commit
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Thu, 13 Jun 2019 14:57:22 +0200 |
parents | |
children | d9e1b60a9aa6 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:520cba9a0d42 |
---|---|
1 /** | |
2 * Google Cloud Platform credentials for DICOMweb and Orthanc | |
3 * Copyright (C) 2019 Osimis S.A., Belgium | |
4 * | |
5 * This program is free software: you can redistribute it and/or | |
6 * modify it under the terms of the GNU General Public License as | |
7 * published by the Free Software Foundation, either version 3 of the | |
8 * License, or (at your option) any later version. | |
9 * | |
10 * In addition, as a special exception, the copyright holders of this | |
11 * program give permission to link the code of its release with the | |
12 * OpenSSL project's "OpenSSL" library (or with modified versions of it | |
13 * that use the same license as the "OpenSSL" library), and distribute | |
14 * the linked executables. You must obey the GNU General Public License | |
15 * in all respects for all of the code used other than "OpenSSL". If you | |
16 * modify file(s) with this exception, you may extend this exception to | |
17 * your version of the file(s), but you are not obligated to do so. If | |
18 * you do not wish to do so, delete this exception statement from your | |
19 * version. If you delete this exception statement from all source files | |
20 * in the program, then also delete it here. | |
21 * | |
22 * This program is distributed in the hope that it will be useful, but | |
23 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
25 * General Public License for more details. | |
26 * | |
27 * You should have received a copy of the GNU General Public License | |
28 * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
29 **/ | |
30 | |
31 | |
32 #include "GoogleConfiguration.h" | |
33 #include "GoogleUpdater.h" | |
34 | |
35 #include <Core/HttpClient.h> | |
36 #include <Core/Toolbox.h> | |
37 #include <Plugins/Samples/Common/OrthancPluginCppWrapper.h> | |
38 | |
39 | |
40 static bool CheckDicomWebVersion() | |
41 { | |
42 Json::Value dicomweb; | |
43 if (!OrthancPlugins::RestApiGet(dicomweb, "/plugins/dicom-web", false)) | |
44 { | |
45 LOG(ERROR) << "The DICOMweb plugin is not installed, cannot start Google Cloud Platform plugin"; | |
46 return false; | |
47 } | |
48 else if (dicomweb.type() != Json::objectValue || | |
49 !dicomweb.isMember("Version") || | |
50 dicomweb["Version"].type() != Json::stringValue) | |
51 { | |
52 LOG(ERROR) << "Cannot determine the version of the DICOMweb plugin, start Google Cloud Platform plugin"; | |
53 return false; | |
54 } | |
55 else | |
56 { | |
57 std::string version = dicomweb["Version"].asString(); | |
58 | |
59 std::vector<std::string> tokens; | |
60 Orthanc::Toolbox::TokenizeString(tokens, version, '.'); | |
61 | |
62 bool ok = true; | |
63 | |
64 if (tokens.size() == 2) | |
65 { | |
66 try | |
67 { | |
68 int major = boost::lexical_cast<int>(tokens[0]); | |
69 int minor = boost::lexical_cast<int>(tokens[1]); | |
70 | |
71 ok = (major >= 1 || | |
72 (major == 0 && minor >= 7)); | |
73 } | |
74 catch (boost::bad_lexical_cast&) | |
75 { | |
76 ok = false; | |
77 } | |
78 } | |
79 | |
80 if (!ok) | |
81 { | |
82 LOG(ERROR) << "The DICOMweb version (currently " << version | |
83 << ") must be above 0.7 to use the Google Cloud Platform plugin"; | |
84 } | |
85 | |
86 return ok; | |
87 } | |
88 } | |
89 | |
90 | |
91 OrthancPluginErrorCode OnChangeCallback(OrthancPluginChangeType changeType, | |
92 OrthancPluginResourceType resourceType, | |
93 const char* resourceId) | |
94 { | |
95 try | |
96 { | |
97 switch (changeType) | |
98 { | |
99 case OrthancPluginChangeType_OrthancStarted: | |
100 { | |
101 if (CheckDicomWebVersion()) | |
102 { | |
103 GoogleUpdater::GetInstance().Start(); | |
104 } | |
105 | |
106 break; | |
107 } | |
108 | |
109 case OrthancPluginChangeType_OrthancStopped: | |
110 GoogleUpdater::GetInstance().Stop(); | |
111 break; | |
112 | |
113 default: | |
114 break; | |
115 } | |
116 } | |
117 catch (Orthanc::OrthancException& e) | |
118 { | |
119 LOG(ERROR) << "Exception in the change callback: " << e.What(); | |
120 return static_cast<OrthancPluginErrorCode>(e.GetErrorCode()); | |
121 } | |
122 | |
123 return OrthancPluginErrorCode_Success; | |
124 } | |
125 | |
126 | |
127 | |
128 extern "C" | |
129 { | |
130 ORTHANC_PLUGINS_API int32_t OrthancPluginInitialize(OrthancPluginContext* context) | |
131 { | |
132 OrthancPlugins::SetGlobalContext(context); | |
133 Orthanc::Logging::Initialize(context); | |
134 | |
135 /* Check the version of the Orthanc core */ | |
136 if (OrthancPluginCheckVersion(context) == 0) | |
137 { | |
138 OrthancPlugins::ReportMinimalOrthancVersion(ORTHANC_PLUGINS_MINIMAL_MAJOR_NUMBER, | |
139 ORTHANC_PLUGINS_MINIMAL_MINOR_NUMBER, | |
140 ORTHANC_PLUGINS_MINIMAL_REVISION_NUMBER); | |
141 return -1; | |
142 } | |
143 | |
144 try | |
145 { | |
146 Orthanc::Toolbox::InitializeOpenSsl(); | |
147 Orthanc::HttpClient::GlobalInitialize(); | |
148 | |
149 GoogleConfiguration::GetInstance(); // Force the initialization of the singleton | |
150 | |
151 OrthancPluginRegisterOnChangeCallback(context, OnChangeCallback); | |
152 } | |
153 catch (Orthanc::OrthancException& e) | |
154 { | |
155 OrthancPlugins::LogError("Error while initializing the Google Cloud Platform plugin: " + | |
156 std::string(e.What())); | |
157 return -1; | |
158 } | |
159 | |
160 return 0; | |
161 } | |
162 | |
163 | |
164 ORTHANC_PLUGINS_API void OrthancPluginFinalize() | |
165 { | |
166 try | |
167 { | |
168 GoogleUpdater::GetInstance().Stop(); | |
169 Orthanc::HttpClient::GlobalFinalize(); | |
170 Orthanc::Toolbox::FinalizeOpenSsl(); | |
171 } | |
172 catch (Orthanc::OrthancException&) | |
173 { | |
174 } | |
175 } | |
176 | |
177 | |
178 ORTHANC_PLUGINS_API const char* OrthancPluginGetName() | |
179 { | |
180 return "google-cloud-platform"; | |
181 } | |
182 | |
183 | |
184 ORTHANC_PLUGINS_API const char* OrthancPluginGetVersion() | |
185 { | |
186 return GCP_PLUGIN_VERSION; | |
187 } | |
188 } |