Mercurial > hg > orthanc-python
annotate Sources/OnChangeCallback.cpp @ 43:eec99a934b6a OrthancPython-2.0
OrthancPython-2.0
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Wed, 08 Jul 2020 15:17:58 +0200 |
parents | fd58eb5749ed |
children | ee76cced46a5 |
rev | line source |
---|---|
0 | 1 /** |
2 * Python plugin for Orthanc | |
3 * Copyright (C) 2017-2020 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 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 | |
22 #include "PythonObject.h" | |
23 | |
36
fd58eb5749ed
CMake simplification using DownloadOrthancFramework.cmake
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
13
diff
changeset
|
24 #include "../Resources/Orthanc/Plugins/OrthancPluginCppWrapper.h" |
fd58eb5749ed
CMake simplification using DownloadOrthancFramework.cmake
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
13
diff
changeset
|
25 |
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 | |
73 class PendingChanges : public boost::noncopyable | |
74 { | |
75 private: | |
76 typedef std::list<PendingChange*> Queue; | |
77 | |
78 boost::mutex mutex_; | |
79 Queue queue_; | |
80 boost::condition_variable elementAvailable_; | |
81 | |
82 public: | |
83 ~PendingChanges() | |
84 { | |
85 for (Queue::iterator it = queue_.begin(); it != queue_.end(); ++it) | |
86 { | |
87 assert(*it != NULL); | |
88 delete *it; | |
89 } | |
90 } | |
91 | |
92 void Enqueue(OrthancPluginChangeType changeType, | |
93 OrthancPluginResourceType resourceType, | |
94 const char* resourceId) | |
95 { | |
96 boost::mutex::scoped_lock lock(mutex_); | |
97 queue_.push_back(new PendingChange(changeType, resourceType, resourceId)); | |
98 elementAvailable_.notify_one(); | |
99 } | |
100 | |
101 PendingChange* Dequeue(unsigned int millisecondsTimeout) | |
102 { | |
13 | 103 if (millisecondsTimeout == 0) |
0 | 104 { |
105 ORTHANC_PLUGINS_THROW_EXCEPTION(ParameterOutOfRange); | |
106 } | |
107 | |
108 boost::mutex::scoped_lock lock(mutex_); | |
109 | |
110 // Wait for a message to arrive in the FIFO queue | |
111 while (queue_.empty()) | |
112 { | |
113 bool success = elementAvailable_.timed_wait | |
114 (lock, boost::posix_time::milliseconds(millisecondsTimeout)); | |
115 if (!success) | |
116 { | |
117 return NULL; | |
118 } | |
119 } | |
120 | |
36
fd58eb5749ed
CMake simplification using DownloadOrthancFramework.cmake
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
13
diff
changeset
|
121 std::unique_ptr<PendingChange> change(queue_.front()); |
0 | 122 queue_.pop_front(); |
123 | |
124 return change.release(); | |
125 } | |
126 }; | |
127 | |
128 | |
129 | |
130 static PendingChanges pendingChanges_; | |
131 static bool stopping_ = false; | |
132 static boost::thread changesThread_; | |
133 static PyObject* changesCallback_ = NULL; | |
134 | |
135 | |
136 static void ChangesWorker() | |
137 { | |
138 while (!stopping_) | |
139 { | |
140 for (;;) | |
141 { | |
142 std::unique_ptr<PendingChange> change(pendingChanges_.Dequeue(100)); | |
143 if (change.get() == NULL) | |
144 { | |
145 break; | |
146 } | |
147 else if (changesCallback_ != NULL) | |
148 { | |
149 try | |
150 { | |
151 PythonLock lock; | |
152 PythonObject args(lock, PyTuple_New(3)); | |
153 PyTuple_SetItem(args.GetPyObject(), 0, PyLong_FromLong(change->GetChangeType())); | |
154 PyTuple_SetItem(args.GetPyObject(), 1, PyLong_FromLong(change->GetResourceType())); | |
155 PyTuple_SetItem(args.GetPyObject(), 2, PyUnicode_FromString(change->GetResourceId().c_str())); | |
156 PythonObject result(lock, PyObject_CallObject(changesCallback_, args.GetPyObject())); | |
3
26762eb9d704
reporting of exceptions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
157 |
26762eb9d704
reporting of exceptions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
158 std::string traceback; |
26762eb9d704
reporting of exceptions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
159 if (lock.HasErrorOccurred(traceback)) |
26762eb9d704
reporting of exceptions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
160 { |
26762eb9d704
reporting of exceptions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
161 OrthancPlugins::LogError("Error in the Python on-change callback, " |
26762eb9d704
reporting of exceptions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
162 "traceback:\n" + traceback); |
26762eb9d704
reporting of exceptions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
163 } |
0 | 164 } |
165 catch (OrthancPlugins::PluginException& e) | |
166 { | |
167 OrthancPlugins::LogError("Error during Python on-change callback: " + | |
168 std::string(e.What(OrthancPlugins::GetGlobalContext()))); | |
169 } | |
170 } | |
171 } | |
172 } | |
173 } | |
174 | |
175 | |
176 static OrthancPluginErrorCode OnChangeCallback(OrthancPluginChangeType changeType, | |
177 OrthancPluginResourceType resourceType, | |
178 const char* resourceId) | |
179 { | |
180 { | |
181 PythonLock lock; | |
182 pendingChanges_.Enqueue(changeType, resourceType, resourceId); | |
183 } | |
184 | |
185 return OrthancPluginErrorCode_Success; | |
186 } | |
187 | |
188 | |
189 PyObject* RegisterOnChangeCallback(PyObject* module, PyObject* args) | |
190 { | |
191 // The GIL is locked at this point (no need to create "PythonLock") | |
192 | |
193 // https://docs.python.org/3/extending/extending.html#calling-python-functions-from-c | |
194 PyObject* callback = NULL; | |
195 | |
196 if (!PyArg_ParseTuple(args, "O", &callback) || | |
197 callback == NULL) | |
198 { | |
199 PyErr_SetString(PyExc_ValueError, "Expected a callback function"); | |
200 return NULL; | |
201 } | |
202 | |
203 if (changesCallback_ != NULL) | |
204 { | |
205 PyErr_SetString(PyExc_RuntimeError, "Can only register one Python on-changes callback"); | |
206 return NULL; | |
207 } | |
208 | |
209 OrthancPlugins::LogInfo("Registering a Python on-changes callback"); | |
210 | |
211 OrthancPluginRegisterOnChangeCallback(OrthancPlugins::GetGlobalContext(), OnChangeCallback); | |
212 | |
213 stopping_ = false; | |
214 changesThread_ = boost::thread(ChangesWorker); | |
215 | |
216 changesCallback_ = callback; | |
217 Py_XINCREF(changesCallback_); | |
218 | |
219 Py_INCREF(Py_None); | |
220 return Py_None; | |
221 } | |
222 | |
223 | |
224 | |
225 | |
226 void FinalizeOnChangeCallback() | |
227 { | |
228 stopping_ = true; | |
229 | |
230 if (changesThread_.joinable()) | |
231 { | |
232 changesThread_.join(); | |
233 } | |
234 | |
235 { | |
236 PythonLock lock; | |
237 | |
238 if (changesCallback_ != NULL) | |
239 { | |
240 Py_XDECREF(changesCallback_); | |
241 } | |
242 } | |
243 } |