Mercurial > hg > orthanc-python
comparison Sources/IncomingHttpRequestFilter.cpp @ 68:0b3ef425db31
refactoring using ICallbackRegistration::Apply()
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Fri, 11 Jun 2021 14:34:08 +0200 |
parents | 23f3099bed47 |
children | eb6ac5a801d1 |
comparison
equal
deleted
inserted
replaced
67:9d0460d0f15b | 68:0b3ef425db31 |
---|---|
17 **/ | 17 **/ |
18 | 18 |
19 | 19 |
20 #include "IncomingHttpRequestFilter.h" | 20 #include "IncomingHttpRequestFilter.h" |
21 | 21 |
22 #include "../Resources/Orthanc/Plugins/OrthancPluginCppWrapper.h" | |
23 #include "Autogenerated/sdk.h" | |
24 #include "ICallbackRegistration.h" | |
22 #include "PythonString.h" | 25 #include "PythonString.h" |
23 #include "Autogenerated/sdk.h" | |
24 | |
25 #include "../Resources/Orthanc/Plugins/OrthancPluginCppWrapper.h" | |
26 | 26 |
27 | 27 |
28 static PyObject* incomingHttpRequestFilter_ = NULL; | 28 static PyObject* incomingHttpRequestFilter_ = NULL; |
29 | 29 |
30 | 30 |
115 | 115 |
116 | 116 |
117 PyObject* RegisterIncomingHttpRequestFilter(PyObject* module, PyObject* args) | 117 PyObject* RegisterIncomingHttpRequestFilter(PyObject* module, PyObject* args) |
118 { | 118 { |
119 // The GIL is locked at this point (no need to create "PythonLock") | 119 // The GIL is locked at this point (no need to create "PythonLock") |
120 | |
121 // https://docs.python.org/3/extending/extending.html#calling-python-functions-from-c | |
122 PyObject* callback = NULL; | |
123 | 120 |
124 if (!PyArg_ParseTuple(args, "O", &callback) || | 121 class Registration : public ICallbackRegistration |
125 callback == NULL) | |
126 { | 122 { |
127 PyErr_SetString(PyExc_ValueError, "Expected a callback function"); | 123 public: |
128 return NULL; | 124 virtual void Register() ORTHANC_OVERRIDE |
129 } | 125 { |
126 OrthancPluginRegisterIncomingHttpRequestFilter2( | |
127 OrthancPlugins::GetGlobalContext(), IncomingHttpRequestFilter); | |
128 } | |
129 }; | |
130 | 130 |
131 if (incomingHttpRequestFilter_ != NULL) | 131 Registration registration; |
132 { | 132 return ICallbackRegistration::Apply( |
133 PyErr_SetString(PyExc_RuntimeError, "Can only register one Python incoming-http-request filter"); | 133 registration, args, incomingHttpRequestFilter_, "Python incoming-http-request filter"); |
134 return NULL; | |
135 } | |
136 | |
137 OrthancPlugins::LogInfo("Registering a Python incoming-http-request filter"); | |
138 | |
139 OrthancPluginRegisterIncomingHttpRequestFilter2( | |
140 OrthancPlugins::GetGlobalContext(), IncomingHttpRequestFilter); | |
141 | |
142 incomingHttpRequestFilter_ = callback; | |
143 Py_XINCREF(incomingHttpRequestFilter_); | |
144 | |
145 Py_INCREF(Py_None); | |
146 return Py_None; | |
147 } | 134 } |
148 | 135 |
149 | 136 |
150 void FinalizeIncomingHttpRequestFilter() | 137 void FinalizeIncomingHttpRequestFilter() |
151 { | 138 { |
152 PythonLock lock; | 139 ICallbackRegistration::Unregister(incomingHttpRequestFilter_); |
153 | |
154 if (incomingHttpRequestFilter_ != NULL) | |
155 { | |
156 Py_XDECREF(incomingHttpRequestFilter_); | |
157 } | |
158 } | 140 } |