File: StorageCommitmentScpCallback.cpp

package info (click to toggle)
orthanc-python 7.0%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,216 kB
  • sloc: cpp: 8,830; python: 795; sh: 62; makefile: 33
file content (288 lines) | stat: -rw-r--r-- 9,800 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/**
 * SPDX-FileCopyrightText: 2020-2023 Osimis S.A., 2024-2025 Orthanc Team SRL, 2021-2025 Sebastien Jodogne, ICTEAM UCLouvain
 * SPDX-License-Identifier: AGPL-3.0-or-later
 */

/**
 * Python plugin for Orthanc
 * Copyright (C) 2020-2023 Osimis S.A., Belgium
 * Copyright (C) 2024-2025 Orthanc Team SRL, Belgium
 * Copyright (C) 2021-2025 Sebastien Jodogne, ICTEAM UCLouvain, Belgium
 *
 * This program is free software: you can redistribute it and/or
 * modify it under the terms of the GNU Affero General Public License
 * as published by the Free Software Foundation, either version 3 of
 * the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Affero General Public License for more details.
 * 
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 **/


#include "StorageCommitmentScpCallback.h"

#include "PythonHeaderWrapper.h"

#include "../Resources/Orthanc/Plugins/OrthancPluginCppWrapper.h"
#include "ICallbackRegistration.h"
#include "PythonString.h"

#include <sdk.h>

static PyObject*   storageCommitmentScpCallback_ = NULL;
static PyObject*   storageCommitmentLookupCallback_ = NULL;


static OrthancPluginErrorCode StorageCommitmentSCPCallback(
  void**              handler /* out */,
  const char*         jobId,
  const char*         transactionUid,
  const char* const*  sopClassUids,
  const char* const*  sopInstanceUids,
  uint32_t            countInstances,
  const char*         remoteAet,
  const char*         calledAet)
{
  try
  {
    PythonLock lock;

    PythonObject args(lock, PyTuple_New(6));
    {
      PythonString str(lock, jobId);
      PyTuple_SetItem(args.GetPyObject(), 0, str.Release());
    }
    {
      PythonString str(lock, transactionUid);
      PyTuple_SetItem(args.GetPyObject(), 1, str.Release());
    }
    {
      PythonObject sopClassUidList(lock, PyList_New(countInstances));
      for (uint32_t i = 0; i < countInstances; i++)
      {
        PythonString str(lock, sopClassUids[i]);
        PyList_SetItem(sopClassUidList.GetPyObject(), i, str.Release());
      }
      PyTuple_SetItem(args.GetPyObject(), 2, sopClassUidList.Release());
      PythonObject sopInstanceUidList(lock, PyList_New(countInstances));
      for (uint32_t i = 0; i < countInstances; i++)
      {
        PythonString str(lock, sopInstanceUids[i]);
        PyList_SetItem(sopInstanceUidList.GetPyObject(), i, str.Release());
      }
      PyTuple_SetItem(args.GetPyObject(), 3, sopInstanceUidList.Release());
    }
    {
      PythonString str(lock, remoteAet);
      PyTuple_SetItem(args.GetPyObject(), 4, str.Release());
    }
    {
      PythonString str(lock, calledAet);
      PyTuple_SetItem(args.GetPyObject(), 5, str.Release());
    }

    PythonObject result(lock, PyObject_CallObject(storageCommitmentScpCallback_, args.GetPyObject()));
    *handler = result.Release();

    std::string traceback;
    if (lock.HasErrorOccurred(traceback))
    {
      ORTHANC_PLUGINS_LOG_ERROR("Error in the Python storage commitment SCP callback, traceback:\n" + traceback);
      return OrthancPluginErrorCode_Plugin;
    }
  }
  catch (OrthancPlugins::PluginException& e)
  {
    ORTHANC_PLUGINS_LOG_ERROR("Error in the Python storage commitment SCP callback: " +
                              std::string(e.What(OrthancPlugins::GetGlobalContext())));
  }
  return OrthancPluginErrorCode_Success;
}

#if ORTHANC_PLUGINS_VERSION_IS_ABOVE(1, 12, 10)
static OrthancPluginErrorCode StorageCommitmentSCPCallback2(
  void**              handler /* out */,
  const char*         jobId,
  const char*         transactionUid,
  const char* const*  sopClassUids,
  const char* const*  sopInstanceUids,
  uint32_t            countInstances,
  const OrthancPluginDicomConnection* connection)
{
  try
  {
    PythonLock lock;

    PythonObject args(lock, PyTuple_New(5));
    {
      PythonString str(lock, jobId);
      PyTuple_SetItem(args.GetPyObject(), 0, str.Release());
    }
    {
      PythonString str(lock, transactionUid);
      PyTuple_SetItem(args.GetPyObject(), 1, str.Release());
    }
    {
      PythonObject sopClassUidList(lock, PyList_New(countInstances));
      for (uint32_t i = 0; i < countInstances; i++)
      {
        PythonString str(lock, sopClassUids[i]);
        PyList_SetItem(sopClassUidList.GetPyObject(), i, str.Release());
      }
      PyTuple_SetItem(args.GetPyObject(), 2, sopClassUidList.Release());
      PythonObject sopInstanceUidList(lock, PyList_New(countInstances));
      for (uint32_t i = 0; i < countInstances; i++)
      {
        PythonString str(lock, sopInstanceUids[i]);
        PyList_SetItem(sopInstanceUidList.GetPyObject(), i, str.Release());
      }
      PyTuple_SetItem(args.GetPyObject(), 3, sopInstanceUidList.Release());
    }
    {
      PythonObject argsConnection(lock, PyTuple_New(2));
      PyTuple_SetItem(argsConnection.GetPyObject(), 0, PyLong_FromSsize_t((intptr_t) connection));
      PyTuple_SetItem(argsConnection.GetPyObject(), 1, PyBool_FromLong(true /* borrowed, don't destruct */));
      PyObject *pConnection = PyObject_CallObject((PyObject*) GetOrthancPluginDicomConnectionType(), argsConnection.GetPyObject());
  
      PyTuple_SetItem(args.GetPyObject(), 4, pConnection);
    }

    PythonObject result(lock, PyObject_CallObject(storageCommitmentScpCallback_, args.GetPyObject()));
    *handler = result.Release();

    std::string traceback;
    if (lock.HasErrorOccurred(traceback))
    {
      ORTHANC_PLUGINS_LOG_ERROR("Error in the Python storage commitment SCP callback (v2), traceback:\n" + traceback);
      return OrthancPluginErrorCode_Plugin;
    }
  }
  catch (OrthancPlugins::PluginException& e)
  {
    ORTHANC_PLUGINS_LOG_ERROR("Error in the Python storage commitment SCP callback (v2): " +
                              std::string(e.What(OrthancPlugins::GetGlobalContext())));
  }
  return OrthancPluginErrorCode_Success;
}
#endif

static OrthancPluginErrorCode StorageCommitmentLookupCallback(
  OrthancPluginStorageCommitmentFailureReason* target /* out */,
  void*                                        handler,
  const char*                                  sopClassUid,
  const char*                                  sopInstanceUid)
{
  try
  {
    PythonLock lock;

    PythonObject args(lock, PyTuple_New(3));
    {
      PythonString str(lock, sopClassUid);
      PyTuple_SetItem(args.GetPyObject(), 0, str.Release());
    }
    {
      PythonString str(lock, sopInstanceUid);
      PyTuple_SetItem(args.GetPyObject(), 1, str.Release());
    }
    {
      PyObject* data = (PyObject*) handler;
      Py_INCREF(data);  // Keep a reference before it was stolen by PyTuple_SetItem.
      PyTuple_SetItem(args.GetPyObject(), 2, data);
    }

    PythonObject result(lock, PyObject_CallObject(storageCommitmentLookupCallback_, args.GetPyObject()));

    if (!PyLong_Check(result.GetPyObject()))
    {
      ORTHANC_PLUGINS_LOG_ERROR("The Python storage commitment Lookup callback has not returned an int as the return value");
      return OrthancPluginErrorCode_Plugin;
    }

    *target = static_cast<OrthancPluginStorageCommitmentFailureReason>(PyLong_AsLong(result.GetPyObject()));

    std::string traceback;
    if (lock.HasErrorOccurred(traceback))
    {
      ORTHANC_PLUGINS_LOG_ERROR("Error in the Python storage commitment Lookup callback, traceback:\n" + traceback);
      return OrthancPluginErrorCode_Plugin;
    }
  }
  catch (OrthancPlugins::PluginException& e)
  {
    ORTHANC_PLUGINS_LOG_ERROR("Error in the Python storage commitment Lookup callback: " +
                              std::string(e.What(OrthancPlugins::GetGlobalContext())));
  }
  return OrthancPluginErrorCode_Success;
}

static void StorageCommitmentDestructor(void *handler)
{
  PythonLock lock;
  Py_DECREF((PyObject*)handler);  // Release the reference
}

PyObject* RegisterStorageCommitmentScpCallback(PyObject* module, PyObject* args)
{
  // The GIL is locked at this point (no need to create "PythonLock")

  class Registration : public ICallbackRegistration
  {
  public:
    virtual void Register() ORTHANC_OVERRIDE
    {
      OrthancPluginRegisterStorageCommitmentScpCallback(
        OrthancPlugins::GetGlobalContext(),
        StorageCommitmentSCPCallback,
        StorageCommitmentDestructor,
        StorageCommitmentLookupCallback);
    }
  };

  {
    Registration registration;
    return ICallbackRegistration::Apply2(registration, args,
      storageCommitmentScpCallback_,
      storageCommitmentLookupCallback_,
      "Python storage commitment SCP & Lookup callback");
  }
}

#if ORTHANC_PLUGINS_VERSION_IS_ABOVE(1, 12, 10)
PyObject* RegisterStorageCommitmentScpCallback2(PyObject* module, PyObject* args)
{
  // The GIL is locked at this point (no need to create "PythonLock")

  class Registration : public ICallbackRegistration
  {
  public:
    virtual void Register() ORTHANC_OVERRIDE
    {
      OrthancPluginRegisterStorageCommitmentScpCallback2(
        OrthancPlugins::GetGlobalContext(),
        StorageCommitmentSCPCallback2,
        StorageCommitmentDestructor,
        StorageCommitmentLookupCallback);
    }
  };

  {
    Registration registration;
    return ICallbackRegistration::Apply2(registration, args,
      storageCommitmentScpCallback_,
      storageCommitmentLookupCallback_,
      "Python storage commitment SCP & Lookup callback (v2)");
  }
}
#endif

void FinalizeStorageCommitmentScpCallback()
{
  ICallbackRegistration::Unregister(storageCommitmentScpCallback_);
  ICallbackRegistration::Unregister(storageCommitmentLookupCallback_);
}