Mercurial > hg > orthanc-python
annotate Sources/OnChangeCallback.cpp @ 75:cbfc72a53970
refactoring calls to PythonLock::RaiseException()
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Mon, 09 Aug 2021 16:59:59 +0200 |
parents | 0b3ef425db31 |
children | 55c41aa7053b |
rev | line source |
---|---|
0 | 1 /** |
2 * Python plugin for Orthanc | |
56
23f3099bed47
upgrade to year 2021
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
45
diff
changeset
|
3 * Copyright (C) 2020-2021 Osimis S.A., Belgium |
0 | 4 * |
5 * This program is free software: you can redistribute it and/or | |
6 * modify it under the terms of the GNU Affero General Public License | |
7 * as published by the Free Software Foundation, either version 3 of | |
8 * the License, or (at your option) any later version. | |
9 * | |
10 * This program is distributed in the hope that it will be useful, but | |
11 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 * Affero General Public License for more details. | |
14 * | |
15 * You should have received a copy of the GNU Affero General Public License | |
16 * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
17 **/ | |
18 | |
19 | |
20 #include "OnChangeCallback.h" | |
21 | |
68
0b3ef425db31
refactoring using ICallbackRegistration::Apply()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
58
diff
changeset
|
22 #include "../Resources/Orthanc/Plugins/OrthancPluginCppWrapper.h" |
0b3ef425db31
refactoring using ICallbackRegistration::Apply()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
58
diff
changeset
|
23 #include "ICallbackRegistration.h" |
45
ee76cced46a5
Fix issue #185 (segfaults on non-UTF8 special characters in request URI)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
36
diff
changeset
|
24 #include "PythonString.h" |
0 | 25 |
36
fd58eb5749ed
CMake simplification using DownloadOrthancFramework.cmake
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
13
diff
changeset
|
26 #include <Compatibility.h> // For std::unique_ptr<> |
0 | 27 |
28 #include <boost/thread.hpp> | |
29 | |
30 | |
31 class PendingChange : public boost::noncopyable | |
32 { | |
33 private: | |
34 OrthancPluginChangeType changeType_; | |
35 OrthancPluginResourceType resourceType_; | |
36 std::string resourceId_; | |
37 | |
38 public: | |
39 PendingChange(OrthancPluginChangeType changeType, | |
40 OrthancPluginResourceType resourceType, | |
41 const char* resourceId) : | |
42 changeType_(changeType), | |
43 resourceType_(resourceType) | |
44 { | |
45 if (resourceId == NULL) | |
46 { | |
47 resourceId_.clear(); | |
48 } | |
49 else | |
50 { | |
51 resourceId_.assign(resourceId); | |
52 } | |
53 } | |
54 | |
55 OrthancPluginChangeType GetChangeType() const | |
56 { | |
57 return changeType_; | |
58 } | |
59 | |
60 OrthancPluginResourceType GetResourceType() const | |
61 { | |
62 return resourceType_; | |
63 } | |
64 | |
65 const std::string& GetResourceId() const | |
66 { | |
67 return resourceId_; | |
68 } | |
69 }; | |
70 | |
71 | |
72 | |
57
46fe70776d61
Fix possible deadlock with "orthanc.RegisterOnChangeCallback()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
56
diff
changeset
|
73 // This corresponds to a simplified, standalone version of |
46fe70776d61
Fix possible deadlock with "orthanc.RegisterOnChangeCallback()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
56
diff
changeset
|
74 // "Orthanc::SharedMessageQueue" from the Orthanc framework |
0 | 75 class PendingChanges : public boost::noncopyable |
76 { | |
77 private: | |
78 typedef std::list<PendingChange*> Queue; | |
79 | |
80 boost::mutex mutex_; | |
81 Queue queue_; | |
82 boost::condition_variable elementAvailable_; | |
83 | |
84 public: | |
85 ~PendingChanges() | |
86 { | |
87 for (Queue::iterator it = queue_.begin(); it != queue_.end(); ++it) | |
88 { | |
89 assert(*it != NULL); | |
90 delete *it; | |
91 } | |
92 } | |
93 | |
94 void Enqueue(OrthancPluginChangeType changeType, | |
95 OrthancPluginResourceType resourceType, | |
96 const char* resourceId) | |
97 { | |
98 boost::mutex::scoped_lock lock(mutex_); | |
99 queue_.push_back(new PendingChange(changeType, resourceType, resourceId)); | |
100 elementAvailable_.notify_one(); | |
101 } | |
102 | |
103 PendingChange* Dequeue(unsigned int millisecondsTimeout) | |
104 { | |
13 | 105 if (millisecondsTimeout == 0) |
0 | 106 { |
107 ORTHANC_PLUGINS_THROW_EXCEPTION(ParameterOutOfRange); | |
108 } | |
109 | |
110 boost::mutex::scoped_lock lock(mutex_); | |
111 | |
112 // Wait for a message to arrive in the FIFO queue | |
113 while (queue_.empty()) | |
114 { | |
115 bool success = elementAvailable_.timed_wait | |
116 (lock, boost::posix_time::milliseconds(millisecondsTimeout)); | |
117 if (!success) | |
118 { | |
119 return NULL; | |
120 } | |
121 } | |
122 | |
36
fd58eb5749ed
CMake simplification using DownloadOrthancFramework.cmake
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
13
diff
changeset
|
123 std::unique_ptr<PendingChange> change(queue_.front()); |
0 | 124 queue_.pop_front(); |
125 | |
126 return change.release(); | |
127 } | |
128 }; | |
129 | |
130 | |
131 | |
132 static PendingChanges pendingChanges_; | |
133 static bool stopping_ = false; | |
134 static boost::thread changesThread_; | |
135 static PyObject* changesCallback_ = NULL; | |
136 | |
137 | |
58
ef1a1ce0c1e3
During Orthanc shutdown, wait for all the pending events to have been processed
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
57
diff
changeset
|
138 static void StopThread() |
ef1a1ce0c1e3
During Orthanc shutdown, wait for all the pending events to have been processed
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
57
diff
changeset
|
139 { |
ef1a1ce0c1e3
During Orthanc shutdown, wait for all the pending events to have been processed
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
57
diff
changeset
|
140 stopping_ = true; |
ef1a1ce0c1e3
During Orthanc shutdown, wait for all the pending events to have been processed
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
57
diff
changeset
|
141 |
ef1a1ce0c1e3
During Orthanc shutdown, wait for all the pending events to have been processed
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
57
diff
changeset
|
142 if (changesThread_.joinable()) |
ef1a1ce0c1e3
During Orthanc shutdown, wait for all the pending events to have been processed
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
57
diff
changeset
|
143 { |
ef1a1ce0c1e3
During Orthanc shutdown, wait for all the pending events to have been processed
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
57
diff
changeset
|
144 changesThread_.join(); |
ef1a1ce0c1e3
During Orthanc shutdown, wait for all the pending events to have been processed
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
57
diff
changeset
|
145 } |
ef1a1ce0c1e3
During Orthanc shutdown, wait for all the pending events to have been processed
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
57
diff
changeset
|
146 } |
ef1a1ce0c1e3
During Orthanc shutdown, wait for all the pending events to have been processed
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
57
diff
changeset
|
147 |
ef1a1ce0c1e3
During Orthanc shutdown, wait for all the pending events to have been processed
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
57
diff
changeset
|
148 |
0 | 149 static void ChangesWorker() |
150 { | |
151 while (!stopping_) | |
152 { | |
153 for (;;) | |
154 { | |
155 std::unique_ptr<PendingChange> change(pendingChanges_.Dequeue(100)); | |
156 if (change.get() == NULL) | |
157 { | |
158 break; | |
159 } | |
160 else if (changesCallback_ != NULL) | |
161 { | |
162 try | |
163 { | |
164 PythonLock lock; | |
45
ee76cced46a5
Fix issue #185 (segfaults on non-UTF8 special characters in request URI)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
36
diff
changeset
|
165 |
ee76cced46a5
Fix issue #185 (segfaults on non-UTF8 special characters in request URI)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
36
diff
changeset
|
166 PythonString resourceId(lock, change->GetResourceId()); |
ee76cced46a5
Fix issue #185 (segfaults on non-UTF8 special characters in request URI)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
36
diff
changeset
|
167 |
0 | 168 PythonObject args(lock, PyTuple_New(3)); |
169 PyTuple_SetItem(args.GetPyObject(), 0, PyLong_FromLong(change->GetChangeType())); | |
170 PyTuple_SetItem(args.GetPyObject(), 1, PyLong_FromLong(change->GetResourceType())); | |
45
ee76cced46a5
Fix issue #185 (segfaults on non-UTF8 special characters in request URI)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
36
diff
changeset
|
171 PyTuple_SetItem(args.GetPyObject(), 2, resourceId.Release()); |
ee76cced46a5
Fix issue #185 (segfaults on non-UTF8 special characters in request URI)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
36
diff
changeset
|
172 |
0 | 173 PythonObject result(lock, PyObject_CallObject(changesCallback_, args.GetPyObject())); |
3
26762eb9d704
reporting of exceptions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
174 |
26762eb9d704
reporting of exceptions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
175 std::string traceback; |
26762eb9d704
reporting of exceptions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
176 if (lock.HasErrorOccurred(traceback)) |
26762eb9d704
reporting of exceptions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
177 { |
26762eb9d704
reporting of exceptions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
178 OrthancPlugins::LogError("Error in the Python on-change callback, " |
26762eb9d704
reporting of exceptions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
179 "traceback:\n" + traceback); |
26762eb9d704
reporting of exceptions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
180 } |
0 | 181 } |
182 catch (OrthancPlugins::PluginException& e) | |
183 { | |
184 OrthancPlugins::LogError("Error during Python on-change callback: " + | |
185 std::string(e.What(OrthancPlugins::GetGlobalContext()))); | |
186 } | |
187 } | |
188 } | |
189 } | |
190 } | |
191 | |
192 | |
193 static OrthancPluginErrorCode OnChangeCallback(OrthancPluginChangeType changeType, | |
194 OrthancPluginResourceType resourceType, | |
195 const char* resourceId) | |
196 { | |
57
46fe70776d61
Fix possible deadlock with "orthanc.RegisterOnChangeCallback()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
56
diff
changeset
|
197 pendingChanges_.Enqueue(changeType, resourceType, resourceId); |
58
ef1a1ce0c1e3
During Orthanc shutdown, wait for all the pending events to have been processed
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
57
diff
changeset
|
198 |
ef1a1ce0c1e3
During Orthanc shutdown, wait for all the pending events to have been processed
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
57
diff
changeset
|
199 if (changeType == OrthancPluginChangeType_OrthancStopped) |
ef1a1ce0c1e3
During Orthanc shutdown, wait for all the pending events to have been processed
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
57
diff
changeset
|
200 { |
ef1a1ce0c1e3
During Orthanc shutdown, wait for all the pending events to have been processed
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
57
diff
changeset
|
201 StopThread(); |
ef1a1ce0c1e3
During Orthanc shutdown, wait for all the pending events to have been processed
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
57
diff
changeset
|
202 } |
ef1a1ce0c1e3
During Orthanc shutdown, wait for all the pending events to have been processed
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
57
diff
changeset
|
203 |
0 | 204 return OrthancPluginErrorCode_Success; |
205 } | |
206 | |
207 | |
208 PyObject* RegisterOnChangeCallback(PyObject* module, PyObject* args) | |
209 { | |
210 // The GIL is locked at this point (no need to create "PythonLock") | |
211 | |
68
0b3ef425db31
refactoring using ICallbackRegistration::Apply()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
58
diff
changeset
|
212 class Registration : public ICallbackRegistration |
0 | 213 { |
68
0b3ef425db31
refactoring using ICallbackRegistration::Apply()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
58
diff
changeset
|
214 public: |
0b3ef425db31
refactoring using ICallbackRegistration::Apply()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
58
diff
changeset
|
215 virtual void Register() ORTHANC_OVERRIDE |
0b3ef425db31
refactoring using ICallbackRegistration::Apply()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
58
diff
changeset
|
216 { |
0b3ef425db31
refactoring using ICallbackRegistration::Apply()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
58
diff
changeset
|
217 OrthancPluginRegisterOnChangeCallback(OrthancPlugins::GetGlobalContext(), OnChangeCallback); |
0 | 218 |
68
0b3ef425db31
refactoring using ICallbackRegistration::Apply()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
58
diff
changeset
|
219 stopping_ = false; |
0b3ef425db31
refactoring using ICallbackRegistration::Apply()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
58
diff
changeset
|
220 changesThread_ = boost::thread(ChangesWorker); |
0b3ef425db31
refactoring using ICallbackRegistration::Apply()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
58
diff
changeset
|
221 } |
0b3ef425db31
refactoring using ICallbackRegistration::Apply()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
58
diff
changeset
|
222 }; |
0 | 223 |
68
0b3ef425db31
refactoring using ICallbackRegistration::Apply()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
58
diff
changeset
|
224 Registration registration; |
0b3ef425db31
refactoring using ICallbackRegistration::Apply()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
58
diff
changeset
|
225 return ICallbackRegistration::Apply( |
0b3ef425db31
refactoring using ICallbackRegistration::Apply()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
58
diff
changeset
|
226 registration, args, changesCallback_, "Python on-changes callback"); |
0 | 227 } |
228 | |
229 | |
230 void FinalizeOnChangeCallback() | |
231 { | |
58
ef1a1ce0c1e3
During Orthanc shutdown, wait for all the pending events to have been processed
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
57
diff
changeset
|
232 StopThread(); |
68
0b3ef425db31
refactoring using ICallbackRegistration::Apply()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
58
diff
changeset
|
233 ICallbackRegistration::Unregister(changesCallback_); |
0 | 234 } |