comparison Framework/Plugins/StorageBackend.cpp @ 1:d17b2631bb67

starting StorageBackend
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 04 Jul 2018 18:05:24 +0200
parents
children 41543239072d
comparison
equal deleted inserted replaced
0:7cea966b6829 1:d17b2631bb67
1 /**
2 * Orthanc - A Lightweight, RESTful DICOM Store
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
4 * Department, University Hospital of Liege, Belgium
5 * Copyright (C) 2017-2018 Osimis S.A., Belgium
6 *
7 * This program is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU Affero General Public License
9 * as published by the Free Software Foundation, either version 3 of
10 * the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Affero General Public License for more details.
16 *
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 **/
20
21
22 #include "StorageBackend.h"
23
24 #if HAS_ORTHANC_EXCEPTION != 1
25 # error HAS_ORTHANC_EXCEPTION must be set to 1
26 #endif
27
28 #include <Core/OrthancException.h>
29
30
31 #define ORTHANC_PLUGINS_DATABASE_CATCH \
32 catch (::Orthanc::OrthancException& e) \
33 { \
34 return static_cast<OrthancPluginErrorCode>(e.GetErrorCode()); \
35 } \
36 catch (::std::runtime_error& e) \
37 { \
38 std::string s = "Exception in storage area back-end: " + std::string(e.what()); \
39 OrthancPluginLogError(context_, s.c_str()); \
40 return OrthancPluginErrorCode_DatabasePlugin; \
41 } \
42 catch (...) \
43 { \
44 OrthancPluginLogError(context_, "Native exception"); \
45 return OrthancPluginErrorCode_DatabasePlugin; \
46 }
47
48
49 namespace OrthancDatabases
50 {
51 StorageBackend::StorageBackend(IDatabaseFactory* factory) :
52 manager_(factory)
53 {
54 }
55
56
57
58 static OrthancPluginContext* context_ = NULL;
59 static std::auto_ptr<StorageBackend> backend_;
60
61 static OrthancPluginErrorCode StorageCreate(const char* uuid,
62 const void* content,
63 int64_t size,
64 OrthancPluginContentType type)
65 {
66 try
67 {
68 backend_->Create(uuid, content, static_cast<size_t>(size), type);
69 return OrthancPluginErrorCode_Success;
70 }
71 ORTHANC_PLUGINS_DATABASE_CATCH;
72 }
73
74
75 static OrthancPluginErrorCode StorageRead(void** content,
76 int64_t* size,
77 const char* uuid,
78 OrthancPluginContentType type)
79 {
80 try
81 {
82 size_t tmp;
83 backend_->Read(*content, tmp, uuid, type);
84 *size = static_cast<int64_t>(tmp);
85 return OrthancPluginErrorCode_Success;
86 }
87 ORTHANC_PLUGINS_DATABASE_CATCH;
88 }
89
90
91 static OrthancPluginErrorCode StorageRemove(const char* uuid,
92 OrthancPluginContentType type)
93 {
94 try
95 {
96 backend_->Remove(uuid, type);
97 return OrthancPluginErrorCode_Success;
98 }
99 ORTHANC_PLUGINS_DATABASE_CATCH;
100 }
101
102
103 void StorageBackend::Register(OrthancPluginContext* context,
104 StorageBackend* backend)
105 {
106 if (context == NULL ||
107 backend == NULL)
108 {
109 throw Orthanc::OrthancException(Orthanc::ErrorCode_NullPointer);
110 }
111
112 if (context_ != NULL ||
113 backend_.get() != NULL)
114 {
115 // This function can only be invoked once in the plugin
116 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls);
117 }
118 else
119 {
120 context_ = context;
121 backend_.reset(backend);
122
123 OrthancPluginRegisterStorageArea(context_, StorageCreate, StorageRead, StorageRemove);
124 }
125 }
126
127
128 void StorageBackend::Finalize()
129 {
130 backend_.reset(NULL);
131 context_ = NULL;
132 }
133 }