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