comparison OrthancServer/Sources/ServerJobs/OrthancPeerStoreJob.cpp @ 4044:d25f4c0fa160 framework

splitting code into OrthancFramework and OrthancServer
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 10 Jun 2020 20:30:34 +0200
parents OrthancServer/ServerJobs/OrthancPeerStoreJob.cpp@5fe8c6d3212e
children 05b8fd21089c
comparison
equal deleted inserted replaced
4043:6c6239aec462 4044:d25f4c0fa160
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-2020 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 General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
11 *
12 * In addition, as a special exception, the copyright holders of this
13 * program give permission to link the code of its release with the
14 * OpenSSL project's "OpenSSL" library (or with modified versions of it
15 * that use the same license as the "OpenSSL" library), and distribute
16 * the linked executables. You must obey the GNU General Public License
17 * in all respects for all of the code used other than "OpenSSL". If you
18 * modify file(s) with this exception, you may extend this exception to
19 * your version of the file(s), but you are not obligated to do so. If
20 * you do not wish to do so, delete this exception statement from your
21 * version. If you delete this exception statement from all source files
22 * in the program, then also delete it here.
23 *
24 * This program is distributed in the hope that it will be useful, but
25 * WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
27 * General Public License for more details.
28 *
29 * You should have received a copy of the GNU General Public License
30 * along with this program. If not, see <http://www.gnu.org/licenses/>.
31 **/
32
33
34 #include "../PrecompiledHeadersServer.h"
35 #include "OrthancPeerStoreJob.h"
36
37 #include "../../Core/Logging.h"
38 #include "../../Core/SerializationToolbox.h"
39 #include "../ServerContext.h"
40
41 #include <dcmtk/dcmdata/dcfilefo.h>
42
43
44 namespace Orthanc
45 {
46 bool OrthancPeerStoreJob::HandleInstance(const std::string& instance)
47 {
48 //boost::this_thread::sleep(boost::posix_time::milliseconds(500));
49
50 if (client_.get() == NULL)
51 {
52 client_.reset(new HttpClient(peer_, "instances"));
53 client_->SetMethod(HttpMethod_Post);
54 }
55
56 LOG(INFO) << "Sending instance " << instance << " to peer \""
57 << peer_.GetUrl() << "\"";
58
59 try
60 {
61 if (transcode_)
62 {
63 std::string dicom;
64 context_.ReadDicom(dicom, instance);
65
66 std::set<DicomTransferSyntax> syntaxes;
67 syntaxes.insert(transferSyntax_);
68
69 IDicomTranscoder::DicomImage source, transcoded;
70 source.SetExternalBuffer(dicom);
71
72 if (context_.Transcode(transcoded, source, syntaxes, true))
73 {
74 client_->GetBody().assign(reinterpret_cast<const char*>(transcoded.GetBufferData()),
75 transcoded.GetBufferSize());
76 }
77 else
78 {
79 client_->GetBody().swap(dicom);
80 }
81 }
82 else
83 {
84 context_.ReadDicom(client_->GetBody(), instance);
85 }
86 }
87 catch (OrthancException& e)
88 {
89 LOG(WARNING) << "An instance was removed after the job was issued: " << instance;
90 return false;
91 }
92
93 std::string answer;
94 if (client_->Apply(answer))
95 {
96 return true;
97 }
98 else
99 {
100 throw OrthancException(ErrorCode_NetworkProtocol);
101 }
102 }
103
104
105 bool OrthancPeerStoreJob::HandleTrailingStep()
106 {
107 throw OrthancException(ErrorCode_InternalError);
108 }
109
110
111 void OrthancPeerStoreJob::SetPeer(const WebServiceParameters& peer)
112 {
113 if (IsStarted())
114 {
115 throw OrthancException(ErrorCode_BadSequenceOfCalls);
116 }
117 else
118 {
119 peer_ = peer;
120 }
121 }
122
123
124 DicomTransferSyntax OrthancPeerStoreJob::GetTransferSyntax() const
125 {
126 if (transcode_)
127 {
128 return transferSyntax_;
129 }
130 else
131 {
132 throw OrthancException(ErrorCode_BadSequenceOfCalls);
133 }
134 }
135
136
137 void OrthancPeerStoreJob::SetTranscode(DicomTransferSyntax syntax)
138 {
139 if (IsStarted())
140 {
141 throw OrthancException(ErrorCode_BadSequenceOfCalls);
142 }
143 else
144 {
145 transcode_ = true;
146 transferSyntax_ = syntax;
147 }
148 }
149
150
151 void OrthancPeerStoreJob::SetTranscode(const std::string& transferSyntaxUid)
152 {
153 DicomTransferSyntax s;
154 if (LookupTransferSyntax(s, transferSyntaxUid))
155 {
156 SetTranscode(s);
157 }
158 else
159 {
160 throw OrthancException(ErrorCode_BadFileFormat,
161 "Unknown transfer syntax UID: " + transferSyntaxUid);
162 }
163 }
164
165
166 void OrthancPeerStoreJob::ClearTranscode()
167 {
168 if (IsStarted())
169 {
170 throw OrthancException(ErrorCode_BadSequenceOfCalls);
171 }
172 else
173 {
174 transcode_ = false;
175 }
176 }
177
178
179 void OrthancPeerStoreJob::Stop(JobStopReason reason) // For pausing jobs
180 {
181 client_.reset(NULL);
182 }
183
184
185 void OrthancPeerStoreJob::GetPublicContent(Json::Value& value)
186 {
187 SetOfInstancesJob::GetPublicContent(value);
188
189 Json::Value v;
190 peer_.Serialize(v,
191 false /* allow simple format if possible */,
192 false /* don't include passwords */);
193 value["Peer"] = v;
194
195 if (transcode_)
196 {
197 value["Transcode"] = GetTransferSyntaxUid(transferSyntax_);
198 }
199 }
200
201
202 static const char* PEER = "Peer";
203 static const char* TRANSCODE = "Transcode";
204
205 OrthancPeerStoreJob::OrthancPeerStoreJob(ServerContext& context,
206 const Json::Value& serialized) :
207 SetOfInstancesJob(serialized),
208 context_(context)
209 {
210 assert(serialized.type() == Json::objectValue);
211 peer_ = WebServiceParameters(serialized[PEER]);
212
213 if (serialized.isMember(TRANSCODE))
214 {
215 SetTranscode(SerializationToolbox::ReadString(serialized, TRANSCODE));
216 }
217 else
218 {
219 transcode_ = false;
220 }
221 }
222
223
224 bool OrthancPeerStoreJob::Serialize(Json::Value& target)
225 {
226 if (!SetOfInstancesJob::Serialize(target))
227 {
228 return false;
229 }
230 else
231 {
232 assert(target.type() == Json::objectValue);
233 peer_.Serialize(target[PEER],
234 true /* force advanced format */,
235 true /* include passwords */);
236
237 if (transcode_)
238 {
239 target[TRANSCODE] = GetTransferSyntaxUid(transferSyntax_);
240 }
241
242 return true;
243 }
244 }
245 }